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 & Partners
    Products & Partners
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • 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
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • 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 & Partners
  • 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 1334 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 over 1 year ago

    C++ has a mechanism to enforce that a class has to provide particular operations. This allows to only accept classes and objects that can do a particular thing, without the need for a base class. And without inheritance.

    In the lib that I'm writing, it's expected that the objects you pass to it for interrupt handling, can actually be executed. They need to provide the () operator.

    C++ has concepts. Guards that you can use to investigate capabilities at compile time. There are standard ones, and you can define your own checks.

    In this case, I'm lucky, because there is a standard concept to check if a template type can be executed: std::invocable. If I set that as a requirement for a template parameter, the code will not compile if you use the template class with a type that doesn't have the () operator:

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

    This is a help to the user of the library. They get a clear message that they are using the library with objects that aren't fit to handle the interrupts.

    image

    The nice thing is that E can actually be a simple function pointer. Doesn't need to be a class with () operator.

    Another nice thing is that the runtime cost is zero.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • shabaz
    shabaz over 1 year ago

    Looking forward to this! And it's becoming more relevant since number of PIO are increasing in each RPxxxx chip generation it seems. It will remove a bit of the 'wild west' when it comes to developing with them, when there are decent libraries to help with these common tasks.

    • 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 © 2026 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.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube