Skip to content

Instantly share code, notes, and snippets.

@benpeoples
Created June 30, 2022 19:19
Show Gist options
  • Save benpeoples/1468fa5faa0d339c953fb571cfdc082c to your computer and use it in GitHub Desktop.
Save benpeoples/1468fa5faa0d339c953fb571cfdc082c to your computer and use it in GitHub Desktop.
/*
If systemclock is 144MHz, this generates an interrupt every millisecond
*/
void init_millis() {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
TIM6->PSC = 2; // 144MHz / (65536 - 48000) / (2+1) = 1 milliseconds per tick
TIM6->DMAINTENR = TIM_UIE;
TIM6->ATRLR = 48000;
TIM6->CTLR1 = TIM_ARPE | TIM_URS | TIM_CEN;
NVIC_InitTypeDef NVIC_InitStructure = {0};
NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
TIM6->INTFR = 0;
NVIC_Init(&NVIC_InitStructure);
}
volatile uint64_t millis;
void TIM6_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM6_IRQHandler(void) {
millis++;
TIM6->INTFR = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment