sandeepmistry / arduino-BLEPeripheral

An Arduino library for creating custom BLE peripherals with Nordic Semiconductor's nRF8001 or nR51822.
MIT License
462 stars 179 forks source link

sd_app_evt_wait only waits until central is connected #127

Closed acaballero closed 7 years ago

acaballero commented 7 years ago

Hello,

I'm trying to put a BLENano board to slepp every loop iteration. These are te steps:

  1. setup:
     [...]
     sd_power_mode_set(NRF_POWER_MODE_LOWPWR);    
     NRF_POWER->TASKS_LOWPWR = 0;
     NRF_POWER->DCDCEN = 0x00000000;
     [...]

2: loop:

    [...]

    sd_app_evt_wait();

    BLECentral central = ble.central(); 

    // if a central is connected to peripheral:
    if (central) {        

         sd_app_evt_wait();

        // while the central is still connected to peripheral:
        while (central.connected()) {
               sd_app_evt_wait()
               [...]
     }

    [...]

The first time sd_app_evt_wait is called, it waits until a central is connected. But then, the execution enters the inner loop and sd_app_evt_wait returns instantly.

I've read things about pending events or interrupts, bu I've not managed to get to work. I've also tried calling sd_nvic_EnableIRQ(SWI2_IRQn); with a proper empty SWI2_IRQHandler, but then sd_app_evt_wait never returns and no central can connect to the device.

Please, ¿is there something with the library I have to setup in order for this to work?

Thanks!

sandeepmistry commented 7 years ago

Hey @acaballero,

It's better to use the poll style API with event handlers as many library calls check for SD events internally.

Examples can be found here: https://gist.github.com/sandeepmistry/6a6dfe9021b19bfddd60

jeanmatthieud commented 5 years ago

I have the same issue. I read that if the IRQ is not enabled, the flag is not cleared and so the sd_app_evt_wait returns instantly.

I tried to enable SWI2_IRQn, and I added an empty 'SWI2_IRQHandler()' (as provided in the gist example). The device is still visible, but when I try to connect to it, it crashes (no more visible, connection fails...). I don't know (yet) how to dump the crash informations.

A temporary solution for me is to NOT enable the SWI2 interrupt, and clear this flag with 'sd_nvic_ClearPendingIRQ()' (which should be done automatically when the interrupt is enabled according to the documentation).

It looks like the SWI2_IRQHandler() is not accessible / executed. Any idea ?