In the implementation of PWM control function, two instances cannot be created

static APP_PWM_INSTANCE(PWM0,1);
static APP_PWM_INSTANCE(PWM1,2);
static app_pwm_config_t m_pwm0_config = APP_PWM_DEFAULT_CONFIG_2CH(200, BSP_LED_0, BSP_LED_1);
static app_pwm_config_t m_pwm0_config1 = APP_PWM_DEFAULT_CONFIG_1CH(200, BSP_LED_2);


static pwm_utils_contex_t m_pwm = {
                                     .p_pwm = &PWM0,
                                     .p_pwm_config = &m_pwm0_config,
                                     .channel = 0
                                   };
static pwm_utils_contex_t m_pwm2 = {
                                     .p_pwm = &PWM0,
                                     .p_pwm_config = &m_pwm0_config,
                                     .channel = 1
                                   };
static pwm_utils_contex_t m_pwm3 = {
                                     .p_pwm = &PWM1,
                                     .p_pwm_config = &m_pwm0_config1,
                                     .channel = 2
                                   };
static void PWM_controll(int duty_ratio)
{
    app_pwm_channel_duty_set(&PWM0, 0, duty_ratio);
    
    app_pwm_channel_duty_set(&PWM0, 2, duty_ratio);
}
int main(void)
{
    initialize();
    start();

    // LED POWER ON
    nrf_gpio_cfg_output(BSP_LED_3);
    nrf_gpio_pin_write(BSP_LED_3,1);

    nrf_gpio_cfg_output(BSP_LED_0);
    nrf_gpio_cfg_output(BSP_LED_1);
    nrf_gpio_cfg_output(BSP_LED_2);

    // Change duty ratio from 0 to 100
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "started \n");
    int i=0;
    while (i<=10)
    {
        nrf_delay_ms(1000);
        __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "duty ratio %u \n", i*10);
        PWM_controll(i*10);
        if(i==10)
        {
            i=0;
        }
        else
        {
            i++;
        }
    }

    // for(i=0;i<=9;i++){
    //     nrf_delay_ms(1000);
    //     update_pwm(i*10000,i*10000,0);
    // }
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "finished \n");

    for (;;)
    {
        (void)sd_app_evt_wait();
    }
}

Thank you very much for your help.
I am currently working on implementing a PWM control function using the nrf52840. I would like to ask you about a build error that occurred in that case.
In order to implement three different implementations of the PWM control function with two color channels using three color LEDs, I created two instances of the function as shown in the first program, and then configured the three channels.
Then, as in the second program, we set the duty ratio for each of the two color channels. This is then written and executed in the main function as shown in the third program.
However, when I build the program under these conditions, I get the error shown below. Incidentally, this error occurred as soon as the second instance was added, so the error did not occur when only Timer1 was used.
Can you please tell me what this means? Thank you very much. Thank you in advance.

  • build/light_lightness_server_nrf52840_xxAA_s140_7.0.1_Debug/obj/nrfx_timer.o: in function `TIMER2_IRQHandler':
  • multiple definition of `TIMER2_IRQHandler'; build/light_lightness_server_nrf52840_xxAA_s140_7.0.1_Debug/obj/bearer_handler.o: user\nrf5_SDK_for_Mesh_v4.2.0_src\mesh\bearer\src/bearer_handler.c:259: first defined here

LemonCake

  • Hi

    From the "multiple definition" error message you're seeing it seems like you're trying to use the TIMER2 instance multiple times in your application. I think the Mesh stack uses this timer instance already, so trying to add it in your application is likely what's causing this issue. Since you're working on the nRF52840, there should be more than enough TIMER instances (5) to go around, so try using another one for your application. 

    Best regards,

    Simon

  • app_pwm_channel_duty_set(&PWM0, 0, duty_ratio);
    app_pwm_channel_duty_set(&PWM1, 0, duty_ratio); 

    Thank you very much for your answer. From your answer, I changed TIMER2 to TIMER3 and changed "NRFX_TIMER3_ENABLED" and "TIMER3_ENABLED" in app_config.h. and created the second instance. Then the error disappeared and the build succeeded. When I set the duty ratio for the two LED channels as in the first program, the duty ratio changed only for one LED and not for the other.
    By the way, the LED whose duty ratio did not change is enabled by using "pwm_utils_enable()" as initialization process. If this function is set for multiple LEDs, both LEDs will not light up, so it is not possible to set the same function for multiple LEDs.
    If you have any suggestions on how to fix this, please let me know.
    Thank you very much.

    LemonCake

  • Hi

    Glad to hear we found the cause of this first issue at least.

    Are you working on a development kit or a custom board here? I don't see why the duty cycle set for your LEDs should only affect one of them. How do you enable the working LEDs exactly versus how you're enabling the problematic one?

    Best regards,

    Simon

Related