ESPHome 2025.6.3
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 ESP_LOGCONFIG(TAG, "Running setup");
17 this->spi_setup();
18
19 this->enable();
20 uint8_t cmd = 0b01000000;
21 this->transfer_byte(cmd);
23 this->transfer_byte(0b00011000); // Enable HAEN pins for addressing
24 this->disable();
25
26 this->enable();
27 cmd = 0b01001000;
28 this->transfer_byte(cmd);
30 this->transfer_byte(0b00011000); // Enable HAEN pins for addressing
31 this->disable();
32
33 // Read current output register state
36
37 if (this->open_drain_ints_) {
38 // enable open-drain interrupt pins, 3.3V-safe
41 }
42}
43
45 ESP_LOGCONFIG(TAG, "MCP23S17:");
46 LOG_PIN(" CS Pin: ", this->cs_);
47}
48
49bool MCP23S17::read_reg(uint8_t reg, uint8_t *value) {
50 this->enable();
51 this->transfer_byte(this->device_opcode_ | 1);
52 this->transfer_byte(reg);
53 *value = this->transfer_byte(0xFF);
54 this->disable();
55 return true;
56}
57
58bool MCP23S17::write_reg(uint8_t reg, uint8_t value) {
59 this->enable();
60 this->transfer_byte(this->device_opcode_);
61 this->transfer_byte(reg);
62 this->transfer_byte(value);
63
64 this->disable();
65 return true;
66}
67
68} // namespace mcp23s17
69} // namespace esphome
void dump_config() override
Definition mcp23s17.cpp:44
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:49
bool write_reg(uint8_t reg, uint8_t value) override
Definition mcp23s17.cpp:58
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7