In case someone is interested, I took the approach to a lpc2106 -> the superstar from 10+ years ago,
tmr0_init(0x12); //initialize the timer with a prescaler
tmr0_setpr0(0x5678); //set the prescaler
tmr0_act0(led_flp); //install user handler
ei(); //global interrupt enable
it runs TIMER0 at 0x12 prescaler, and then MR0 channel at a 0x5678 increment. The isr calls led_flp().
tmr0_init() initializes the module:
//reset tmr with a prescaler
void tmr0_init(uint32_t ps) {
//stop tmr0
CTx->TCR &=~(1<<0); //0=disable timer, 1=enable timer
//reset timer
CTx->TCR |= (1<<1); //1=reset timer on next pusitive edge of pclk, 0=timer can be started
CTx->CTCR &=~(0x03); //0b00->timer mode
CTx->TC = 0; //reset tc -> not needed if TCR is reset first
CTx->PR = ps - 1; //set prescaler
//enable timer
CTx->TCR &=~(1<<1); //1=reset timer on next pusitive edge of pclk, 0=timer can be started
CTx->TCR |= (1<<0); //0=disable timer, 1=enable timer
//now timer is running
}
tmr_setpr() sets the prescaler:
//set period register
void tmr0_setpr0(uint32_t pr) {
CTx->MR0 = _tmr_pr0 = pr - 1; //set pr
CTx->IR &=~(1<<0); //clear the flag
CTx->MCR = (CTx->MCR & ~(0x07 << (3*0))) | //clear the mcr bits
(0x00 << (3*0)); //interrupt not yet enabled
}
The whole thing was compiled on Keil uv3 / ADS compiler (really really old). No CMSIS, but the code was adopted from one that I wrote for LPC11xx so you can see the struct approach I took there - the stock lpc210x.h is not used.
As such, the code can be easily modified to run on newer LPC chips which has similar timer peripherals (CT16B0/1 and CT32B0/1).