ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
sx1509.cpp
Go to the documentation of this file.
1#include "sx1509.h"
3#include "esphome/core/log.h"
4
5namespace esphome::sx1509 {
6
7ESPHOME_LOG_TAG(TAG, "sx1509");
8
10 ESP_LOGV(TAG, " Resetting devices");
11 if (!this->write_byte(REG_RESET, 0x12)) {
12 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
13 return;
14 }
15 this->write_byte(REG_RESET, 0x34);
16
17 uint16_t data;
18 if (!this->read_byte_16(REG_INTERRUPT_MASK_A, &data)) {
19 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
20 return;
21 }
22 if (data != 0xFF00) {
23 this->mark_failed(LOG_STR("Unexpected reset value"));
24 return;
25 }
28 if (this->has_keypad_)
29 this->setup_keypad_();
30}
31
33 ESP_LOGCONFIG(TAG, "SX1509:");
34 LOG_I2C_DEVICE(this);
35}
36
38 // Reset cache at the start of each loop
39 this->reset_pin_cache_();
40
41 if (this->has_keypad_) {
43 return;
45 uint16_t key_data = this->read_key_data();
46 for (auto *binary_sensor : this->keypad_binary_sensors_)
47 binary_sensor->process(key_data);
48 if (this->keys_ == nullptr)
49 return;
50 if (key_data == 0) {
51 this->last_key_ = 0;
52 return;
53 }
54 int row, col;
55 for (row = 0; row < 8; row++) {
56 if (key_data & (1 << row))
57 break;
58 }
59 for (col = 8; col < 16; col++) {
60 if (key_data & (1 << col))
61 break;
62 }
63 col -= 8;
64 if (row >= this->rows_ || col >= this->cols_) // a partial read can leave only a row or a column bit
65 return;
66 uint8_t key = progmem_read_byte(&this->keys_[row * this->cols_ + col]);
67 if (key == this->last_key_)
68 return;
69 this->last_key_ = key;
70 ESP_LOGV(TAG, "row %d, col %d, key '%c'", row, col, key);
71 for (auto &trigger : this->key_triggers_)
72 trigger->trigger(key);
73 this->send_key_(key);
74 }
75}
76
78 // Always read all pins when any input pin is accessed
79 return this->read_byte_16(REG_DATA_B, &this->input_mask_);
80}
81
83 // Return cached value for input pins, false for output pins
84 if (this->ddr_mask_ & (1 << pin)) {
85 return (this->input_mask_ & (1 << pin)) != 0;
86 }
87 return false;
88}
89
90void SX1509Component::digital_write_hw(uint8_t pin, bool bit_value) {
91 if ((~this->ddr_mask_) & (1 << pin)) {
92 // If the pin is an output, write high/low
93 uint16_t temp_reg_data = 0;
94 this->read_byte_16(REG_DATA_B, &temp_reg_data);
95 assign_bits(this->output_state_, 1 << pin, bit_value);
96 // Outputs take the shadow state, inputs keep what was read
97 temp_reg_data = (temp_reg_data & this->ddr_mask_) | (this->output_state_ & ~this->ddr_mask_);
98 this->write_byte_16(REG_DATA_B, temp_reg_data);
99 }
100}
101
103 ESP_LOGI(TAG, "Configuring pin %u with flags %x", pin, flags);
104
105 uint16_t temp_word = 0;
106
107 this->read_byte_16(REG_DIR_B, &this->ddr_mask_);
108 if (flags & gpio::FLAG_OUTPUT) {
109 // Always disable input buffer
110 this->read_byte_16(REG_INPUT_DISABLE_B, &temp_word);
111 temp_word |= (1 << pin);
112 this->write_byte_16(REG_INPUT_DISABLE_B, temp_word);
113
115 // Pullup must be disabled for open drain mode
116 this->read_byte_16(REG_PULL_UP_B, &temp_word);
117 temp_word &= ~(1 << pin);
118 this->write_byte_16(REG_PULL_UP_B, temp_word);
119 this->read_byte_16(REG_OPEN_DRAIN_B, &temp_word);
120 temp_word |= (1 << pin);
121 this->write_byte_16(REG_OPEN_DRAIN_B, temp_word);
122 ESP_LOGD(TAG, "Open drain output mode set for %u", pin);
123 } else {
124 ESP_LOGD(TAG, "Output Mode for %u", pin);
125 }
126
127 // Set direction to output
128 this->ddr_mask_ &= ~(1 << pin);
129 this->write_byte_16(REG_DIR_B, this->ddr_mask_);
130 } else {
131 ESP_LOGD(TAG, "Input Mode for %u", pin);
132
133 // Always enable input buffer
134 this->read_byte_16(REG_INPUT_DISABLE_B, &temp_word);
135 temp_word &= ~(1 << pin);
136 this->write_byte_16(REG_INPUT_DISABLE_B, temp_word);
137
138 // Pullup
139 this->read_byte_16(REG_PULL_UP_B, &temp_word);
140 assign_bits(temp_word, 1 << pin, flags & gpio::FLAG_PULLUP);
141 this->write_byte_16(REG_PULL_UP_B, temp_word);
142
143 // Pulldown
144 this->read_byte_16(REG_PULL_DOWN_B, &temp_word);
145 assign_bits(temp_word, 1 << pin, flags & gpio::FLAG_PULLDOWN);
146 this->write_byte_16(REG_PULL_DOWN_B, temp_word);
147
148 // Set direction to input
149 this->ddr_mask_ |= (1 << pin);
150 this->write_byte_16(REG_DIR_B, this->ddr_mask_);
151 }
152}
153
155 uint16_t temp_word = 0;
156
157 this->read_byte_16(REG_INPUT_DISABLE_B, &temp_word);
158 temp_word |= (1 << pin);
159 this->write_byte_16(REG_INPUT_DISABLE_B, temp_word);
160
161 this->ddr_mask_ &= ~(1 << pin); // 0=output
162 this->write_byte_16(REG_DIR_B, this->ddr_mask_);
163
164 // Internal 2MHz oscillator: set bit 6, clear bit 5
165 this->update_register(REG_CLOCK, (1 << 6) | (1 << 5), 1 << 6);
166
167 // Linear mode for banks A (bit 3) and B (bit 7); LED driver clock ClkX of all IOs (bits 6:4)
168 this->update_register(REG_MISC, (1 << 7) | (1 << 3) | 0x70, 0x70);
169
170 this->read_byte_16(REG_LED_DRIVER_ENABLE_B, &temp_word);
171 temp_word |= (1 << pin);
172 this->write_byte_16(REG_LED_DRIVER_ENABLE_B, temp_word);
173
174 this->read_byte_16(REG_DATA_B, &temp_word);
175 temp_word &= ~(1 << pin);
176 output_state_ &= ~(1 << pin);
177 this->write_byte_16(REG_DATA_B, temp_word);
178}
179
180void SX1509Component::clock_(uint8_t osc_source, uint8_t osc_pin_function, uint8_t osc_freq_out, uint8_t osc_divider) {
181 osc_source = (osc_source & 0b11) << 5; // 2-bit value, bits 6:5
182 osc_pin_function = (osc_pin_function & 1) << 4; // 1-bit value bit 4
183 osc_freq_out = (osc_freq_out & 0b1111); // 4-bit value, bits 3:0
184 uint8_t reg_clock = osc_source | osc_pin_function | osc_freq_out;
185 this->write_byte(REG_CLOCK, reg_clock);
186
187 osc_divider = clamp<uint8_t>(osc_divider, 1, 7u);
188 this->clk_x_ = 2000000;
189 osc_divider = (osc_divider & 0b111) << 4; // 3-bit value, bits 6:4
190
191 this->update_register(REG_MISC, 0b111 << 4, osc_divider);
192}
193
195 uint8_t temp_byte = 0;
196
197 // setup row/col pins for INPUT OUTPUT
198 this->read_byte_16(REG_DIR_B, &this->ddr_mask_);
199 for (int i = 0; i < this->rows_; i++)
200 this->ddr_mask_ &= ~(1 << i);
201 for (int i = 8; i < (8 + this->cols_); i++)
202 this->ddr_mask_ |= (1 << i);
203 this->write_byte_16(REG_DIR_B, this->ddr_mask_);
204
205 const uint8_t row_bits = (1 << this->rows_) - 1;
206 this->update_register(REG_OPEN_DRAIN_A, row_bits, row_bits);
207
208 const uint8_t col_bits = (1 << this->cols_) - 1;
209 this->update_register(REG_PULL_UP_B, col_bits, col_bits);
210
211 if (debounce_time_ >= scan_time_) {
212 debounce_time_ = scan_time_ >> 1; // Force debounce_time to be less than scan_time
213 }
215 uint8_t scan_time_bits = 0;
216 for (uint8_t i = 7; i > 0; i--) {
217 if (scan_time_ & (1 << i)) {
218 scan_time_bits = i;
219 break;
220 }
221 }
222 scan_time_bits &= 0b111; // Scan time is bits 2:0
223 temp_byte = sleep_time_ | scan_time_bits;
224 this->write_byte(REG_KEY_CONFIG_1, temp_byte);
225 temp_byte = ((this->rows_ - 1) & 0b111) << 3; // 0 = off, 0b001 = 2 rows, 0b111 = 8 rows, etc.
226 temp_byte |= (this->cols_ - 1) & 0b111; // 0b000 = 1 column, ob111 = 8 columns, etc.
227 this->write_byte(REG_KEY_CONFIG_2, temp_byte);
228}
229
231 uint16_t key_data = 0;
232 this->read_byte_16(REG_KEY_DATA_1, &key_data);
233 return (0xFFFF ^ key_data);
234}
235
236void SX1509Component::set_debounce_config_(uint8_t config_value) {
237 // First make sure clock is configured
238 this->update_register(REG_MISC, 1 << 4, 1 << 4); // Just default to no divider if not set
239 this->update_register(REG_CLOCK, 1 << 6, 1 << 6); // default to internal osc.
240
241 config_value &= 0b111; // 3-bit value
242 this->write_byte(REG_DEBOUNCE_CONFIG, config_value);
243}
244
246 uint8_t config_value = 0;
247
248 for (int i = 7; i >= 0; i--) {
249 if (time & (1 << i)) {
250 config_value = i + 1;
251 break;
252 }
253 }
254 config_value = clamp<uint8_t>(config_value, 0, 7);
255
256 set_debounce_config_(config_value);
257}
258
260 uint16_t debounce_enable = 0;
261 this->read_byte_16(REG_DEBOUNCE_ENABLE_B, &debounce_enable);
262 debounce_enable |= (1 << pin);
263 this->write_byte_16(REG_DEBOUNCE_ENABLE_B, debounce_enable);
264}
265
267
268void SX1509Component::set_debounce_keypad_(uint8_t time, uint8_t num_rows, uint8_t num_cols) {
269 set_debounce_time_(time);
270 for (uint16_t i = 0; i < num_rows; i++)
272 for (uint16_t i = 0; i < num_cols; i++)
273 set_debounce_pin_(i + 8);
274}
275
276} // namespace esphome::sx1509
277
278ESPHOME_EXPANDER_PIN_SUMMARY(esphome::sx1509::SX1509Component, "%u via sx1509")
void mark_failed()
Mark this component as failed.
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:281
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.cpp:118
bool update_register(uint8_t a_register, uint8_t mask, uint8_t value)
Read a register, replace the bits in mask with those of value and write it back.
Definition i2c.cpp:95
bool digital_read_cache(uint8_t pin) override
Definition sx1509.cpp:82
void clock_(uint8_t osc_source=2, uint8_t osc_pin_function=1, uint8_t osc_freq_out=0, uint8_t osc_divider=0)
Definition sx1509.cpp:180
void set_debounce_pin_(uint8_t pin)
Definition sx1509.cpp:266
bool digital_read_hw(uint8_t pin) override
Definition sx1509.cpp:77
void set_debounce_config_(uint8_t config_value)
Definition sx1509.cpp:236
std::vector< SX1509KeyTrigger * > key_triggers_
Definition sx1509.h:86
void set_debounce_time_(uint8_t time)
Definition sx1509.cpp:245
void set_debounce_keypad_(uint8_t time, uint8_t num_rows, uint8_t num_cols)
Definition sx1509.cpp:268
void pin_mode(uint8_t pin, gpio::Flags flags)
Definition sx1509.cpp:102
std::vector< SX1509Processor * > keypad_binary_sensors_
Definition sx1509.h:85
void digital_write_hw(uint8_t pin, bool value) override
Definition sx1509.cpp:90
void setup_led_driver(uint8_t pin)
Definition sx1509.cpp:154
void set_debounce_enable_(uint8_t pin)
Definition sx1509.cpp:259
const uint32_t min_loop_period_
Definition sx1509.h:89
uint16_t flags
const char * key
Here you'll find the Arduino code used to interface with the SX1509 I2C 16 I/O expander.
const uint8_t REG_DEBOUNCE_CONFIG
const uint8_t REG_MISC
const uint8_t REG_CLOCK
const uint8_t REG_KEY_CONFIG_2
ESPHOME_LOG_TAG(TAG, "sx1509_float_channel")
const uint8_t REG_DATA_B
const uint8_t REG_LED_DRIVER_ENABLE_B
const uint8_t REG_KEY_CONFIG_1
const uint8_t REG_OPEN_DRAIN_B
const uint8_t REG_PULL_UP_B
const uint8_t REG_INPUT_DISABLE_B
const uint8_t REG_RESET
const uint8_t REG_DEBOUNCE_ENABLE_B
const uint8_t REG_INTERRUPT_MASK_A
const uint8_t REG_PULL_DOWN_B
const uint8_t REG_DIR_B
const uint8_t INTERNAL_CLOCK_2MHZ
Definition sx1509.h:16
const uint8_t REG_OPEN_DRAIN_A
const uint8_t REG_KEY_DATA_1
ESPHOME_ALWAYS_INLINE constexpr void assign_bits(T &value, std::type_identity_t< T > mask, bool on)
Set the bits of mask in value when on is true, clear them otherwise.
Definition helpers.h:1150
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition hal.cpp:48
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51