https://www.cnx-software.com/2021/01/27/a-closer-look-at-raspberry-pi-rp2040-programmable-ios-pio/ Skip to content CNX Software - Embedded Systems News News, Tutorials, Reviews, and How-Tos related to Embedded Linux and Android, Raspberry Pi, Arduino, ESP8266, Development Boards, SBC's, TV Boxes, Mini PCs, etc.. [Wisblock-Modular-IoT-System] Menu * About + About CNX Software + Contact Us + Advertisement & Consulting Services + Work for Us + Support CNX Software + Privacy Policy * Development Kits + x86 & Arm Linux Development Boards + MCU Development Kits + Hackable Gadgets + My Hardware * How-Tos & Training Materials + Embedded Linux Development + Technical Glossary + AllWinner How-tos + AMLogic How-tos + Android How-tos + Automation & IoT How-tos + Freescale/ NXP i.MX How-tos + Raspberry Pi How-tos + Rockchip How-tos * Reviews * Jobs & Events + Embedded Systems Jobs + Events * Shop + Buy Review Samples + Coupon Codes & Promos + Recommended Products Posted on January 27, 2021January 27, 2021 by Abhishek Jadhav - 7 Comments on A closer look at Raspberry Pi RP2040 Programmable IOs (PIO) A closer look at Raspberry Pi RP2040 Programmable IOs (PIO) The popularity of Raspberry Pico board powered by RP2040 microcontroller has made every reader wanting to know more about the board and chip. So today we will be talking about RP2040's Programmable IOs, a feature that makes it different from most other microcontroller boards. The two PIO blocks or let's call it the hardware interfaces in the RP2040 have four state machines each. These two PIO blocks can simultaneously execute programs to manipulate GPIOs and transfer raw data. Now, what do these state machines do? Well, the PIO state machines execute the programs fetched from various sources. Sometimes the programs are taken from the PIO library (UART, SPI, or I2C) or user software. RP2040 Programmable IOs State Machine Why Programmable I/O? All the boards usually come with hardware support for digital communications protocols such as I2C, SPI, and UART. However, if you plan to use more of these interfaces than what is available on the board, you can use the programmable IOs provided in RP2040 microcontroller. Well, this has more capabilities than one can think of. Let's say you want to output a DPI video or "communicate with a serial device found on AliExpress" is now possible with Programmable I/O. As the name says, 'Programmable' IO makes it clear that it can be programmed directly to support several interfaces including SD card interface, VGA output, and higher speed data transfer. Hang on! We have the most exciting part of the article coming up - 'How to program these programmable I/Os to make your job easy'. How do I get started with the RP2040 PIO programming? The Pico SDK (Software Development Kit) provides the headers, libraries and build system necessary to write programs for RP2040-based devices such as Raspberry Pi Pico in C, C++ or Arm assembly language If you plan to use Python to code, you only require a suitable editor (let's say Thonny) and MicroPython installed on the development board. But in the case of C/C++, you require CMake file that tells the Pico SDK how to turn the C file into a binary application for an RP2040-based microcontroller board, as explained in our recent MicroPython and C tutorial for Raspberry Pi Pico. The PIO Assembler parses a PIO source file and outputs the assembled version ready for inclusion in an RP2040 application. This includes the C and C++ applications built against the Pico SDK, and Python programs running on the RP2040 MicroPython port. To get started with programming the state machine for your PIO application, there are three components for C/C++ based program. * A PIO program * C-language based software to run the show * A CMake file describing how these two are combined into a program image to load onto an RP2040-based development board. PIO Assembly Instructions Now, when it comes to programming these IO interfaces, there are nine assembly instructions "JMP, WAIT, IN, OUT, PUSH, PULL, MOV, IRQ, and SET". Although most people may be interested in programming the PIO interfaces with C/C++ or Python language, let us look into some of the assembly language instructions used for the IO interfaces. * JMP: This 'jump' instruction can be a conditional or a non-conditional statement. In this, it transfers the flow of execution by changing the instruction pointer register. In simple words, with 'jmp' statement the flow of execution goes to another part of the code. * WAIT: This instruction stalls the execution of the code. Each instruction takes one cycle unless it is stalled (using the WAIT instructions). * OUT: This instruction shifts data from the output shift register to other destinations, 1...32 bits at a time. * PULL: This instruction pops 32-bit words from TX FIFO into the output shift register. * IN: This instruction shift 1...32 bits at a time into the register. * PUSH: This instruction to write the ISR content to the RX FIFO. RP2040 Programmable IOs Assembly Language More information about the assembly language instructions is available in the RP2040 datasheet. RP2040 PIO programming example in C/C++ and MicroPython To make it easier, we will look into the program of hello_world that blinks the onboard LED using the Programmable IOs and TX FIFO's 32-bit data (PULL instructions). The program in C/C++ looks something like this: C [#include "pico/stdli] #include "pico/stdlib.h" #include "hardware/pio.h" // Our assembled program: 1 #include "hello.pio.h" 2 3 int main() { 4 // Choose which PIO instance to use (there are two instances) 5 PIO pio = pio0; 6 7 // Our assembled program needs to be loaded into this PIO's 8 instruction 9 // memory. This SDK function will find a location (offset) in 10 the 11 // instruction memory where there is enough space for our 12 program. We need 13 // to remember this location! 14 uint offset = pio_add_program(pio, &hello_program); 15 16 // Find a free state machine on our chosen PIO (erroring if 17 there are 18 // none). Configure it to run our program, and start it, using 19 the 20 // helper function we included in our .pio file. 21 uint sm = pio_claim_unused_sm(pio, true); 22 hello_program_init(pio, sm, offset, PICO_DEFAULT_LED_PIN); 23 24 // The state machine is now running. Any value we push to its 25 TX FIFO will 26 // appear on the LED pin. 27 while (true) { 28 // Blink 29 pio_sm_put_blocking(pio, sm, 1); 30 sleep_ms(500); 31 // Blonk 32 pio_sm_put_blocking(pio, sm, 0); sleep_ms(500); } } The above C/C++ code blinks the LED with one complete cycle of 1 second. LED is programmed in such a way that it will be on for 500 ms followed by off for 500 ms. But, before state machines can run the program, we need to load the program into this instruction memory. "The function pio_add_program() finds free space for our program in a given PIO's instruction memory, and loads it." With this, we configure the state machine to output its data to the onboard LED. The assembly code for .pio file shown below has all the C helper functions to set C/C++ code. C [.program hello loop:] .program hello 1 loop: 2 pull 3 out pins, 1 4 jmp loop 5 6 % c-sdk { 7 8 static inline void hello_program_init(PIO pio, uint sm, uint 9 offset, uint pin) { 10 pio_sm_config c = hello_program_get_default_config(offset); 11 12 // Map the state machine's OUT pin group to one pin, namely 13 the `pin` 14 // parameter to this function. 15 sm_config_set_out_pins(&c, pin, 1); 16 // Set this pin's GPIO function (connect PIO to the pad) 17 pio_gpio_init(pio, pin); 18 // Set the pin direction to output at the PIO 19 pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true); 20 21 // Load our configuration, and jump to the start of the 22 program 23 pio_sm_init(pio, sm, offset, &c); 24 // Set the state machine running 25 pio_sm_set_enabled(pio, sm, true); } %} Apart from these, you also require CMake file that describes how .pio and .c files are built into a binary suitable for loading onto your Raspberry Pi Pico development board. There's no equivalent sample written with MicroPython, but we can see a simpler PIO MicroPython code used to blink the onboard LED: Python [import time from rp2] import time 1 from rp2 import PIO, asm_pio 2 from machine import Pin 3 4 # Define the blink program. It has one GPIO to bind to on the set 5 instruction, which is an output pin. 6 # Use lots of delays to make the blinking visible by eye. 7 @asm_pio(set_init=rp2.PIO.OUT_LOW) 8 def blink(): 9 wrap_target() 10 set(pins, 1) [31] 11 nop() [31] 12 nop() [31] 13 nop() [31] 14 nop() [31] 15 set(pins, 0) [31] 16 nop() [31] 17 nop() [31] 18 nop() [31] 19 nop() [31] 20 wrap() 21 22 # Instantiate a state machine with the blink program, at 1000Hz, 23 with set bound to Pin(25) (LED on the rp2 board) 24 sm = rp2.StateMachine(0, blink, freq=1000, set_base=Pin(25)) 25 26 # Run the state machine for 3 seconds. The LED should blink. 27 sm.active(1) 28 time.sleep(3) sm.active(0) There's no separate .pio file in this case, and both MicroPython and assembly code are placed into the .py file. Note that even though PIO can be programmed with MicroPython, the Python SDK documentation says it's currently unstable/ work-in-progress, so C/C++ is recommended. There can be many modifications to the code by adding the color you want to display with the help of the hex format in RGB. However, there are many real-life examples like PWM, UART or even interfacing NeoPixels. For those interested, you can find many PIO programming examples in the GitHub repositories for C and MicroPython samples. Conclusion RP2040 Programmable IOs have the capability to simultaneously execute programs to support interfaces like VGA output and higher speed data transfer. You can check Chapter 3 in the SDK documentation for C/C++ and Python to find out more about RP2040 Programmable IOs. [Abhishek_P] Abhishek Jadhav Abhishek Jadhav is an engineering student, RISC-V Ambassador, freelance tech writer, and leader of the Open Hardware Developer Community. Support CNX Software - Donate via PayPal or cryptocurrencies, become a Patron on Patreon, or buy review samples Related posts: 1. Getting Started with Raspberry Pi Pico using MicroPython and C 2. DIY Stripboard/ Veroboard Enclosure for Raspberry Pi Advertisements (Part 2) 3. Status of Orange Pi Boards GPIO Support 4. FOSDEM 2017 Open Source Meeting Schedule 5. Getting Started with MicroPython on ESP32 - Hello World, GPIO, and WiFi CategoriesHardware, Processors, Programming, Testing TagsC/C++, gpio, how-to, micropython, raspberry pi Connect with: Facebook Twitter Subscribe Login Notify of [new follow-up comments ] [ ] [>] guest [ ] {} [+] [ ] [ ] Name* [ ] Email* [ ] Website [ ] I agree to the Privacy Policy The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment. [ ] [Post Comment] guest [ ] {} [+] [ ] [ ] Name* [ ] Email* [ ] Website [ ] I agree to the Privacy Policy The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment. [ ] [Post Comment] 7 Comments oldest newest most voted Load More Comments Advertisements Post navigation Previous PostPrevious Rockchip RV1109 SBC's provide access control solutions with optional 4G LTE connectivity Next PostNext Mini replica of DEC PDP-11 computer runs 2.11 BSD UNIX on ESP32 SoC Follow Us on FacebookFollow Us on TwitterFollow Us on LinkedInFollow Us on MeWeFollow Us on YouTubeFollow Us on RSS Follow CNX Software on Google NewsSubscribe to CNX Software by Email Search for: [ ] Search [cExpress-T] Trending Posts - Last 7 Days SPONSORS [Gateworks-] [TrustOnX-T] [rockchip-s] [MK39-4K-Di] [firefly-sb] Advertisements Recent Comments * Willy on AmpliPi - A Raspberry Pi-based whole house audio amplifier (Crowdfunding) * tkaiser on AAEON PICO-TGU4 Pico-ITX Tiger Lake UP3 SBC Comes with 2.5 GbE, SATA, HDMI & eDP * Steven on AAEON PICO-TGU4 Pico-ITX Tiger Lake UP3 SBC Comes with 2.5 GbE, SATA, HDMI & eDP * Jeroen on Feather compatible shield Integrates BG96 Module with LTE Cat-M1, NB-IoT, and GPS * Jon Smirl on AmpliPi - A Raspberry Pi-based whole house audio amplifier (Crowdfunding) Subscribe to Comments RSS Feed Advertisements Latest Reviews Vacos Cam AI Security Camera Review - Part 1: Specifications, Unboxing and Teardown Vacos Cam AI Security Camera Review - Part 1: Specifications, Unboxing and Teardown As we've seen in our Reolink RLC-810A review, AI security cameras greatly reduce the number of false alerts generated by motion sensors, and the Reolink 4K security camera we tested was capable of people and vehicle detection. The Reolink model... [...] Beelink SEI Review - A Core i3-10110U Mini PC Tested with Windows and Ubuntu Beelink SEI Review - A Core i3-10110U Mini PC Tested with Windows and Ubuntu Beelink has launched a new range of mini PCs called the SEi Series. Similar in size and appearance to an Intel 'NUC' they are available in various configurations. Beelink sent a Core i3-10110U SEi model for review which is the... [...] Getting Started with Raspberry Pi Pico using MicroPython and C Getting Started with Raspberry Pi Pico using MicroPython and C Raspberry Pi Pico board was just launched last Thursday, but thanks to Cytron I received a sample a few hours after the announcement, and I've now had time to play with the board using MicroPython and C programming language. I... [...] Change Ad Consent Do not sell my data Copyright 2021 - CNX Software Limited Privacy Policy Proudly powered by WordPress This website uses cookies to improve your experience. We'll assume you're ok with this, but if you don't like these, you can remove them Accept Read more Privacy & Cookies Policy Close Privacy Overview This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience. Necessary [*] Necessary Always Enabled Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information. Non-necessary [*] Non-necessary Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website. wpDiscuz [ ] Insert