From 72e41ad66f641700db801c5707aad68110c7c2cd Mon Sep 17 00:00:00 2001 From: Graeme Winter Date: Mon, 16 May 2022 21:17:42 +0100 Subject: [PATCH] Fix PIO count (#211) Works fine currently for low frequency, breaks down at higher frequency e.g. 1MHz Chased down with a scope to the 3 extra cycles needed for wait+1; mov; jmp so account for these in demo program --- pio/pio_blink/blink.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pio/pio_blink/blink.c b/pio/pio_blink/blink.c index 1edc698..9d253bb 100644 --- a/pio/pio_blink/blink.c +++ b/pio/pio_blink/blink.c @@ -31,5 +31,8 @@ void blink_pin_forever(PIO pio, uint sm, uint offset, uint pin, uint freq) { pio_sm_set_enabled(pio, sm, true); printf("Blinking pin %d at %d Hz\n", pin, freq); - pio->txf[sm] = clock_get_hz(clk_sys) / (2 * freq); + + // PIO counter program takes 3 more cycles in total than we pass as + // input (wait for n + 1; mov; jmp) + pio->txf[sm] = (clock_get_hz(clk_sys) / (2 * freq)) - 3; }