123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- #include "sdk_common.h"
- #if NRF_MODULE_ENABLED(APP_TIMER)
- #include "FreeRTOS.h"
- #include "task.h"
- #include "timers.h"
- #include "app_timer.h"
- #include <stdlib.h>
- #include <string.h>
- #include "nrf.h"
- #include "app_error.h"
- #if configTICK_SOURCE != FREERTOS_USE_RTC
- #error app_timer in FreeRTOS variant have to be used with RTC tick source configuration. Default configuration have to be used in other case.
- #endif
- #define APP_TIMER_WAIT_FOR_QUEUE 2
- typedef struct
- {
- void * argument;
- TimerHandle_t osHandle;
- app_timer_timeout_handler_t func;
-
- bool active;
- bool single_shot;
- }app_timer_info_t;
- #if configUSE_TIMERS == 0
- #error app_timer for freeRTOS requires configUSE_TIMERS option to be activated.
- #endif
- STATIC_ASSERT(sizeof(app_timer_info_t) <= sizeof(app_timer_t));
- static void app_timer_callback(TimerHandle_t xTimer)
- {
- app_timer_info_t * pinfo = (app_timer_info_t*)(pvTimerGetTimerID(xTimer));
- ASSERT(pinfo->osHandle == xTimer);
- ASSERT(pinfo->func != NULL);
- if (pinfo->active)
- {
- pinfo->active = (pinfo->single_shot) ? false : true;
- pinfo->func(pinfo->argument);
- }
- }
- uint32_t app_timer_init(void)
- {
- return NRF_SUCCESS;
- }
- uint32_t app_timer_create(app_timer_id_t const * p_timer_id,
- app_timer_mode_t mode,
- app_timer_timeout_handler_t timeout_handler)
- {
- app_timer_info_t * pinfo = (app_timer_info_t*)(*p_timer_id);
- uint32_t err_code = NRF_SUCCESS;
- unsigned long timer_mode;
- if ((timeout_handler == NULL) || (p_timer_id == NULL))
- {
- return NRF_ERROR_INVALID_PARAM;
- }
- if (pinfo->active)
- {
- return NRF_ERROR_INVALID_STATE;
- }
- if (pinfo->osHandle == NULL)
- {
-
- memset(pinfo, 0, sizeof(app_timer_info_t));
- timer_mode = (mode == APP_TIMER_MODE_SINGLE_SHOT) ? pdFALSE : pdTRUE;
- pinfo->single_shot = (mode == APP_TIMER_MODE_SINGLE_SHOT);
- pinfo->func = timeout_handler;
- pinfo->osHandle = xTimerCreate(" ", 1000, timer_mode, pinfo, app_timer_callback);
- if (pinfo->osHandle == NULL)
- err_code = NRF_ERROR_NULL;
- }
- else
- {
-
- return NRF_ERROR_INVALID_STATE;
- }
- return err_code;
- }
- uint32_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context)
- {
- app_timer_info_t * pinfo = (app_timer_info_t*)(timer_id);
- TimerHandle_t hTimer = pinfo->osHandle;
- if (hTimer == NULL)
- {
- return NRF_ERROR_INVALID_STATE;
- }
- if (pinfo->active)
- {
-
- return NRF_SUCCESS;
- }
- pinfo->argument = p_context;
- if (__get_IPSR() != 0)
- {
- BaseType_t yieldReq = pdFALSE;
- if (xTimerChangePeriodFromISR(hTimer, timeout_ticks, &yieldReq) != pdPASS)
- {
- return NRF_ERROR_NO_MEM;
- }
- if ( xTimerStartFromISR(hTimer, &yieldReq) != pdPASS )
- {
- return NRF_ERROR_NO_MEM;
- }
- portYIELD_FROM_ISR(yieldReq);
- }
- else
- {
- if (xTimerChangePeriod(hTimer, timeout_ticks, APP_TIMER_WAIT_FOR_QUEUE) != pdPASS)
- {
- return NRF_ERROR_NO_MEM;
- }
- if (xTimerStart(hTimer, APP_TIMER_WAIT_FOR_QUEUE) != pdPASS)
- {
- return NRF_ERROR_NO_MEM;
- }
- }
- pinfo->active = true;
- return NRF_SUCCESS;
- }
- uint32_t app_timer_stop(app_timer_id_t timer_id)
- {
- app_timer_info_t * pinfo = (app_timer_info_t*)(timer_id);
- TimerHandle_t hTimer = pinfo->osHandle;
- if (hTimer == NULL)
- {
- return NRF_ERROR_INVALID_STATE;
- }
- if (__get_IPSR() != 0)
- {
- BaseType_t yieldReq = pdFALSE;
- if (xTimerStopFromISR(hTimer, &yieldReq) != pdPASS)
- {
- return NRF_ERROR_NO_MEM;
- }
- portYIELD_FROM_ISR(yieldReq);
- }
- else
- {
- if (xTimerStop(hTimer, APP_TIMER_WAIT_FOR_QUEUE) != pdPASS)
- {
- return NRF_ERROR_NO_MEM;
- }
- }
- pinfo->active = false;
- return NRF_SUCCESS;
- }
- #endif
|