Hello,
I am currently working on a project where I need to be able to control an RGB by writing the PWM duty cycle to a custom BLE Service and Characteristic. I am using SDK 8.1 and s110 SoftDevice 8.0. I am also using the app_pwm library from the SDK. I init the PWM module in the following way:
APP_PWM_INSTANCE(PWM1,1);
APP_PWM_INSTANCE(PWM2,2);
bool pwm_1_ready_flag;
bool pwm_2_ready_flag;
void pwm_1_ready_callback(uint32_t pwm_id)
{
pwm_1_ready_flag = true;
}
void pwm_2_ready_callback(uint32_t pwm_id)
{
pwm_2_ready_flag = true;
}
void pwm_init(void)
{
ret_code_t err_code;
/* 2-channel PWM, 100Hz, output on LED pins. */
app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(10000L, 21, 22);
app_pwm_config_t pwm2_cfg = APP_PWM_DEFAULT_CONFIG_1CH(10000L, 23);
// Set the polarity of the channel.
pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
pwm2_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
// Initialize and enable PWM.
err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_1_ready_callback);
APP_ERROR_CHECK(err_code);
err_code = app_pwm_init(&PWM2,&pwm2_cfg,pwm_2_ready_callback);
APP_ERROR_CHECK(err_code);
app_pwm_enable(&PWM1);
app_pwm_enable(&PWM2);
}
I can successfully get the desired values to the write handler for the characteristic and I set the values in the following way:
void set_rgb(uint8_t red_val, uint8_t green_val, uint8_t blue_val)
{
while (app_pwm_channel_duty_set(&PWM1, 0, red_val) == NRF_ERROR_BUSY);
while (app_pwm_channel_duty_set(&PWM1, 1, green_val) == NRF_ERROR_BUSY);
while (app_pwm_channel_duty_set(&PWM2, 0, blue_val) == NRF_ERROR_BUSY);
}
My PWM module always hangs and gives me a NRF_ERROR_BUSY code when I attempt to set the PWM value of the second channel of PWM1. The first channel of PWM1 can be written and the only channel of PWM2 can be written but when I attempt to write all three simultaneously as I do in the above code, the code gets trapped in the while loop trying to write the green_val to the PWM1 channel 0 and I can only recover by using the reset button on my nRF51-DK.
If I dont use the while loop to wait for the PWM module to be ready, I will sometimes be unsuccessful when writing to the RGB characteristic over BLE but I can at least recover and attempt to rewrite the RGB value instead of being trappen in a while loop while my PWM module gives me an NRF_ERROR_BUSY return code.
Any help you can offer is greatly appreciated.
Edit: It appears as though the problem is being caused when I try to write to both channels of the PWM1 instance sequentially. If I write to channel 0 of PWM1 and channel 0 of PWM2 there are no issues. If i write to channel 1 of PWM1 and channel 0 of PWM2, still there are no problems. If I write to channel 0 of PWM1 and then channel 1 of PWM1 I get the above mentioned error of endlessly waiting for the PWM module to be ready.
Are there any reasons why writing to both channels of a single instance would have this effect?
Thanks and Best Regards,
Cory Mast