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

There is a problem with the PWM.

Hello

I use sdk v17.0 and nrf52832.
I'd like to use 1-channel PWM and 2-channel PWM.  However, an error is output from the pwm_enable when using these two together.
This is my code and an error printed in debug.
Debug Terminal
<error> app: ERROR 3735928559 [Unknown error code] at Y:\YSC\Project\CODE\Nordic\SDK17.0\nRF5_SDK_17.0.0_9d13099\modules\nrfx\drivers\src\nrfx_timer.c:143
PC at: 0x0003418B
<error> app: End of error report
nrfx_timer.c (143 Line)
void nrfx_timer_enable(nrfx_timer_t const * const p_instance)
{
    NRFX_ASSERT(m_cb[p_instance->instance_id].state == NRFX_DRV_STATE_INITIALIZED); //143 line, error
    nrf_timer_task_trigger(p_instance->p_reg, NRF_TIMER_TASK_START);
    m_cb[p_instance->instance_id].state = NRFX_DRV_STATE_POWERED_ON;
    NRFX_LOG_INFO("Enabled instance: %d.", p_instance->instance_id);
}
PWM code in main.c
APP_PWM_INSTANCE(PWM1,1);                   // Create the instance "PWM1" using TIMER1  (name, NRF_DRV_TIMER_INSTANCE(num)).
APP_PWM_INSTANCE(PWM2,3);
static volatile bool ready_flag;            // A flag indicating PWM status.
static volatile bool ready_flag2;


void pwm_play_buzzer_duration(uint32_t duration) //buzzer play time
{
    uint8_t i;
    uint32_t duty = 50;

    NRF_LOG_INFO("pwm_play_buzzer_duration =>");    
    ready_flag = false;

    //Set the duty cycle - keep trying until PWM is ready... 
    while (app_pwm_channel_duty_set(&PWM1, 0, duty) == NRF_ERROR_BUSY);

    nrf_delay_ms(duration);
    app_pwm_disable(&PWM1);
}


void pwm_ready_callback(uint32_t pwm_id)    // PWM callback function
{
    ready_flag = true;
}

void pwm_ready_callback2(uint32_t pwm_id)    // PWM callback function
{
    ready_flag2 = true;
}


/*
 init, Once executed
 */
//buzzer PWM initializing
void pwm_init(void) 
{
    ret_code_t err_code;

    // 1-channel PWM, output on buzzer pin. 
    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(1500, BUZZER_PIN);
    // 2-channel PWM, output 50kHz
    app_pwm_config_t pwm2_cfg = APP_PWM_DEFAULT_CONFIG_2CH(20, PWM_1, PWM_2);
    
    /* Switch the polarity of the second channel. */
    pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
    pwm2_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
    
    /* Initialize and enable PWM. */
    err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
    APP_ERROR_CHECK(err_code);

    err_code = app_pwm_init(&PWM2,&pwm2_cfg,pwm_ready_callback2);
    APP_ERROR_CHECK(err_code);

    app_pwm_enable(&PWM1);
}


/*
 pwm_change_frequency,Can be executed multiple times
*/
void pwm_change_frequency(uint16_t period_us) //change buzzer sound, ex) pwm_change_frequency(500);
{
    ret_code_t err_code;

    app_pwm_disable(&PWM1); //disable PWM for change PWM
    app_pwm_uninit(&PWM1); //uninitializing a PWM

    // 1-channel PWM output on buzzer pin. 
    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(period_us, BUZZER_PIN);
    
    /* Switch the polarity of the second channel. */
    pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
    
    /* Initialize and enable PWM. */
    err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
    APP_ERROR_CHECK(err_code);
    app_pwm_enable(&PWM1);
}


static void do_play_buzzer(void)
{
  app_pwm_enable(&PWM1);
  pwm_play_buzzer_duration(200); //buzzer play time
}


int main(void)
{
    uint32_t err_code;
    bool erase_bonds = false;

    // Initialize.
    gpio_init(); 
    uart_init();
    log_init();
    timers_init();
    app_buttons_init(); 
    power_management_init();
        
    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();
    peer_manager_init();

    err_code = app_button_enable();
    //err_code = app_button_disable();
    APP_ERROR_CHECK(err_code);

    adc_configure(); //baterry value & LED
    pwm_init();
    bsp_board_init(BSP_INIT_LEDS); //use bsp

    // Start execution.
    printf("\r\nCleaner Device started.\r\n");
    NRF_LOG_INFO("Debug logging for UART over RTT started.");
    
    advertising_start(erase_bonds); //false

    pwm_change_frequency(1500); 
    do_play_buzzer(); //When start play buzzer, error!
    //nrf_gpio_pin_set(POWER_SW); //HIGH : power on

    //PWM2 setting
    ready_flag2 = false;
    /* Set the duty cycle - keep trying until PWM is ready... */
    while (app_pwm_channel_duty_set(&PWM2, 0, 50) == NRF_ERROR_BUSY); //channel 1
    /* ... or wait for callback. */
    while (!ready_flag);
    APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM2, 1, 50)); //channel 2


    // Enter main loop.
    for (;;)
    {
        pm_sec_params_set(NULL); //No Security parameters   
         
        if(PWM2_state == true)
        {
          app_pwm_enable(&PWM2); //PWM2 On (50kHz)
        }

        else
        {
          app_pwm_disable(&PWM2); //PWM2 Off
        }
    }
}
sdk_config.h (timer instance)
#ifndef NRFX_TIMER_ENABLED
#define NRFX_TIMER_ENABLED 1
#endif
// <q> NRFX_TIMER0_ENABLED  - Enable TIMER0 instance
 

#ifndef NRFX_TIMER0_ENABLED
#define NRFX_TIMER0_ENABLED 1
#endif

// <q> NRFX_TIMER1_ENABLED  - Enable TIMER1 instance
 

#ifndef NRFX_TIMER1_ENABLED
#define NRFX_TIMER1_ENABLED 1
#endif

// <q> NRFX_TIMER2_ENABLED  - Enable TIMER2 instance
 

#ifndef NRFX_TIMER2_ENABLED
#define NRFX_TIMER2_ENABLED 1
#endif

// <q> NRFX_TIMER3_ENABLED  - Enable TIMER3 instance
 

#ifndef NRFX_TIMER3_ENABLED
#define NRFX_TIMER3_ENABLED 1
#endif

// <q> NRFX_TIMER4_ENABLED  - Enable TIMER4 instance
 

#ifndef NRFX_TIMER4_ENABLED
#define NRFX_TIMER4_ENABLED 1
#endif


#ifndef TIMER0_ENABLED
#define TIMER0_ENABLED 1
#endif

// <q> TIMER1_ENABLED  - Enable TIMER1 instance
 

#ifndef TIMER1_ENABLED
#define TIMER1_ENABLED 1
#endif

// <q> TIMER2_ENABLED  - Enable TIMER2 instance
 

#ifndef TIMER2_ENABLED
#define TIMER2_ENABLED 1
#endif

// <q> TIMER3_ENABLED  - Enable TIMER3 instance
 

#ifndef TIMER3_ENABLED
#define TIMER3_ENABLED 1
#endif

// <q> TIMER4_ENABLED  - Enable TIMER4 instance
 

#ifndef TIMER4_ENABLED
#define TIMER4_ENABLED 1
#endif
It's my first time using multiple PWMs, so I'm not sure. Can you tell me the cause of this problem?

Thank you.
Parents Reply Children
Related