Skip to content

Commit 2959c11

Browse files
committed
[DNM] samples: basic: fade_led: pwm120_test
pwm120 100% duty cycle bug showcase Signed-off-by: Piotr Krzyzanowski <[email protected]>
1 parent ba1d06f commit 2959c11

File tree

2 files changed

+68
-19
lines changed

2 files changed

+68
-19
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Test requires wire connection between:
6+
* - PWM120 OUT[0] at P7.0
7+
* - LED[0-4] input at P9.[0-4]
8+
*/
9+
10+
#include <zephyr/dt-bindings/pwm/pwm.h>
11+
#include <zephyr/dt-bindings/gpio/gpio.h>
12+
#include <zephyr/dt-bindings/gpio/nordic-nrf-gpio.h>
13+
14+
&pwm130 {
15+
status = "disabled";
16+
};
17+
18+
&pwm_led2 {
19+
// pwms = <&pwm130 0 PWM_MSEC(10) PWM_POLARITY_NORMAL>;
20+
pwms = <&pwm120 0 PWM_MSEC(10) PWM_POLARITY_NORMAL>;
21+
};
22+
23+
&pinctrl {
24+
pwm120_default: pwm120_default {
25+
group1 {
26+
psels = <NRF_PSEL(PWM_OUT0, 7, 0)>;
27+
};
28+
};
29+
pwm120_sleep: pwm120_sleep {
30+
group1 {
31+
psels = <NRF_PSEL(PWM_OUT0, 7, 0)>;
32+
low-power-enable;
33+
};
34+
};
35+
};
36+
37+
&pwm120 {
38+
status = "okay";
39+
pinctrl-0 = <&pwm120_default>;
40+
pinctrl-1 = <&pwm120_sleep>;
41+
pinctrl-names = "default", "sleep";
42+
memory-regions = <&dma_fast_region>;
43+
};
44+
45+
&dma_fast_region {
46+
status = "okay";
47+
};

samples/basic/fade_led/src/main.c

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,11 @@
1616

1717
static const struct pwm_dt_spec pwm_led0 = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led0));
1818

19-
#define NUM_STEPS 50U
20-
#define SLEEP_MSEC 25U
2119

2220
int main(void)
2321
{
24-
uint32_t pulse_width = 0U;
25-
uint32_t step = pwm_led0.period / NUM_STEPS;
26-
uint8_t dir = 1U;
2722
int ret;
23+
uint32_t pulse_width;
2824

2925
printk("PWM-based LED fade\n");
3026

@@ -35,29 +31,35 @@ int main(void)
3531
}
3632

3733
while (1) {
34+
pulse_width = 0;
3835
ret = pwm_set_pulse_dt(&pwm_led0, pulse_width);
3936
if (ret) {
4037
printk("Error %d: failed to set pulse width\n", ret);
4138
return 0;
4239
}
4340
printk("Using pulse width %d%%\n", 100 * pulse_width / pwm_led0.period);
4441

45-
if (dir) {
46-
pulse_width += step;
47-
if (pulse_width >= pwm_led0.period) {
48-
pulse_width = pwm_led0.period - step;
49-
dir = 0U;
50-
}
51-
} else {
52-
if (pulse_width >= step) {
53-
pulse_width -= step;
54-
} else {
55-
pulse_width = step;
56-
dir = 1U;
57-
}
42+
k_msleep(1000);
43+
44+
pulse_width = pwm_led0.period / 2;
45+
ret = pwm_set_pulse_dt(&pwm_led0, pulse_width);
46+
if (ret) {
47+
printk("Error %d: failed to set pulse width\n", ret);
48+
return 0;
49+
}
50+
printk("Using pulse width %d%%\n", 100 * pulse_width / pwm_led0.period);
51+
52+
k_msleep(1000);
53+
54+
pulse_width = pwm_led0.period;
55+
ret = pwm_set_pulse_dt(&pwm_led0, pulse_width);
56+
if (ret) {
57+
printk("Error %d: failed to set pulse width\n", ret);
58+
return 0;
5859
}
60+
printk("Using pulse width %d%%\n", 100 * pulse_width / pwm_led0.period);
5961

60-
k_sleep(K_MSEC(SLEEP_MSEC));
62+
k_msleep(1000);
6163
}
6264
return 0;
6365
}

0 commit comments

Comments
 (0)