|
| 1 | +/* |
| 2 | + * Copyright (c) 2016, Sascha Schade |
| 3 | + * Copyright (c) 2017, Niklas Hauser |
| 4 | + * |
| 5 | + * This file is part of the modm project. |
| 6 | + * |
| 7 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 8 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 9 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 10 | + */ |
| 11 | +// ---------------------------------------------------------------------------- |
| 12 | + |
| 13 | +#include <modm/board.hpp> |
| 14 | +using namespace Board; |
| 15 | + |
| 16 | +/* from https://github.com/raspberrypi/pico-examples/blob/master/pio/ws2812/ws2812.pio */ |
| 17 | + |
| 18 | +// constants WS2812 |
| 19 | +// static constexpr uint32_t T0H = 350; // ns |
| 20 | +// static constexpr uint32_t T0L = 800; |
| 21 | +// static constexpr uint32_t T1H = 700; |
| 22 | +// static constexpr uint32_t T1L = 600; |
| 23 | + |
| 24 | +// constants WS2812B |
| 25 | +static constexpr uint32_t T0H = 400; // ns |
| 26 | +static constexpr uint32_t T0L = 850; |
| 27 | +static constexpr uint32_t T1H = 850; |
| 28 | +static constexpr uint32_t T1L = 400; |
| 29 | + |
| 30 | +static constexpr uint32_t TimeScale = 50; |
| 31 | + |
| 32 | +static constexpr uint32_t TH_Common = T0H/TimeScale; |
| 33 | +static constexpr uint32_t TH_Add = (T1H-T0H)/TimeScale; |
| 34 | +static constexpr uint32_t TL_Common = T1L/TimeScale; |
| 35 | +static constexpr uint32_t TL_Add = (T0L-T1L)/TimeScale; |
| 36 | + |
| 37 | +static constexpr uint32_t T3 = 12;// |
| 38 | + |
| 39 | +// labels |
| 40 | +struct bitloop {}; |
| 41 | +struct do_one {}; |
| 42 | +struct do_zero {}; |
| 43 | + |
| 44 | + |
| 45 | +/* |
| 46 | +.wrap_target |
| 47 | +bitloop: |
| 48 | + out x, 1 side 0 [T3 - 1] ; Side-set still takes place when instruction stalls |
| 49 | + jmp !x do_zero side 1 [T1 - 1] ; Branch on the bit we shifted out. Positive pulse |
| 50 | +do_one: |
| 51 | + jmp bitloop side 1 [T2 - 1] ; Continue driving high, for a long pulse |
| 52 | +do_zero: |
| 53 | + nop side 0 [T2 - 1] ; Or drive low, for a short pulse |
| 54 | +.wrap |
| 55 | +*/ |
| 56 | +// PIO program |
| 57 | +static constexpr auto pio_prog = modm::platform::PIOProgram::begin() |
| 58 | + .sideset<1>() |
| 59 | + .wrapTarget() |
| 60 | + .label<bitloop>() |
| 61 | + .instr(pio::Out().x<1>() .side<0>().delay<TL_Common-1>()) |
| 62 | + .instr(pio::Jmp().not_x().to<do_zero>() .side<1>().delay<TH_Common-1>()) |
| 63 | + .label<do_one>() |
| 64 | + .instr(pio::Jmp().to<bitloop>() .side<1>().delay<TH_Add-1>()) |
| 65 | + .label<do_zero>() |
| 66 | + .instr(pio::Nop() .side<0>().delay<TL_Add-1>()) |
| 67 | + .wrap() |
| 68 | + .end(); |
| 69 | + |
| 70 | + |
| 71 | +struct HW |
| 72 | +{ |
| 73 | + using PIO = modm::platform::Pio0; |
| 74 | + using PIO_SM = PIO::StateMachine<0>; |
| 75 | + using DataGpio = modm::platform::GpioOutput23; |
| 76 | +}; |
| 77 | + |
| 78 | + |
| 79 | +int |
| 80 | +main() |
| 81 | +{ |
| 82 | + |
| 83 | + |
| 84 | + Board::initialize(); |
| 85 | + |
| 86 | + HW::DataGpio::setOutput(Gpio::OutputType::PushPull, Gpio::SlewRate::Fast); |
| 87 | + HW::DataGpio::setDriveStrength(Gpio::DriveStrength::mA_12); |
| 88 | + |
| 89 | + auto pio_prog_offset = HW::PIO::addProgram(pio_prog); |
| 90 | + |
| 91 | + HW::PIO::connect<HW::DataGpio::Pad>(); |
| 92 | + |
| 93 | + HW::PIO_SM::addOutput<HW::DataGpio>(); |
| 94 | + |
| 95 | + HW::PIO_SM::config() |
| 96 | + .setup(pio_prog_offset,pio_prog) |
| 97 | + .setSidesetPins<HW::DataGpio,1,false,false>() |
| 98 | + .setFifoJoinTx() |
| 99 | + .setOutShift<false,true,24>() |
| 100 | + .setFrequency<Board::SystemClock,1000000000/TimeScale>() |
| 101 | + .init(pio_prog_offset+pio_prog.getOffset<bitloop>()); |
| 102 | + |
| 103 | + HW::PIO_SM::setEnabled(true); |
| 104 | + |
| 105 | + constexpr auto delay_val = 5ms; |
| 106 | + |
| 107 | + while (true) |
| 108 | + { |
| 109 | + uint32_t clr = 0; |
| 110 | + while (clr!=0xff0000) { |
| 111 | + clr = clr + 0x010000; |
| 112 | + HW::PIO_SM::writeBlocking(clr); |
| 113 | + modm::delay(delay_val); |
| 114 | + } |
| 115 | + while (clr!=0x000000) { |
| 116 | + clr = clr - 0x010000; |
| 117 | + HW::PIO_SM::writeBlocking(clr); |
| 118 | + modm::delay(delay_val); |
| 119 | + } |
| 120 | + while (clr!=0x00ff00) { |
| 121 | + clr = clr + 0x000100; |
| 122 | + HW::PIO_SM::writeBlocking(clr); |
| 123 | + modm::delay(delay_val); |
| 124 | + } |
| 125 | + while (clr!=0x000000) { |
| 126 | + clr = clr - 0x000100; |
| 127 | + HW::PIO_SM::writeBlocking(clr); |
| 128 | + modm::delay(delay_val); |
| 129 | + } |
| 130 | + while (clr!=0x0000ff) { |
| 131 | + clr = clr + 0x000001; |
| 132 | + HW::PIO_SM::writeBlocking(clr); |
| 133 | + modm::delay(delay_val); |
| 134 | + } |
| 135 | + while (clr!=0x000000) { |
| 136 | + clr = clr - 0x000001; |
| 137 | + HW::PIO_SM::writeBlocking(clr); |
| 138 | + modm::delay(delay_val); |
| 139 | + } |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + return 0; |
| 144 | +} |
0 commit comments