Skip to content

Commit b2e49f2

Browse files
committed
Adacallback support fromISR call
- change Prioprity for loop() to LOW - defer interrupt for both hw and sw encoder to ada callback
1 parent 9b71709 commit b2e49f2

File tree

8 files changed

+26
-12
lines changed

8 files changed

+26
-12
lines changed

cores/nRF5/freertos/config/FreeRTOSConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131

132132
/* Software timer definitions. */
133133
#define configUSE_TIMERS 1
134-
#define configTIMER_TASK_PRIORITY ( 2 )
134+
#define configTIMER_TASK_PRIORITY ( 2 ) // Normal
135135
#define configTIMER_QUEUE_LENGTH 32
136136
#define configTIMER_TASK_STACK_DEPTH ( 100 )
137137

cores/nRF5/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ int main( void )
9393

9494
// Create a task for loop()
9595
TaskHandle_t _loopHandle;
96-
xTaskCreate( loop_task, "loop", _loopStacksize, NULL, TASK_PRIO_NORMAL, &_loopHandle);
96+
xTaskCreate( loop_task, "loop", _loopStacksize, NULL, TASK_PRIO_LOW, &_loopHandle);
9797

9898
// Initialize callback task
9999
ada_callback_init();

cores/nRF5/rtos.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ bool SchedulerRTOS::startLoop(taskfunc_t task, uint32_t stack_size)
7979
bool SchedulerRTOS::startLoop(taskfunc_t task, const char* name, uint32_t stack_size)
8080
{
8181
TaskHandle_t handle;
82-
return pdPASS == xTaskCreate( _redirect_task, name, stack_size, (void*) task, TASK_PRIO_NORMAL, &handle);
82+
return pdPASS == xTaskCreate( _redirect_task, name, stack_size, (void*) task, TASK_PRIO_LOW, &handle);
8383
}
8484

8585
extern "C"

cores/nRF5/rtos.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
enum
5656
{
5757
TASK_PRIO_LOWEST = 0, // Idle task, should not be used
58-
TASK_PRIO_LOW = 1,
59-
TASK_PRIO_NORMAL = 2, // Loop, Timer Task
58+
TASK_PRIO_LOW = 1, // Loop
59+
TASK_PRIO_NORMAL = 2, // Timer Task, Callback Task
6060
TASK_PRIO_HIGH = 3, // Bluefruit Task
6161
TASK_PRIO_HIGHEST = 4,
6262
};

cores/nRF5/utility/AdaCallback.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ void adafruit_callback_task(void* arg)
7474

7575
void ada_callback_queue(ada_callback_t* cb_data)
7676
{
77-
xQueueSend(_cb_queue, (void*) &cb_data, CFG_CALLBACK_TIMEOUT);
77+
if ( cb_data->from_isr )
78+
{
79+
xQueueSendFromISR(_cb_queue, (void*) &cb_data, NULL);
80+
}else
81+
{
82+
xQueueSend(_cb_queue, (void*) &cb_data, CFG_CALLBACK_TIMEOUT);
83+
}
7884
}
7985

8086
void ada_callback_init(void)

cores/nRF5/utility/AdaCallback.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ typedef struct
5252
void* callback_func;
5353

5454
uint8_t arg_count;
55+
bool from_isr;
5556
// uint8_t callback_type;
5657
// uint8_t _reserved[2];
5758

@@ -86,12 +87,13 @@ typedef void (*adacb_5arg_t) (uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
8687
* Macro function is called by other module with all intended parameters.
8788
* The first parameter is malloced Pointer (NULL if not), so that callback could know to free memory
8889
*/
89-
#define ada_callback(_malloced, _func, ... ) \
90+
#define _cb_setup(_from_isr, _malloced, _func, ... ) \
9091
do { \
9192
uint8_t const _count = VA_ARGS_NUM(__VA_ARGS__);\
9293
ada_callback_t* cb_data = (ada_callback_t*) rtos_malloc( sizeof(ada_callback_t) + (_count ? (_count-1)*4 : 0) ); \
9394
cb_data->malloced_data = _malloced;\
9495
cb_data->callback_func = (void*)_func;\
96+
cb_data->from_isr = _from_isr;\
9597
cb_data->arg_count = _count;\
9698
if ( _count ) {\
9799
uint32_t arguments[] = { _ADA_CB_ARGS(__VA_ARGS__) };\
@@ -100,6 +102,9 @@ typedef void (*adacb_5arg_t) (uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
100102
ada_callback_queue(cb_data);\
101103
} while(0)
102104

105+
#define ada_callback(... ) _cb_setup(false, __VA_ARGS__)
106+
#define ada_callback_fromISR(... ) _cb_setup(true , __VA_ARGS__)
107+
103108
void ada_callback_init(void);
104109
void ada_callback_queue(ada_callback_t* cb_data);
105110

libraries/RotaryEncoder/RotaryEncoder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ void HwRotaryEncoder::_irq_handler(void)
198198
if ( _cb )
199199
{
200200
int32_t step = read();
201-
if ( step ) ada_callback(NULL, _cb, step);
201+
if ( step ) ada_callback_fromISR(NULL, _cb, step);
202202
}
203203
}
204204
}

libraries/RotaryEncoder/SwRotaryEncoder.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,20 @@ void SwRotaryEncoder::_irq_handler(void)
124124

125125
if ( val != _a_last )
126126
{
127+
int32_t step = 0;
128+
127129
if ( val != digitalRead(_pinb) )
128130
{
129-
_abs++;
130-
if (_cb) _cb(1);
131+
step = 1;
131132
}else
132133
{
133-
_abs--;
134-
if (_cb) _cb(-1);
134+
step = -1;
135135
}
136136

137+
_abs += step;
137138
_a_last = val;
139+
140+
if (_cb) ada_callback_fromISR(NULL, _cb, step);
138141
}
139142
}
140143

0 commit comments

Comments
 (0)