static void pwm_timer_init(unsigned timer) { /* enable the timer clock before we try to talk to it */ modifyreg32(pwm_timers[timer].clock_register, 0, pwm_timers[timer].clock_bit); /* disable and configure the timer */ rCR1(timer) = 0; rCR2(timer) = 0; rSMCR(timer) = 0; rDIER(timer) = 0; rCCER(timer) = 0; rCCMR1(timer) = 0; rCCMR2(timer) = 0; rCCER(timer) = 0; rDCR(timer) = 0; /* configure the timer to free-run at 1MHz */ rPSC(timer) = (pwm_timers[timer].clock_freq / 1000000) - 1; /* default to updating at 50Hz */ pwm_timer_set_rate(timer, 50); /* note that the timer is left disabled - arming is performed separately */ }
int up_pwm_servo_set_rate(unsigned rate) { if ((rate < 50) || (rate > 400)) return -ERANGE; for (unsigned i = 0; i < PWM_SERVO_MAX_TIMERS; i++) { if (pwm_timers[i].base != 0) pwm_timer_set_rate(i, rate); } return OK; }
int up_pwm_servo_set_rate_group_update(unsigned group, unsigned rate) { /* limit update rate to 1..10000Hz; somewhat arbitrary but safe */ if (rate < 1) return -ERANGE; if (rate > 10000) return -ERANGE; if ((group >= PWM_SERVO_MAX_TIMERS) || (pwm_timers[group].base == 0)) return ERROR; pwm_timer_set_rate(group, rate); return OK; }