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