Be taught How To Entry The Programmable I/O on the Raspberry Pi RP2040

0
86

[ad_1]

One distinctive function of the brand new RP2040 microcontroller from the Raspberry Pi Basis is its Programmable I/O (PIO) peripheral. The main target of PIO is serial communication. Most microcontrollers have {hardware} help for well-liked serial protocols comparable to I²C, SPI, and USART. Nonetheless, this {hardware} help is all the time restricted each within the variety of serial interfaces and the kinds of serial interfaces that can be utilized. If in case you have a non-standard serial interface you could help, you might have to resort to bit-banging I/O pins to implement it, tying up the microcontroller core to get the timing proper. PIO goals to resolve this downside by offering a extremely configurable, programmable I/O peripheral that may handle the bit-banging and supply easy enter and output FIFO queues to the microcontroller core.

The RP2040 options two PIO blocks, and every has 4 state machines. Every state machine has its personal enter and output FIFO queues for speaking with the ARM cores, and might function on any GPIO pins.

Every PIO block has reminiscence to carry 32 directions, with all 4 state machines studying from the identical reminiscence. Therefore you may create 4 cases of the identical serial interface on 4 units of pins utilizing one PIO block. The instruction set for the state machines consists of simply 9 directions, however with versatile parameters aimed toward facilitating well-timed I/O.

PROGRAMMING THE PIO

applications for the PIO block are written within the PIO’s meeting language, these applications may be embedded in both MicroPython or C/C++ applications. Instance code for the Pico is supplied within the Raspberry Pi Pico Python SDK and the Raspberry Pi Pico C/C++ SDK, with full documentation of PIO meeting within the RP2040 Datasheet (These datasheets additionally provide the tables used on this article). Adafruit has supplied an introduction to PIO in CircuitPython of their Studying System. Though help for the Raspberry Pi Pico has been added to the Arduino IDE, there is no such thing as a help but for programming the PIO blocks and few libraries help it.

Let’s have a look at one instance software of the PIO that demonstrates what it may possibly do. WS2812 LEDs, also called Neopixels, use a one-wire serial interface the place the lengths of the pulses are used to point ones and zeros.

I’ve by no means encountered a microcontroller with {hardware} WS2812 help and since the timing of pulses have to be exact, the PIO’s capacity to help these LEDs is welcome. We are going to have a look at simply the PIO meeting code to get a way of what it’s like. There’s rather more code not proven that configures the PIO and its default behaviors.

Beginning at line 20, we see an out instruction. On this case the instruction pulls 1 bit from the output shift register and sends it to the x scratchpad register. There’s an output shift register (OSR) and an enter shift register (ISR) for every state machine. These registers preserve observe of what number of bits have been shifted in or out and may be configured to robotically transfer information to or from the FIFO queues. There are additionally two scratchpad registers known as x and y which are sometimes used for counters. We additionally see two further parameters on this line. The aspect parameter signifies a continuing bit or bits which are to be despatched to a beforehand specified side-set pin or pins concurrent with the execution of the instruction. On this case we’re setting a single pin low. The quantity in brackets is a delay. It instructs the state machine to attend an extra two cycles earlier than transferring to the following instruction. The size of a PIO instruction cycle is decided by the state machine’s clock divider, which is ready when the state machine is configured.

As you may see, so much can occur in a single instruction. Word that due to the way in which the WS2812 protocol works, the information from the output shift register isn’t despatched to a pin. In additional standard serial interfaces, the out instruction would shift information to a pin (or pins) and the aspect parameter would set one thing like a clock sign on one other pin.

Persevering with with the code, line 21 performs a leap primarily based on the bit worth that was moved into x. On the identical time, it units the side-set pin excessive and delays for an extra cycle. If the bit moved into x is a zero, this system jumps to line 25 the place the output is ready to zero for 5 cycles, making the heartbeat quick. If the bit is one then this system proceeds to line 23 the place the output is stored excessive for 5 cycles, making the heartbeat lengthy, earlier than leaping again to the start. The directives .wrap and .wrap_target inform the state machine the place the code ends and will restart. It’s a particular zero-cycle leap.

APPLICATIONS

So what sort of interfaces are potential with PIO? For starters, they don’t actually should be serial. You’ll be able to specify that the out instruction shifts information to a contiguous set of as much as 32 pins, and likewise for in. (Word solely 30 pins can be found within the RP2040’s present package deal, and solely 26 are damaged out on the Pico.) The datasheet notes you could implement a parallel bus to speak with 8080 or 6800 microprocessors. Makers have used output pins related to a resistor ladder to generate VGA output from PIO. A library out there by way of Arduino IDE charlieplexes LEDs by way of the PIO. However when you simply need extra I²C, SPI, or UART interfaces, the PIO can do this too. If an I/O process is easy, repetitive and would tie up the processor, it’s a candidate for off-loading to a PIO block. 

[ad_2]