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

nrf52840 PWM Driver update duty cycle

Hello,

SDK: 15.3.0

Hardware: nRF52840-DK

I am attempting to make a simple dimmer with 2 pins/LEDs. I have used the following post.

https://devzone.nordicsemi.com/f/nordic-q-a/38607/nrf5-sdk15-0---pwm-driver-just-set-a-fixed-duty-cycle

https://devzone.nordicsemi.com/f/nordic-q-a/39314/pwm-update_function/152441#152441

I am attempting to do a very similar task, but software may need to adjust the duty cycle (sequence value) at any point to adjust the brightness of the LEDs. I have made several attempts. in all attempts I am using nrf_delay_ms(2000) to delay the program calls, and act as a time delay between when the program would call different update functions. pwm would always be on, and the LED would be off at seq_value = 0, and 100% Duty Cycle (Fully On) at seq_value = .top_value

Q1: There are various built in update functions like nrfx_pwm_sequence_values_update, both legacy, and not. Am I using the correct function to complete this goal?

Q2: Which method that I have attempted is the preferred method, and is there an alternative method that may be better?

Attempt 1: fails to build due to the 3rd param of nrfx_pwm_sequence_values_update(&m_pwm0,0, pwm_count); not matching

static void update_LED(uint16_t duty_cycle)
{
    //Attempt 1
    uint16_t pwm_count = 32768 *(duty_cycle / 100);
    nrfx_pwm_sequence_values_update(&m_pwm0,0, pwm_count);
}

static void demo3(void)
{
    NRF_LOG_INFO("Demo 3");

    /*
     * This demo uses only one channel, which is reflected on LED 1.
     * The LED blinks three times (200 ms on, 200 ms off), then it stays off
     * for one second.
     * This scheme is performed three times before the peripheral is stopped.
     */

    nrf_drv_pwm_config_t const config0 =
    {
        .output_pins =
        {
            BSP_LED_0 | NRF_DRV_PWM_PIN_INVERTED, // 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_2MHz,//NRF_PWM_CLK_125kHz,
        .count_mode   = NRF_PWM_MODE_UP,
        .top_value    = 32768, // = 0x8000
        .load_mode    = NRF_PWM_LOAD_COMMON,
        .step_mode    = NRF_PWM_STEP_AUTO
    };
    APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm0, &config0, NULL));
    m_used |= USED_PWM(0);

    // This array cannot be allocated on stack (hence "static") and it must
    // be in RAM (hence no "const", though its content is not changed).
    static uint16_t /*const*/ seq_values[] =
    {
        0x3000,
//        0x2000,
//        0x4000,
//        0x6000,
//        0x7FFF,
//        0x8000
    };
    nrf_pwm_sequence_t const seq =
    {
        .values.p_common = seq_values,
        .length          = NRF_PWM_VALUES_LENGTH(seq_values),
        .repeats         = 0,
        .end_delay       = 0
    };

    (void)nrf_drv_pwm_simple_playback(&m_pwm0, &seq, 3, NRF_DRV_PWM_FLAG_LOOP);
}

int main(void)
{
...
    demo3();
    nrf_delay_ms(2000);
    update_LED(100);
    nrf_delay_ms(2000);
    update_LED(25);
...
}

Attempt 2: still fails to build due to the 3rd param of nrfx_pwm_sequence_values_update(&m_pwm0,0, pwm_count); not matching

static void update_LED(uint16_t duty_cycle)
{ 
    //Attempt 2
    uint16_t pwm_count = 32768 *(duty_cycle / 100);
    nrf_pwm_values_common_t const *p_pwm_count = pwm_count;
    nrfx_pwm_sequence_values_update(&m_pwm0,0, p_pwm_count);
}

static void demo3(void)
{
    NRF_LOG_INFO("Demo 3");

    /*
     * This demo uses only one channel, which is reflected on LED 1.
     * The LED blinks three times (200 ms on, 200 ms off), then it stays off
     * for one second.
     * This scheme is performed three times before the peripheral is stopped.
     */

    nrf_drv_pwm_config_t const config0 =
    {
        .output_pins =
        {
            BSP_LED_0 | NRF_DRV_PWM_PIN_INVERTED, // 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_2MHz,//NRF_PWM_CLK_125kHz,
        .count_mode   = NRF_PWM_MODE_UP,
        .top_value    = 32768, // = 0x8000
        .load_mode    = NRF_PWM_LOAD_COMMON,
        .step_mode    = NRF_PWM_STEP_AUTO
    };
    APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm0, &config0, NULL));
    m_used |= USED_PWM(0);
    // This array cannot be allocated on stack (hence "static") and it must
    // be in RAM (hence no "const", though its content is not changed).
    static uint16_t /*const*/ seq_values[] =
    {
        0x3000,
//        0x2000,
//        0x4000,
//        0x6000,
//        0x7FFF,
//        0x8000
    };
    nrf_pwm_sequence_t const seq =
    {
        .values.p_common = seq_values,
        .length          = NRF_PWM_VALUES_LENGTH(seq_values),
        .repeats         = 0,
        .end_delay       = 0
    };

    (void)nrf_drv_pwm_simple_playback(&m_pwm0, &seq, 3, NRF_DRV_PWM_FLAG_LOOP);

}

