|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, Andrey Kunitsyn |
| 3 | + * |
| 4 | + * This file is part of the modm project. |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | + */ |
| 10 | +// ---------------------------------------------------------------------------- |
| 11 | + |
| 12 | +#pragma once |
| 13 | + |
| 14 | +#include <modm/board.hpp> |
| 15 | + |
| 16 | +/* from https://github.com/raspberrypi/pico-examples/blob/master/pio/ws2812/ws2812.pio */ |
| 17 | + |
| 18 | +/* .program ws2812 */ |
| 19 | +namespace ws2812 |
| 20 | +{ |
| 21 | + using namespace modm::platform::pio; |
| 22 | + |
| 23 | + // constants WS2812 |
| 24 | + static constexpr uint32_t T0H = 350; // ns |
| 25 | + static constexpr uint32_t T0L = 800; |
| 26 | + static constexpr uint32_t T1H = 700; |
| 27 | + static constexpr uint32_t T1L = 600; |
| 28 | + |
| 29 | + // constants WS2812B |
| 30 | + // static constexpr uint32_t T0H = 400; // ns |
| 31 | + // static constexpr uint32_t T0L = 850; |
| 32 | + // static constexpr uint32_t T1H = 850; |
| 33 | + // static constexpr uint32_t T1L = 400; |
| 34 | + |
| 35 | + static constexpr uint32_t TimeScale = 50; |
| 36 | + |
| 37 | + static constexpr uint32_t TH_Common = T0H/TimeScale; |
| 38 | + static constexpr uint32_t TH_Add = (T1H-T0H)/TimeScale; |
| 39 | + static constexpr uint32_t TL_Common = T1L/TimeScale; |
| 40 | + static constexpr uint32_t TL_Add = (T0L-T1L)/TimeScale; |
| 41 | + |
| 42 | + // labels |
| 43 | + struct bitloop {}; |
| 44 | + struct do_one {}; |
| 45 | + struct do_zero {}; |
| 46 | + |
| 47 | + /* |
| 48 | + .side_set 1 |
| 49 | + .wrap_target |
| 50 | + bitloop: |
| 51 | + out x, 1 side 0 [T3 - 1] ; Side-set still takes place when instruction stalls |
| 52 | + jmp !x do_zero side 1 [T1 - 1] ; Branch on the bit we shifted out. Positive pulse |
| 53 | + do_one: |
| 54 | + jmp bitloop side 1 [T2 - 1] ; Continue driving high, for a long pulse |
| 55 | + do_zero: |
| 56 | + nop side 0 [T2 - 1] ; Or drive low, for a short pulse |
| 57 | + .wrap |
| 58 | + */ |
| 59 | + |
| 60 | + // PIO program |
| 61 | + static constexpr auto program = PIOProgram::begin() |
| 62 | + .sideset<1> |
| 63 | + .wrapTarget<> |
| 64 | + .label<bitloop> |
| 65 | + .instr(pio::Out.x<1> .side<0>.delay<TL_Common-1>) |
| 66 | + .instr(pio::Jmp.not_x.to<do_zero> .side<1>.delay<TH_Common-1>) |
| 67 | + .label<do_one> |
| 68 | + .instr(pio::Jmp.to<bitloop> .side<1>.delay<TH_Add-1>) |
| 69 | + .label<do_zero> |
| 70 | + .instr(pio::Nop .side<0>.delay<TL_Add-1>) |
| 71 | + .wrap<> |
| 72 | + .end(); |
| 73 | + |
| 74 | + /* |
| 75 | + pio_gpio_init(pio, pin); |
| 76 | + pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true); |
| 77 | +
|
| 78 | + pio_sm_config c = ws2812_program_get_default_config(offset); |
| 79 | + sm_config_set_sideset_pins(&c, pin); |
| 80 | + sm_config_set_out_shift(&c, false, true, rgbw ? 32 : 24); |
| 81 | + sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX); |
| 82 | +
|
| 83 | + int cycles_per_bit = ws2812_T1 + ws2812_T2 + ws2812_T3; |
| 84 | + float div = clock_get_hz(clk_sys) / (freq * cycles_per_bit); |
| 85 | + sm_config_set_clkdiv(&c, div); |
| 86 | +
|
| 87 | + pio_sm_init(pio, sm, offset, &c); |
| 88 | + pio_sm_set_enabled(pio, sm, true); |
| 89 | + */ |
| 90 | + // initialization code |
| 91 | + template <typename SystemClock,typename PIO,typename SM,typename DataGpio> |
| 92 | + static constexpr void init(uint16_t offset) { |
| 93 | + PIO::template connect<typename DataGpio::Pad>(); |
| 94 | + SM::template addOutput<DataGpio>(); |
| 95 | + |
| 96 | + SM::config(offset,program) |
| 97 | + .template setSidesetPins<DataGpio>() |
| 98 | + .template setOutShift<false,true,24>() |
| 99 | + .setFifoJoinTx() |
| 100 | + .template setFrequency<SystemClock,1000000000/TimeScale>() |
| 101 | + .init(offset+program.getOffset<bitloop>()); |
| 102 | + SM::setEnabled(true); |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments