Skip to content

Commit 86d019f

Browse files
samples: peripheral: add PWM sample
Add sample to show the use of PWM. Signed-off-by: Eivind Jølsgard <[email protected]>
1 parent 5a1f98e commit 86d019f

File tree

6 files changed

+243
-0
lines changed

6 files changed

+243
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
cmake_minimum_required(VERSION 3.20.0)
8+
9+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
10+
project(pwm)
11+
12+
target_sources(app PRIVATE src/main.c)

samples/peripherals/pwm/Kconfig

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
menu "PWM sample"
8+
9+
config PWM_IRQ_PRIO
10+
int "PWM IRQ priority"
11+
default 5
12+
13+
config PWM_PLAYBACK_COUNT
14+
int "PWM playback count"
15+
default 1
16+
help
17+
Number of times the sequence will be replayed until the
18+
NRFX_PWM_EVT_FINISHED event is raised.
19+
20+
config PWM_VALUE_REPEATS
21+
int "PWM value repeats"
22+
default 150
23+
help
24+
Number of times that each duty cycle is to be repeated (after being played once).
25+
26+
27+
module=PWM_SAMPLE
28+
module-dep=LOG
29+
module-str=PWM Sample
30+
source "$(ZEPHYR_BASE)/subsys/logging/Kconfig.template.log_config"
31+
32+
endmenu # "PWM sample"
33+
34+
source "Kconfig.zephyr"

samples/peripherals/pwm/README.rst

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
.. _pwm_sample:
2+
3+
PWM
4+
###
5+
6+
.. contents::
7+
:local:
8+
:depth: 2
9+
10+
The PWM sample demonstrates how to configure and use PWM with |BMlong|.
11+
12+
Requirements
13+
************
14+
15+
The sample supports the following development kits:
16+
17+
.. tabs::
18+
19+
.. group-tab:: Simple board variants
20+
21+
The following board variants do **not** have DFU capabilities.
22+
23+
.. list-table::
24+
:header-rows: 1
25+
26+
* - Hardware platform
27+
- PCA
28+
- SoftDevice
29+
- Board target
30+
* - `nRF54L15 DK`_
31+
- PCA10156
32+
- S115
33+
- bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice
34+
* - `nRF54L15 DK`_ (emulating nRF54L10)
35+
- PCA10156
36+
- S115
37+
- bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice
38+
* - `nRF54L15 DK`_ (emulating nRF54L05)
39+
- PCA10156
40+
- S115
41+
- bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice
42+
43+
.. group-tab:: MCUboot board variants
44+
45+
The following board variants have DFU capabilities.
46+
47+
.. list-table::
48+
:header-rows: 1
49+
50+
* - Hardware platform
51+
- PCA
52+
- SoftDevice
53+
- Board target
54+
* - `nRF54L15 DK`_
55+
- PCA10156
56+
- S115
57+
- bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice/mcuboot
58+
* - `nRF54L15 DK`_ (emulating nRF54L10)
59+
- PCA10156
60+
- S115
61+
- bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice/mcuboot
62+
* - `nRF54L15 DK`_ (emulating nRF54L05)
63+
- PCA10156
64+
- S115
65+
- bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice/mcuboot
66+
67+
Overview
68+
********
69+
70+
The sample initializes a PWM instance and use that to iluminate LED1 and LED3 on the device.
71+
72+
.. note::
73+
74+
PWM signal can be exposed on GPIO pin only within same domain.
75+
For nRF54L-series there is only one domain which contains both PWM and GPIO: PWM20/21/22 and GPIO Port P1.
76+
Only LEDs connected to P1 can work with PWM, in this case LED1 and LED3.
77+
78+
Building and running
79+
********************
80+
81+
This sample can be found under :file:`samples/peripherals/pwm/` in the |BMshort| folder structure.
82+
83+
.. include:: /includes/create_sample.txt
84+
85+
.. include:: /includes/configure_and_build_sample.txt
86+
87+
.. include:: /includes/program_sample.txt
88+
89+
Testing
90+
=======
91+
92+
You can test this sample by performing the following steps:
93+
94+
1. Compile and program the application.
95+
#. Observe that the ``PWM sample started`` message is printed.
96+
#. Observe that LED1 and LED3 are blinking.

