ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
led_strip.cpp
Go to the documentation of this file.
1#include "led_strip.h"
2
3#ifdef USE_RP2
4
6#include "esphome/core/log.h"
7
8#include <hardware/clocks.h>
9#include <hardware/dma.h>
10#include <hardware/irq.h>
11#include <hardware/pio.h>
12#include <pico/sem.h>
13#include <pico/stdlib.h>
14
16
17ESPHOME_LOG_TAG(TAG, "rp2040_pio_led_strip");
18
19// DMA interrupt service routine
21 uint32_t channel = dma_hw->ints0;
22 for (uint dma_chan = 0; dma_chan < NUM_DMA_CHANNELS; ++dma_chan) {
23 if (RP2040PIOLEDStripLightOutput::dma_chan_active[dma_chan] && (channel & (1u << dma_chan))) {
24 dma_hw->ints0 = (1u << dma_chan); // Clear the interrupt
25 sem_release(&RP2040PIOLEDStripLightOutput::dma_write_complete_sem[dma_chan]); // Handle the interrupt
26 }
27 }
28}
29
31 RAMAllocator<uint8_t> allocator;
32 if (!this->internal_accessor_.allocate(allocator, this->num_leds_)) {
33 this->mark_failed(LOG_STR(ESP_LOG_MSG_ALLOC_FAIL));
34 return;
35 }
36
37 // Initialize the PIO program
38
39 // Select PIO instance to use (0 or 1)
40 if (this->pio_ == nullptr) {
41 this->mark_failed(LOG_STR("Failed to claim PIO instance"));
42 return;
43 }
44
45 // if there are multiple strips, we can reuse the same PIO program and save space
46 // but there are only 4 state machines on each PIO so we can only have 4 strips per PIO
47 uint offset = 0;
48
49 if (RP2040PIOLEDStripLightOutput::num_instance[this->pio_ == pio0 ? 0 : 1] >= 4) {
50 this->mark_failed(LOG_STR("Too many instances of PIO program"));
51 return;
52 }
53 // keep track of how many instances of the PIO program are running on each PIO
54 RP2040PIOLEDStripLightOutput::num_instance[this->pio_ == pio0 ? 0 : 1]++;
55
56 // if there are multiple strips of the same chipset, we can reuse the same PIO program and save space
57 if (RP2040PIOLEDStripLightOutput::conf_count[this->chipset_]) {
58 offset = RP2040PIOLEDStripLightOutput::chipset_offsets[this->chipset_];
59 } else {
60 // Load the assembled program into the PIO and get its location in the PIO's instruction memory and save it
61 offset = pio_add_program(this->pio_, this->program_);
62 RP2040PIOLEDStripLightOutput::chipset_offsets[this->chipset_] = offset;
63 RP2040PIOLEDStripLightOutput::conf_count[this->chipset_] = true;
64 }
65
66 // Configure the state machine's PIO, and start it
67 int sm = pio_claim_unused_sm(this->pio_, false);
68 if (sm < 0) {
69 this->mark_failed(LOG_STR("Failed to claim PIO state machine"));
70 return;
71 }
72 this->sm_ = sm;
73
74 // Other code may already hold every DMA channel
75 int dma_chan = dma_claim_unused_channel(false);
76 if (dma_chan < 0) {
77 pio_sm_unclaim(this->pio_, this->sm_);
78 this->mark_failed(LOG_STR("Failed to claim DMA channel"));
79 return;
80 }
81 this->dma_chan_ = dma_chan;
82
83 // Mark the DMA channel as active
84 RP2040PIOLEDStripLightOutput::dma_chan_active[this->dma_chan_] = true;
85
86 this->dma_config_ = dma_channel_get_default_config(this->dma_chan_);
87 channel_config_set_transfer_data_size(
88 &this->dma_config_,
89 DMA_SIZE_8); // 8 bit transfers (could be 32 but the pio program would need to be changed to handle junk data)
90 channel_config_set_read_increment(&this->dma_config_, true); // increment the read address
91 channel_config_set_write_increment(&this->dma_config_, false); // don't increment the write address
92 channel_config_set_dreq(&this->dma_config_,
93 pio_get_dreq(this->pio_, this->sm_, true)); // set the DREQ to the state machine's TX FIFO
94
95 dma_channel_configure(this->dma_chan_, &this->dma_config_,
96 &this->pio_->txf[this->sm_], // write to the state machine's TX FIFO
97 this->internal_accessor_.color_data, // read from memory
98 this->get_buffer_size_(), // number of bytes to transfer
99 false // don't start yet
100 );
101
102 // Initialize the semaphore for this DMA channel
103 sem_init(&RP2040PIOLEDStripLightOutput::dma_write_complete_sem[this->dma_chan_], 1, 1);
104
105 irq_set_exclusive_handler(DMA_IRQ_0, dma_write_complete_handler); // after DMA all data, raise an interrupt
106 dma_channel_set_irq0_enabled(this->dma_chan_, true); // map DMA channel to interrupt
107 irq_set_enabled(DMA_IRQ_0, true); // enable interrupt
108
109 this->init_(this->pio_, this->sm_, offset, this->pin_, this->max_refresh_rate_);
110
111 this->accessor_ = &this->internal_accessor_;
112}
113
115 ESP_LOGVV(TAG, "Writing state");
116
117 if (this->is_failed()) {
118 ESP_LOGW(TAG, "Light is in failed state, not writing state.");
119 return;
120 }
121
122 if (this->internal_accessor_.color_data == nullptr) {
123 ESP_LOGW(TAG, "Buffer is null, not writing state.");
124 return;
125 }
126
127 // the bits are already in the correct order for the pio program so we can just copy the buffer using DMA
128 sem_acquire_blocking(&RP2040PIOLEDStripLightOutput::dma_write_complete_sem[this->dma_chan_]);
129 dma_channel_transfer_from_buffer_now(this->dma_chan_, this->internal_accessor_.color_data, this->get_buffer_size_());
130}
131
133 char channel_colors[5];
134 ESP_LOGCONFIG(TAG,
135 "RP2040 PIO LED Strip Light Output:\n"
136 " Pin: GPIO%d\n"
137 " Number of LEDs: %d\n"
138 " Channel colors: %s\n"
139 " Max Refresh Rate: %f Hz",
140 this->pin_, this->num_leds_, this->internal_accessor_.channel_colors.to_string(channel_colors),
141 this->max_refresh_rate_);
142}
143
145
146} // namespace esphome::rp2040_pio_led_strip
147
148#endif
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:287
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:2396
bool allocate(RAMAllocator< uint8_t > &allocator, size_t num_leds, size_t extra_bytes=0)
Allocate the color and effect data for num_leds LEDs as one zeroed block, followed by extra_bytes mor...
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:68
void write_state(light::LightState *state) override
light::CorrectedBufferColorAccessor internal_accessor_
Definition led_strip.h:63
bool state
Definition fan.h:2
ESPHOME_LOG_TAG(TAG, "rp2040_pio_led_strip")
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:43
static void uint32_t
const char * to_string(char *buf) const
Write the order back out as text, e.g.