Skip to content

Commit 4a8f65c

Browse files
bliu11-intelAnas Nashif
authored andcommitted
samples: pwm: adapt to new api argument definition
Change the api argument format following the API argument definition change in the driver. Some definitions of CONSTANTs were changed accordingly. Correct some comment error. Jira: ZEP-642 Change-Id: I1b34640aa70f1597076e035e2b3b576d8337129a Signed-off-by: Baohong Liu <[email protected]>
1 parent 26f37ba commit 4a8f65c

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

samples/drivers/pwm/src/main.c

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
* The sample runs like this on PWM[0] (IO3):
2929
* 1. At first, the period for the PWM is 1 second on, 1 second off.
3030
* 2. Every 4 seconds, the period is halved.
31-
* 3. After the period is faster than 10ms, period starts to double.
31+
* 3. After the period is faster than 1 ms on and 1 ms off, period
32+
* starts to double.
3233
* 4. Every 4 seconds, period is doubled.
3334
* 5. When period is longer than 1 seconds, repeat from 2.
3435
*
@@ -44,11 +45,20 @@
4445
#include <pwm.h>
4546
#include <sys_clock.h>
4647

47-
/* about 1 ms */
48-
#define MIN_PERIOD 32000
48+
#define HW_CLOCK_CYCLES_PER_USEC (CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC / \
49+
USEC_PER_SEC)
4950

50-
/* about 1 second */
51-
#define MAX_PERIOD 32000000
51+
/**
52+
* unit: micro second.
53+
* 1 m sec for "on" or "off" time.
54+
*/
55+
#define MIN_PERIOD 1000
56+
57+
/**
58+
* unit: micro second.
59+
* 1 sec for "on" or "off" time
60+
*/
61+
#define MAX_PERIOD 1000000
5262

5363
#define SLEEPTICKS SECONDS(4)
5464

@@ -69,25 +79,34 @@ void main(void)
6979
printk("Cannot find PWM_0!\n");
7080
}
7181

72-
period = MAX_PERIOD;
82+
period = MAX_PERIOD * 2;
7383
dir = 0;
7484

7585
while (1) {
76-
pwm_pin_set_values(pwm_dev, 0, period, period);
86+
if (pwm_pin_set_period(pwm_dev, 0, period)) {
87+
printk("set period fails\n");
88+
return;
89+
}
90+
91+
if (pwm_pin_set_values(pwm_dev, 0, 0,
92+
(period * HW_CLOCK_CYCLES_PER_USEC) / 2)) {
93+
printk("set values fails\n");
94+
return;
95+
}
7796

7897
if (dir) {
7998
period *= 2;
8099

81-
if (period > MAX_PERIOD) {
100+
if (period > (MAX_PERIOD * 2)) {
82101
dir = 0;
83-
period = MAX_PERIOD;
102+
period = MAX_PERIOD * 2;
84103
}
85104
} else {
86105
period /= 2;
87106

88-
if (period < MIN_PERIOD) {
107+
if (period < (MIN_PERIOD * 2)) {
89108
dir = 1;
90-
period = MIN_PERIOD;
109+
period = MIN_PERIOD * 2;
91110
}
92111
}
93112

0 commit comments

Comments
 (0)