RP2040 PIO Programming Model & FSM Principles: Hardware to Working Logic
RP2040 PIO state‑machines use pioasm instructions, shift registers and side‑set, with GPIO mapping and IRQ flags for multi‑state‑machine synchronization.
【Preliminary Note】The original hardware example in this article was written based on the RP2040. The actual hardware used in this hands-on demonstration features the W55RP20 as the main controller chip. The circuit logic and UF2 flashing operation principles are universally applicable, with only the main controller model differing. The original chip model mentioned in the circuit descriptions below is provided for reference purposes only.
Programming Model of PIO and Working Principle of finite-state machine
OSR): It receives data from the TX FIFO, shifts the data according to the rules configured by the instruction, and then outputs it to the GPIO.ISR): It reads data from GPIO, shifts and temporarily stores the data according to instruction rules before writing it to the RX FIFO. The shift register serves as the core data path connecting the finite-state machine to the FIFO and GPIO.SHIFTCTRL_FJOIN option to merge them into an 8×32-bit unidirectional FIFO, which is suitable for high-bandwidth scenarios such as DPI image display;1. Control Flow of PIO
1.1 Instruction Execution Cycle of the finite-state machine
WAIT instruction, which requires waiting for the condition to be met before proceeding) will break this rule and cause the finite-state machine to pause execution.1.2 Working Mechanism of the Program Counter (PC)
JMP (jump) instruction, the default auto-increment logic of the PC will be overridden;the JMP instruction explicitly specifies the next value of the PC (i. e., the position of the next instruction to be executed in the instruction memory), thereby implementing control flow logic such as program branching, looping, and conditional jumps.1.3 Extended Acquisition Sources of Instructions
SMx INSTR register corresponding to the finite-state machine (where x is the finite-state machine number, ranging from 0 to 3), this instruction will immediately interrupt the current instruction execution flow of the finite-state machine and be executed with priority.a SMx INSTR entry with the JMP instruction; the finite-state machine will immediately abandon the currently executing instruction sequence and start executing new instructions from the address specified by the JMP instruction.MOV EXEC instruction, the finite-state machine can read and execute instructions from internal registers (instructions are stored in registers in the form of data).MOV EXEC the instruction itself requires 1 clock cycle to execute, followed by another 1 clock cycle to execute the target instruction read from the register.OUT EXEC instruction, the finite-state machine can read and execute instructions from the Output Shift Register (OSR), where the instructions are embedded as data in the data stream of the shift register.MOV EXEC, the OUT EXEC instruction itself occupies 1 clock cycle, and then the target instruction is executed.2. Introduction to internal registers of PIO finite-state machine
the IN instruction shifts GPIO data into the In Shift, after which the data can be copied to the Scratch X for temporary storage via the MOV instruction; once processing is completed, the data is written to the Out Shift via the MOV instruction, and finally output to GPIO via the OUT instruction. The entire process requires no intervention from the main CPU, ensuring the independence, high efficiency and timing accuracy of the PIO.2.1 Output Shift Register (OSR)
Output Shift Register module in the output data flow diagram, which serves as the temporary storage and processing unit for finite-state machine output data. Its hardware connection is structured as: TX FIFO (Transmit FIFO Memory) → OSR → Bidirectional Shifter → Output Destinations (such as Pins Output enables).PULL instruction is used to pull data from the TX FIFO and store it in the OSR; when the OSR is empty, data will also be automatically loaded from the TX FIFO.OUT instruction is used to output the data in the OSR to the specified target, with a single output supporting 1 to 32 bits of data; upon completion of the output, the OSR will be fully filled with 0s.PULL instruction to load data from the TX FIFO. This function eliminates the need to manually execute the PULL instruction, thus saving instruction cycles and improving data throughput.2.2 Input Shift Register (ISR)
Input Shift Register module, it is the temporary storage and processing unit for the input data of the finite-state machine, and its hardware connection relationship is: input source (Pins, etc.) → bidirectional shifter (Shifter) → ISR → RX FIFO (receive FIFO memory).IN instruction is used to shift data from the bidirectional shifter into the ISR, with a single input supporting 1 to 32 bits of data.PUSH instruction for reading by the CPU or DMA.autopush): This function can be enabled by configuring the control logic block. When the input shift counter reaches the set threshold, the finite-state machine automatically executes the PUSH instruction to push the data in the ISR to the RX FIFO.2.3 Shift Counter Shifter
IN instruction is executed, the input shift counter increments by the corresponding number of data bits;OUT instruction is executed, the output shift counter increments by the corresponding number of data bits;OUT instruction to output the OSR data to the target;IN instruction is automatically executed to shift the data of the bidirectional shifter into the ISR;PUSH / PULL:PULL (between OSR and TX FIFO): The output shift counter is cleared upon execution;PUSH (between ISR and RX FIFO): The input shift counter is cleared upon execution.CTRL_SM_RESTART): the input shift counter is cleared (indicating no input data in the ISR), and the output shift counter is set to 32 (indicating no output data in the OSR):MOV OSR, . ..(the MOV instruction written to OSR): clear the output shift counter;MOV ISR, . ..(the MOV instruction written to ISR): input shift counter cleared;OUT ISR, count: Set the input shift counter to count.2.4 Erasable and Writable Registers
IN / OUT / SET / MOV and other instructions for data transfer;JMP and other instructions for conditional judgment..program ws2812_led
public entry_point:
pull ; 用于获取输入引脚的当前状态
set x, 23 ; 将寄存器x初始化为23,用于循环控制24个位
bitloop:
set pins, 1 ; 将输出引脚驱动为高电平
out y, 1 [5] ; 将输出移位寄存器的1位数据输出,并写入y寄存器中
jmp !y skip ; 根据y寄存器的值判断是否需要执行额外的延时,若y为0则跳转到skip程序
nop [5] ; 延时5个时钟周期
skip:
set pins, 0 [5] ; 将输出引脚驱动为低电平
jmp x-- bitloop ; 若x寄存器非0则递减,并根据x的值判断是否需要继续循环,非0则继续跳转到bitloop程序
jmp entry_point ; 循环结束后,跳转回程序入口点the public entry_point: statement is used to define the program entry point, and the bitloop: statement label defines the start position of the bit loop:x-- in each loop to realize the control of 24-bit output;JMP instruction condition judgment (to distinguish different delays corresponding to 0/1 and match the timing requirements of WS2812);3. Wait state of the PIO finite-state machine
3.1 Trigger Conditions for the finite-state machine to Enter the Waiting State
WAIT delay instruction, if the preset delay time has not yet elapsed (or the GPIO pin status or interrupt flag does not meet the waiting condition), the finite-state machine will suspend execution until the condition is satisfied;PULL instruction, if the TX FIFO is empty (no data available to be pulled), the finite-state machine will pause until the CPU/DMA writes data to the TX FIFO;PUSH instruction, if the RX FIFO is full (no space to store data), the state machine will pause until the CPU/DMA reads data from the RX FIFO;the IRQ WAIT instruction, if the IRQ interrupt flag bit set in the instruction is not cleared (the interrupt is not released), the state machine will pause execution until the interrupt flag bit is cleared;PULL operation; if the TX FIFO is empty (no data available to load) at this time, the state machine will pause and wait for the CPU/DMA to write data to the TX FIFO.PUSH operation; if the RX FIFO is full (no space for storage) at this time, the state machine will pause and wait for the CPU/DMA to read data from the RX FIFO.3.2 Behavior of Program Counter (PC) in Wait State
3.3 Side-set Operations and Side-set Mapping
3.4 Side Set Mapping (Hardware Configuration Level)
| 映射方式 | 控制时机 | 核心用途 |
| 输入映射 | 主动读取 GPIO 电平 | 接收外部设备输入信号 |
| 输出映射 | 通过OUT指令控制 | 向外部设备发送数据 |
| 设置映射 | 通过SET指令控制 | 单独控制 GPIO 电平(置高 / 置低) |
| 侧集映射 | 执行任意指令时附带控制 | 指令与 GPIO 控制的同步时序(如通信协议控制引脚) |
3.5 Side Set Operation
PIO_SM_CONFIG 's side_set_bits field (1~5 bits, corresponding to 1~5 side-set pins);3.6 Special Functions of Side-set Operations
OUT transmitting data; even if the pull instruction waits due to an empty TX FIFO, CS remains asserted to prevent communication abnormalities of the slave device.4. GPIO Pin Mapping of PIO
OUT / SET instructions, or read the GPIO level via IN instruction;PINCTRL register:OUT / SET / IN /side-set) can specify a consecutive block of 4 GPIOs, and the starting pin number of this GPIO block is configured via the PINCTRL register;OUT and side-set) can overlap, allowing the same GPIO group to be controlled by multiple operations.SET / OUT and side-set operations on the same GPIO simultaneously, the result of the side-set operation takes effect first;5. IRQ Interrupt Flag Bit of PIO
INTR (Interrupt Status Register) and INTS (Interrupt Original Status Register).IRQ0_INTE and IRQ1_INTE are the interrupt enable configuration registers of PIO, which are used to control which IRQ flags can trigger interrupt requests (corresponding to the irq0 and irq1 interrupt lines of RP2040), and the two structures are completely consistent, only corresponding to different interrupt lines.IRQ0_INTE IRQ1_INTE The low 12 bits of the register (Bit0~ Bit11) correspond to various IRQ interrupt flags of PIO (including 4 system-level SMx flags, 4 TX FIFO non-full flags, and 4 RX FIFO non-empty flags), and the high bits (Bit12~ Bit31) are reserved bits reserved by the chip (no function, no need to worry):1 to the corresponding bit of the register, indicating that the enable flag triggers the corresponding irq (irq0 or irq1); write 0 to mask the interrupt request of the flag;0x0 , that is, after PIO initialization, the interrupts of all IRQ flags are in the masked state by default;1 , PIO will send an irq0/irq1 request to the interrupt controller of RP2040, triggering the interrupt processing flow of the main CPU.IRQ flags are mainly used for the following purposes:IRQ instructions (for setting/clearing flag bits) and WAIT instructions (for waiting for flag bit states);6. Synchronization of PIO finite-state machine
IRQ instruction: Any finite-state machine can use this instruction to set or clear any IRQ flag bit (for example, after finite-state machine 0 completes data transmission, the SM0 flag bit is set);WAIT IRQ Instruction: The finite-state machine can use this instruction to wait for a certain IRQ flag bit to be set or cleared (for example, finite-state machine 1 waits for the SM0 flag bit to be set before starting data reception).