-
Notifications
You must be signed in to change notification settings - Fork 13
samples: peripheral: add PWM sample #346
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
Open
eivindj-nordic
wants to merge
1
commit into
nrfconnect:main
Choose a base branch
from
eivindj-nordic:pwm_sample
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# | ||
# Copyright (c) 2025 Nordic Semiconductor ASA | ||
# | ||
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause | ||
# | ||
|
||
cmake_minimum_required(VERSION 3.20.0) | ||
|
||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
project(pwm) | ||
|
||
target_sources(app PRIVATE src/main.c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# | ||
# Copyright (c) 2025 Nordic Semiconductor ASA | ||
# | ||
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause | ||
# | ||
|
||
menu "PWM sample" | ||
|
||
config PWM_IRQ_PRIO | ||
int "PWM IRQ priority" | ||
default 5 | ||
|
||
config PWM_PLAYBACK_COUNT | ||
int "PWM playback count" | ||
default 1 | ||
help | ||
Number of times the sequence will be replayed until the | ||
NRFX_PWM_EVT_FINISHED event is raised. | ||
|
||
config PWM_VALUE_REPEATS | ||
int "PWM value repeats" | ||
default 150 | ||
help | ||
Number of times that each duty cycle is to be repeated (after being played once). | ||
|
||
|
||
module=PWM_SAMPLE | ||
module-dep=LOG | ||
module-str=PWM Sample | ||
source "$(ZEPHYR_BASE)/subsys/logging/Kconfig.template.log_config" | ||
|
||
endmenu # "PWM sample" | ||
|
||
source "Kconfig.zephyr" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
.. _pwm_sample: | ||
|
||
PWM | ||
### | ||
|
||
.. contents:: | ||
:local: | ||
:depth: 2 | ||
|
||
The PWM sample demonstrates how to configure and use PWM with |BMlong|. | ||
|
||
Requirements | ||
************ | ||
|
||
The sample supports the following development kits: | ||
|
||
.. tabs:: | ||
|
||
.. group-tab:: Simple board variants | ||
|
||
The following board variants do **not** have DFU capabilities. | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
|
||
* - Hardware platform | ||
- PCA | ||
- SoftDevice | ||
- Board target | ||
* - `nRF54L15 DK`_ | ||
- PCA10156 | ||
- S115 | ||
- bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice | ||
* - `nRF54L15 DK`_ (emulating nRF54L10) | ||
- PCA10156 | ||
- S115 | ||
- bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice | ||
* - `nRF54L15 DK`_ (emulating nRF54L05) | ||
- PCA10156 | ||
- S115 | ||
- bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice | ||
|
||
.. group-tab:: MCUboot board variants | ||
|
||
The following board variants have DFU capabilities. | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
|
||
* - Hardware platform | ||
- PCA | ||
- SoftDevice | ||
- Board target | ||
* - `nRF54L15 DK`_ | ||
- PCA10156 | ||
- S115 | ||
- bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice/mcuboot | ||
* - `nRF54L15 DK`_ (emulating nRF54L10) | ||
- PCA10156 | ||
- S115 | ||
- bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice/mcuboot | ||
* - `nRF54L15 DK`_ (emulating nRF54L05) | ||
- PCA10156 | ||
- S115 | ||
- bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice/mcuboot | ||
|
||
Overview | ||
******** | ||
|
||
The sample initializes a PWM instance that blinks LED1 and LED3 on the device. | ||
|
||
.. note:: | ||
|
||
The PWM signal can be exposed only on GPIO pins that belong to the same domain as the target pin. | ||
For nRF54L-series there is only one domain which contains both PWM and GPIO: PWM20/21/22 and GPIO Port P1. | ||
For the nRF54L Series only LEDs connected to P1 can work with PWM, for the nRF54L15DK this is LED1 and LED3. | ||
|
||
Building and running | ||
******************** | ||
|
||
This sample can be found under :file:`samples/peripherals/pwm/` in the |BMshort| folder structure. | ||
|
||
.. include:: /includes/create_sample.txt | ||
|
||
.. include:: /includes/configure_and_build_sample.txt | ||
|
||
.. include:: /includes/program_sample.txt | ||
|
||
Testing | ||
======= | ||
|
||
You can test this sample by performing the following steps: | ||
|
||
1. Compile and program the application. | ||
#. Observe that the ``PWM sample started`` message is printed. | ||
#. Observe that LED1 and LED3 are blinking. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
CONFIG_LOG=y | ||
CONFIG_LOG_BACKEND_BM_UARTE=y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
sample: | ||
name: PWM Sample | ||
tests: | ||
sample.pwm: | ||
sysbuild: true | ||
build_only: true | ||
integration_platforms: | ||
- bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice | ||
- bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice | ||
- bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice | ||
- bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice/mcuboot | ||
- bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice/mcuboot | ||
- bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice/mcuboot | ||
platform_allow: | ||
- bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice | ||
- bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice | ||
- bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice | ||
- bm_nrf54l15dk/nrf54l05/cpuapp/s115_softdevice/mcuboot | ||
- bm_nrf54l15dk/nrf54l10/cpuapp/s115_softdevice/mcuboot | ||
- bm_nrf54l15dk/nrf54l15/cpuapp/s115_softdevice/mcuboot | ||
tags: ci_build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright (c) 2025 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause | ||
*/ | ||
|
||
#include <zephyr/logging/log.h> | ||
#include <zephyr/logging/log_ctrl.h> | ||
#include <nrfx_pwm.h> | ||
#include <board-config.h> | ||
|
||
LOG_MODULE_REGISTER(app, CONFIG_PWM_SAMPLE_LOG_LEVEL); | ||
|
||
#define PWM_INST_IDX 20 | ||
|
||
nrf_pwm_values_common_t pwm_val[] = { | ||
0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, | ||
900, 800, 700, 600, 500, 400, 300, 200, 100, 0 | ||
}; | ||
|
||
static void pwm_handler(nrfx_pwm_evt_type_t event_type, void *ctx) | ||
{ | ||
static uint32_t curr_loop = 1; | ||
|
||
LOG_INF("Loops: %u ", curr_loop); | ||
curr_loop++; | ||
} | ||
|
||
int main(void) | ||
{ | ||
nrfx_err_t err; | ||
nrfx_pwm_t pwm_instance = NRFX_PWM_INSTANCE(PWM_INST_IDX); | ||
/* | ||
* PWM signal can be exposed on GPIO pin only within same domain. | ||
* For nRF54L-series there is only one domain which contains both PWM and GPIO: | ||
* PWM20/21/22 and GPIO Port P1. | ||
* Only LEDs connected to P1 can work with PWM, in this case LED1 and LED3. | ||
*/ | ||
nrfx_pwm_config_t config = NRFX_PWM_DEFAULT_CONFIG(BOARD_PIN_LED_1, BOARD_PIN_LED_3, | ||
NRF_PWM_PIN_NOT_CONNECTED, | ||
NRF_PWM_PIN_NOT_CONNECTED); | ||
nrf_pwm_sequence_t seq = { | ||
.values = {pwm_val}, | ||
.length = NRFX_ARRAY_SIZE(pwm_val), | ||
.repeats = CONFIG_PWM_VALUE_REPEATS, | ||
.end_delay = 0 | ||
}; | ||
|
||
LOG_INF("PWM sample started"); | ||
|
||
IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_PWM_INST_GET(PWM_INST_IDX)), | ||
CONFIG_PWM_IRQ_PRIO, NRFX_PWM_INST_HANDLER_GET(20), 0, 0); | ||
|
||
err = nrfx_pwm_init(&pwm_instance, &config, pwm_handler, &pwm_instance); | ||
if (err != NRFX_SUCCESS) { | ||
LOG_ERR("Failed to initialize PWM, nrfx_error %#x", err); | ||
goto idle; | ||
} | ||
|
||
nrfx_pwm_simple_playback(&pwm_instance, &seq, CONFIG_PWM_PLAYBACK_COUNT, | ||
NRFX_PWM_FLAG_LOOP); | ||
|
||
idle: | ||
while (true) { | ||
while (LOG_PROCESS()) { | ||
} | ||
|
||
/* Wait for an event. */ | ||
__WFE(); | ||
|
||
/* Clear Event Register */ | ||
__SEV(); | ||
__WFE(); | ||
|
||
} | ||
|
||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this mirror other similar Kconfigs of other peripheral libraries? Taking into account SoftDevice and such.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It already mirrors how it is done for LPUARTE and UARTE.