From 209a6bb6b7b39882e0efdf25eab29931fb2de859 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stig=20Bj=C3=B8rlykke?= Date: Thu, 15 Dec 2022 18:32:10 +0100 Subject: [PATCH] docs/rp2: Make LED have exactly 50% duty cycle in PIO 1Hz example. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This ensures the same number of cycles are used for LED on and LED off in the PIO 1Hz example. It's also possible to swap the first set() and the irq() to avoid using an extra instruction, but this tutorial is a good example of how to calculate the cycles. Signed-off-by: Stig Bjørlykke --- docs/rp2/tutorial/pio.rst | 7 +++++-- examples/rp2/pio_1hz.py | 5 +++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/docs/rp2/tutorial/pio.rst b/docs/rp2/tutorial/pio.rst index 9981aed832..4e519650ee 100644 --- a/docs/rp2/tutorial/pio.rst +++ b/docs/rp2/tutorial/pio.rst @@ -76,9 +76,10 @@ and state machines. Below is the code for reference. nop() [29] jmp(x_dec, "delay_high") - # Cycles: 1 + 7 + 32 * (30 + 1) = 1000 + # Cycles: 1 + 1 + 6 + 32 * (30 + 1) = 1000 + nop() set(pins, 0) - set(x, 31) [6] + set(x, 31) [5] label("delay_low") nop() [29] jmp(x_dec, "delay_low") @@ -113,6 +114,8 @@ the following: X starts with the value 31 this jump will happen 31 times, so the ``nop() [29]`` runs 32 times in total (note there is also one instruction cycle taken by the ``jmp`` for each of these 32 loops). +- The single ``nop()`` correlates with the cycle used for IRQ raise, and ensures + the same number of cycles are used for LED on and LED off. - ``set(pins, 0)`` will turn the LED off by setting pin 25 low. - Another 32 loops of ``nop() [29]`` and ``jmp(...)`` will execute. - Because ``wrap_target()`` and ``wrap()`` are not specified, their default will diff --git a/examples/rp2/pio_1hz.py b/examples/rp2/pio_1hz.py index c18aa22fc0..4b6fe95bbd 100644 --- a/examples/rp2/pio_1hz.py +++ b/examples/rp2/pio_1hz.py @@ -16,9 +16,10 @@ def blink_1hz(): nop() [29] jmp(x_dec, "delay_high") - # Cycles: 1 + 7 + 32 * (30 + 1) = 1000 + # Cycles: 1 + 1 + 6 + 32 * (30 + 1) = 1000 + nop() set(pins, 0) - set(x, 31) [6] + set(x, 31) [5] label("delay_low") nop() [29] jmp(x_dec, "delay_low")