int main(void)
{
...
    demo3();
    nrf_delay_ms(2000);
    update_LED(100);
    nrf_delay_ms(2000);
    update_LED(25);
...
}

Attempt 3: Build and runs, but not as expected, it is as if the update_LED function has updated the value at the same time, and the two duty cycles are overlapping. The result is a 2 second delay (expected), with a rapid blinking LED (not expected), followed by LED off.

nrf_pwm_values_individual_t seq_values_test[] = { 16384};
nrf_pwm_sequence_t const seq_test =
{
     .values.p_common = seq_values_test,
     .length          = NRF_PWM_VALUES_LENGTH(seq_values_test),
     .repeats         = 0,
     .end_delay       = 0
};


static void update_LED(uint16_t duty_cycle)
{
    //Attempt 3
    seq_values_test->channel_0 = pwm_count;
    nrf_drv_pwm_simple_playback(&m_pwm0, &seq_test, 1, NRF_DRV_PWM_FLAG_LOOP);
}

static void demo3(void)
{
    NRF_LOG_INFO("Demo 3");

    /*
     * This demo uses only one channel, which is reflected on LED 1.
     * The LED blinks three times (200 ms on, 200 ms off), then it stays off
     * for one second.
     * This scheme is performed three times before the peripheral is stopped.
     */

    nrf_drv_pwm_config_t const config0 =
    {
        .output_pins =
        {
            BSP_LED_0 | NRF_DRV_PWM_PIN_INVERTED, // 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_2MHz,//NRF_PWM_CLK_125kHz,
        .count_mode   = NRF_PWM_MODE_UP,
        .top_value    = 32768, // = 0x8000
        .load_mode    = NRF_PWM_LOAD_COMMON,
        .step_mode    = NRF_PWM_STEP_AUTO
    };
    APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm0, &config0, NULL));
    m_used |= USED_PWM(0);
}

int main(void)
{
...
    demo3();
    nrf_delay_ms(2000);
    update_LED(100);
    nrf_delay_ms(2000);
    update_LED(25);
...
}

Parents
  • I am able to build and run the one of the methods, although the LED is not being dimmed. I have tested previously that sequence value = 16k is ~50% duty cycle and the LED is dimmed as expected, but this was by changing the value and recompiling. What I am seeing with this code is the LED is 100%, but for a duty cycle of 50% the LED is turned off.

    SDK: 15.3.0

    Hardware: nRF52840-DK

    #include <stdio.h>
    #include <string.h>
    #include "nrf_drv_pwm.h"
    #include "app_util_platform.h"
    #include "app_error.h"
    #include "boards.h"
    #include "bsp.h"
    #include "app_timer.h"
    #include "nrf_drv_clock.h"
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    #include "nrf_delay.h"
    
    static nrf_drv_pwm_t m_pwm0 = NRF_DRV_PWM_INSTANCE(0);
    
    
    static uint8_t m_used = 0;
    
    
    nrf_pwm_values_individual_t seq_values_test[] = { 16384};
    nrf_pwm_sequence_t const seq_test =
    {
        .values.p_individual = seq_values_test,
        .length          = NRF_PWM_VALUES_LENGTH(seq_values_test),
        .repeats         = 0,
        .end_delay       = 0
    };
    
    
    static void update_LED(uint16_t duty_cycle)
    {
        uint16_t pwm_count = 32768 *(duty_cycle / 100);
        //nrf_pwm_values_common_t const *p_pwm_count = pwm_count;
        //nrfx_pwm_sequence_values_update(&m_pwm0,0, p_pwm_count);
        
        seq_values_test->channel_0 = pwm_count;
        nrf_drv_pwm_simple_playback(&m_pwm0, &seq_test, 1, NRF_DRV_PWM_FLAG_LOOP);
    }
    
    static void demo3(void)
    {
        NRF_LOG_INFO("Demo 3");
    
        nrf_drv_pwm_config_t const config0 =
        {
            .output_pins =
            {
                BSP_LED_0 | NRF_DRV_PWM_PIN_INVERTED, // 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_2MHz, //NRF_PWM_CLK_125kHz,
            .count_mode   = NRF_PWM_MODE_UP,
            .top_value    = 32768, //25000,
            .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,
            .step_mode    = NRF_PWM_STEP_AUTO
        };
        APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm0, &config0, NULL));
    }
    
    static void init_bsp()
    {
        APP_ERROR_CHECK(nrf_drv_clock_init());
        nrf_drv_clock_lfclk_request(NULL);
    
        APP_ERROR_CHECK(app_timer_init());
    
    }
    
    
    void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
    {
        bsp_board_leds_on();
        app_error_save_and_stop(id, pc, info);
    }
    
    
    int main(void)
    {
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        init_bsp();
    
        NRF_LOG_INFO("PWM example started.");
    
        // Start with Demo 1, then switch to another one when the user presses
        // button 1 or button 2 (see the 'bsp_evt_handler' function).
        demo3();
        nrf_delay_ms(2000);
        update_LED(100);
        nrf_delay_ms(2000);
        update_LED(50);
        nrf_delay_ms(2000);
        update_LED(100);
        nrf_delay_ms(2000);
        update_LED(50);
        NRF_LOG_FLUSH();
    
    
        for (;;)
        {
            // Wait for an event.
            __WFE();
    
            // Clear the event register.
            __SEV();
            __WFE();
    
            NRF_LOG_FLUSH();
        }
        
    }

