0
votes

I am trying to get a pulse of 100us to occur 4 times a second through GPIO. The way I am doing this is by having two timer-based interrupts; one that triggers 4 times every second, and another that gets triggered 100us after the first.

Within the interrupt handler of the first timer, the target pin is set high, the second timer is reset, and interrupts on the second timer are enabled. Within the second interrupt handler, the target pin is set low and interrupts are disabled. Here is what my code looks like:

First timer's ISR:

void TIM4_IRQHandler(void)
{
    {
        TIM4 -> SR = ~(TIM_SR_UIF); // clear UIF flag
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_HIGH); // target pin
        endTrigger->restartTimer();
        endTrigger->enableInterrupts();
    }

}

Second Timer's ISR:

void TIM5_IRQHandler(void)
{
    {
        TIM5 -> SR = ~(TIM_SR_UIF); // clear UIF flag
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_LOW); // target pin
        endTrigger->disableInterrupts();
    }

}

retart timer function:

void Timer::restartTimer() {
    myhTim->CR1 &= ~TIM_CR1_CEN; // disable the timer
    myhTim->CNT = 0; // reset count
    myhTim->SR = 0; // clear any interrupt flags
    myhTim->CR1 = TIM_CR1_CEN; // re-engage timer

}

For whatever reason, the second I write to CR1 I get a hard fault... Any idea why? I am aware that there are other approaches to getting a 100us pulse but this seemed to be the simplest way for what our needs are... We aren't going to need the additional timer and we will need to be semi-frequently syncing the pulse to an external piece of hardware.

1
So, is myhTim->CR1 = TIM_CR1_CEN; generating the hard fault?perencia
Is myhTim pointing to a valid address?perencia
I think I figured it out! I was having a timer interrupt occurring immediately after initializing the clock! Because I hadn't initialzed the second timer yet at that point, a hard fault was occurring. Thank you for pointing me in the right direction!Jonathan Just

1 Answers

0
votes

The timer interrupt occurred immediately after initializing the first timer. I had to add a line of code to my second IRQ such that it would only attempt to monkey with the second timer in the case of it not being a nullptr.