ESPHome  2024.12.2
mcp23s17.cpp
Go to the documentation of this file.
1 #include "mcp23s17.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace mcp23s17 {
6 
7 static const char *const TAG = "mcp23s17";
8 
9 void 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, "Setting up MCP23S17...");
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 
49 bool 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 
58 bool 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
GPIOPin * cs_
Definition: spi.h:378
void set_device_address(uint8_t device_addr)
Definition: mcp23s17.cpp:9
void setup() override
Definition: mcp23s17.cpp:15
void dump_config() override
Definition: mcp23s17.cpp:44
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool write_reg(uint8_t reg, uint8_t value) override
Definition: mcp23s17.cpp:58
stm32_cmd_t * cmd
Definition: stm32flash.h:96
bool read_reg(uint8_t reg, uint8_t *value) override
Definition: mcp23s17.cpp:49