refactor Software Timer, auto detect if in ISR mode
This commit is contained in:
1 parent
ef11cdb391
commit
54c87e8f29
4 files changed
+127
-52
No files matched your search
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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_ */
|
||||
@@ -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
|
||||
Reference in new issue
Block a user