From 641eea5fba6afa16452567b95f851ffcb89ddb81 Mon Sep 17 00:00:00 2001 From: unregist <22579156+unregist@users.noreply.github.com> Date: Fri, 6 Dec 2024 08:29:36 +1000 Subject: [PATCH] Move pin configuration to YAML Move pin configuration to YAML --- .../components/MhiAcCtrl/MHI-AC-Ctrl-core.cpp | 43 +++++++++++++------ .../components/MhiAcCtrl/MHI-AC-Ctrl-core.h | 10 ++--- esphome/components/MhiAcCtrl/climate.py | 41 ++++++++++++++++-- esphome/components/MhiAcCtrl/mhi_ac_ctrl.h | 11 +++++ esphome/example.yaml | 6 +++ 5 files changed, 91 insertions(+), 20 deletions(-) diff --git a/esphome/components/MhiAcCtrl/MHI-AC-Ctrl-core.cpp b/esphome/components/MhiAcCtrl/MHI-AC-Ctrl-core.cpp index 22bcc89..8776a65 100644 --- a/esphome/components/MhiAcCtrl/MHI-AC-Ctrl-core.cpp +++ b/esphome/components/MhiAcCtrl/MHI-AC-Ctrl-core.cpp @@ -8,6 +8,7 @@ #include #include +#include "esphome/core/gpio.h" #include "driver/timer.h" #include "driver/spi_slave.h" #include "driver/gpio.h" @@ -43,6 +44,12 @@ static uint8_t mosi_frame_snapshot[DB14]; static uint8_t mosi_frame_snapshot_prev[DB14]; static uint32_t frame_errors = 0; +extern int gpio_mosi_pin; +extern int gpio_miso_pin; +extern int gpio_sclk_pin; +extern int gpio_cs_in_pin; +extern int gpio_cs_out_pin; + namespace mhi_ac { MHIEnergy mhi_energy(230); @@ -58,12 +65,14 @@ static bool IRAM_ATTR timer_group_isr_callback(void *args) esp_err_t err; BaseType_t xHigherPriorityTaskWoken; // Trigger Chip Select - gpio_set_level(GPIO_CS_OUT, 1); + gpio_set_level((gpio_num_t)gpio_cs_out_pin, 1); + // gpio_set_level(GPIO_CS_OUT, 1); if(ready) { spi_slave_transaction_t *t = (spi_slave_transaction_t *) args; err = spi_slave_queue_trans(RCV_HOST, t, 0); } - gpio_set_level(GPIO_CS_OUT, 0); + gpio_set_level((gpio_num_t)gpio_cs_out_pin, 0); + // gpio_set_level(GPIO_CS_OUT, 0); return false; } @@ -322,9 +331,12 @@ static void mhi_poll_task(void *arg) // configuration for the SPI bus spi_bus_config_t buscfg={ - .mosi_io_num = GPIO_MOSI, - .miso_io_num = GPIO_MISO, - .sclk_io_num = GPIO_SCLK, + .mosi_io_num = (gpio_num_t)gpio_mosi_pin, + .miso_io_num = (gpio_num_t)gpio_miso_pin, + .sclk_io_num = (gpio_num_t)gpio_sclk_pin, + // .mosi_io_num = GPIO_MOSI, + // .miso_io_num = GPIO_MISO, + // .sclk_io_num = GPIO_SCLK, .quadwp_io_num = -1, .quadhd_io_num = -1, .flags = SPICOMMON_BUSFLAG_GPIO_PINS | SPICOMMON_BUSFLAG_SLAVE, @@ -332,7 +344,8 @@ static void mhi_poll_task(void *arg) // configuration for the SPI slave interface spi_slave_interface_config_t slvcfg={ - .spics_io_num = GPIO_CS_IN, + .spics_io_num = (gpio_num_t)gpio_cs_in_pin, + // .spics_io_num = GPIO_CS_IN, .flags = SPI_SLAVE_BIT_LSBFIRST, .queue_size = 1, .mode = 3, //CPOL=1, CPHA=1 @@ -366,7 +379,8 @@ static void mhi_poll_task(void *arg) gpio_config_t io_conf = {}; io_conf.mode = GPIO_MODE_INPUT; - io_conf.pin_bit_mask = (1ULL<get_pin(); } + void set_miso_pin(InternalGPIOPin *pin) { gpio_miso_pin = pin->get_pin(); } + void set_sclk_pin(InternalGPIOPin *pin) { gpio_sclk_pin = pin->get_pin(); } + void set_cs_in_pin(InternalGPIOPin *pin) { gpio_cs_in_pin = pin->get_pin(); } + void set_cs_out_pin(InternalGPIOPin *pin) { gpio_cs_out_pin = pin->get_pin(); } void setup() override { auto restore = this->restore_state_(); diff --git a/esphome/example.yaml b/esphome/example.yaml index cab62f3..6aef0a8 100755 --- a/esphome/example.yaml +++ b/esphome/example.yaml @@ -30,6 +30,12 @@ climate: mhi_ac_ctrl_id: ${mhi_device_id} id: ${device_id} name: "${device_name}" + ## Optional PIN config. Default values: + #mosi_pin: GPIO7 + #miso_pin: GPIO2 + #sclk_pin: GPIO6 + #cs_in_pin: GPIO10 + #cs_out_pin: GPIO9 select: - platform: MhiAcCtrl