This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nrf52832 pwm interrupt at end of period

Hello everyone,

I'm trying to make a PWM signal that fires an interrupt at the end of each PWM period. I've had the same question here:

https://devzone.nordicsemi.com/f/nordic-q-a/45182/nordic-nrf51822-pwm-ppi-timer1

but now I'm using nrf52832.

Is it possible to have such an interrupt?

If yes, can I do it with nrf_drv_ APIs instead of direct register access like in the link above?

Regards,

L. B.

Parents Reply Children
  • OK, so there is no legal way to do it, because the event NRF_PWM_EVENT_PWMPERIODEND is not being used by the driver.

    Here is how I did it, with combined approach driver API + register access:

    #include "nrf_drv_pwm.h"
    #include "nrfx_pwm.h"

    static nrf_drv_pwm_t m_pwm1 = NRF_DRV_PWM_INSTANCE(1);

    static nrf_drv_pwm_config_t config1 = {
            .output_pins = {
                    3, // channel 0
                    NRF_DRV_PWM_PIN_NOT_USED, // channel 1
                    NRF_DRV_PWM_PIN_NOT_USED, // channel 2
                    NRF_DRV_PWM_PIN_NOT_USED  // channel 3
            },
            .irq_priority = APP_IRQ_PRIORITY_LOWEST,
            .base_clock   = NRF_PWM_CLK_16MHz,
            .count_mode   = NRF_PWM_MODE_UP_AND_DOWN,
            .top_value    = 116, //~70kHz
            .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,
            .step_mode    = NRF_PWM_STEP_AUTO
    };

    static nrf_pwm_values_individual_t /*const*/ seq_values[] = {
            {0x4D}
    };

    static nrf_pwm_sequence_t const pwm_seq_settings = {
            .values.p_individual = seq_values,
            .length              = NRF_PWM_VALUES_LENGTH(seq_values),
            .repeats             = 0,
            .end_delay           = 0
    };

    void user_touch_pwm_handler(nrf_drv_pwm_evt_type_t event_type){
        static uint32_t count = 0;

        if(count == 9){
            nrf_drv_pwm_uninit(&m_pwm1);
        }

        count++;
    }

    void user_touch_pwm_init(void){
        nrf_drv_pwm_init(&m_pwm1, &config1, user_touch_pwm_handler);
        m_pwm1.p_registers->INTENSET |= NRF_PWM_INT_PWMPERIODEND_MASK;
        nrf_drv_pwm_simple_playback(&m_pwm1, &pwm_seq_settings, 1, NRF_DRV_PWM_FLAG_LOOP);
    }

    This program will generate 10 pulses on P0.03 with 70 kHz frequency and will stop.

    Regards,

    L. B.

Related