-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
RP2 - PIO module #7496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
RP2 - PIO module #7496
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
3c0781b
brief overwiew with explaination of each command
NitiKaur d0a38b6
changed self to set instruction
NitiKaur e49eafe
IRQ instruction updated as per review
NitiKaur 45b666a
all asm PIo instructions moved to new location(docs/library/rp2.rst )…
NitiKaur 8b0e238
asm PIO instructions moved to new location (2)
NitiKaur b25ea04
typos corrected
NitiKaur 218d0d8
brief description of all instructions added
NitiKaur 1d761a6
blink_1hz example added
NitiKaur 541ab4a
heading added
NitiKaur b182b08
uppercase to lowercase changes made where necessary
NitiKaur 5ba4a96
typos corrected
NitiKaur 8dce437
Update rp2.rst
NitiKaur 7abf0e7
pio_1hz blink example added
NitiKaur fc8084d
file location changed
NitiKaur 2a78748
nop instruction added
NitiKaur cd85f4a
lines wrapped
NitiKaur 08fb1ea
nop syntax added
NitiKaur 286eab1
instructions shifted to lowercase
NitiKaur c72b933
meta-instructions added
NitiKaur ab3f11e
IRQ instruction form corrected
NitiKaur b25d26d
jump instruction syntax corrected
NitiKaur 8a05b83
push instruction forms corrected
NitiKaur 5dcfbd0
pull instruction syntax corrected
NitiKaur 65c1f7d
files corrected
NitiKaur 52b9371
typos corrected
NitiKaur b76453a
directives,label, delay, side-set added
NitiKaur f830f4c
set instruction added
NitiKaur 753cb43
typos corrected
NitiKaur 708da6b
typos corrected(2)
NitiKaur 565f65f
typos (3)
NitiKaur 469f318
docs build corrected
NitiKaur 641b949
Merge branch 'master' into rp2_pio
NitiKaur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
.. _rp2_pio: | ||
|
||
RP2040 has standard hardware support like I2C, SPI and UART for communication | ||
protocols with standard devices. For devices where we do not have hardware | ||
support for such protocols or where there is a requirement of more I/O | ||
channels, PIO(Programmable Input Output) comes into play. Also, typical | ||
micropython devices make use of a technique called bit banging in which pins | ||
are rapidly turned on and off to transmit data. This makes the entire process | ||
very slow as the processor concentrates on bit banging rather than excecuting | ||
any other process. However, PIO allows it to carry on with the bit banging | ||
process while the processor resources are used for executing some other work. | ||
|
||
Along with the two main cortex - MO+ processing cores, RP2040 has two PIO | ||
blocks each of which has four state machines. These state machines take | ||
process data in FIFO (First-In-First-Out) format. When these state machines | ||
are free, they will put in data from the queue and execute the instruction. | ||
This way, the PIO state machines and the main process works simultaneously | ||
without being much dependent on the other. | ||
|
||
Each FIFO has four words (each of 32 bits) which can be linked to the DMA to | ||
transmit larger amounts of data to the PIO state machine and allows it to | ||
carry on execution without the main program keeping a track of it at each | ||
instant of time. | ||
|
||
All pioasm instructions follow a common pattern: | ||
<instruction> (side <side_set_value>) ([<delay_value>]) | ||
|
||
The state machines have 9 instructions which can perform the following tasks: | ||
|
||
* IN : shifts the bits from a source (scratch register or set of pins) to the | ||
* input shift register. | ||
|
||
* OUT : shifts the bits from the output shift register to a destination | ||
|
||
* PUSH : sends data to RX FIFO | ||
|
||
* PULL : receives data from TX FIFO | ||
|
||
* MOV : moves data from a source to a destination | ||
|
||
* IRQ : sets or clears the input flag | ||
|
||
* WAIT : pauses until a particular action happens | ||
|
||
* JMP : moves to some target locations in different parts of code | ||
|
||
* SET : writes data at destination | ||
|
||
Apart from the PIO assembly instructions we have the following: | ||
|
||
<side_set_value> : value applied to the side set pins at the start of the | ||
instruction which helps configure the side pin of the | ||
program. | ||
|
||
<delay_value> : the number of clock cycles to delay after execution of | ||
any instruction. | ||
|
||
We also have directives which are listed as: | ||
|
||
* .wrap_target : specifies where the program execution will get continued from | ||
|
||
* .wrap : specifies the instruction where the control flow of the program will | ||
get wrapped from. | ||
|
||
* .word<value> : stores a raw 16-bit value which acts as an instruction to the | ||
program. | ||
|
||
* label : instruction offset of label used with jmp instruction. | ||
|
||
We can take the pio_1hz example for a simple understanding. Below is the code | ||
of pio_1hz.py for reference. | ||
|
||
.. code-block:: python3 | ||
|
||
# Example using PIO to blink an LED and raise an IRQ at 1Hz. | ||
|
||
import time | ||
from machine import Pin | ||
import rp2 | ||
|
||
|
||
@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW) | ||
def blink_1hz(): | ||
# fmt: off | ||
# Cycles: 1 + 1 + 6 + 32 * (30 + 1) = 1000 | ||
irq(rel(0)) | ||
set(pins, 1) | ||
set(x, 31) [5] | ||
label("delay_high") | ||
nop() [29] | ||
jmp(x_dec, "delay_high") | ||
|
||
# Cycles: 1 + 7 + 32 * (30 + 1) = 1000 | ||
set(pins, 0) | ||
set(x, 31) [6] | ||
label("delay_low") | ||
nop() [29] | ||
jmp(x_dec, "delay_low") | ||
# fmt: on | ||
|
||
|
||
# Create the StateMachine with the blink_1hz program, outputting on Pin(25). | ||
sm = rp2.StateMachine(0, blink_1hz, freq=2000, set_base=Pin(25)) | ||
|
||
# Set the IRQ handler to print the millisecond timestamp. | ||
sm.irq(lambda p: print(time.ticks_ms())) | ||
|
||
# Start the StateMachine. | ||
sm.active(1) | ||
|
||
We are creating an object of class StateMachine which will display the output | ||
on pin 25. The 'blink_1hz' program uses the PIO to blink an LED and raise the | ||
IRQ at 1 HZ. Turn the LED on with the help of the set instruction. Set value | ||
31 on register X. Then wait for 30 cycles with the help of nop() instruction. | ||
Then with the help of jmp, go to the code part which has label as 'delay high' | ||
for the instruction offset. The next part of the code teaches us to turn the | ||
LED off by turning the LED off with the help of set instruction. Here the | ||
label is 'delay_low' for the instruction offset part of the jmp instruction. | ||
|
||
We then print the millisecond timestamp by setting the IRQ handler. | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of this line, I'd suggest breaking this instruction into 2 parts:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or it might be clearer to just write out all 7 possible options explicitly, as done below