element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • 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 OO Library to handle Pico PIO relative interrupts: intro
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
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: 23 Aug 2025 4:04 PM Date Created
  • Views 732 views
  • Likes 8 likes
  • Comments 7 comments
Related
Recommended
  • interrupt
  • raspberry
  • pico
  • PIO

OO Library to handle Pico PIO relative interrupts: intro

Jan Cumps
Jan Cumps
23 Aug 2025

Object Oriented library to design C++ classes that support PIO interrupts.
Goal: make a PIO state machine call the very C++ object that's managing (or watching) it.

When I designed a Pico library to manage stepper motors with the PIO co-controllers, I created a utility to handle PIO relative interrupts.
My design was object oriented, and I used objects to handle the PIO state machine for each stepper motor. One of the things I got working then, was to call the right stepper object when a PIO interrupt occurred for its particular PIO state machine. The PIO APIs aren't very OO friendly, and require bridging mechanisms to translate raw interrupts into something that's delegating duties to objects. Here is the documentation of that exercise: Handle Raspberry Pico PIO "relative interrupts" in C++ 

This is functionality that is useful in many OO PIO designs. I developed it specifically for stepper motors. In this series, I try to make it generic. Usable for any OO designer that wants to react on PIO notifications.

A PIO relative interrupt is an interrupt that contains info of the PIO controller and the state machine that generated the interrupt.

This will be an evolving series. I'm going to make it as flexible as reasonable, for an embedded system. Initially, I'll try to support objects that can be executed. (in C++ language, that means that they overload the () operator).

Watch this thread while I'm trying to transform a project specific construct into a reusable library ...

sneak preview:

/*
PIO interrupts can only call functions without parameters. They can't call object members.
This static embedded class delegates interrupts to the relevant object.
The objects have to implement the () operation.
*/

// guard that E has to support () operator.
template <std::invocable E, uint32_t interrupt_number> class pio_irq {

How to use the library, with example:  OO Library to handle Pico PIO relative interrupts: usage and example 

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

    How to use the library, with example:  OO Library to handle Pico PIO relative interrupts: usage and example 

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB 1 month ago

    Great solution to the problem Jan.

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

    ... and - to be embedded savvy - no heap allocation. If you are concerned about dynamic memory use: the library doesn't allocate any dynamic memory at runtime. Its API will use stack - as any C function call does. But no new(), ...

    It uses 1 array of fixed size*. The size is either 8 (Pico) or 12 (Pico2) memory pointers. Really not a lot.

    *If you use more than one handler class (not object), or if your PIO state machine throws more than 1 distinct relative interrupt and you want to handle all of them, you have to count that array size per combination. Still very little data. You'd have a hard time to make it require half a kilobyte of static data.

    • There is some code that needs to be executed at the start of the program, to enable the interrupt and register the handler. This is something you would have to do when using C too.
    • No runtime cost. It doesn't run anything in the background.
    • When an interrupt fires, it has to decide what object to execute. This logic isn't more expensive than what you 'd have to do in a C program, to understand what state machine fired what interrupt, and what to do with it ...
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 1 month ago

    All 12 available PIO state machines on a Pico2 having all their PIO relative interrupts managed by their own object:

    image

    This is captured on a RISCV core. It works the same on the ARMs

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

    Early results:

    image

    The mechanism works. A single class with 2 public methods supports the mechanism.

    image

    One to tell that we're interested in managing an interrupt. The other to register the object that will handle the interrupt.

    The screen capture shows a minimal handler, that just prints a message and counts how many times it's been invoked. 
    The only requirement for a handler class is that it has line 15. All other implementation parts are for demo use.

    • 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