Hi,
I am trying to do pwm a rgb led using nrf54l15dk and custom board using nrf54l15 on port1 with pin connections on (p1.10, p1.8 and p1.13 for custom) or on (p1.11, p1.12 and p1.10 for nrf54l15dk).
So, on nrf54l15dk I am able to do pwm using pwm_set_dt command, but it consumes CPU cycles so, I want to move that to nrf_pwm_sequence_t command which works fine in nrf52840. When I tried nrf_pwm_sequence_t it did not modulate pulse which it should do.
I tried doing using both common and individual load, still no luck.
#define TOP_VAL_FADE 250
#define STEP_COUNT_FADE 25
static nrfx_pwm_t m_pwm20 = NRFX_PWM_INSTANCE(20);
static nrfx_pwm_t m_pwm21 = NRFX_PWM_INSTANCE(21);
static nrf_pwm_values_common_t fade_in_out_values[2 * STEP_COUNT_FADE];
static nrf_pwm_sequence_t led_on_seq;
static nrf_pwm_sequence_t led_off_seq;
static nrf_pwm_values_common_t stay_off_values[2] = {0};
void set_fade_values_common(void)
{
for (uint32_t i = 0; i < STEP_COUNT_FADE; i++)
{
uint16_t value = (TOP_VAL_FADE * (i + 1)) / STEP_COUNT_FADE;
fade_in_out_values[i] = value;
fade_in_out_values[STEP_COUNT_FADE + i] = TOP_VAL_FADE - value;
LOG_INF("value of fade %d: %d", i, value);
}
}
void start_pwm_pulse(void)
{
// Configure fade in/out sequence
led_on_seq.values.p_common = fade_in_out_values;
led_on_seq.length = NRF_PWM_VALUES_LENGTH(fade_in_out_values);
led_on_seq.repeats = 160; // All we have to do is update the repeats! Less repeats = faster fade in/out
led_on_seq.end_delay = 0;
// Configure off sequence
led_off_seq.values.p_common = stay_off_values;
led_off_seq.length = NRF_PWM_VALUES_LENGTH(stay_off_values);
led_off_seq.repeats = 30;
led_off_seq.end_delay = 0;
// Start playback
nrfx_pwm_complex_playback(&m_pwm20, &led_on_seq, &led_off_seq, 1, NRFX_PWM_FLAG_LOOP);
nrfx_pwm_complex_playback(&m_pwm21, &led_on_seq, &led_off_seq, 1, NRFX_PWM_FLAG_LOOP);
LOG_INF("PWM pulse started");
}
static int ktd2026_init(const struct device *dev) {
LOG_DBG("LED Softblink init");
nrfx_pwm_config_t config20 = {
// .output_pins = { RED_PIN, GREEN_PIN, 0xFF, 0xFF }, // 0xFF = NRFX_PWM_PIN_NOT_USED
.output_pins = { RED_PIN, 0xFF, 0xFF, 0xFF }, // 0xFF = NRFX_PWM_PIN_NOT_USED
.irq_priority = 7,
.base_clock = NRF_PWM_CLK_1MHz,
.count_mode = NRF_PWM_MODE_UP,
.top_value = TOP_VAL_FADE,
.load_mode = NRF_PWM_LOAD_COMMON,
.step_mode = NRF_PWM_STEP_AUTO
};
nrfx_pwm_config_t config21 = {
.output_pins = { GREEN_PIN, 0xFF, 0xFF, 0xFF }, // 0xFF = NRFX_PWM_PIN_NOT_USED
.irq_priority = 7,
.base_clock = NRF_PWM_CLK_1MHz,
.count_mode = NRF_PWM_MODE_UP,
.top_value = TOP_VAL_FADE,
.load_mode = NRF_PWM_LOAD_COMMON,
.step_mode = NRF_PWM_STEP_AUTO
};
if (!nrfx_pwm_init_check(&m_pwm20)) {
nrfx_err_t err = nrfx_pwm_init(&m_pwm20, &config20, NULL, NULL);
if (err != NRFX_SUCCESS) {
LOG_ERR("PWM20 init failed: %d", err);
return -EIO;
}
} else {
LOG_INF("PWM20 already initialized");
}
if (!nrfx_pwm_init_check(&m_pwm21)) {
nrfx_err_t err = nrfx_pwm_init(&m_pwm21, &config21, NULL, NULL);
if (err != NRFX_SUCCESS) {
LOG_ERR("PWM21 init failed: %d", err);
return -EIO;
}
} else {
LOG_INF("PWM21 already initialized");
}
set_fade_values_common();
return 0;
}and with individual
#define TOP_VAL_FADE 250
#define STEP_COUNT_FADE 25
static nrfx_pwm_t m_pwm20 = NRFX_PWM_INSTANCE(20);
static nrfx_pwm_t m_pwm21 = NRFX_PWM_INSTANCE(21);
static nrf_pwm_values_individual_t fade_in_out_values[2 * STEP_COUNT_FADE];
static nrf_pwm_sequence_t led_on_seq;
static nrf_pwm_sequence_t led_off_seq;
static nrf_pwm_values_individual_t stay_off_values[2] = {0};
void set_fade_values_individual(void)
{
for (uint32_t i = 0; i < STEP_COUNT_FADE; i++)
{
uint16_t value = (TOP_VAL_FADE * (i + 1)) / STEP_COUNT_FADE;
// Fade in
fade_in_out_values[i].channel_0 = value; // Red
fade_in_out_values[i].channel_1 = value; // Green
fade_in_out_values[i].channel_2 = 0; // Blue
fade_in_out_values[i].channel_3 = 0;
// Fade out
fade_in_out_values[STEP_COUNT_FADE + i].channel_0 = TOP_VAL_FADE - value;
fade_in_out_values[STEP_COUNT_FADE + i].channel_1 = TOP_VAL_FADE - value;
fade_in_out_values[STEP_COUNT_FADE + i].channel_2 = 0;
fade_in_out_values[STEP_COUNT_FADE + i].channel_3 = 0;
LOG_INF("value of fade %d: %d", i, value);
}
}
void start_pwm_pulse(uint32_t ramp_time_ms)
{
// Configure fade in/out sequence
led_on_seq.values.p_individual = fade_in_out_values;
led_on_seq.length = NRF_PWM_VALUES_LENGTH(fade_in_out_values);
led_on_seq.repeats = 160; // All we have to do is update the repeats! Less repeats = faster fade in/out
led_on_seq.end_delay = 0;
// Configure off sequence
led_off_seq.values.p_individual = stay_off_values;
led_off_seq.length = NRF_PWM_VALUES_LENGTH(stay_off_values);
led_off_seq.repeats = 30;
led_off_seq.end_delay = 0;
// Start playback
nrfx_pwm_complex_playback(&m_pwm20, &led_on_seq, &led_off_seq, 1, NRFX_PWM_FLAG_LOOP);
nrfx_pwm_complex_playback(&m_pwm21, &led_on_seq, &led_off_seq, 1, NRFX_PWM_FLAG_LOOP);
LOG_INF("PWM pulse started");
}
static int ktd2026_init(const struct device *dev) {
LOG_DBG("LED Softblink init");
nrfx_pwm_config_t config20 = {
// .output_pins = { RED_PIN, GREEN_PIN, 0xFF, 0xFF }, // 0xFF = NRFX_PWM_PIN_NOT_USED
.output_pins = { RED_PIN, 0xFF, 0xFF, 0xFF }, // 0xFF = NRFX_PWM_PIN_NOT_USED
.irq_priority = 7,
.base_clock = NRF_PWM_CLK_1MHz,
.count_mode = NRF_PWM_MODE_UP,
.top_value = RED_PWM_PERIOD_NS,
.load_mode = NRF_PWM_LOAD_INDIVIDUAL,
.step_mode = NRF_PWM_STEP_AUTO
};
nrfx_pwm_config_t config21 = {
.output_pins = { GREEN_PIN, 0xFF, 0xFF, 0xFF }, // 0xFF = NRFX_PWM_PIN_NOT_USED
.irq_priority = 7,
.base_clock = NRF_PWM_CLK_1MHz,
.count_mode = NRF_PWM_MODE_UP,
.top_value = RED_PWM_PERIOD_NS,
.load_mode = NRF_PWM_LOAD_INDIVIDUAL,
.step_mode = NRF_PWM_STEP_AUTO
};
if (!nrfx_pwm_init_check(&m_pwm20)) {
nrfx_err_t err = nrfx_pwm_init(&m_pwm20, &config20, NULL, NULL);
if (err != NRFX_SUCCESS) {
LOG_ERR("PWM20 init failed: %d", err);
return -EIO;
}
} else {
LOG_INF("PWM20 already initialized");
}
if (!nrfx_pwm_init_check(&m_pwm21)) {
nrfx_err_t err = nrfx_pwm_init(&m_pwm21, &config21, NULL, NULL);
if (err != NRFX_SUCCESS) {
LOG_ERR("PWM21 init failed: %d", err);
return -EIO;
}
} else {
LOG_INF("PWM21 already initialized");
}
set_fade_values_individual();
return 0;
}


