@ -1,26 +1,71 @@
# include <Arduino.h>
# include <Arduino.h>
# include <freertos/task.h>
# include <freertos/semphr.h>
# define TIMER_ID 0
# define TIMER_ID 0
hw_timer_t * _uclockTimer = NULL ;
hw_timer_t * _uclockTimer = NULL ;
portMUX_TYPE _uclockTimerMux = portMUX_INITIALIZER_UNLOCKED ;
// mutex control for ISR
# define ATOMIC(X) portENTER_CRITICAL_ISR(&_uclockTimerMux); X; portEXIT_CRITICAL_ISR(&_uclockTimerMux);
//portMUX_TYPE _uclockTimerMux = portMUX_INITIALIZER_UNLOCKED;
//#define ATOMIC(X) portENTER_CRITICAL_ISR(&_uclockTimerMux); X; portEXIT_CRITICAL_ISR(&_uclockTimerMux);
// FreeRTOS main clock task size in bytes
# define CLOCK_STACK_SIZE 2048
// semaphore to signal the task from the ISR
SemaphoreHandle_t _semaphore ;
// mutex to protect the shared resource
SemaphoreHandle_t _mutex ;
// mutex control for task
# define ATOMIC(X) \
do { \
xSemaphoreTake ( _mutex , portMAX_DELAY ) ; \
X ; \
xSemaphoreGive ( _mutex ) ; \
} while ( 0 ) ;
// forward declaration of uClockHandler
void uClockHandler ( ) ;
// ISR handler
void ARDUINO_ISR_ATTR handlerISR ( void )
{
// notify the clockTask using the semaphore
BaseType_t xHigherPriorityTaskWoken = pdFALSE ;
xSemaphoreGiveFromISR ( _semaphore , & xHigherPriorityTaskWoken ) ;
// context switch is required? request it
if ( xHigherPriorityTaskWoken = = pdTRUE )
portYIELD_FROM_ISR ( xHigherPriorityTaskWoken ) ;
}
// forward declaration of ISR
// task for user clock process
void ARDUINO_ISR_ATTR uclockISR ( ) ;
void clockTask ( void * pvParameters )
{
while ( 1 )
if ( xSemaphoreTake ( _semaphore , portMAX_DELAY ) = = pdTRUE )
uClockHandler ( ) ;
}
void initTimer ( uint32_t init_clock )
void initTimer ( uint32_t init_clock )
{
{
_uclockTimer = timerBegin ( TIMER_ID , 80 , true ) ;
_uclockTimer = timerBegin ( TIMER_ID , 80 , true ) ;
// attach to generic uclock ISR
// attach to generic uclock ISR
timerAttachInterrupt ( _uclockTimer , & uclockISR , false ) ;
timerAttachInterrupt ( _uclockTimer , & handler ISR, false ) ;
// init clock tick time
// init clock tick time
timerAlarmWrite ( _uclockTimer , init_clock , true ) ;
timerAlarmWrite ( _uclockTimer , init_clock , true ) ;
// activate it!
// activate it!
timerAlarmEnable ( _uclockTimer ) ;
timerAlarmEnable ( _uclockTimer ) ;
// initialize the semaphore
_semaphore = xSemaphoreCreateBinary ( ) ;
// initialize the mutex for shared resource access
_mutex = xSemaphoreCreateMutex ( ) ;
// create the clockTask
xTaskCreate ( clockTask , " clockTask " , CLOCK_STACK_SIZE , NULL , 1 , NULL ) ;
}
}
void setTimer ( uint32_t us_interval )
void setTimer ( uint32_t us_interval )