2024-11-18 12:52 PM - edited 2024-11-18 01:06 PM
I am using the B-L072Z-LRWAN1 and trying to code LPTIM with interrupts and no HAL. The code seems to be getting stuck in the Infinite_Loop section in the startup.s file. The comments around this loop say that an unexpected interrupt was triggered. If I remove the NVIC functions in lptim_init(), the controller does not get stuck anymore. I have tried:
-setting the IER register to 127 (all interrupt sources enabled) with a breakpoint on the lptimer IRQ handler. The handler is never called.
-declared & defined all possible IRQ_Handler functions in stm32l0xx_it.c with a while(1) inside each one, and watched all of them with breakpoints. nothing.
-putting the NVIC functions before or after setting the IER register. no change.
here is my code for the lptimer:
void lptim_init(LPTIM_TypeDef *LPTIM, lpTimerMode mode)
{
uint32_t tmpcfg = 0;
uint16_t tmpier = 0;
//enale the oscillator for the peripheral
__HAL_RCC_LPTIM1_CLK_ENABLE();
//encoder mode disabled
tmpcfg &= ~LPTIM_CFGR_ENC;
//counter mode disabled
tmpcfg &= ~LPTIM_CFGR_COUNTMODE;
//preload ARR
tmpcfg |= LPTIM_CFGR_PRELOAD;
//set wave polarity.
tmpcfg &= ~LPTIM_CFGR_WAVPOL;
tmpcfg &= ~LPTIM_CFGR_WAVE;
//disable trigger event reset
tmpcfg &= ~LPTIM_CFGR_TIMOUT;
//disable hardware triggers
tmpcfg &= ~LPTIM_CFGR_TRIGEN;
//clear trigger selection
tmpcfg &= ~LPTIM_CFGR_TRIGSEL;
//set prescalar division
tmpcfg |= LPTIM_PRESCALER_DIV1;
//clear trig filter settings
tmpcfg &= ~LPTIM_CFGR_TRGFLT;
//external clock settings; clear.
tmpcfg &= ~LPTIM_CFGR_CKFLT;
//external source clock polarity. not relevant
tmpcfg &= ~LPTIM_CFGR_CKPOL;
//clock selection: internal clock
tmpcfg &= ~LPTIM_CFGR_CKSEL;
//write the configurations to the register
LPTIM->CFGR = tmpcfg;
//set interrupts to trigger on every ARR match
if(mode == lptim_mode_interrupt)
{
//select the interrupt trigger sources
tmpier |= LPTIM_IER_ARRMIE;
//write the configurations to the register
LPTIM->IER = tmpier;
NVIC_SetVector(LPTIM1_IRQn, (uint32_t)&LPTIM1_IRQ_Handler);
NVIC_SetPriority(LPTIM1_IRQn, 3);
NVIC_EnableIRQ(LPTIM1_IRQn);
//set the interrupt vector
}
}
void lptim_start(LPTIM_TypeDef *LPTIM, uint16_t period)
{
//enable the timer
LPTIM->CR = LPTIM_CR_ENABLE;
LPTIM->ARR = period;
//enable continuous mode
LPTIM->CR |= LPTIM_CR_CNTSTRT;
}
void lptim_stop(LPTIM_TypeDef *LPTIM)
{
//clear control register, disabling the timer.
LPTIM->CR &= ~0b111U;
}
and in the main() and before while(1) I have
lptim_init(LPTIM1, lptim_mode_interrupt);
lptim_start(LPTIM1, LPTIMER_PERIOD);
Does anyone know why this is?
2024-11-18 02:38 PM
There is no interrupt handler properly defined in your code, so the default handler is invoked.
NVIC_SetVector normally doesn't work - remove it.
Rename your interrupt routine to the name found in startup_xxx.s file - LPTIM1_IRQHandler.