123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- #ifndef APP_TIMER_H__
- #define APP_TIMER_H__
- #include "sdk_config.h"
- #include "app_error.h"
- #include "app_util.h"
- #include "compiler_abstraction.h"
- #include "nordic_common.h"
- #ifdef APP_TIMER_V2
- #include "nrf_log_instance.h"
- #include "nrf_sortlist.h"
- #endif
- #include <stdint.h>
- #include <stdbool.h>
- #include <stdio.h>
- #ifdef __cplusplus
- extern "C" {
- #endif
- #define APP_TIMER_LOG_NAME app_timer
- #define APP_TIMER_CLOCK_FREQ 32768
- #define APP_TIMER_MIN_TIMEOUT_TICKS 5
- #ifdef RTX
- #define APP_TIMER_NODE_SIZE 40
- #else
- #define APP_TIMER_NODE_SIZE 32
- #endif
- #define APP_TIMER_SCHED_EVENT_DATA_SIZE sizeof(app_timer_event_t)
- #define APP_TIMER_MAX_CNT_VAL RTC_COUNTER_COUNTER_Msk
- #ifndef FREERTOS
- #define APP_TIMER_TICKS(MS) \
- ((uint32_t)ROUNDED_DIV( \
- (MS) * (uint64_t)APP_TIMER_CLOCK_FREQ, \
- 1000 * (APP_TIMER_CONFIG_RTC_FREQUENCY + 1)))
- #else
- #include "FreeRTOSConfig.h"
- #define APP_TIMER_TICKS(MS) (uint32_t)ROUNDED_DIV((MS)*configTICK_RATE_HZ,1000)
- #endif
- #define APP_TIMER_DEF(timer_id) _APP_TIMER_DEF(timer_id)
- typedef void (*app_timer_timeout_handler_t)(void * p_context);
- #ifdef APP_TIMER_V2
- typedef struct
- {
- nrf_sortlist_item_t list_item;
- uint64_t end_val;
- uint32_t repeat_period;
- app_timer_timeout_handler_t handler;
- void * p_context;
- NRF_LOG_INSTANCE_PTR_DECLARE(p_log)
- volatile bool active;
- } app_timer_t;
- typedef app_timer_t * app_timer_id_t;
- #define _APP_TIMER_DEF(timer_id) \
- NRF_LOG_INSTANCE_REGISTER(APP_TIMER_LOG_NAME, timer_id, \
- APP_TIMER_CONFIG_INFO_COLOR, \
- APP_TIMER_CONFIG_DEBUG_COLOR, \
- APP_TIMER_CONFIG_INITIAL_LOG_LEVEL, \
- APP_TIMER_CONFIG_LOG_ENABLED ? \
- APP_TIMER_CONFIG_LOG_LEVEL : NRF_LOG_SEVERITY_NONE); \
- static app_timer_t CONCAT_2(timer_id,_data) = { \
- .active = false, \
- NRF_LOG_INSTANCE_PTR_INIT(p_log, APP_TIMER_LOG_NAME, timer_id) \
- }; \
- static const app_timer_id_t timer_id = &CONCAT_2(timer_id,_data)
- #else
- typedef struct app_timer_t { uint32_t data[CEIL_DIV(APP_TIMER_NODE_SIZE, sizeof(uint32_t))]; } app_timer_t;
- typedef app_timer_t * app_timer_id_t;
- #define _APP_TIMER_DEF(timer_id) \
- static app_timer_t CONCAT_2(timer_id,_data) = { {0} }; \
- static const app_timer_id_t timer_id = &CONCAT_2(timer_id,_data)
- #endif
- typedef struct
- {
- app_timer_timeout_handler_t timeout_handler;
- void * p_context;
- } app_timer_event_t;
- typedef enum
- {
- APP_TIMER_MODE_SINGLE_SHOT,
- APP_TIMER_MODE_REPEATED
- } app_timer_mode_t;
- ret_code_t app_timer_init(void);
- 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);
- ret_code_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context);
- ret_code_t app_timer_stop(app_timer_id_t timer_id);
- ret_code_t app_timer_stop_all(void);
- uint32_t app_timer_cnt_get(void);
- uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to,
- uint32_t ticks_from);
- uint8_t app_timer_op_queue_utilization_get(void);
- void app_timer_pause(void);
- void app_timer_resume(void);
- #ifdef __cplusplus
- }
- #endif
- #endif
|