From b2e49f2fc98432bd61a96ba2caea64779608652a Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 15 Sep 2017 17:56:24 +0700 Subject: [PATCH] Adacallback support fromISR call - change Prioprity for loop() to LOW - defer interrupt for both hw and sw encoder to ada callback --- cores/nRF5/freertos/config/FreeRTOSConfig.h | 2 +- cores/nRF5/main.cpp | 2 +- cores/nRF5/rtos.cpp | 2 +- cores/nRF5/rtos.h | 4 ++-- cores/nRF5/utility/AdaCallback.c | 8 +++++++- cores/nRF5/utility/AdaCallback.h | 7 ++++++- libraries/RotaryEncoder/RotaryEncoder.cpp | 2 +- libraries/RotaryEncoder/SwRotaryEncoder.cpp | 11 +++++++---- 8 files changed, 26 insertions(+), 12 deletions(-) diff --git a/cores/nRF5/freertos/config/FreeRTOSConfig.h b/cores/nRF5/freertos/config/FreeRTOSConfig.h index eb26cb83..70a1ac2b 100644 --- a/cores/nRF5/freertos/config/FreeRTOSConfig.h +++ b/cores/nRF5/freertos/config/FreeRTOSConfig.h @@ -131,7 +131,7 @@ /* Software timer definitions. */ #define configUSE_TIMERS 1 -#define configTIMER_TASK_PRIORITY ( 2 ) +#define configTIMER_TASK_PRIORITY ( 2 ) // Normal #define configTIMER_QUEUE_LENGTH 32 #define configTIMER_TASK_STACK_DEPTH ( 100 ) diff --git a/cores/nRF5/main.cpp b/cores/nRF5/main.cpp index b3e865ca..9a3d5316 100644 --- a/cores/nRF5/main.cpp +++ b/cores/nRF5/main.cpp @@ -93,7 +93,7 @@ int main( void ) // Create a task for loop() TaskHandle_t _loopHandle; - xTaskCreate( loop_task, "loop", _loopStacksize, NULL, TASK_PRIO_NORMAL, &_loopHandle); + xTaskCreate( loop_task, "loop", _loopStacksize, NULL, TASK_PRIO_LOW, &_loopHandle); // Initialize callback task ada_callback_init(); diff --git a/cores/nRF5/rtos.cpp b/cores/nRF5/rtos.cpp index aa5b2321..bcbd92c0 100644 --- a/cores/nRF5/rtos.cpp +++ b/cores/nRF5/rtos.cpp @@ -79,7 +79,7 @@ bool SchedulerRTOS::startLoop(taskfunc_t task, uint32_t stack_size) bool SchedulerRTOS::startLoop(taskfunc_t task, const char* name, uint32_t stack_size) { TaskHandle_t handle; - return pdPASS == xTaskCreate( _redirect_task, name, stack_size, (void*) task, TASK_PRIO_NORMAL, &handle); + return pdPASS == xTaskCreate( _redirect_task, name, stack_size, (void*) task, TASK_PRIO_LOW, &handle); } extern "C" diff --git a/cores/nRF5/rtos.h b/cores/nRF5/rtos.h index bfd746b0..03bd4479 100644 --- a/cores/nRF5/rtos.h +++ b/cores/nRF5/rtos.h @@ -55,8 +55,8 @@ enum { TASK_PRIO_LOWEST = 0, // Idle task, should not be used - TASK_PRIO_LOW = 1, - TASK_PRIO_NORMAL = 2, // Loop, Timer Task + TASK_PRIO_LOW = 1, // Loop + TASK_PRIO_NORMAL = 2, // Timer Task, Callback Task TASK_PRIO_HIGH = 3, // Bluefruit Task TASK_PRIO_HIGHEST = 4, }; diff --git a/cores/nRF5/utility/AdaCallback.c b/cores/nRF5/utility/AdaCallback.c index 7cff83e5..d31a1d56 100644 --- a/cores/nRF5/utility/AdaCallback.c +++ b/cores/nRF5/utility/AdaCallback.c @@ -74,7 +74,13 @@ void adafruit_callback_task(void* arg) void ada_callback_queue(ada_callback_t* cb_data) { - xQueueSend(_cb_queue, (void*) &cb_data, CFG_CALLBACK_TIMEOUT); + if ( cb_data->from_isr ) + { + xQueueSendFromISR(_cb_queue, (void*) &cb_data, NULL); + }else + { + xQueueSend(_cb_queue, (void*) &cb_data, CFG_CALLBACK_TIMEOUT); + } } void ada_callback_init(void) diff --git a/cores/nRF5/utility/AdaCallback.h b/cores/nRF5/utility/AdaCallback.h index 7e12fd2e..b753fcc6 100644 --- a/cores/nRF5/utility/AdaCallback.h +++ b/cores/nRF5/utility/AdaCallback.h @@ -52,6 +52,7 @@ typedef struct void* callback_func; uint8_t arg_count; + bool from_isr; // uint8_t callback_type; // uint8_t _reserved[2]; @@ -86,12 +87,13 @@ typedef void (*adacb_5arg_t) (uint32_t, uint32_t, uint32_t, uint32_t, uint32_t); * Macro function is called by other module with all intended parameters. * The first parameter is malloced Pointer (NULL if not), so that callback could know to free memory */ -#define ada_callback(_malloced, _func, ... ) \ +#define _cb_setup(_from_isr, _malloced, _func, ... ) \ do { \ uint8_t const _count = VA_ARGS_NUM(__VA_ARGS__);\ ada_callback_t* cb_data = (ada_callback_t*) rtos_malloc( sizeof(ada_callback_t) + (_count ? (_count-1)*4 : 0) ); \ cb_data->malloced_data = _malloced;\ cb_data->callback_func = (void*)_func;\ + cb_data->from_isr = _from_isr;\ cb_data->arg_count = _count;\ if ( _count ) {\ 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); ada_callback_queue(cb_data);\ } while(0) +#define ada_callback(... ) _cb_setup(false, __VA_ARGS__) +#define ada_callback_fromISR(... ) _cb_setup(true , __VA_ARGS__) + void ada_callback_init(void); void ada_callback_queue(ada_callback_t* cb_data); diff --git a/libraries/RotaryEncoder/RotaryEncoder.cpp b/libraries/RotaryEncoder/RotaryEncoder.cpp index 38f457e3..ad3bb193 100644 --- a/libraries/RotaryEncoder/RotaryEncoder.cpp +++ b/libraries/RotaryEncoder/RotaryEncoder.cpp @@ -198,7 +198,7 @@ void HwRotaryEncoder::_irq_handler(void) if ( _cb ) { int32_t step = read(); - if ( step ) ada_callback(NULL, _cb, step); + if ( step ) ada_callback_fromISR(NULL, _cb, step); } } } diff --git a/libraries/RotaryEncoder/SwRotaryEncoder.cpp b/libraries/RotaryEncoder/SwRotaryEncoder.cpp index b528d4d7..b10e9299 100644 --- a/libraries/RotaryEncoder/SwRotaryEncoder.cpp +++ b/libraries/RotaryEncoder/SwRotaryEncoder.cpp @@ -124,17 +124,20 @@ void SwRotaryEncoder::_irq_handler(void) if ( val != _a_last ) { + int32_t step = 0; + if ( val != digitalRead(_pinb) ) { - _abs++; - if (_cb) _cb(1); + step = 1; }else { - _abs--; - if (_cb) _cb(-1); + step = -1; } + _abs += step; _a_last = val; + + if (_cb) ada_callback_fromISR(NULL, _cb, step); } }