123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- #include "sdk_common.h"
- #if NRF_MODULE_ENABLED(APP_TIMER)
- #include "app_timer.h"
- #include <stdlib.h>
- #include "nrf.h"
- #include "nrf_soc.h"
- #include "app_error.h"
- #include "cmsis_os.h"
- #include "app_util_platform.h"
- #define RTC1_IRQ_PRI APP_IRQ_PRIORITY_LOWEST
- #define MAX_RTC_COUNTER_VAL 0x00FFFFFF
- typedef struct
- {
- osTimerDef_t timerDef;
- uint32_t buffer[6];
- osTimerId id;
- }app_timer_info_t;
- typedef struct
- {
- uint8_t max_timers;
- uint32_t prescaler;
- app_timer_info_t * app_timers;
- }app_timer_control_t;
- app_timer_control_t app_timer_control;
- typedef struct os_timer_cb_
- {
- struct os_timer_cb_ * next;
- uint8_t state;
- uint8_t type;
- uint16_t reserved;
- uint32_t tcnt;
- uint32_t icnt;
- void * arg;
- const osTimerDef_t * timer;
- } os_timer_cb;
- extern osStatus svcTimerStop(osTimerId timer_id);
- extern osStatus svcTimerStart(osTimerId timer_id, uint32_t millisec);
- static void * rt_id2obj (void *id)
- {
- if ((uint32_t)id & 3U)
- {
- return NULL;
- }
- #ifdef OS_SECTIONS_LINK_INFO
- if ((os_section_id$$Base != 0U) && (os_section_id$$Limit != 0U))
- {
- if (id < (void *)os_section_id$$Base)
- {
- return NULL;
- }
- if (id >= (void *)os_section_id$$Limit)
- {
- return NULL;
- }
- }
- #endif
- return id;
- }
- ret_code_t app_timer_init(void)
- {
- if (p_buffer == NULL)
- {
- return NRF_ERROR_INVALID_PARAM;
- }
- app_timer_control.app_timers = p_buffer;
- NVIC_SetPriority(RTC1_IRQn, RTC1_IRQ_PRI);
- return NRF_SUCCESS;
- }
- ret_code_t app_timer_create(app_timer_id_t const * p_timer_id,
- app_timer_mode_t mode,
- app_timer_timeout_handler_t timeout_handler)
- {
- if ((timeout_handler == NULL) || (p_timer_id == NULL))
- {
- return NRF_ERROR_INVALID_PARAM;
- }
- app_timer_info_t * p_timer_info = (app_timer_info_t *)*p_timer_id;
- p_timer_info->timerDef.timer = p_timer_info->buffer;
- p_timer_info->timerDef.ptimer = (os_ptimer)timeout_handler;
- p_timer_info->id = osTimerCreate(&(p_timer_info->timerDef), (os_timer_type)mode, NULL);
- if (p_timer_info->id)
- return NRF_SUCCESS;
- else
- {
- return NRF_ERROR_INVALID_PARAM;
- }
- }
- #define osTimerRunning 2
- ret_code_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context)
- {
- if ((timeout_ticks < APP_TIMER_MIN_TIMEOUT_TICKS))
- {
- return NRF_ERROR_INVALID_PARAM;
- }
- uint32_t timeout_ms =
- ((uint32_t)ROUNDED_DIV(timeout_ticks * 1000 * (APP_TIMER_CONFIG_RTC_FREQUENCY + 1),
- (uint32_t)APP_TIMER_CLOCK_FREQ));
- app_timer_info_t * p_timer_info = (app_timer_info_t *)timer_id;
- if (rt_id2obj((void *)p_timer_info->id) == NULL)
- return NRF_ERROR_INVALID_PARAM;
-
- ((os_timer_cb *)(p_timer_info->id))->arg = p_context;
- if (((os_timer_cb *)(p_timer_info->id))->state == osTimerRunning)
- {
- return NRF_SUCCESS;
- }
-
- switch (osTimerStart((osTimerId)p_timer_info->id, timeout_ms) )
- {
- case osOK:
- return NRF_SUCCESS;
- case osErrorISR:
- break;
- case osErrorParameter:
- return NRF_ERROR_INVALID_PARAM;
- default:
- return NRF_ERROR_INVALID_PARAM;
- }
-
- switch (svcTimerStart((osTimerId)p_timer_info->id, timeout_ms))
- {
- case osOK:
- return NRF_SUCCESS;
- case osErrorISR:
- return NRF_ERROR_INVALID_STATE;
- case osErrorParameter:
- return NRF_ERROR_INVALID_PARAM;
- default:
- return NRF_ERROR_INVALID_PARAM;
- }
- }
- ret_code_t app_timer_stop(app_timer_id_t timer_id)
- {
- app_timer_info_t * p_timer_info = (app_timer_info_t *)timer_id;
- switch (osTimerStop((osTimerId)p_timer_info->id) )
- {
- case osOK:
- return NRF_SUCCESS;
- case osErrorISR:
- break;
- case osErrorParameter:
- return NRF_ERROR_INVALID_PARAM;
- case osErrorResource:
- return NRF_SUCCESS;
- default:
- return NRF_ERROR_INVALID_PARAM;
- }
-
- switch (svcTimerStop((osTimerId)p_timer_info->id))
- {
- case osOK:
- return NRF_SUCCESS;
- case osErrorISR:
- return NRF_ERROR_INVALID_STATE;
- case osErrorParameter:
- return NRF_ERROR_INVALID_PARAM;
- case osErrorResource:
- return NRF_SUCCESS;
- default:
- return NRF_ERROR_INVALID_PARAM;
- }
- }
- ret_code_t app_timer_stop_all(void)
- {
- for (int i = 0; i < app_timer_control.max_timers; i++)
- {
- if (app_timer_control.app_timers[i].id)
- {
- (void)app_timer_stop((app_timer_id_t)app_timer_control.app_timers[i].id);
- }
- }
- return 0;
- }
- extern uint32_t os_tick_val(void);
- uint32_t app_timer_cnt_get(void)
- {
- return os_tick_val();
- }
- uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to,
- uint32_t ticks_from)
- {
- return ((ticks_to - ticks_from) & MAX_RTC_COUNTER_VAL);
- }
- #endif
|