45 uint32_t timer_setup_ex(
int timer_num, uint32_t rate,
int enable_int)
48 volatile struct TMR_struct *
timer = TMR_ADDR(timer_num);
53 TMR0->ENBL &= ~(1 << timer_num);
56 for (log_divisor = 0; log_divisor < 8; log_divisor++)
58 int denom = (rate * (1 << log_divisor));
59 period = (REF_OSC + denom/2) / denom;
69 if (period < 2) period = 2;
71 actual_rate = REF_OSC / (period * (1 << log_divisor));
76 timer->CMPLD1 = (period - 1);
77 timer->COMP1 = timer->CMPLD1;
78 timer->CNTR = timer->LOAD;
80 timer->CSCTRLbits = (
struct TMR_CSCTRL) {
83 timer->CTRLbits = (
struct TMR_CTRL) {
85 .PRIMARY_CNT_SOURCE = 8 + log_divisor,
89 TMR0->ENBL |= (1 << timer_num);
93 timer->SCTRLbits.TCFIE = 1;