Hello,
I am trying to use the nrfx driver example to run an RGB led connected externally to the nrf5340 board.
I dont see the led/pwm cycle running successfully. Can anyone please have a look & suggest corrections?
Device Tree Overlay File:
&sw_pwm { status = "okay"; channel-gpios = <&gpio1 13 PWM_POLARITY_NORMAL>, <&gpio1 14 PWM_POLARITY_NORMAL>, <&gpio1 15 PWM_POLARITY_NORMAL>; }; / { pwmleds { compatible = "pwm-leds"; status = "okay"; red_pwm_led: red_pwm_led { pwms = <&sw_pwm 2 PWM_MSEC(100) PWM_POLARITY_INVERTED>; label = "RED PWM LED"; }; green_pwm_led: green_pwm_led { pwms = <&sw_pwm 0 PWM_MSEC(100) PWM_POLARITY_INVERTED>; label = "GREEN PWM LED"; }; blue_pwm_led: blue_pwm_led { pwms = <&sw_pwm 1 PWM_MSEC(100) PWM_POLARITY_INVERTED>; label = "BLUE PWM LED"; }; }; aliases { red-pwm-led = &red_pwm_led; green-pwm-led = &green_pwm_led; blue-pwm-led = &blue_pwm_led; swpwm = &sw_pwm; }; };
Code Snippet:
#include "nrfx_pwm.h" #define GREEN_LED_PIN (NRF_GPIO_PIN_MAP(1, 13)) #define BLUE_LED_PIN (NRF_GPIO_PIN_MAP(1, 14)) #define RED_LED_PIN (NRF_GPIO_PIN_MAP(1, 15)) static nrfx_pwm_t m_pwm0 = NRFX_PWM_INSTANCE(0); // Sequence: LED ON for 25 ms, OFF for 9975 ms (assuming 10s period, 1 Hz) static nrf_pwm_values_common_t seq_values[] = { 25, // ON (duty cycle value for 25 ms) 0 // OFF (duty cycle value for 9975 ms) }; static nrf_pwm_sequence_t const seq = { .values.p_common = seq_values, .length = NRF_PWM_VALUES_LENGTH(seq_values), .repeats = 0, .end_delay = 0 }; void pwm_init(void) { nrfx_pwm_config_t const config0 = { .output_pins = { GREEN_LED_PIN, BLUE_LED_PIN, RED_LED_PIN }, .irq_priority = NRFX_PWM_DEFAULT_CONFIG_IRQ_PRIORITY, .base_clock = NRF_PWM_CLK_1MHz, .count_mode = NRF_PWM_MODE_UP, .top_value = 10000, // 10,000 ticks at 1 MHz = 10 ms per period .load_mode = NRF_PWM_LOAD_COMMON, .step_mode = NRF_PWM_STEP_AUTO }; nrfx_pwm_init(&m_pwm0, &config0, NULL, NULL); } void start_pwm(void) { nrfx_pwm_simple_playback(&m_pwm0, &seq, 1, NRFX_PWM_FLAG_LOOP); }