Skip to content
Permalink
Browse files

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>
  • Loading branch information...
Loic Poulain authored and MaureenHelm committed Jul 16, 2019
1 parent 5533da9 commit 16d8ce519c9fe13a2a7bfd23552eafe93b422b49
Showing with 12 additions and 11 deletions.
  1. +12 −11 samples/basic/blink_led/src/main.c
@@ -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;
}
}

0 comments on commit 16d8ce5

Please sign in to comment.
You can’t perform that action at this time.