This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

PWM failed to work with SoftDevice 8.0 in nRF51822

I may be missing something. I am trying to generate a square wave from a GPIO pin in a BLE application (SoftDevice 8.0). I tried libraries (nrf_pwm.c and nrf_pwm.h) in this post and also in this repo. I created two functions as follows:

#define V_CLK 5
static void pwm_init(void) 
{
	  nrf_pwm_config_t pwm_config = PWM_DEFAULT_CONFIG;
    
    pwm_config.mode             = PWM_MODE_LED_255;
    pwm_config.num_channels     = 1;
    pwm_config.gpio_num[0]      = V_CLK;
    
    // Initialize the PWM library
    nrf_pwm_init(&pwm_config);
}

static void v_clock_start(void)
{
	nrf_pwm_set_value(0, 127);
}

I have tested they worked without SoftDevice, as I could see a square wave between 0V and VCC in GPIO pin 5.

After that I went ahead with SoftDevice. I used "ble_app_beacon" from SDK 8.0.0 as the starting point, and removed BSP code since I was using a standalone nRF51822 module. I set USE_WITH_SOFTDEVICE to 1 in nrf_pwm.h. Also, I called pwm_init() before ble_stack_init() in main() function:

int main(void)
{

    pwm_init();
    ble_stack_init();
    advertising_init();

    advertising_start();
    v_clock_start(); 

    for (;; )
    {
        power_manage();
    }
}

While BLE started successfully as I could see the iBeacon in my Master Control Panel Android app, I saw a flat 0V output from GPIO pin 5 in my oscilloscope. I tried to do the same thing to other working BLE apps of mine and they showed the same behavior. What was I missing?

Parents Reply Children
  • Hi Øyvind, your advice works. I also found that the libraries in this post just don't work, no matter I call pwm_init() before or after ble_stack_init(). For this repo, it only works if calling pwm_init() after ble_stack_init(). Why is the calling sequence reversed here?

  • Hi

    The SoftDevice API's (sd_ppi_... for instance) will only work after the SD is enabled, which is why you have to initialize PWM after the SD when setting USE_SOFTDEVICE = 1 in the PWM library.

    When USE_SOFTDEVICE = 0 the PWM driver will access peripherals such as NRF_PPI directly, and if you try to do this when the SD is enabled you will get a hard fault. That is why the advice earlier was to initialize the PWM driver before the SD, so that the PPI configuration would work.

    Now that the USE_SOFTDEVICE define is added you should just use this when using PWM with the SD.

    Regards Torbjørn

Related