Using timer interrupts with the Arduino Portenta Machine Control

Hi All,

I am trying to use the Arduino Portenta Machine Control to carry out integration at every 1 millisecond using timer interrupts. However, when I run the program, the integration value 'integralValue' is always zero seeming that the interrupt handler is never activated.

Kindly requesting for assistance in solving this issue. The code I used is as follows:

#include <Arduino_MachineControl.h>;
#include "Wire.h";
using namespace machinecontrol;
#include "Portenta_H7_TimerInterrupt.h" 
#include "Portenta_H7_ISR_Timer.h"
Portenta_H7_Timer timer(TIM1);
#include "stm32h7xx.h"

// Integration parameters
const int integrationStepMs = 1; // Integration step size in milliseconds
const float wrapMin = 0.0; // Lower limit of wrapped range
const float wrapMax = 2*M_PI; // Upper limit of wrapped range

float integralValue;

void setup()
{
  Serial.begin(9600);

  // Initialize integration variables

  integralValue = 0.0;
  
__HAL_RCC_TIM1_CLK_ENABLE();

// Set up a timer interrupt to trigger the integration
  NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 0); // Set the interrupt priority
  NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); // Enable the interrupt
  TIM_TypeDef *timer = TIM1; // Timer1
  RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // Enable clock for Timer1
  timer->CR1 = 0; // Disable the timer
  timer->PSC = 63999; // Prescaler for 1ms (64 MHz / 64000 = 1 kHz)
  timer->ARR = 1; // Auto-reload value to 1 (1ms)
  timer->DIER |= TIM_DIER_UIE; // Enable update interrupt
  timer->CR1 |= TIM_CR1_CEN; // Enable the timer

}

void loop()
{

}

// Timer1 Update Interrupt Handler
extern "C"{ void TIM1_UP_TIM10_IRQHandler() {
    
  if (TIM1->SR & TIM_SR_UIF) {
      
        float integrationStep = 10 * (integrationStepMs / 1000.0); 

        integralValue += integrationStep;

        integralValue = fmod(integralValue - wrapMin, wrapMax - wrapMin) + wrapMin;
  
    // Clear the interrupt flag
    TIM1->SR &= ~TIM_SR_UIF;

                            }
                                                }
        }

Does it works ?