samples/peripherals/pwm/prj.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_LOG=y
2+
CONFIG_LOG_BACKEND_BM_UARTE=y

samples/peripherals/pwm/sample.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
sample:
2+
name: PWM Sample
3+
tests:
4+
sample.pwm:
5+
sysbuild: true
6+
build_only: true
7+
integration_platforms:
8+
- bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice
9+
- bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice
10+
- bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice
11+
- bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice/mcuboot
12+
- bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice/mcuboot
13+
- bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice/mcuboot
14+
platform_allow:
15+
- bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice
16+
- bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice
17+
- bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice
18+
- bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice/mcuboot
19+
- bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice/mcuboot
20+
- bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice/mcuboot
21+
tags: ci_build

samples/peripherals/pwm/src/main.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#include <zephyr/logging/log.h>
8+
#include <zephyr/logging/log_ctrl.h>
9+
#include <nrfx_pwm.h>
10+
#include <board-config.h>
11+
12+
LOG_MODULE_REGISTER(app, CONFIG_PWM_SAMPLE_LOG_LEVEL);
13+
14+
#define PWM_INST_IDX 20
15+
16+
nrf_pwm_values_common_t pwm_val[] = {
17+
0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000,
18+
900, 800, 700, 600, 500, 400, 300, 200, 100, 0
19+
};
20+
21+
static void pwm_handler(nrfx_pwm_evt_type_t event_type, void *ctx)
22+
{
23+
static uint32_t curr_loop = 1;
24+
25+
LOG_INF("Loops: %u ", curr_loop);
26+
curr_loop++;
27+
}
28+
29+
int main(void)
30+
{
31+
nrfx_err_t err;
32+
nrfx_pwm_t pwm_instance = NRFX_PWM_INSTANCE(PWM_INST_IDX);
33+
/*
34+
* PWM signal can be exposed on GPIO pin only within same domain.
35+
* For nRF54L-series there is only one domain which contains both PWM and GPIO:
36+
* PWM20/21/22 and GPIO Port P1.
37+
* Only LEDs connected to P1 can work with PWM, in this case LED1 and LED3.
38+
*/
39+
nrfx_pwm_config_t config = NRFX_PWM_DEFAULT_CONFIG(BOARD_PIN_LED_1, BOARD_PIN_LED_3,
40+
NRF_PWM_PIN_NOT_CONNECTED,
41+
NRF_PWM_PIN_NOT_CONNECTED);
42+
nrf_pwm_sequence_t seq = {
43+
.values = {pwm_val},
44+
.length = NRFX_ARRAY_SIZE(pwm_val),
45+
.repeats = CONFIG_PWM_VALUE_REPEATS,
46+
.end_delay = 0
47+
};
48+
49+
LOG_INF("PWM sample started");
50+
51+
IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_PWM_INST_GET(PWM_INST_IDX)),
52+
CONFIG_PWM_IRQ_PRIO, NRFX_PWM_INST_HANDLER_GET(20), 0, 0);
53+
54+
err = nrfx_pwm_init(&pwm_instance, &config, pwm_handler, &pwm_instance);
55+
if (err != NRFX_SUCCESS) {
56+
LOG_ERR("Failed to initialize PWM, nrfx_error %#x", err);
57+
goto idle;
58+
}
59+
60+
nrfx_pwm_simple_playback(&pwm_instance, &seq, CONFIG_PWM_PLAYBACK_COUNT,
61+
NRFX_PWM_FLAG_LOOP);
62+
63+
idle:
64+
while (true) {
65+
while (LOG_PROCESS()) {
66+
}
67+
68+
/* Wait for an event. */
69+
__WFE();
70+
71+
/* Clear Event Register */
72+
__SEV();
73+
__WFE();
74+
75+
}
76+
77+
return 0;
78+
}

0 commit comments

Comments
 (0)