NRFX PWM Pin Polarity

Hello,

I'm using NRFX PWM directly because I need something more custom than zephyr's PWM driver provides. I'm trying to understand what .pin_polarity field in the config does. It does not seem to invert the polarity of the signal.

All .pin_polarity seems to be doing is setting the pin value after PWM has stopped.

.pin_polarity = {true, true, false, false}

.pin_polarity = {false, false, false, false}

-------------------
nrf_pwm_values_individual_t pwm_vals[] = { { 800, 0, 0, 800 },
{ 0, 800, 0, 800 },
{ 0, 0, 0, 800 },
{ 0, 0, 0, 800 } };
nrfx_pwm_t pwm_instance = NRFX_PWM_INSTANCE(HEARTBEATPWM_INST_IDX);

nrfx_pwm_config_t config = {
.output_pins = { 22, 23, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED },
.pin_inverted = { false, false, false, false },
.irq_priority = NRFX_PWM_DEFAULT_CONFIG_IRQ_PRIORITY,
.base_clock = NRF_PWM_CLK_16MHz,
.count_mode = NRF_PWM_MODE_UP,
.top_value = 800, // 50 µs period (16MHz / 1 prescaler)
.load_mode = NRF_PWM_LOAD_WAVE_FORM,
.step_mode = NRF_PWM_STEP_AUTO,
};

status = nrfx_pwm_init(&pwm_instance, &config, pwm_handler, &pwm_instance);
nrf_pwm_sequence_t seq = { .values.p_individual = pwm_vals,
.length = NRF_PWM_VALUES_LENGTH(pwm_vals),
.repeats = 0,
.end_delay = 0 };

nrfx_pwm_simple_playback(&pwm_instance, &seq, 100, NRFX_PWM_FLAG_LOOP);
-------------------
Is the only way to invert polarity by setting MSB of the PWM to 1 (eg: 800 | (1 << 15))?
Also, I'm a little confused why the default "active" state is LOW. Is that expected?
Parents Reply Children
  • Hi,

    I should likely look into the details here, but on a high level:

    You can't compare the zephyr pwm driver with the nrfx pwm driver, the configuration and setup is completely different.

    When using the zephyr pwm driver it will setup the interface as configured by devicetree as part of the pre-kernel before running to main, in which case the pwm will be controlled according to the zephyr pwm driver.

    If you are using nrfx instead, then the pins are completely untouched by zephyr, it's you that need to manually setup the pins to behave as you want, that include potentially setting pins as output pins with desired level, normally this will occur when you call nrfx_pwm_init, until then the pins are likely floating input input pins. The pwm sequence itself will only occur when calling the nrfx_pwm_simple_playback(), in which case the pwm will behave directly according to pwm hardware.

    Kenneth

Related