Skip to content

Commit 7053bfa

Browse files
committed
Plasma: Add support for GPIOs >=32.
For both APA102 and WS2812 the pins used on the same PIO must be in the same range. The GPIO base offset applies to the whole PIO and not individual state machines. This means that for APA102 both data and clock must be in the same pin range, ie: either 16-48 inclusive or 0-31 inclusive.
1 parent 1003c12 commit 7053bfa

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

drivers/plasma/apa102.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,26 @@
44
namespace plasma {
55

66
APA102::APA102(uint num_leds, PIO pio, uint sm, uint pin_dat, uint pin_clk, uint freq, RGB* buffer) : buffer(buffer), num_leds(num_leds), pio(pio), sm(sm) {
7+
// NOTE: This sets the gpio_base for *the entire PIO* not just this state machine
8+
uint range_max = std::max(pin_dat, pin_clk);
9+
uint range_min = std::min(pin_dat, pin_clk);
10+
11+
// Both pins in 16-48 range
12+
if(range_max >= 32 && range_min >= 16) {
13+
pio_set_gpio_base(pio, 16);
14+
// Both pins in 0-31 range
15+
} else if(range_max <= 31) {
16+
pio_set_gpio_base(pio, 0);
17+
// Pins in different ranges: invalid combo!
18+
} else {
19+
// TODO: Need some means to notify the caller
20+
pio_set_gpio_base(pio, 0);
21+
}
22+
723
pio_program_offset = pio_add_program(pio, &apa102_program);
824

9-
pio_sm_set_pins_with_mask(pio, sm, 0, (1u << pin_clk) | (1u << pin_dat));
10-
pio_sm_set_pindirs_with_mask(pio, sm, ~0u, (1u << pin_clk) | (1u << pin_dat));
25+
pio_sm_set_pins_with_mask(pio, sm, 0, (1u << (pin_clk - pio_get_gpio_base(pio))) | (1u << (pin_dat - pio_get_gpio_base(pio))));
26+
pio_sm_set_pindirs_with_mask(pio, sm, ~0u, (1u << (pin_clk - pio_get_gpio_base(pio))) | (1u << (pin_dat - pio_get_gpio_base(pio))));
1127
pio_gpio_init(pio, pin_clk);
1228
pio_gpio_init(pio, pin_dat);
1329

drivers/plasma/ws2812.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
namespace plasma {
55

66
WS2812::WS2812(uint num_leds, PIO pio, uint sm, uint pin, uint freq, bool rgbw, COLOR_ORDER color_order, RGB* buffer) : buffer(buffer), num_leds(num_leds), color_order(color_order), pio(pio), sm(sm) {
7+
// NOTE: This sets the gpio_base for *the entire PIO* not just this state machine
8+
pio_set_gpio_base(pio, pin >= 32 ? 16 : 0);
9+
710
pio_program_offset = pio_add_program(pio, &ws2812_program);
811

912
pio_gpio_init(pio, pin);

0 commit comments

Comments
 (0)