element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog emulate Raspberry PICO PIO programs
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Raspberry Pi requires membership for participation - click to join
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
GPIO Pinout
Raspberry Pi Wishlist
Comparison Chart
Quiz
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 11 Apr 2025 8:12 AM Date Created
  • Views 469 views
  • Likes 10 likes
  • Comments 6 comments
Related
Recommended
  • raspberry
  • pico
  • PIO

emulate Raspberry PICO PIO programs

Jan Cumps
Jan Cumps
11 Apr 2025

The Pico has a set of PIO co-processors. They are real-time controllers that can execute logic with deterministic timing. Ideal to run strict-timed sequences and state machines. And to implement extra peripherals.
I'm trying a PIO emulator with my first PIO design..

image

PIO controllers are tiny and specialised. Focused on running a limited set of instructions with predictable timing. One of the things that add to the - rather steep - learning curve: they can't be debugged.

There is a solution. Jürgen Reuter developed an emulator. You can load your design and step through the code. At the same time, you can see what happens with the registers, and how GPIO pins behave during the execution. You can also set registers, read and write the FIFOs, move the program counter.

I've used it to verify a stepper motor controller that I wrote for PIO (source). And it's really useful to see how your program actually works. In stead of double guessing and trial & error - I did a lot of that when writing my first design.

The emulator is a server. It runs your state machine designs. The server by default runs on port 2040 :). It can be controlled and queried via a set of client tools. The one that decides what happens, is the monitor client. With this one, you configure all aspects of the PIO state machine, program it, and make it run, step, get and set values.

image

Then there is a set of graphical clients, that show specific aspects. The code observer shows what's loaded in the emulator, and where the program counter is:

image

Also the interesting GPIO observer. That one shows how the state machine drives Pico GPIO pins. In the image below (and above too), the state machine has just read the direction bit for the stepper, and set GPIO1 to that value:

image

.. and a diagram client. Here showing my state machine, with the changes in X + Y scratch registers.

image

Here are example monitor commands that I used to entertain the emulator. If you know the architecture of the PIO controllers, this all makes sense:

; pin setup
pinctrl -p 0 -s 0 --out-count=2 --out-base=1
gpio -p 0 -g 0 -e
gpio -p 0 -g 1 -e
gpio -p 0 -g 0 -c
gpio -p 0 -g 1 -c

gpio --pio=0 --gpio=0 --init
gpio --pio=0 --gpio=1 --init

side-set -c 1

; shift LSBs
fifo --shift-right -t


; load program. the source has to be the hex values that the assembler generated
; you 'll typically find that back in the build/<mypiodesign>.pio.h file
load -f stepper_hex.pio

; enable state machine 0
sm +e

; set steps and dir in the TX fifo (will pull into OSR)
fifo -v 0x21 -e -t

; single step
trace -c 1

; get all register values
register

; move program counter to instruction 1 (0 based)
register -a 1

... and the hex representation of my PIO program, retrieved when building the code. This is what you load into the emulator.
You can also use https://wokwi.com/tools/pioasm to get the hex output of your design right away.

pioasm output emulator input emulator code observer
image 80a0
6001
a027
0020
0045
b046
1086
b042
a046
0089
0045
image

Thank you for reading.

  • Sign in to reply
Parents
  • Jan Cumps
    Jan Cumps 1 month ago

    There's also a signal viewer.

    image

    GPIO 0 is the stepper motor steps pin, 1 the stepper direction pin.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • Jan Cumps
    Jan Cumps 1 month ago

    There's also a signal viewer.

    image

    GPIO 0 is the stepper motor steps pin, 1 the stepper direction pin.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
  • Jan Cumps
    Jan Cumps 1 month ago in reply to Jan Cumps

    Same code, where I passed a delay of 1 to the PIO ISR.

    You can see that in the stepper pin trace, each half period is stretched with 1 clock cycle. In effect: adding two clocks to a pulse's duration:

    image

    That's how the slow-the-stepper-motor-down works in my state machine.

    The instruction that's stretched (looped), is the first jmp of each half cycle. I make the PIO repeat that for the number of delay steps passed to the PIO.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube