sample: blink_led: Adjust PWM period
The maximum period a PWM controller can generate depends on its input clock and its resolution (16-bit, 32-bit...). Setting a 1s max period generates error with some hardwares (mimxrt1064_evk) because of clock limitation. This patch fixes this issue by calculating maximum period of the PWM device to adjust max and min periods accordingly. Signed-off-by: Loic Poulain <loic.poulain@linaro.org>
This commit is contained in:
parent
5533da9aa2
commit
16d8ce519c
1 changed files with 12 additions and 11 deletions
|
@ -23,16 +23,11 @@
|
|||
#error "Choose supported PWM driver"
|
||||
#endif
|
||||
|
||||
/* in micro second */
|
||||
#define MIN_PERIOD (USEC_PER_SEC / 64U)
|
||||
|
||||
/* in micro second */
|
||||
#define MAX_PERIOD USEC_PER_SEC
|
||||
|
||||
void main(void)
|
||||
{
|
||||
struct device *pwm_dev;
|
||||
u32_t period = MAX_PERIOD;
|
||||
u32_t period, min_period, max_period;
|
||||
u64_t cycles;
|
||||
u8_t dir = 0U;
|
||||
|
||||
printk("PWM demo app-blink LED\n");
|
||||
|
@ -43,6 +38,12 @@ void main(void)
|
|||
return;
|
||||
}
|
||||
|
||||
/* Adjust max_period depending pwm capabilities */
|
||||
pwm_get_cycles_per_sec(pwm_dev, PWM_CHANNEL, &cycles);
|
||||
/* in very worst case, PWM has a 8 bit resolution and count up to 256 */
|
||||
period = max_period = 256 * USEC_PER_SEC / cycles;
|
||||
min_period = max_period / 64;
|
||||
|
||||
while (1) {
|
||||
if (pwm_pin_set_usec(pwm_dev, PWM_CHANNEL,
|
||||
period, period / 2U)) {
|
||||
|
@ -53,16 +54,16 @@ void main(void)
|
|||
if (dir) {
|
||||
period *= 2U;
|
||||
|
||||
if (period > MAX_PERIOD) {
|
||||
if (period > max_period) {
|
||||
dir = 0U;
|
||||
period = MAX_PERIOD;
|
||||
period = max_period;
|
||||
}
|
||||
} else {
|
||||
period /= 2U;
|
||||
|
||||
if (period < MIN_PERIOD) {
|
||||
if (period < min_period) {
|
||||
dir = 1U;
|
||||
period = MIN_PERIOD;
|
||||
period = min_period;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue