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

Updating PWM output signal

Hi,

May be it's some what basic but I'm not able to update my pwm value. 
I'm using sdk 14.02 with softdevice in SES. 
This is my code:

PWM Config:

static uint16_t const              base_pwm_top  = 10000;
static uint16_t const              base_pwm_step = 100;

static uint8_t                       pwm_phase;
static nrf_pwm_values_individual_t   pwm_seq_values;
static nrf_pwm_sequence_t const      pwm_seq =
{
    .values.p_individual = &pwm_seq_values,
    .length              = NRF_PWM_VALUES_LENGTH(pwm_seq_values),
    .repeats             = 0,
    .end_delay           = 0
};

PWM init:

  //Base PWM config
    ret_code_t err_code_base;
    // configure nrf driver
    nrf_drv_pwm_config_t const config1 =
    {
        .output_pins =
        {
            BASE_PWM_PIN,//| NRF_DRV_PWM_PIN_INVERTED, // channel 0
            NRF_DRV_PWM_PIN_NOT_USED,            
            NRF_DRV_PWM_PIN_NOT_USED,
            NRF_DRV_PWM_PIN_NOT_USED,
        },
        .irq_priority = APP_IRQ_PRIORITY_LOWEST,
        .base_clock   = NRF_PWM_CLK_1MHz,
        .count_mode   = NRF_PWM_MODE_UP,
        .top_value    = base_pwm_top,
        .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,
        .step_mode    = NRF_PWM_STEP_AUTO
    };

    err_code_base = nrf_drv_pwm_init(&m_base_pwm, &config1, base_pwm_event_handler);
    APP_ERROR_CHECK(err_code_base);
    if (err_code_base != NRF_SUCCESS)
    {
        // Initialization failed. Take recovery action.
        NRF_LOG_INFO("PWM initialization failed");
    
    }



Update pwm value in pwm event handler: 

//base_pwm_event_handler
static void base_pwm_event_handler(nrf_drv_pwm_evt_type_t event_type)
{
   if (event_type == NRF_DRV_PWM_EVT_FINISHED)
   {
       
      pwm_seq_values.channel_0 = base_pwm_val;
     
   } 
}

Update pwm sequence:

static void pwm_timer_timeout_handler(void *p_context)
{

      UNUSED_PARAMETER(p_context); 
      pid_control_loop();
      /* Update duty cycle cap PWM ... */
      nrf_drv_pwm_simple_playback(&m_base_pwm, &pwm_seq, 1, NRF_DRV_PWM_FLAG_LOOP);
    

}

Parents
  • Hello,

    Have you tried debugging to see that the update functions are actually called?

    BR,

    Edvin

  • Here it's getting update but not output side(Pin out)   

      nrf_drv_pwm_simple_playback(&m_base_pwm, &pwm_seq_2, 1, 0);
    
        NRF_LOG_INFO("PWM0 C value: %d", *pwm_seq_2.values.p_common); 
        NRF_LOG_INFO("PWM0 I value: %d", (*pwm_seq_2.values.p_individual).channel_0); 

  • Hello,

    Can you upload the project here? Just zip the pwm_driver folder from SDK\examples\peripheral (if that is the project you are using). If you have trouble uploading it, try to delete the build files before zipping the folder. This will reduce the size drastically. 

    Also, please specify what IDE you used (Keil? Segger Embedded Studio? other?) and what SDK version you use. 

    Best regards,

    Edvin

  • ## HW Requirements

    - nRF52832 Development Kit

    ## SW Requirements

    - nRF5 SDK v14.2.0

    - Segger Embedded Studio

    code:

    drive.google.com/open

  • I tested below PWM code without soft-device, it's working fine but not above code.
     

    #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_delay.h"
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    uint16_t pwm_val=0;
    static nrf_drv_pwm_t m_pwm0 = NRF_DRV_PWM_INSTANCE(0);
    static uint16_t const              m_demo1_top  = 10000;
    static uint16_t const              m_demo1_step = 200;
    static uint8_t                     m_demo1_phase;
    static nrf_pwm_values_individual_t m_demo1_seq_values;
    static nrf_pwm_sequence_t const    m_demo1_seq =
    {
        .values.p_individual = &m_demo1_seq_values,
        .length              = NRF_PWM_VALUES_LENGTH(m_demo1_seq_values),
        .repeats             = 0,
        .end_delay           = 2000
    };
    
    void update()
    {
         if(pwm_val<=10000)
         {
            m_demo1_seq_values.channel_0 = pwm_val;
            pwm_val+=5;
        
        
         }
         else if(pwm_val>10000)
         {
            m_demo1_seq_values.channel_0 = pwm_val;
            pwm_val=0;
        
        
         }
         (void)nrf_drv_pwm_simple_playback(&m_pwm0, &m_demo1_seq, 1,0);
        
    }
    
    static void demo1_handler(nrf_drv_pwm_evt_type_t event_type)
    {
        if (event_type == NRF_DRV_PWM_EVT_FINISHED)
        {
            //update();
              (void)nrf_drv_pwm_simple_playback(&m_pwm0, &m_demo1_seq, 1,0);
    
         
        }
    }
    static void demo1(void)
    {
        NRF_LOG_INFO("Demo 1");
    
        nrf_drv_pwm_config_t const config0 =
        {
            .output_pins =
            {
                ARDUINO_1_PIN, // 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_1MHz,
            .count_mode   = NRF_PWM_MODE_UP,
            .top_value    = m_demo1_top,
            .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,
            .step_mode    = NRF_PWM_STEP_AUTO
        };
        APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm0, &config0, demo1_handler));
        
        m_demo1_seq_values.channel_0 = 0;
        m_demo1_seq_values.channel_1 = 0;
        m_demo1_seq_values.channel_2 = 0;
        m_demo1_seq_values.channel_3 = 0;
        m_demo1_phase                = 0;
    
        (void)nrf_drv_pwm_simple_playback(&m_pwm0, &m_demo1_seq, 1,0);
    }
    
    
    int main(void)
    {
        
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_INFO("PWM example");
    
        demo1();
    
        for (;;)
        {
            // Wait for an event.
            __WFE();
    
            // Clear the event register.
            __SEV();
            __WFE();
              update(); 
              nrf_delay_us(1000);
    
         
          
        }
    }
    
    
    /** @} */
    

  • Hello, 

    When I run your application, it runs into the hardfault handler. Does the same happen for you?

    Does the application all of the sudden stop?

    Best regards,

    Edvin

Reply Children
No Data
Related