Photon and Timer5 direct usage

Hi,

I’m trying to get Timer5 working so I can get sub millisecond precise timings.

I’m using the following to test the Photon’s suitablility to do the timer call but it doesn’t seem to trigger the interrupt. Any ideas?

#include "Particle.h"

volatile bool twip=true;

void TIM5_IRQHandler(void)
{
    digitalWrite(TX,twip);
    twip=!twip;
}

void setup() {
    
    Serial.begin(115200);
    while (!Serial.available()) { delay(1);}
    Serial.println("Starting Interrupt Test");
    digitalWrite(TX,false);
    pinMode(TX,OUTPUT);
    NVIC_InitTypeDef NVIC_InitStruct;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
    NVIC_InitStruct.NVIC_IRQChannel = TIM5_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);
	TIM_TimeBaseStructInit(&TIM_TimeBaseStruct);
	TIM_TimeBaseStruct.TIM_Period = 45000;
	TIM_TimeBaseStruct.TIM_Prescaler = 1;
	TIM_TimeBaseStruct.TIM_ClockDivision = 0;
	TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStruct);
	
	TIM_ClearITPendingBit(TIM5,TIM_IT_Update);
    TIM_ITConfig(TIM5,TIM_IT_Update,ENABLE);
    TIM_Cmd(TIM5, ENABLE);
    attachSystemInterrupt(SysInterrupt_TIM5_IRQ, TIM5_IRQHandler);
    Serial.println("Interrupt added");
    
}

void loop() {
    
}

Do you insist in using Timer5?
If you’d be free to use any means for sub millisecond precision (~10µs) you could use SparkIntervalTime library or have a look how it’s done there.

But if you want to toggle a pin faster you may want to use the low level IO calls pinSetFast(), pinResetFast() or the slightly slower digitalWriteFast()

I also think there is a function to have the timer directly manipulate a GPIO without going through an ISR.

1 Like

The SparkIntervalTimer is also a good reference of how to hook up the timer and interrupts. You can set up the timer using the standard techniques in the STM32 standard peripheral library but getting the interrupts working is a little trickier. I just refer to the SparkIntervalTimer source whenever I need to do it.

3 Likes