2020-10-10 09:57:57 +08:00
|
|
|
#include "concurrency/InterruptableDelay.h"
|
|
|
|
|
#include "configuration.h"
|
|
|
|
|
|
|
|
|
|
namespace concurrency
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
InterruptableDelay::InterruptableDelay()
|
|
|
|
|
{
|
|
|
|
|
semaphore = xSemaphoreCreateBinary();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InterruptableDelay::~InterruptableDelay()
|
|
|
|
|
{
|
|
|
|
|
vSemaphoreDelete(semaphore);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns false if we were interrupted
|
|
|
|
|
*/
|
|
|
|
|
bool InterruptableDelay::delay(uint32_t msec)
|
|
|
|
|
{
|
|
|
|
|
if (msec) {
|
2020-10-11 08:12:53 +08:00
|
|
|
// DEBUG_MSG("delay %u ", msec);
|
2020-10-10 09:57:57 +08:00
|
|
|
|
|
|
|
|
// sem take will return false if we timed out (i.e. were not interrupted)
|
|
|
|
|
bool r = xSemaphoreTake(semaphore, pdMS_TO_TICKS(msec));
|
|
|
|
|
|
2020-10-11 08:12:53 +08:00
|
|
|
// DEBUG_MSG("interrupt=%d\n", r);
|
2020-10-10 09:57:57 +08:00
|
|
|
return !r;
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InterruptableDelay::interrupt()
|
|
|
|
|
{
|
|
|
|
|
xSemaphoreGive(semaphore);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IRAM_ATTR void InterruptableDelay::interruptFromISR(BaseType_t *pxHigherPriorityTaskWoken)
|
|
|
|
|
{
|
|
|
|
|
xSemaphoreGiveFromISR(semaphore, pxHigherPriorityTaskWoken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace concurrency
|