ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
pca9685_output.cpp
Go to the documentation of this file.
1#include "pca9685_output.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6namespace esphome::pca9685 {
7
8ESPHOME_LOG_TAG(TAG, "pca9685");
9
10// PCA9685 mode constants are now inline constexpr in pca9685_output.h
11
12static const uint8_t PCA9685_REGISTER_SOFTWARE_RESET = 0x06;
13static const uint8_t PCA9685_REGISTER_MODE1 = 0x00;
14static const uint8_t PCA9685_REGISTER_MODE2 = 0x01;
15static const uint8_t PCA9685_REGISTER_LED0 = 0x06;
16static const uint8_t PCA9685_REGISTER_PRE_SCALE = 0xFE;
17
18static const uint8_t PCA9685_MODE1_RESTART = 0b10000000;
19static const uint8_t PCA9685_MODE1_EXTCLK = 0b01000000;
20static const uint8_t PCA9685_MODE1_AUTOINC = 0b00100000;
21static const uint8_t PCA9685_MODE1_SLEEP = 0b00010000;
22
24 if (!this->setup_registers_())
25 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
26}
27
29 ESP_LOGV(TAG, " Resetting devices");
30 if (!this->write_bytes(PCA9685_REGISTER_SOFTWARE_RESET, nullptr, 0)) {
31 return false;
32 }
33
34 if (!this->write_byte(PCA9685_REGISTER_MODE1, PCA9685_MODE1_RESTART | PCA9685_MODE1_AUTOINC)) {
35 return false;
36 }
37 if (!this->write_byte(PCA9685_REGISTER_MODE2, this->mode_)) {
38 return false;
39 }
40
41 uint8_t mode1;
42 if (!this->read_byte(PCA9685_REGISTER_MODE1, &mode1)) {
43 return false;
44 }
45 mode1 = (mode1 & ~PCA9685_MODE1_RESTART) | PCA9685_MODE1_SLEEP;
46 if (!this->write_byte(PCA9685_REGISTER_MODE1, mode1)) {
47 return false;
48 }
49
50 int pre_scaler = 3;
51 if (this->extclk_) {
52 mode1 = mode1 | PCA9685_MODE1_EXTCLK;
53 if (!this->write_byte(PCA9685_REGISTER_MODE1, mode1)) {
54 return false;
55 }
56 } else {
57 pre_scaler = static_cast<int>((25000000 / (4096 * this->frequency_)) - 1);
58 pre_scaler = clamp(pre_scaler, 3, 255);
59
60 ESP_LOGV(TAG, " -> Prescaler: %d", pre_scaler);
61 }
62 if (!this->write_byte(PCA9685_REGISTER_PRE_SCALE, pre_scaler)) {
63 return false;
64 }
65
66 mode1 = (mode1 & ~PCA9685_MODE1_SLEEP) | PCA9685_MODE1_RESTART;
67 if (!this->write_byte(PCA9685_REGISTER_MODE1, mode1)) {
68 return false;
69 }
71
72 this->loop();
73 return true;
74}
75
77 ESP_LOGCONFIG(TAG,
78 "PCA9685:\n"
79 " Mode: 0x%02X",
80 this->mode_);
81 if (this->extclk_) {
82 ESP_LOGCONFIG(TAG, " EXTCLK: enabled");
83 } else {
84 ESP_LOGCONFIG(TAG,
85 " EXTCLK: disabled\n"
86 " Frequency: %.0f Hz",
87 this->frequency_);
88 }
89}
90
92 if (this->min_channel_ == 0xFF || !this->update_)
93 return;
94
95 const uint16_t num_channels = this->max_channel_ - this->min_channel_ + 1;
96 const uint16_t phase_delta_begin = 4096 / num_channels;
97 for (uint8_t channel = this->min_channel_; channel <= this->max_channel_; channel++) {
98 uint16_t phase_begin;
99 switch (this->balancer_) {
101 phase_begin = 0;
102 break;
104 phase_begin = (channel - this->min_channel_) * phase_delta_begin;
105 break;
106 default:
107 ESP_LOGE(TAG, "Unknown phase balancer %d", static_cast<int>(this->balancer_));
108 return;
109 }
110 uint16_t phase_end;
111 uint16_t amount = this->pwm_amounts_[channel];
112 if (amount == 0) {
113 phase_end = 4096;
114 } else if (amount >= 4096) {
115 phase_begin = 4096;
116 phase_end = 0;
117 } else {
118 phase_end = phase_begin + amount;
119 if (phase_end >= 4096)
120 phase_end -= 4096;
121 }
122
123 ESP_LOGVV(TAG, "Channel %02u: amount=%04u phase_begin=%04u phase_end=%04u", channel, amount, phase_begin,
124 phase_end);
125
126 uint8_t data[4];
127 data[0] = phase_begin & 0xFF;
128 data[1] = (phase_begin >> 8) & 0xFF;
129 data[2] = phase_end & 0xFF;
130 data[3] = (phase_end >> 8) & 0xFF;
131
132 uint8_t reg = PCA9685_REGISTER_LED0 + 4 * channel;
133 if (!this->write_bytes(reg, data, 4)) {
134 this->status_set_warning();
135 return;
136 }
137 }
138
139 this->status_clear_warning();
140 this->update_ = false;
141}
142
144 auto c = channel->channel_;
145 this->min_channel_ = std::min(this->min_channel_, c);
146 this->max_channel_ = std::max(this->max_channel_, c);
147 channel->set_parent(this);
148}
149
151 const uint16_t max_duty = 4096;
152 const float duty_rounded = roundf(state * max_duty);
153 auto duty = static_cast<uint16_t>(duty_rounded);
154 this->parent_->set_channel_value_(this->channel_, duty);
155}
156
157} // namespace esphome::pca9685
void mark_failed()
Mark this component as failed.
void status_clear_warning()
Definition component.h:296
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:298
void write_state(float state) override
void set_parent(PCA9685Output *parent)
void set_channel_value_(uint8_t channel, uint16_t value)
void register_channel(PCA9685Channel *channel)
bool setup_registers_()
Configure the chip; false on an I2C failure.
bool state
Definition fan.h:2
ESPHOME_LOG_TAG(TAG, "pca9685")
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition hal.cpp:48