Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DNM] samples: basic: fade_led: pwm120_test #2413

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions samples/basic/fade_led/boards/nrf54h20dk_nrf54h20_cpuapp.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
* SPDX-License-Identifier: Apache-2.0
*
* Test requires wire connection between:
* - PWM120 OUT[0] at P7.0
* - LED[0-4] input at P9.[0-4]
*/

#include <zephyr/dt-bindings/pwm/pwm.h>
#include <zephyr/dt-bindings/gpio/gpio.h>
#include <zephyr/dt-bindings/gpio/nordic-nrf-gpio.h>

&pwm130 {
status = "disabled";
};

&pwm_led2 {
// pwms = <&pwm130 0 PWM_MSEC(10) PWM_POLARITY_NORMAL>;
pwms = <&pwm120 0 PWM_MSEC(10) PWM_POLARITY_NORMAL>;
};

&pinctrl {
pwm120_default: pwm120_default {
group1 {
psels = <NRF_PSEL(PWM_OUT0, 7, 0)>;
};
};
pwm120_sleep: pwm120_sleep {
group1 {
psels = <NRF_PSEL(PWM_OUT0, 7, 0)>;
low-power-enable;
};
};
};

&pwm120 {
status = "okay";
pinctrl-0 = <&pwm120_default>;
pinctrl-1 = <&pwm120_sleep>;
pinctrl-names = "default", "sleep";
memory-regions = <&dma_fast_region>;
};

&dma_fast_region {
status = "okay";
};
40 changes: 21 additions & 19 deletions samples/basic/fade_led/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@

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

#define NUM_STEPS 50U
#define SLEEP_MSEC 25U

int main(void)
{
uint32_t pulse_width = 0U;
uint32_t step = pwm_led0.period / NUM_STEPS;
uint8_t dir = 1U;
int ret;
uint32_t pulse_width;

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

Expand All @@ -35,29 +31,35 @@ int main(void)
}

while (1) {
pulse_width = 0;
ret = pwm_set_pulse_dt(&pwm_led0, pulse_width);
if (ret) {
printk("Error %d: failed to set pulse width\n", ret);
return 0;
}
printk("Using pulse width %d%%\n", 100 * pulse_width / pwm_led0.period);

if (dir) {
pulse_width += step;
if (pulse_width >= pwm_led0.period) {
pulse_width = pwm_led0.period - step;
dir = 0U;
}
} else {
if (pulse_width >= step) {
pulse_width -= step;
} else {
pulse_width = step;
dir = 1U;
}
k_msleep(1000);

pulse_width = pwm_led0.period / 2;
ret = pwm_set_pulse_dt(&pwm_led0, pulse_width);
if (ret) {
printk("Error %d: failed to set pulse width\n", ret);
return 0;
}
printk("Using pulse width %d%%\n", 100 * pulse_width / pwm_led0.period);

k_msleep(1000);

pulse_width = pwm_led0.period;
ret = pwm_set_pulse_dt(&pwm_led0, pulse_width);
if (ret) {
printk("Error %d: failed to set pulse width\n", ret);
return 0;
}
printk("Using pulse width %d%%\n", 100 * pulse_width / pwm_led0.period);

k_sleep(K_MSEC(SLEEP_MSEC));
k_msleep(1000);
}
return 0;
}
Loading