Skip to content

Commit d7caa64

Browse files
committed
improve ada callback, automatically resize queue depth when needed
1 parent c54d4ed commit d7caa64

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

cores/nRF5/utility/AdaCallback.c

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@
3636

3737
#include "Arduino.h"
3838

39+
#define INITIAL_QUEUE_DEPTH 64
40+
3941
static QueueHandle_t _cb_queue = NULL;
40-
static uint8_t _cb_qbuf[CFG_CALLBACK_QUEUE_LENGTH*sizeof(void*)];
41-
static StaticQueue_t _cb_static_q;
42+
static uint32_t _cb_qdepth;
4243

4344
void adafruit_callback_task(void* arg)
4445
{
@@ -79,14 +80,26 @@ static inline bool is_isr(void)
7980

8081
void ada_callback_queue(ada_callback_t* cb_item)
8182
{
82-
if ( is_isr() )
83-
{
84-
xQueueSendFromISR(_cb_queue, (void*) &cb_item, NULL);
85-
}else
83+
BaseType_t ret = is_isr() ? xQueueSendFromISR(_cb_queue, (void*) &cb_item, NULL) : xQueueSend(_cb_queue, (void*) &cb_item, CFG_CALLBACK_TIMEOUT);
84+
85+
if ( ret != pdTRUE )
8686
{
87-
if ( !xQueueSend(_cb_queue, (void*) &cb_item, CFG_CALLBACK_TIMEOUT) )
87+
// run out of space, resize queue with double the size
88+
if ( ada_callback_queue_resize(2*_cb_qdepth) )
89+
{
90+
_cb_qdepth = 2*_cb_qdepth;
91+
92+
// try again
93+
if ( is_isr() )
94+
{
95+
xQueueSendFromISR(_cb_queue, (void*) &cb_item, NULL);
96+
}else
97+
{
98+
xQueueSend(_cb_queue, (void*) &cb_item, CFG_CALLBACK_TIMEOUT);
99+
}
100+
}else
88101
{
89-
LOG_LV1("MEMORY", "AdaCallback run out of queue item, increase CFG_CALLBACK_QUEUE_LENGTH");
102+
LOG_LV1("MEMORY", "AdaCallback run out of queue spaces");
90103
}
91104
}
92105
}
@@ -124,8 +137,37 @@ void ada_callback_invoke(const void* malloc_data, uint32_t malloc_len, const voi
124137
void ada_callback_init(void)
125138
{
126139
// queue to hold "Pointer to callback data"
127-
_cb_queue = xQueueCreateStatic(CFG_CALLBACK_QUEUE_LENGTH, sizeof(void*), _cb_qbuf, &_cb_static_q);
140+
_cb_qdepth = INITIAL_QUEUE_DEPTH;
141+
_cb_queue = xQueueCreate(_cb_qdepth, sizeof(void*));
128142

129143
TaskHandle_t callback_task_hdl;
130144
xTaskCreate( adafruit_callback_task, "Callback", CFG_CALLBACK_TASK_STACKSIZE, NULL, TASK_PRIO_NORMAL, &callback_task_hdl);
131145
}
146+
147+
bool ada_callback_queue_resize(uint32_t new_depth)
148+
{
149+
// create new queue
150+
QueueHandle_t new_queue = xQueueCreate(new_depth, sizeof(void*));
151+
VERIFY(new_queue);
152+
153+
LOG_LV1("MEMORY", "AdaCallback increase queue depth to %d", new_depth);
154+
155+
taskENTER_CRITICAL();
156+
157+
// move item from old queue
158+
ada_callback_t* cb_data;
159+
while ( xQueueReceive(_cb_queue, (void*) &cb_data, 0) )
160+
{
161+
xQueueSend(new_queue, (void*) &cb_data, CFG_CALLBACK_TIMEOUT);
162+
}
163+
164+
// delete old queue
165+
vQueueDelete(_cb_queue);
166+
167+
// Switch to new queue
168+
_cb_queue = new_queue;
169+
170+
taskEXIT_CRITICAL();
171+
172+
return true;
173+
}

cores/nRF5/utility/AdaCallback.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,10 @@
4242
#define CFG_CALLBACK_TASK_STACKSIZE (512*2)
4343
#endif
4444

45-
#ifndef CFG_CALLBACK_QUEUE_LENGTH
46-
#define CFG_CALLBACK_QUEUE_LENGTH 60
47-
#endif
48-
4945
#ifndef CFG_CALLBACK_TIMEOUT
5046
#define CFG_CALLBACK_TIMEOUT 100
5147
#endif
5248

53-
5449
#ifdef __cplusplus
5550
extern "C"{
5651
#endif
@@ -115,6 +110,7 @@ typedef void (*adacb_5arg_t) (uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
115110
void ada_callback_init(void);
116111
void ada_callback_invoke(const void* mdata, uint32_t mlen, const void* func, uint32_t arguments[], uint8_t argcount);
117112
void ada_callback_queue(ada_callback_t* cb_item);
113+
bool ada_callback_queue_resize(uint32_t new_depth);
118114

119115
#ifdef __cplusplus
120116
}

0 commit comments

Comments
 (0)