Skip to content

Commit

Permalink
Add flushing PIO data
Browse files Browse the repository at this point in the history
[Problem]
When providing data fast enough, multiple frames could be concatenated
since we only waited 60 microseconds after the last bytes were pushed
to the PIO, not since the last bytes were actually pushed to the
neopixels.

[Solution]
"Flush" the PIO after pushing the last bytes, by waiting until it's
empty.
  • Loading branch information
krzmaz committed Jan 5, 2024
1 parent 48617eb commit 4c8429d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions neopixel-server/src/bin/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ async fn main(spawner: Spawner) {
.for_each(|(r, g, b)| ws2812.write(r, g, b));

num_bytes = None;
// wait for the state machine to write all bytes
ws2812.flush();
// let the neopixels latch on
Timer::after_micros(60).await;
}
Expand Down
2 changes: 2 additions & 0 deletions neopixel-server/src/bin/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ async fn main(spawner: Spawner) {
.for_each(|(r, g, b)| ws2812.write(r, g, b));

num_bytes = None;
// wait for the state machine to write all bytes
ws2812.flush();
// let the neopixels latch on
Timer::after_micros(60).await;
}
Expand Down
9 changes: 9 additions & 0 deletions neopixel-server/src/ws2812.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ impl<'d, P: Instance, const S: usize> Ws2812<'d, P, S> {
}
}

// see https://github.com/rp-rs/ws2812-pio-rs/blob/944494ca9dad73933f700408c3054c8f14c78998/src/lib.rs#L262-L263
pub fn flush(&mut self) {
// clear stalled flag first
self.sm.tx().stalled();
while !self.sm.tx().empty() && !self.sm.tx().stalled() {
cortex_m::asm::nop();
}
}

// left here for future experiments
pub async fn _write_dma(&mut self, data: &[u32]) {
self.sm.tx().dma_push(self.dma.reborrow(), data).await;
Expand Down

0 comments on commit 4c8429d

Please sign in to comment.