mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	support spi for sn74hc595 (#5491)
Co-authored-by: Jimmy Hedman <jimmy.hedman@gmail.com> Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
		
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							78e3ce7718
						
					
				
				
					commit
					ccffbfd3ae
				
			| @@ -1,8 +1,10 @@ | |||||||
| import esphome.codegen as cg | import esphome.codegen as cg | ||||||
| import esphome.config_validation as cv | import esphome.config_validation as cv | ||||||
| from esphome import pins | from esphome import pins | ||||||
|  | from esphome.components import spi | ||||||
| from esphome.const import ( | from esphome.const import ( | ||||||
|     CONF_ID, |     CONF_ID, | ||||||
|  |     CONF_SPI_ID, | ||||||
|     CONF_MODE, |     CONF_MODE, | ||||||
|     CONF_NUMBER, |     CONF_NUMBER, | ||||||
|     CONF_INVERTED, |     CONF_INVERTED, | ||||||
| @@ -10,13 +12,20 @@ from esphome.const import ( | |||||||
|     CONF_CLOCK_PIN, |     CONF_CLOCK_PIN, | ||||||
|     CONF_OUTPUT, |     CONF_OUTPUT, | ||||||
| ) | ) | ||||||
|  | from esphome.core import EsphomeError | ||||||
|  |  | ||||||
| DEPENDENCIES = [] |  | ||||||
| MULTI_CONF = True | MULTI_CONF = True | ||||||
|  |  | ||||||
| sn74hc595_ns = cg.esphome_ns.namespace("sn74hc595") | sn74hc595_ns = cg.esphome_ns.namespace("sn74hc595") | ||||||
|  |  | ||||||
| SN74HC595Component = sn74hc595_ns.class_("SN74HC595Component", cg.Component) | SN74HC595Component = sn74hc595_ns.class_("SN74HC595Component", cg.Component) | ||||||
|  | SN74HC595GPIOComponent = sn74hc595_ns.class_( | ||||||
|  |     "SN74HC595GPIOComponent", SN74HC595Component | ||||||
|  | ) | ||||||
|  | SN74HC595SPIComponent = sn74hc595_ns.class_( | ||||||
|  |     "SN74HC595SPIComponent", SN74HC595Component, spi.SPIDevice | ||||||
|  | ) | ||||||
|  |  | ||||||
| SN74HC595GPIOPin = sn74hc595_ns.class_( | SN74HC595GPIOPin = sn74hc595_ns.class_( | ||||||
|     "SN74HC595GPIOPin", cg.GPIOPin, cg.Parented.template(SN74HC595Component) |     "SN74HC595GPIOPin", cg.GPIOPin, cg.Parented.template(SN74HC595Component) | ||||||
| ) | ) | ||||||
| @@ -25,25 +34,51 @@ CONF_SN74HC595 = "sn74hc595" | |||||||
| CONF_LATCH_PIN = "latch_pin" | CONF_LATCH_PIN = "latch_pin" | ||||||
| CONF_OE_PIN = "oe_pin" | CONF_OE_PIN = "oe_pin" | ||||||
| CONF_SR_COUNT = "sr_count" | CONF_SR_COUNT = "sr_count" | ||||||
| CONFIG_SCHEMA = cv.Schema( |  | ||||||
|  |  | ||||||
|  | CONFIG_SCHEMA = cv.Any( | ||||||
|  |     cv.Schema( | ||||||
|         { |         { | ||||||
|         cv.Required(CONF_ID): cv.declare_id(SN74HC595Component), |             cv.Required(CONF_ID): cv.declare_id(SN74HC595GPIOComponent), | ||||||
|             cv.Required(CONF_DATA_PIN): pins.gpio_output_pin_schema, |             cv.Required(CONF_DATA_PIN): pins.gpio_output_pin_schema, | ||||||
|             cv.Required(CONF_CLOCK_PIN): pins.gpio_output_pin_schema, |             cv.Required(CONF_CLOCK_PIN): pins.gpio_output_pin_schema, | ||||||
|             cv.Required(CONF_LATCH_PIN): pins.gpio_output_pin_schema, |             cv.Required(CONF_LATCH_PIN): pins.gpio_output_pin_schema, | ||||||
|             cv.Optional(CONF_OE_PIN): pins.gpio_output_pin_schema, |             cv.Optional(CONF_OE_PIN): pins.gpio_output_pin_schema, | ||||||
|             cv.Optional(CONF_SR_COUNT, default=1): cv.int_range(min=1, max=256), |             cv.Optional(CONF_SR_COUNT, default=1): cv.int_range(min=1, max=256), | ||||||
|         } |         } | ||||||
| ).extend(cv.COMPONENT_SCHEMA) |     ).extend(cv.COMPONENT_SCHEMA), | ||||||
|  |     cv.Schema( | ||||||
|  |         { | ||||||
|  |             cv.Required(CONF_ID): cv.declare_id(SN74HC595SPIComponent), | ||||||
|  |             cv.Required(CONF_LATCH_PIN): pins.gpio_output_pin_schema, | ||||||
|  |             cv.Optional(CONF_OE_PIN): pins.gpio_output_pin_schema, | ||||||
|  |             cv.Optional(CONF_SR_COUNT, default=1): cv.int_range(min=1, max=256), | ||||||
|  |         } | ||||||
|  |     ) | ||||||
|  |     .extend(cv.COMPONENT_SCHEMA) | ||||||
|  |     .extend(spi.spi_device_schema(cs_pin_required=False)) | ||||||
|  |     .extend( | ||||||
|  |         { | ||||||
|  |             cv.Required(CONF_SPI_ID): cv.use_id(spi.SPIComponent), | ||||||
|  |         } | ||||||
|  |     ), | ||||||
|  |     msg='Either "data_pin" and "clock_pin" must be set or "spi_id" must be set.', | ||||||
|  | ) | ||||||
|  |  | ||||||
|  |  | ||||||
| async def to_code(config): | async def to_code(config): | ||||||
|     var = cg.new_Pvariable(config[CONF_ID]) |     var = cg.new_Pvariable(config[CONF_ID]) | ||||||
|     await cg.register_component(var, config) |     await cg.register_component(var, config) | ||||||
|  |     if CONF_DATA_PIN in config: | ||||||
|         data_pin = await cg.gpio_pin_expression(config[CONF_DATA_PIN]) |         data_pin = await cg.gpio_pin_expression(config[CONF_DATA_PIN]) | ||||||
|         cg.add(var.set_data_pin(data_pin)) |         cg.add(var.set_data_pin(data_pin)) | ||||||
|         clock_pin = await cg.gpio_pin_expression(config[CONF_CLOCK_PIN]) |         clock_pin = await cg.gpio_pin_expression(config[CONF_CLOCK_PIN]) | ||||||
|         cg.add(var.set_clock_pin(clock_pin)) |         cg.add(var.set_clock_pin(clock_pin)) | ||||||
|  |     elif CONF_SPI_ID in config: | ||||||
|  |         await spi.register_spi_device(var, config) | ||||||
|  |     else: | ||||||
|  |         raise EsphomeError("Not supported") | ||||||
|  |  | ||||||
|     latch_pin = await cg.gpio_pin_expression(config[CONF_LATCH_PIN]) |     latch_pin = await cg.gpio_pin_expression(config[CONF_LATCH_PIN]) | ||||||
|     cg.add(var.set_latch_pin(latch_pin)) |     cg.add(var.set_latch_pin(latch_pin)) | ||||||
|     if CONF_OE_PIN in config: |     if CONF_OE_PIN in config: | ||||||
|   | |||||||
| @@ -6,26 +6,39 @@ namespace sn74hc595 { | |||||||
|  |  | ||||||
| static const char *const TAG = "sn74hc595"; | static const char *const TAG = "sn74hc595"; | ||||||
|  |  | ||||||
| void SN74HC595Component::setup() { | void SN74HC595Component::pre_setup_() { | ||||||
|   ESP_LOGCONFIG(TAG, "Setting up SN74HC595..."); |   ESP_LOGCONFIG(TAG, "Setting up SN74HC595..."); | ||||||
|  |  | ||||||
|   if (this->have_oe_pin_) {  // disable output |   if (this->have_oe_pin_) {  // disable output | ||||||
|     this->oe_pin_->setup(); |     this->oe_pin_->setup(); | ||||||
|     this->oe_pin_->digital_write(true); |     this->oe_pin_->digital_write(true); | ||||||
|   } |   } | ||||||
|  | } | ||||||
|   // initialize output pins | void SN74HC595Component::post_setup_() { | ||||||
|   this->clock_pin_->setup(); |  | ||||||
|   this->data_pin_->setup(); |  | ||||||
|   this->latch_pin_->setup(); |   this->latch_pin_->setup(); | ||||||
|   this->clock_pin_->digital_write(false); |  | ||||||
|   this->data_pin_->digital_write(false); |  | ||||||
|   this->latch_pin_->digital_write(false); |   this->latch_pin_->digital_write(false); | ||||||
|  |  | ||||||
|   // send state to shift register |   // send state to shift register | ||||||
|   this->write_gpio_(); |   this->write_gpio(); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void SN74HC595GPIOComponent::setup() { | ||||||
|  |   this->pre_setup_(); | ||||||
|  |   this->clock_pin_->setup(); | ||||||
|  |   this->data_pin_->setup(); | ||||||
|  |   this->clock_pin_->digital_write(false); | ||||||
|  |   this->data_pin_->digital_write(false); | ||||||
|  |   this->post_setup_(); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | #ifdef USE_SPI | ||||||
|  | void SN74HC595SPIComponent::setup() { | ||||||
|  |   this->pre_setup_(); | ||||||
|  |   this->spi_setup(); | ||||||
|  |   this->post_setup_(); | ||||||
|  | } | ||||||
|  | #endif | ||||||
|  |  | ||||||
| void SN74HC595Component::dump_config() { ESP_LOGCONFIG(TAG, "SN74HC595:"); } | void SN74HC595Component::dump_config() { ESP_LOGCONFIG(TAG, "SN74HC595:"); } | ||||||
|  |  | ||||||
| void SN74HC595Component::digital_write_(uint16_t pin, bool value) { | void SN74HC595Component::digital_write_(uint16_t pin, bool value) { | ||||||
| @@ -34,17 +47,38 @@ void SN74HC595Component::digital_write_(uint16_t pin, bool value) { | |||||||
|              (this->sr_count_ * 8) - 1); |              (this->sr_count_ * 8) - 1); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|   this->output_bits_[pin] = value; |   if (value) { | ||||||
|   this->write_gpio_(); |     this->output_bytes_[pin / 8] |= (1 << (pin % 8)); | ||||||
|  |   } else { | ||||||
|  |     this->output_bytes_[pin / 8] &= ~(1 << (pin % 8)); | ||||||
|  |   } | ||||||
|  |   this->write_gpio(); | ||||||
| } | } | ||||||
|  |  | ||||||
| void SN74HC595Component::write_gpio_() { | void SN74HC595GPIOComponent::write_gpio() { | ||||||
|   for (auto bit = this->output_bits_.rbegin(); bit != this->output_bits_.rend(); bit++) { |   for (auto byte = this->output_bytes_.rbegin(); byte != this->output_bytes_.rend(); byte++) { | ||||||
|     this->data_pin_->digital_write(*bit); |     for (int8_t i = 7; i >= 0; i--) { | ||||||
|  |       bool bit = (*byte >> i) & 1; | ||||||
|  |       this->data_pin_->digital_write(bit); | ||||||
|       this->clock_pin_->digital_write(true); |       this->clock_pin_->digital_write(true); | ||||||
|       this->clock_pin_->digital_write(false); |       this->clock_pin_->digital_write(false); | ||||||
|     } |     } | ||||||
|  |   } | ||||||
|  |   SN74HC595Component::write_gpio(); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | #ifdef USE_SPI | ||||||
|  | void SN74HC595SPIComponent::write_gpio() { | ||||||
|  |   for (auto byte = this->output_bytes_.rbegin(); byte != this->output_bytes_.rend(); byte++) { | ||||||
|  |     this->enable(); | ||||||
|  |     this->transfer_byte(*byte); | ||||||
|  |     this->disable(); | ||||||
|  |   } | ||||||
|  |   SN74HC595Component::write_gpio(); | ||||||
|  | } | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | void SN74HC595Component::write_gpio() { | ||||||
|   // pulse latch to activate new values |   // pulse latch to activate new values | ||||||
|   this->latch_pin_->digital_write(true); |   this->latch_pin_->digital_write(true); | ||||||
|   this->latch_pin_->digital_write(false); |   this->latch_pin_->digital_write(false); | ||||||
| @@ -60,11 +94,7 @@ float SN74HC595Component::get_setup_priority() const { return setup_priority::IO | |||||||
| void SN74HC595GPIOPin::digital_write(bool value) { | void SN74HC595GPIOPin::digital_write(bool value) { | ||||||
|   this->parent_->digital_write_(this->pin_, value != this->inverted_); |   this->parent_->digital_write_(this->pin_, value != this->inverted_); | ||||||
| } | } | ||||||
| std::string SN74HC595GPIOPin::dump_summary() const { | std::string SN74HC595GPIOPin::dump_summary() const { return str_snprintf("%u via SN74HC595", 18, pin_); } | ||||||
|   char buffer[32]; |  | ||||||
|   snprintf(buffer, sizeof(buffer), "%u via SN74HC595", pin_); |  | ||||||
|   return buffer; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| }  // namespace sn74hc595 | }  // namespace sn74hc595 | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|   | |||||||
| @@ -1,9 +1,14 @@ | |||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
| #include "esphome/core/component.h" | #include "esphome/core/component.h" | ||||||
|  | #include "esphome/core/defines.h" | ||||||
| #include "esphome/core/hal.h" | #include "esphome/core/hal.h" | ||||||
| #include "esphome/core/helpers.h" | #include "esphome/core/helpers.h" | ||||||
|  |  | ||||||
|  | #ifdef USE_SPI | ||||||
|  | #include "esphome/components/spi/spi.h" | ||||||
|  | #endif | ||||||
|  |  | ||||||
| #include <vector> | #include <vector> | ||||||
|  |  | ||||||
| namespace esphome { | namespace esphome { | ||||||
| @@ -13,34 +18,33 @@ class SN74HC595Component : public Component { | |||||||
|  public: |  public: | ||||||
|   SN74HC595Component() = default; |   SN74HC595Component() = default; | ||||||
|  |  | ||||||
|   void setup() override; |   void setup() override = 0; | ||||||
|   float get_setup_priority() const override; |   float get_setup_priority() const override; | ||||||
|   void dump_config() override; |   void dump_config() override; | ||||||
|  |  | ||||||
|   void set_data_pin(GPIOPin *pin) { data_pin_ = pin; } |   void set_latch_pin(GPIOPin *pin) { this->latch_pin_ = pin; } | ||||||
|   void set_clock_pin(GPIOPin *pin) { clock_pin_ = pin; } |  | ||||||
|   void set_latch_pin(GPIOPin *pin) { latch_pin_ = pin; } |  | ||||||
|   void set_oe_pin(GPIOPin *pin) { |   void set_oe_pin(GPIOPin *pin) { | ||||||
|     oe_pin_ = pin; |     this->oe_pin_ = pin; | ||||||
|     have_oe_pin_ = true; |     this->have_oe_pin_ = true; | ||||||
|   } |   } | ||||||
|   void set_sr_count(uint8_t count) { |   void set_sr_count(uint8_t count) { | ||||||
|     sr_count_ = count; |     this->sr_count_ = count; | ||||||
|     this->output_bits_.resize(count * 8); |     this->output_bytes_.resize(count); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  protected: |  protected: | ||||||
|   friend class SN74HC595GPIOPin; |   friend class SN74HC595GPIOPin; | ||||||
|   void digital_write_(uint16_t pin, bool value); |   void digital_write_(uint16_t pin, bool value); | ||||||
|   void write_gpio_(); |   virtual void write_gpio(); | ||||||
|  |  | ||||||
|  |   void pre_setup_(); | ||||||
|  |   void post_setup_(); | ||||||
|  |  | ||||||
|   GPIOPin *data_pin_; |  | ||||||
|   GPIOPin *clock_pin_; |  | ||||||
|   GPIOPin *latch_pin_; |   GPIOPin *latch_pin_; | ||||||
|   GPIOPin *oe_pin_; |   GPIOPin *oe_pin_; | ||||||
|   uint8_t sr_count_; |   uint8_t sr_count_; | ||||||
|   bool have_oe_pin_{false}; |   bool have_oe_pin_{false}; | ||||||
|   std::vector<bool> output_bits_; |   std::vector<uint8_t> output_bytes_; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| /// Helper class to expose a SC74HC595 pin as an internal output GPIO pin. | /// Helper class to expose a SC74HC595 pin as an internal output GPIO pin. | ||||||
| @@ -60,5 +64,31 @@ class SN74HC595GPIOPin : public GPIOPin, public Parented<SN74HC595Component> { | |||||||
|   bool inverted_; |   bool inverted_; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | class SN74HC595GPIOComponent : public SN74HC595Component { | ||||||
|  |  public: | ||||||
|  |   void setup() override; | ||||||
|  |   void set_data_pin(GPIOPin *pin) { data_pin_ = pin; } | ||||||
|  |   void set_clock_pin(GPIOPin *pin) { clock_pin_ = pin; } | ||||||
|  |  | ||||||
|  |  protected: | ||||||
|  |   void write_gpio() override; | ||||||
|  |  | ||||||
|  |   GPIOPin *data_pin_; | ||||||
|  |   GPIOPin *clock_pin_; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | #ifdef USE_SPI | ||||||
|  | class SN74HC595SPIComponent : public SN74HC595Component, | ||||||
|  |                               public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, | ||||||
|  |                                                     spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_4MHZ> { | ||||||
|  |  public: | ||||||
|  |   void setup() override; | ||||||
|  |  | ||||||
|  |  protected: | ||||||
|  |   void write_gpio() override; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | #endif | ||||||
|  |  | ||||||
| }  // namespace sn74hc595 | }  // namespace sn74hc595 | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|   | |||||||
| @@ -189,6 +189,7 @@ i2c: | |||||||
|   id: i2c_bus |   id: i2c_bus | ||||||
|  |  | ||||||
| spi: | spi: | ||||||
|  |   id: spi_bus | ||||||
|   clk_pin: GPIO21 |   clk_pin: GPIO21 | ||||||
|   mosi_pin: GPIO22 |   mosi_pin: GPIO22 | ||||||
|   miso_pin: GPIO23 |   miso_pin: GPIO23 | ||||||
| @@ -2737,7 +2738,7 @@ switch: | |||||||
|   - platform: gpio |   - platform: gpio | ||||||
|     name: "SN74HC595 Pin #0" |     name: "SN74HC595 Pin #0" | ||||||
|     pin: |     pin: | ||||||
|       sn74hc595: sn74hc595_hub |       sn74hc595: sn74hc595_hub_2 | ||||||
|       # Use pin number 0 |       # Use pin number 0 | ||||||
|       number: 0 |       number: 0 | ||||||
|       inverted: false |       inverted: false | ||||||
| @@ -3385,6 +3386,11 @@ sn74hc595: | |||||||
|     latch_pin: GPIO22 |     latch_pin: GPIO22 | ||||||
|     oe_pin: GPIO32 |     oe_pin: GPIO32 | ||||||
|     sr_count: 2 |     sr_count: 2 | ||||||
|  |   - id: sn74hc595_hub_2 | ||||||
|  |     latch_pin: GPIO22 | ||||||
|  |     oe_pin: GPIO32 | ||||||
|  |     sr_count: 2 | ||||||
|  |     spi_id: spi_bus | ||||||
|  |  | ||||||
| rtttl: | rtttl: | ||||||
|   output: gpio_19 |   output: gpio_19 | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user