|
| 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 | +/* nrfx PWM instance index. */ |
| 15 | +#define PWM_INST_IDX 20 |
| 16 | + |
| 17 | +nrf_pwm_values_common_t pwm_val[] = { |
| 18 | + 0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, |
| 19 | + 900, 800, 700, 600, 500, 400, 300, 200, 100, 0 |
| 20 | +}; |
| 21 | + |
| 22 | +static void pwm_handler(nrfx_pwm_evt_type_t event_type, void *ctx) |
| 23 | +{ |
| 24 | + static uint32_t curr_loop = 1; |
| 25 | + |
| 26 | + LOG_INF("Loops: %u ", curr_loop); |
| 27 | + curr_loop++; |
| 28 | +} |
| 29 | + |
| 30 | +int main(void) |
| 31 | +{ |
| 32 | + nrfx_err_t err; |
| 33 | + nrfx_pwm_t pwm_instance = NRFX_PWM_INSTANCE(PWM_INST_IDX); |
| 34 | + /* |
| 35 | + * PWM signal can be exposed on GPIO pin only within the same domain. |
| 36 | + * For nRF54L-series there is only one domain which contains both PWM and GPIO: |
| 37 | + * PWM20/21/22 and GPIO Port P1. |
| 38 | + * Only LEDs connected to P1 can work with PWM, in this case LED1 and LED3. |
| 39 | + */ |
| 40 | + nrfx_pwm_config_t config = NRFX_PWM_DEFAULT_CONFIG(BOARD_PIN_LED_1, BOARD_PIN_LED_3, |
| 41 | + NRF_PWM_PIN_NOT_CONNECTED, |
| 42 | + NRF_PWM_PIN_NOT_CONNECTED); |
| 43 | + nrf_pwm_sequence_t seq = { |
| 44 | + .values = {pwm_val}, |
| 45 | + .length = NRFX_ARRAY_SIZE(pwm_val), |
| 46 | + .repeats = CONFIG_PWM_VALUE_REPEATS, |
| 47 | + .end_delay = 0 |
| 48 | + }; |
| 49 | + |
| 50 | + LOG_INF("PWM sample started"); |
| 51 | + |
| 52 | + IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_PWM_INST_GET(PWM_INST_IDX)), |
| 53 | + CONFIG_PWM_IRQ_PRIO, NRFX_PWM_INST_HANDLER_GET(20), 0, 0); |
| 54 | + |
| 55 | + err = nrfx_pwm_init(&pwm_instance, &config, pwm_handler, &pwm_instance); |
| 56 | + if (err != NRFX_SUCCESS) { |
| 57 | + LOG_ERR("Failed to initialize PWM, nrfx_error %#x", err); |
| 58 | + goto idle; |
| 59 | + } |
| 60 | + |
| 61 | + nrfx_pwm_simple_playback(&pwm_instance, &seq, CONFIG_PWM_PLAYBACK_COUNT, |
| 62 | + NRFX_PWM_FLAG_LOOP); |
| 63 | + |
| 64 | +idle: |
| 65 | + while (true) { |
| 66 | + while (LOG_PROCESS()) { |
| 67 | + } |
| 68 | + |
| 69 | + /* Wait for an event. */ |
| 70 | + __WFE(); |
| 71 | + |
| 72 | + /* Clear Event Register */ |
| 73 | + __SEV(); |
| 74 | + __WFE(); |
| 75 | + } |
| 76 | + |
| 77 | + return 0; |
| 78 | +} |
0 commit comments