Reply
  • I am able to build and run the one of the methods, although the LED is not being dimmed. I have tested previously that sequence value = 16k is ~50% duty cycle and the LED is dimmed as expected, but this was by changing the value and recompiling. What I am seeing with this code is the LED is 100%, but for a duty cycle of 50% the LED is turned off.

    SDK: 15.3.0

    Hardware: nRF52840-DK

    #include <stdio.h>
    #include <string.h>
    #include "nrf_drv_pwm.h"
    #include "app_util_platform.h"
    #include "app_error.h"
    #include "boards.h"
    #include "bsp.h"
    #include "app_timer.h"
    #include "nrf_drv_clock.h"
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    #include "nrf_delay.h"
    
    static nrf_drv_pwm_t m_pwm0 = NRF_DRV_PWM_INSTANCE(0);
    
    
    static uint8_t m_used = 0;
    
    
    nrf_pwm_values_individual_t seq_values_test[] = { 16384};
    nrf_pwm_sequence_t const seq_test =
    {
        .values.p_individual = seq_values_test,
        .length          = NRF_PWM_VALUES_LENGTH(seq_values_test),
        .repeats         = 0,
        .end_delay       = 0
    };
    
    
    static void update_LED(uint16_t duty_cycle)
    {
        uint16_t pwm_count = 32768 *(duty_cycle / 100);
        //nrf_pwm_values_common_t const *p_pwm_count = pwm_count;
        //nrfx_pwm_sequence_values_update(&m_pwm0,0, p_pwm_count);
        
        seq_values_test->channel_0 = pwm_count;
        nrf_drv_pwm_simple_playback(&m_pwm0, &seq_test, 1, NRF_DRV_PWM_FLAG_LOOP);
    }
    
    static void demo3(void)
    {
        NRF_LOG_INFO("Demo 3");
    
        nrf_drv_pwm_config_t const config0 =
        {
            .output_pins =
            {
                BSP_LED_0 | NRF_DRV_PWM_PIN_INVERTED, // 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_2MHz, //NRF_PWM_CLK_125kHz,
            .count_mode   = NRF_PWM_MODE_UP,
            .top_value    = 32768, //25000,
            .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,
            .step_mode    = NRF_PWM_STEP_AUTO
        };
        APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm0, &config0, NULL));
    }
    
    static void init_bsp()
    {
        APP_ERROR_CHECK(nrf_drv_clock_init());
        nrf_drv_clock_lfclk_request(NULL);
    
        APP_ERROR_CHECK(app_timer_init());
    
    }
    
    
    void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
    {
        bsp_board_leds_on();
        app_error_save_and_stop(id, pc, info);
    }
    
    
    int main(void)
    {
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        init_bsp();
    
        NRF_LOG_INFO("PWM example started.");
    
        // Start with Demo 1, then switch to another one when the user presses
        // button 1 or button 2 (see the 'bsp_evt_handler' function).
        demo3();
        nrf_delay_ms(2000);
        update_LED(100);
        nrf_delay_ms(2000);
        update_LED(50);
        nrf_delay_ms(2000);
        update_LED(100);
        nrf_delay_ms(2000);
        update_LED(50);
        NRF_LOG_FLUSH();
    
    
        for (;;)
        {
            // Wait for an event.
            __WFE();
    
            // Clear the event register.
            __SEV();
            __WFE();
    
            NRF_LOG_FLUSH();
        }
        
    }

Children
No Data
Related