The code starts 1MHz 32bit timer and I hope it is only generating an event then ARR is zero, so that's 100ms - I hope there is no event every microsecond. The LED is blinking as expecting, ChibiOS code is not touching the same LED and it is not touching the same timer.
Code: Select all
static TIM_TypeDef *TIM = TIM5;
static void setTimer(int arr) {
TIM->ARR = arr - 1;
TIM->EGR |= TIM_EGR_UG; // generate an update event to reload timer's counter value
TIM->CR1 |= TIM_CR1_CEN; // restart timer
}
static void callback(void) {
GPIOD->ODR ^= (1 << 13); // Toggle D13
setTimer(100000); // 100ms
}
CH_FAST_IRQ_HANDLER(STM32_TIM5_HANDLER) {
if (((TIM->SR & 0x0001) != 0) && ((TIM->DIER & 0x0001) != 0)) {
callback();
}
TIM->SR = (int) ~STM32_TIM_SR_UIF; // Interrupt has been handled
}
void TIM_Init(void) {
RCC->APB1ENR |= RCC_APB1ENR_TIM5EN; // Enable TIM5 clock
NVIC_EnableIRQ(TIM5_IRQn); // Enable TIM5 IRQ
TIM->DIER |= TIM_DIER_UIE; // Enable interrupt on update event
TIM->CR1 |= TIM_CR1_OPM; // one pulse mode: count down ARR and stop
TIM->CR1 &= ~TIM_CR1_ARPE; /* ARR register is NOT buffered, allows to update timer's period on-fly. */
TIM->PSC = 84 - 1; // 168MHz / 2 / 84 = 1MHz, each tick is a microsecond
setTimer(100000); // 100ms
}
The symptoms: looks like the threads under Chibi control stop at some random point (some seconds after the start), the TIMer is blinking happily. To make this more fun, this seems to work better under debugger.
What do I do wrong?
