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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#[allow(unused)]
use crate::hal;
use crate::hal::{
gpio::{Level, Output, Pin, PushPull},
pac,
};
use core::sync::atomic::{compiler_fence, Ordering};
use smart_leds_trait::{SmartLedsWrite, RGB8};
pub fn fill_buf(color: &RGB8, buf: &mut [u16]) -> Result<(), ()> {
if buf.len() < 24 {
return Err(());
}
let red = color.r.reverse_bits();
let green = color.g.reverse_bits();
let blue = color.b.reverse_bits();
for g in 0..8 {
if ((green >> g) & 0b1) == 0b1 {
buf[g] = 0x8000 | 13;
} else {
buf[g] = 0x8000 | 5;
}
}
for r in 0..8 {
if ((red >> r) & 0b1) == 0b1 {
buf[8 + r] = 0x8000 | 13;
} else {
buf[8 + r] = 0x8000 | 5;
}
}
for b in 0..8 {
if ((blue >> b) & 0b1) == 0b1 {
buf[16 + b] = 0x8000 | 13;
} else {
buf[16 + b] = 0x8000 | 5;
}
}
Ok(())
}
pub struct Pwm<T: sealed::Instance> {
pwm: T,
_gpio: Pin<Output<PushPull>>,
}
impl<T> Pwm<T>
where
T: sealed::Instance,
{
pub fn new<Mode>(pwm: T, pin: Pin<Mode>) -> Pwm<T> {
let pin = pin.into_push_pull_output(Level::Low);
pwm.psel.out[0].write(|w| {
#[cfg(feature = "52840")]
match pin.port() {
hal::gpio::Port::Port0 => w.port().clear_bit(),
hal::gpio::Port::Port1 => w.port().set_bit(),
};
unsafe {
w.pin().bits(pin.pin());
}
w.connect().connected()
});
pwm.enable.write(|w| w.enable().enabled());
pwm.mode.write(|w| w.updown().up());
pwm.prescaler.write(|w| w.prescaler().div_1());
pwm.countertop.write(|w| unsafe { w.countertop().bits(20) });
pwm.loop_.write(|w| w.cnt().disabled());
pwm.decoder.write(|w| {
w.load().common();
w.mode().refresh_count()
});
pwm.seq0.refresh.write(|w| unsafe { w.bits(0) });
pwm.seq0.enddelay.write(|w| unsafe { w.bits(0) });
pwm.seq1.refresh.write(|w| unsafe { w.bits(0) });
pwm.seq1.enddelay.write(|w| unsafe { w.bits(0) });
Pwm { pwm, _gpio: pin }
}
pub unsafe fn start_send_raw(&mut self, buf: *const [u16]) -> Result<(), ()> {
if (*buf).is_empty() {
return Err(());
}
if (((*buf).as_ptr() as usize) < hal::target_constants::SRAM_LOWER)
|| (((*buf).as_ptr() as usize) > hal::target_constants::SRAM_UPPER)
{
return Err(());
}
compiler_fence(Ordering::SeqCst);
self.pwm.seq0.ptr.write(|w| w.bits((*buf).as_ptr() as u32));
self.pwm.seq0.cnt.write(|w| w.bits((*buf).len() as u32));
self.pwm.events_seqend[0].write(|w| w.bits(0));
self.pwm.tasks_seqstart[0].write(|w| w.bits(1));
Ok(())
}
pub unsafe fn set_seq1_raw(&mut self, buf: *const [u16]) -> Result<(), ()> {
if (*buf).is_empty() {
return Err(());
}
if (((*buf).as_ptr() as usize) < hal::target_constants::SRAM_LOWER)
|| (((*buf).as_ptr() as usize) > hal::target_constants::SRAM_UPPER)
{
return Err(());
}
compiler_fence(Ordering::SeqCst);
self.pwm.seq1.ptr.write(|w| w.bits((*buf).as_ptr() as u32));
self.pwm.seq1.cnt.write(|w| w.bits((*buf).len() as u32));
Ok(())
}
pub fn is_done_raw(&self) -> bool {
self.pwm.events_seqend[0].read().bits() == 1
}
pub fn send_full_buf(&mut self, colors: &[RGB8], scratch: &mut [u16]) -> Result<(), ()> {
if scratch.len() < u16s_needed_slice(colors) {
return Err(());
}
for (color, buf) in colors.iter().zip(scratch.chunks_exact_mut(24)) {
fill_buf(color, buf)?;
}
let start = colors.len() * 24;
let end = start + 40;
for by in &mut scratch[start..end] {
*by = 0x8000;
}
self.pwm.loop_.write(|w| w.cnt().disabled());
unsafe {
self.start_send_raw(&scratch[..end])?;
}
while !self.is_done_raw() {}
Ok(())
}
}
pub fn u16s_needed_slice(slice: &[RGB8]) -> usize {
u16s_needed_ct(slice.len())
}
pub const fn u16s_needed_ct(leds: usize) -> usize {
leds * 24 + 40
}
impl<T> SmartLedsWrite for Pwm<T>
where
T: sealed::Instance,
{
type Error = ();
type Color = RGB8;
fn write<Iter, I>(&mut self, mut iterator: Iter) -> Result<(), ()>
where
Iter: Iterator<Item = I>,
I: Into<Self::Color>,
{
let mut buf_a = [0u16; 24];
let mut buf_b = [0u16; 24];
let mut blanks_fed = 0;
let mut toggle = false;
match (iterator.next(), iterator.next()) {
(Some(a), Some(b)) => {
fill_buf(&a.into(), &mut buf_a)?;
fill_buf(&b.into(), &mut buf_b)?;
}
(Some(a), None) => {
fill_buf(&a.into(), &mut buf_a)?;
buf_b.copy_from_slice(&[0x8000u16; 24]);
blanks_fed = 1;
}
(None, Some(_)) => {
return Err(());
}
_ => {
return Ok(());
}
}
unsafe {
self.pwm.loop_.write(|w| w.cnt().bits(1));
self.pwm.events_loopsdone.write(|w| w.bits(0));
self.set_seq1_raw(&buf_b)?;
self.start_send_raw(&buf_a)?;
}
#[derive(Copy, Clone)]
enum Data {
Pixel(RGB8),
Blank,
}
let new_iter = iterator
.map(|seq| Data::Pixel(seq.into()))
.chain([Data::Blank; 3].iter().cloned());
for seq in new_iter {
if !toggle {
while !self.is_done_raw() {}
match seq {
Data::Pixel(p) => {
fill_buf(&p, &mut buf_a)?;
}
Data::Blank => {
buf_a.copy_from_slice(&[0x8000u16; 24]);
blanks_fed += 1;
}
}
compiler_fence(Ordering::SeqCst);
} else {
while self.pwm.events_loopsdone.read().bits() == 0 {}
unsafe {
self.pwm.tasks_seqstart[0].write(|w| w.bits(1));
self.pwm.events_seqend[0].write(|w| w.bits(0));
self.pwm.events_loopsdone.write(|w| w.bits(0));
}
compiler_fence(Ordering::SeqCst);
match seq {
Data::Pixel(p) => {
fill_buf(&p, &mut buf_b)?;
}
Data::Blank => {
buf_b.copy_from_slice(&[0x8000u16; 24]);
blanks_fed += 1;
}
}
compiler_fence(Ordering::SeqCst);
if blanks_fed >= 2 {
break;
}
}
toggle = !toggle;
}
while self.pwm.events_loopsdone.read().bits() == 0 {}
Ok(())
}
}
mod sealed {
use core::ops::Deref;
pub trait Instance: Deref<Target = crate::hal::pac::pwm0::RegisterBlock> {}
}
impl sealed::Instance for pac::PWM0 {}
#[cfg(not(any(feature = "52810")))]
impl sealed::Instance for pac::PWM1 {}
#[cfg(not(any(feature = "52810")))]
impl sealed::Instance for pac::PWM2 {}
#[cfg(not(any(feature = "52810", feature = "52832")))]
impl sealed::Instance for pac::PWM3 {}