From 54c87e8f29153b3f5c0719c575ff26cb69040c1c Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 13 Nov 2019 16:29:58 +0700 Subject: [PATCH] refactor Software Timer, auto detect if in ISR mode --- cores/nRF5/utility/AdaCallback.c | 10 +-- cores/nRF5/utility/SoftwareTimer.cpp | 111 +++++++++++++++++++++++++++ cores/nRF5/utility/SoftwareTimer.h | 52 ++----------- cores/nRF5/wiring.h | 6 ++ 4 files changed, 127 insertions(+), 52 deletions(-) create mode 100644 cores/nRF5/utility/SoftwareTimer.cpp diff --git a/cores/nRF5/utility/AdaCallback.c b/cores/nRF5/utility/AdaCallback.c index 64e51b70..3fb9aa0d 100644 --- a/cores/nRF5/utility/AdaCallback.c +++ b/cores/nRF5/utility/AdaCallback.c @@ -73,15 +73,9 @@ void adafruit_callback_task(void* arg) } } -//! Test if in interrupt mode -static inline bool is_isr(void) -{ - return (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0 ; -} - void ada_callback_queue(ada_callback_t* cb_item) { - BaseType_t ret = is_isr() ? xQueueSendFromISR(_cb_queue, (void*) &cb_item, NULL) : xQueueSend(_cb_queue, (void*) &cb_item, CFG_CALLBACK_TIMEOUT); + BaseType_t ret = isInISR() ? xQueueSendFromISR(_cb_queue, (void*) &cb_item, NULL) : xQueueSend(_cb_queue, (void*) &cb_item, CFG_CALLBACK_TIMEOUT); if ( ret != pdTRUE ) { @@ -91,7 +85,7 @@ void ada_callback_queue(ada_callback_t* cb_item) _cb_qdepth = 2*_cb_qdepth; // try again - if ( is_isr() ) + if ( isInISR() ) { xQueueSendFromISR(_cb_queue, (void*) &cb_item, NULL); }else diff --git a/cores/nRF5/utility/SoftwareTimer.cpp b/cores/nRF5/utility/SoftwareTimer.cpp new file mode 100644 index 00000000..d86a0490 --- /dev/null +++ b/cores/nRF5/utility/SoftwareTimer.cpp @@ -0,0 +1,111 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 Ha Thach (tinyusb.org) for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "SoftwareTimer.h" + +SoftwareTimer::SoftwareTimer() +{ + _handle = NULL; +} + +SoftwareTimer::~SoftwareTimer () +{ + if ( _handle != NULL ) xTimerDelete(_handle, 0); +} + +void SoftwareTimer::begin(uint32_t ms, TimerCallbackFunction_t callback, bool repeating) +{ + _handle = xTimerCreate(NULL, ms2tick(ms), repeating, NULL, callback); +} + +bool SoftwareTimer::start(void) +{ + bool ret = true; + if ( isInISR() ) + { + BaseType_t ret, xHigherPriorityTaskWoken = pdFALSE; + ret = (pdPASS == xTimerStartFromISR(_handle, &xHigherPriorityTaskWoken)); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + }else + { + xTimerStart(_handle, 0); + } + return ret; +} + +bool SoftwareTimer::stop (void) +{ + bool ret = true; + if ( isInISR() ) + { + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + ret = (pdPASS == xTimerStopFromISR(_handle, &xHigherPriorityTaskWoken)); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + } + else + { + xTimerStop (_handle, 0); + } + + return ret; +} + +bool SoftwareTimer::reset (void) +{ + bool ret = true; + if ( isInISR() ) + { + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + ret = (pdPASS == xTimerResetFromISR(_handle, &xHigherPriorityTaskWoken)); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + } + else + { + xTimerReset(_handle, 0); + } + + return ret; +} + +bool SoftwareTimer::setPeriod(uint32_t ms) +{ + BaseType_t active = xTimerIsTimerActive(_handle); + bool ret = true; + + if ( isInISR() ) + { + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + ret = (pdPASS == xTimerChangePeriodFromISR(_handle, ms2tick(ms), &xHigherPriorityTaskWoken)); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + } + else + { + xTimerChangePeriod(_handle, ms2tick(ms), 0); + } + + // Change period of inactive timer will also start it !! + if ( !active ) stop(); + + return ret; +} diff --git a/cores/nRF5/utility/SoftwareTimer.h b/cores/nRF5/utility/SoftwareTimer.h index e39b7301..7649187c 100644 --- a/cores/nRF5/utility/SoftwareTimer.h +++ b/cores/nRF5/utility/SoftwareTimer.h @@ -44,52 +44,16 @@ class SoftwareTimer TimerHandle_t _handle; public: - SoftwareTimer() { _handle = NULL; } - virtual ~SoftwareTimer() { if(_handle != NULL) xTimerDelete(_handle, 0); } + SoftwareTimer(); + virtual ~SoftwareTimer(); - void begin(uint32_t ms, TimerCallbackFunction_t callback, bool repeating = true) - { - _handle = xTimerCreate(NULL, ms2tick(ms), repeating, NULL, callback); - } + void begin(uint32_t ms, TimerCallbackFunction_t callback, bool repeating = true); + TimerHandle_t getHandle(void) { return _handle; } - TimerHandle_t getHandle(void) - { - return _handle; - } - - void start(void) { xTimerStart(_handle, 0); } - void stop (void) { xTimerStop (_handle, 0); } - void reset(void) { xTimerReset(_handle, 0); } - - bool startFromISR(void) { - BaseType_t ret, xHigherPriorityTaskWoken = pdFALSE; - ret = xTimerStartFromISR(_handle, &xHigherPriorityTaskWoken); - portYIELD_FROM_ISR(xHigherPriorityTaskWoken); - return (ret == pdPASS); - } - - bool stopFromISR(void) { - BaseType_t ret, xHigherPriorityTaskWoken = pdFALSE; - ret = xTimerStopFromISR(_handle, &xHigherPriorityTaskWoken); - portYIELD_FROM_ISR(xHigherPriorityTaskWoken); - return (ret == pdPASS); - } - - bool resetFromISR(void) { - BaseType_t ret, xHigherPriorityTaskWoken = pdFALSE; - ret = xTimerResetFromISR(_handle, &xHigherPriorityTaskWoken); - portYIELD_FROM_ISR(xHigherPriorityTaskWoken); - return (ret == pdPASS); - } - - void setPeriod(uint32_t ms) - { - BaseType_t active = xTimerIsTimerActive(_handle); - xTimerChangePeriod(_handle, ms2tick(ms), 0); - - // Change period of inactive timer will also start it !! - if ( !active ) xTimerStop(_handle, 0); - } + bool start(void); + bool stop (void); + bool reset (void); + bool setPeriod(uint32_t ms); }; #endif /* SOFTWARETIMER_H_ */ diff --git a/cores/nRF5/wiring.h b/cores/nRF5/wiring.h index 40bbd734..a557f547 100644 --- a/cores/nRF5/wiring.h +++ b/cores/nRF5/wiring.h @@ -36,6 +36,12 @@ void enterUf2Dfu(void); void waitForEvent(void); void systemOff(uint32_t pin, uint8_t wake_logic); +// Test if in interrupt mode +static inline bool isInISR(void) +{ + return (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0 ; +} + #ifdef __cplusplus } #endif