ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
mcp23s08.cpp
Go to the documentation of this file.
1#include "mcp23s08.h"
2#include "esphome/core/log.h"
3
5
6static const char *const TAG = "mcp23s08";
7
8// IOCON register bits
9static constexpr uint8_t IOCON_SEQOP = 0x20; // Sequential operation mode
10static constexpr uint8_t IOCON_HAEN = 0x08; // Hardware address enable
11static constexpr uint8_t IOCON_ODR = 0x04; // Open-drain output for INT pin
12
13void MCP23S08::set_device_address(uint8_t device_addr) {
14 if (device_addr != 0) {
15 this->device_opcode_ |= ((device_addr & 0x03) << 1);
16 }
17}
18
20 this->spi_setup();
21
22 // Enable HAEN (broadcast to all chips since HAEN isn't active yet)
23 this->enable();
24 this->transfer_byte(0b01000000);
26 this->transfer_byte(IOCON_SEQOP | IOCON_HAEN);
27 this->disable();
28
29 // Read current output register state
31
32 if (this->open_drain_ints_) {
33 // enable open-drain interrupt pins, 3.3V-safe (addressed, only this chip)
34 this->write_reg(mcp23x08_base::MCP23X08_IOCON, IOCON_SEQOP | IOCON_HAEN | IOCON_ODR);
35 }
36
38}
39
41 ESP_LOGCONFIG(TAG, "MCP23S08:");
42 LOG_PIN(" CS Pin: ", this->cs_);
43 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
44}
45
46bool MCP23S08::read_reg(uint8_t reg, uint8_t *value) {
47 this->enable();
48 this->transfer_byte(this->device_opcode_ | 1);
49 this->transfer_byte(reg);
50 *value = this->transfer_byte(0);
51 this->disable();
52 return true;
53}
54
55bool MCP23S08::write_reg(uint8_t reg, uint8_t value) {
56 this->enable();
57 this->transfer_byte(this->device_opcode_);
58 this->transfer_byte(reg);
59 this->transfer_byte(value);
60 this->disable();
61 return true;
62}
63
64} // namespace esphome::mcp23s08
bool write_reg(uint8_t reg, uint8_t value) override
Definition mcp23s08.cpp:55
void dump_config() override
Definition mcp23s08.cpp:40
void set_device_address(uint8_t device_addr)
Definition mcp23s08.cpp:13
bool read_reg(uint8_t reg, uint8_t *value) override
Definition mcp23s08.cpp:46