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

PWM not run with Mesh

Hi all,

I am adding PWM to light switch server . I added success. PWM function run OK. I use PWM1,PWM2 with Timer 1 and Timer 3  and PWM library example .If I call function PWM in int main , it run OK . But if I call function PWM in set_cb (function for receiver data from client light switch model) it dont run , call back function of PWM not called . This is my PWM.c  

#include "pwm.h"
#include "nrf.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "app_pwm.h"

APP_PWM_INSTANCE(PWM1,1);
APP_PWM_INSTANCE(PWM2,3);

static volatile bool ready_flag1 = true;     // A flag indicating PWM status.
static volatile bool ready_flag2 = true; 

void pwm1_ready_callback(uint32_t pwm_id)    // PWM callback function
{
    ready_flag1 = true;
}

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

void PWM_Init(void)
{
    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(2000L, R_PIN, G_PIN);
    app_pwm_config_t pwm2_cfg = APP_PWM_DEFAULT_CONFIG_2CH(2000L, B_PIN, D_PIN);

    pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_LOW;
    pwm2_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_LOW;

    ret_code_t err_code;

    err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm1_ready_callback);
    err_code = app_pwm_init(&PWM2,&pwm2_cfg,pwm2_ready_callback);
    APP_ERROR_CHECK(err_code);
    app_pwm_enable(&PWM1);
    app_pwm_enable(&PWM2);
}

void setRGB(uint8_t* rgb_value)       
{    
    ready_flag1 =  false;
    ready_flag2 = false;

    APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 0, *rgb_value));
    while(!ready_flag1);
    APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 1, *(rgb_value+1)));
    while(!ready_flag1);
    APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM2, 0, *(rgb_value+2)));
    while(!ready_flag2);
}

void setD(uint8_t d_value)
{
    ready_flag2 = false;
    APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM2, 1, d_value));
    while (!ready_flag2);
}

this set_cb function : 

static bool set_cb(const simple_message_server_t * p_server, nrf_mesh_address_t src, nrf_mesh_address_t dst,uint8_t *data, uint8_t length)    // event data receivered from provioner
{
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Set_cb event.\n");
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Got data :\n");
	
    for(int i=0;i<length;i++)
    {
      __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, " 0x%02x\n",data[i]);
    }
    
    if(data[0]==0)
    {
      __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Set to DIM Mode.\n");
      __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Set DIM Value = %d.\n",data[1]);
      setD(data[1]);			//	-> not run
    }
    else if(data[0]==0x01)
    {
      __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Set to RGB Mode.\n");
      __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Set RGB Value = R: %d--G: %d--B: %d.\n",data[1],data[2],data[3]);
      setRGB(&data[1]);			//	-> not run
    }
    else
    {
      __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Unknown Mode.\n");
      setD(0);
      setRGB(&RGB[0]);
    }
}

pls, show me advise . Thank !!! 

Parents Reply Children
Related