Portenta (M7) timer interrupt example

Is there any periodic timer interrupt example for Portenta? I tried many libraries claiming it is working but it doesn't compile.


I've used the mbed os tickers with some sucess.

#include "mbed.h"

using namespace mbed;

Ticker t;
uint8_t t0 = 0;

void setVar() {
  if (t0 == 1) {
    t0 = 0;
  else {
    t0 = 1;

void setup() {
  t.attach(&setVar, 0.2); // .attach(&function,seconds) or .attach_us(&function, microseconds)

void loop() {
  if (t0 == 1) {
    digitalWrite(LEDB, LOW);
  if (t0 == 0) {
    digitalWrite(LEDB, HIGH);

Hope this helps.

Thank you for your response,
This is a soft timer which I was not looking for. I was looking for more like a hardware timer like Timer1 in Arduino.

I was able to find a good library for making concurrent threading that was running on top of mbed OS and made an example for reference :

* Blinking example using threaded timer 
* By  : Waleed El-Badry
* Date: 20/02/2021

// Threaded timer library: https://github.com/Aduen/ThreadedTimer
#include <threadedtimer.h>

// Thread Object
ThreadedTimer t1;

// Task ID
int taskBlink;

// Blue LED on RGB
int led = LEDB;

// Variable to save last LED status
volatile bool led_status = true;

// Blinking routine
void blinking()
  digitalWrite(led, led_status);
  led_status = !led_status;

void setup() 

  // Set LED GPIO as output
  pinMode(led, OUTPUT);

  // Create blinking task to enter every blinking() routine every 500 ms
  taskBlink = t1.every(500, blinking);

void loop() 
  delay(2000); //main loop delay does not affect the running Timers

Hi wbadry, Do you know if the Thread library still works? I tried to use it to perform a timer interrupt but it gave me an error which is the following "exit status 1
Error compiling for Arduino Portenta H7 (M7 core) board. "


Yes, it is broken now. I am looking for an alternative

Thank you for your response,
I've also been looking for an alternative, but so far nothing has worked.

Hi @wbadry I have used the registers directly to access the hardware timers.

 * This program blinks built in Blue led of Portenta H7 at a frequency of ~1Hz using Hardware Timer 3.
 * This program uses HSI clock as the source (can also be replaced with HSE, CSI, PLL1 clocks)

 * NOTE: This program doesn't use interrupt so there will be a slight fluctuation in frequency (say 1-2 %) at which LED blinks
 * This code is written by: N Prudhvi


bool state = LOW;

/************ SETUP Function  ************/

void setup ()
  LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_HSI);  //Set clouck sourse to High Speed Internal(HSI) RC Oscillator 
  /** System Clock Source Selection :- LL_RCC_SYS_CLKSOURCE_x, (Where x= HSI,CSI,HSE,PLL1) **/
  LL_RCC_SetSysPrescaler(LL_RCC_SYSCLK_DIV_1);       //Set System clock prescaler to 1
  /** System Clock Prescaler Selection :- LL_RCC_SYSCLK_DIV_x, (Where x= 1,2,4,8,16,32,64,128,512) **/
  LL_RCC_SetAHBPrescaler(LL_RCC_AHB_DIV_1);          //Set main CPU clock (HCLK) prescaler to 1
  /** HCLK Clock Prescaler Selection :- LL_RCC_AHB_DIV_x, (Where x= 1,2,4,8,16,32,64,128,512) **/
  LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);        //Set APB1 clock prescaler to 1 
  /** APB1 Clock Prescaler Selection :- LL_RCC_APB1_DIV_x, (Where x= 1,2,4,8,16) **/

  __HAL_RCC_TIM3_CLK_ENABLE();                       //Enable Clock access to Timer 3
  LL_TIM_SetPrescaler(TIM3, 6399);                   //Prescaler - 6400  (TIMx->PSC)
  LL_TIM_SetAutoReload(TIM3, 9999);                  //Auto Reload Registar value (TIMx->ARR) (NOTE: ARR is the up limit)
  LL_TIM_SetCounterMode(TIM3,LL_TIM_COUNTERMODE_UP); //Set counter mode to "Edge aligned" and direction to "Upcounter" (TIMx->CR1 .. CMS,DIR)
  //NOTE: In Upconting mode timer counts from 0 to ARR
  LL_TIM_EnableUpdateEvent(TIM3);                    // Enable Update Event (TIMx->CR1 .. UDIS)
  LL_TIM_EnableCounter(TIM3);                        // Enable Timer (TIMx->CR1 .. CEN)
/************ LOOP (MAIN) Function  ************/

void loop ()
  if (state == LOW){state=HIGH;}
  else {state = LOW;}
  if(LL_TIM_IsActiveFlag_UPDATE(TIM3))            //Whether update interrupt flag (UIF) is set
    LL_TIM_ClearFlag_UPDATE(TIM3);                 //Clear the update interrupt flag (UIF) (TIMx->SR .. UIF)
     //NOTE: Hardware automatically enables UIF when interrupt occurs

This code doesn't trigger Interrupt. Currently, I am working on it. I will update the code once I am done with it.

Hope this helps.

1 Like

Check and try the newly created Portenta_H7_TimerInterrupt Library

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.