8 #include <hardware/clocks.h> 9 #include <hardware/dma.h> 10 #include <hardware/irq.h> 11 #include <hardware/pio.h> 12 #include <pico/stdlib.h> 16 namespace rp2040_pio_led_strip {
18 static const char *TAG =
"rp2040_pio_led_strip";
20 static uint8_t num_instance_[2] = {0, 0};
21 static std::map<Chipset, uint> chipset_offsets_ = {
22 {
CHIPSET_WS2812, 0}, {
CHIPSET_WS2812B, 0}, {
CHIPSET_SK6812, 0}, {
CHIPSET_SM16703, 0}, {
CHIPSET_CUSTOM, 0},
24 static std::map<Chipset, bool> conf_count_ = {
28 static bool dma_chan_active_[12];
29 static struct semaphore dma_write_complete_sem_[12];
33 uint32_t channel = dma_hw->ints0;
34 for (uint dma_chan = 0; dma_chan < 12; ++dma_chan) {
35 if (RP2040PIOLEDStripLightOutput::dma_chan_active_[dma_chan] && (channel & (1u << dma_chan))) {
36 dma_hw->ints0 = (1u << dma_chan);
37 sem_release(&RP2040PIOLEDStripLightOutput::dma_write_complete_sem_[dma_chan]);
43 ESP_LOGCONFIG(TAG,
"Setting up RP2040 LED Strip...");
49 if (this->
buf_ ==
nullptr) {
50 ESP_LOGE(TAG,
"Failed to allocate buffer of size %u", buffer_size);
57 ESP_LOGE(TAG,
"Failed to allocate effect data of size %u", this->
num_leds_);
65 if (this->
pio_ ==
nullptr) {
66 ESP_LOGE(TAG,
"Failed to claim PIO instance");
75 if (RP2040PIOLEDStripLightOutput::num_instance_[this->
pio_ == pio0 ? 0 : 1] > 4) {
76 ESP_LOGE(TAG,
"Too many instances of PIO program");
81 RP2040PIOLEDStripLightOutput::num_instance_[this->
pio_ == pio0 ? 0 : 1]++;
84 if (this->conf_count_[this->
chipset_]) {
85 offset = RP2040PIOLEDStripLightOutput::chipset_offsets_[this->
chipset_];
88 offset = pio_add_program(this->pio_, this->
program_);
89 RP2040PIOLEDStripLightOutput::chipset_offsets_[this->
chipset_] = offset;
90 RP2040PIOLEDStripLightOutput::conf_count_[this->
chipset_] =
true;
94 this->
sm_ = pio_claim_unused_sm(this->pio_,
true);
97 ESP_LOGE(TAG,
"Failed to claim PIO state machine");
104 this->
dma_chan_ = dma_claim_unused_channel(
true);
106 ESP_LOGE(TAG,
"Failed to claim DMA channel");
112 RP2040PIOLEDStripLightOutput::dma_chan_active_[this->
dma_chan_] =
true;
115 channel_config_set_transfer_data_size(
118 channel_config_set_read_increment(&this->
dma_config_,
true);
119 channel_config_set_write_increment(&this->
dma_config_,
false);
121 pio_get_dreq(this->pio_, this->
sm_,
true));
124 &this->pio_->txf[this->sm_],
131 sem_init(&RP2040PIOLEDStripLightOutput::dma_write_complete_sem_[this->
dma_chan_], 1, 1);
134 dma_channel_set_irq0_enabled(this->dma_chan_,
true);
135 irq_set_enabled(DMA_IRQ_0,
true);
141 ESP_LOGVV(TAG,
"Writing state...");
144 ESP_LOGW(TAG,
"Light is in failed state, not writing state.");
148 if (this->
buf_ ==
nullptr) {
149 ESP_LOGW(TAG,
"Buffer is null, not writing state.");
154 sem_acquire_blocking(&RP2040PIOLEDStripLightOutput::dma_write_complete_sem_[this->
dma_chan_]);
159 int32_t r = 0, g = 0, b = 0, w = 0;
192 uint8_t multiplier = this->
is_rgbw_ ? 4 : 3;
193 return {this->
buf_ + (index * multiplier) + r,
194 this->
buf_ + (index * multiplier) + g,
195 this->
buf_ + (index * multiplier) + b,
196 this->
is_rgbw_ ? this->
buf_ + (index * multiplier) + 3 :
nullptr,
202 ESP_LOGCONFIG(TAG,
"RP2040 PIO LED Strip Light Output:");
203 ESP_LOGCONFIG(TAG,
" Pin: GPIO%d", this->
pin_);
204 ESP_LOGCONFIG(TAG,
" Number of LEDs: %d", this->
num_leds_);
205 ESP_LOGCONFIG(TAG,
" RGBW: %s", YESNO(this->
is_rgbw_));
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
void write_state(light::LightState *state) override
static void dma_write_complete_handler_()
void dump_config() override
const char * rgb_order_to_string(RGBOrder order)
dma_channel_config dma_config_
const pio_program_t * program_
light::ESPColorView get_view_internal(int32_t index) const override
float get_setup_priority() const override
size_t get_buffer_size_() const
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
ESPColorCorrection correction_
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.