1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#![no_std]
use cortex_m;
use embedded_hal::timer::CountDown;
use fugit::{ExtU32, HertzU32};
use rp2040_hal::{
gpio::{Function, FunctionConfig, Pin, PinId, ValidPinMode},
pio::{PIOExt, StateMachineIndex, Tx, UninitStateMachine, PIO},
};
use smart_leds_trait::SmartLedsWrite;
pub struct Ws2812Direct<P, SM, I>
where
I: PinId,
P: PIOExt + FunctionConfig,
Function<P>: ValidPinMode<I>,
SM: StateMachineIndex,
{
tx: Tx<(P, SM)>,
_pin: Pin<I, Function<P>>,
}
impl<P, SM, I> Ws2812Direct<P, SM, I>
where
I: PinId,
P: PIOExt + FunctionConfig,
Function<P>: ValidPinMode<I>,
SM: StateMachineIndex,
{
pub fn new(
pin: Pin<I, Function<P>>,
pio: &mut PIO<P>,
sm: UninitStateMachine<(P, SM)>,
clock_freq: fugit::HertzU32,
) -> Self {
let side_set = pio::SideSet::new(false, 1, false);
let mut a = pio::Assembler::new_with_side_set(side_set);
const T1: u8 = 2; const T2: u8 = 5; const T3: u8 = 3; const CYCLES_PER_BIT: u32 = (T1 + T2 + T3) as u32;
const FREQ: HertzU32 = HertzU32::kHz(800);
let mut wrap_target = a.label();
let mut wrap_source = a.label();
let mut do_zero = a.label();
a.bind(&mut wrap_target);
a.out_with_delay_and_side_set(pio::OutDestination::X, 1, T3 - 1, 0);
a.jmp_with_delay_and_side_set(pio::JmpCondition::XIsZero, &mut do_zero, T1 - 1, 1);
a.jmp_with_delay_and_side_set(pio::JmpCondition::Always, &mut wrap_target, T2 - 1, 1);
a.bind(&mut do_zero);
a.nop_with_delay_and_side_set(T2 - 1, 0);
a.bind(&mut wrap_source);
let program = a.assemble_with_wrap(wrap_source, wrap_target);
let installed = pio.install(&program).unwrap();
let bit_freq = FREQ * CYCLES_PER_BIT;
let mut int = clock_freq / bit_freq;
let rem = clock_freq - (int * bit_freq);
let frac = (rem * 256) / bit_freq;
assert!(
(1..=65536).contains(&int) && (int != 65536 || frac == 0),
"(System Clock / {}) must be within [1.0, 65536.0].",
bit_freq.to_kHz()
);
if int == 65536 {
int = 0;
}
let int: u16 = int as u16;
let frac: u8 = frac as u8;
let (mut sm, _, tx) = rp2040_hal::pio::PIOBuilder::from_program(installed)
.buffers(rp2040_hal::pio::Buffers::OnlyTx)
.side_set_pin_base(I::DYN.num)
.out_shift_direction(rp2040_hal::pio::ShiftDirection::Left)
.autopull(true)
.pull_threshold(24)
.clock_divisor_fixed_point(int, frac)
.build(sm);
sm.set_pindirs([(I::DYN.num, rp2040_hal::pio::PinDir::Output)]);
sm.start();
Self { tx, _pin: pin }
}
}
impl<P, SM, I> SmartLedsWrite for Ws2812Direct<P, SM, I>
where
I: PinId,
P: PIOExt + FunctionConfig,
Function<P>: ValidPinMode<I>,
SM: StateMachineIndex,
{
type Color = smart_leds_trait::RGB8;
type Error = ();
fn write<T, J>(&mut self, iterator: T) -> Result<(), ()>
where
T: Iterator<Item = J>,
J: Into<Self::Color>,
{
for item in iterator {
let color: Self::Color = item.into();
let word =
(u32::from(color.g) << 24) | (u32::from(color.r) << 16) | (u32::from(color.b) << 8);
while !self.tx.write(word) {
cortex_m::asm::nop();
}
}
Ok(())
}
}
pub struct Ws2812<P, SM, C, I>
where
I: PinId,
C: CountDown,
P: PIOExt + FunctionConfig,
Function<P>: ValidPinMode<I>,
SM: StateMachineIndex,
{
driver: Ws2812Direct<P, SM, I>,
cd: C,
}
impl<P, SM, C, I> Ws2812<P, SM, C, I>
where
I: PinId,
C: CountDown,
P: PIOExt + FunctionConfig,
Function<P>: ValidPinMode<I>,
SM: StateMachineIndex,
{
pub fn new(
pin: Pin<I, Function<P>>,
pio: &mut PIO<P>,
sm: UninitStateMachine<(P, SM)>,
clock_freq: fugit::HertzU32,
cd: C,
) -> Ws2812<P, SM, C, I> {
let driver = Ws2812Direct::new(pin, pio, sm, clock_freq);
Self { driver, cd }
}
}
impl<'timer, P, SM, I> SmartLedsWrite for Ws2812<P, SM, rp2040_hal::timer::CountDown<'timer>, I>
where
I: PinId,
P: PIOExt + FunctionConfig,
Function<P>: ValidPinMode<I>,
SM: StateMachineIndex,
{
type Color = smart_leds_trait::RGB8;
type Error = ();
fn write<T, J>(&mut self, iterator: T) -> Result<(), ()>
where
T: Iterator<Item = J>,
J: Into<Self::Color>,
{
self.driver.tx.clear_stalled_flag();
while !self.driver.tx.is_empty() && !self.driver.tx.has_stalled() {}
self.cd.start(60u32.micros());
let _ = nb::block!(self.cd.wait());
self.driver.write(iterator)
}
}