Hi,

I'm still working on my firmware for Pinetime! Thanks to your help a couple of weeks ago, BLE connectivity is now much more reliable. As a remainder, I work on a firmware based on FreeRTOS and running on the NRF52 MCU. It uses NimBLE as BLE stack. I use the FreeRTOS port available in the source tree (https://github.com/apache/mynewt-nimble/tree/master/porting/npl).

However, I noticed that my code would crash after a certain number of connections/disconnections. I noticed that use of the heap memory allocated to FreeRTOS would grow by ~100 bytes each time a new connection is established and this memory is never released.

I discovered that this memory is used by 2 timers (mynewt "callouts") that are created in npl_freertos_callout_init(). This function calls xTimerCreate() which allocates the memory necessary for the timer. This memory is taken from the heap allocated for FreeRTOS. When the connection is closed, xTimerDelete() is never called meaning that the memory is not freed. When a new device connects, 2 new timers are created, allocating ~100B from the heap. This continues until the heap is full and refuses to give any more memory.
Note that I use heap_4.c, which allows memory to be allocated and freed.

To my surprise, there is NO function npl_freertos_callout_uninit() or npl_freertos_callout_delete() or anything else. It looks like timers (callouts) are not intended to be removed once they are created. Should they be reused/recycled?

Anyway, I modified the function npl_freertos_callout_init() so that it does nothing if the handle is not NULL and... it looks like it works (even if I don't think this is a valid fix):

void npl_freertos_callout_init(struct ble_npl_callout *co, struct ble_npl_eventq *evq,
                     ble_npl_event_fn *ev_cb, void *ev_arg)
{
    if(co->handle != NULL) return;  // <---- I added this line.

    memset(co, 0, sizeof(*co));
co->handle = xTimerCreate("co", 1, pdFALSE, co, os_callout_timer_cb);
    co->evq = evq;
    ble_npl_event_init(&co->ev, ev_cb, ev_arg);

}

Is this a bug that must be fixed in the source code or am I missing something about the creation and deletion of timers?

Thanks for your help,

JF

Reply via email to