ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
mcp23s17.cpp
Go to the documentation of this file.
1#include "mcp23s17.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace mcp23s17 {
6
7static const char *const TAG = "mcp23s17";
8
9void MCP23S17::set_device_address(uint8_t device_addr) {
10 if (device_addr != 0) {
11 this->device_opcode_ |= ((device_addr & 0b111) << 1);
12 }
13}
14
16 this->spi_setup();
17
18 this->enable();
19 uint8_t cmd = 0b01000000;
20 this->transfer_byte(cmd);
22 this->transfer_byte(0b00011000); // Enable HAEN pins for addressing
23 this->disable();
24
25 this->enable();
26 cmd = 0b01001000;
27 this->transfer_byte(cmd);
29 this->transfer_byte(0b00011000); // Enable HAEN pins for addressing
30 this->disable();
31
32 // Read current output register state
35
36 if (this->open_drain_ints_) {
37 // enable open-drain interrupt pins, 3.3V-safe
40 }
41}
42
44 ESP_LOGCONFIG(TAG, "MCP23S17:");
45 LOG_PIN(" CS Pin: ", this->cs_);
46}
47
48bool MCP23S17::read_reg(uint8_t reg, uint8_t *value) {
49 this->enable();
50 this->transfer_byte(this->device_opcode_ | 1);
51 this->transfer_byte(reg);
52 *value = this->transfer_byte(0xFF);
53 this->disable();
54 return true;
55}
56
57bool MCP23S17::write_reg(uint8_t reg, uint8_t value) {
58 this->enable();
59 this->transfer_byte(this->device_opcode_);
60 this->transfer_byte(reg);
61 this->transfer_byte(value);
62
63 this->disable();
64 return true;
65}
66
67} // namespace mcp23s17
68} // namespace esphome
void dump_config() override
Definition mcp23s17.cpp:43
void set_device_address(uint8_t device_addr)
Definition mcp23s17.cpp:9
bool read_reg(uint8_t reg, uint8_t *value) override
Definition mcp23s17.cpp:48
bool write_reg(uint8_t reg, uint8_t value) override
Definition mcp23s17.cpp:57
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7