1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075 |
- #include "sdk_common.h"
- #if NRF_MODULE_ENABLED(APP_TIMER)
- #include "app_timer.h"
- #include <stdlib.h>
- #include "nrf.h"
- #include "nrf_peripherals.h"
- #include "nrf_soc.h"
- #include "app_error.h"
- #include "nrf_delay.h"
- #include "app_util_platform.h"
- #if APP_TIMER_CONFIG_USE_SCHEDULER
- #include "app_scheduler.h"
- #endif
- #define RTC1_IRQ_PRI APP_TIMER_CONFIG_IRQ_PRIORITY
- #define SWI_IRQ_PRI APP_TIMER_CONFIG_IRQ_PRIORITY
- STATIC_ASSERT(RTC1_IRQ_PRI == SWI_IRQ_PRI);
- #define MAX_RTC_COUNTER_VAL 0x00FFFFFF
- #define RTC_COMPARE_OFFSET_MIN 3
- #define MAX_RTC_TASKS_DELAY 47
- #ifdef EGU_PRESENT
- #define SWI_PART(_id) CONCAT_2(SWI,_id)
- #define EGU_PART(_id) CONCAT_2(_EGU,_id)
- #define SWI_IRQ_n(_id) CONCAT_3(SWI_PART(_id), EGU_PART(_id),_IRQn)
- #define SWI_IRQ_Handler_n(_id) CONCAT_3(SWI_PART(_id), EGU_PART(_id),_IRQHandler)
- #else
- #define SWI_IRQ_n(_id) CONCAT_3(SWI,_id,_IRQn)
- #define SWI_IRQ_Handler_n(_id) CONCAT_3(SWI,_id,_IRQHandler)
- #endif
- #define SWI_IRQn SWI_IRQ_n(APP_TIMER_CONFIG_SWI_NUMBER)
- #define SWI_IRQHandler SWI_IRQ_Handler_n(APP_TIMER_CONFIG_SWI_NUMBER)
- #define MODULE_INITIALIZED (m_op_queue.size != 0)
- typedef struct
- {
- uint32_t ticks_to_expire;
- uint32_t ticks_at_start;
- uint32_t ticks_first_interval;
- uint32_t ticks_periodic_interval;
- bool is_running;
- app_timer_mode_t mode;
- app_timer_timeout_handler_t p_timeout_handler;
- void * p_context;
- void * next;
- } timer_node_t;
- STATIC_ASSERT(sizeof(timer_node_t) == APP_TIMER_NODE_SIZE);
- typedef enum
- {
- TIMER_USER_OP_TYPE_NONE,
- TIMER_USER_OP_TYPE_START,
- TIMER_USER_OP_TYPE_STOP,
- TIMER_USER_OP_TYPE_STOP_ALL
- } timer_user_op_type_t;
- typedef struct
- {
- uint32_t ticks_at_start;
- uint32_t ticks_first_interval;
- uint32_t ticks_periodic_interval;
- void * p_context;
- } timer_user_op_start_t;
- typedef struct
- {
- timer_user_op_type_t op_type;
- timer_node_t * p_node;
- union
- {
- timer_user_op_start_t start;
- } params;
- } timer_user_op_t;
- typedef struct
- {
- uint8_t first;
- uint8_t last;
- uint8_t size;
- timer_user_op_t user_op_queue[APP_TIMER_CONFIG_OP_QUEUE_SIZE+1];
- } timer_op_queue_t;
- STATIC_ASSERT(sizeof(timer_op_queue_t) % 4 == 0);
- #define CONTEXT_QUEUE_SIZE_MAX (2)
- static timer_op_queue_t m_op_queue;
- static timer_node_t * mp_timer_id_head;
- static uint32_t m_ticks_latest;
- static uint32_t m_ticks_elapsed[CONTEXT_QUEUE_SIZE_MAX];
- static uint8_t m_ticks_elapsed_q_read_ind;
- static uint8_t m_ticks_elapsed_q_write_ind;
- static bool m_rtc1_running;
- static bool m_rtc1_reset;
- #if APP_TIMER_WITH_PROFILER
- static uint8_t m_max_user_op_queue_utilization;
- #endif
- static void rtc1_init(uint32_t prescaler)
- {
- NRF_RTC1->PRESCALER = prescaler;
- NVIC_SetPriority(RTC1_IRQn, RTC1_IRQ_PRI);
- }
- static void rtc1_start(void)
- {
- NRF_RTC1->EVTENSET = RTC_EVTEN_COMPARE0_Msk;
- NRF_RTC1->INTENSET = RTC_INTENSET_COMPARE0_Msk;
- NVIC_ClearPendingIRQ(RTC1_IRQn);
- NVIC_EnableIRQ(RTC1_IRQn);
- NRF_RTC1->TASKS_START = 1;
- nrf_delay_us(MAX_RTC_TASKS_DELAY);
- m_rtc1_running = true;
- }
- static void rtc1_stop(void)
- {
- NVIC_DisableIRQ(RTC1_IRQn);
- NRF_RTC1->EVTENCLR = RTC_EVTEN_COMPARE0_Msk;
- NRF_RTC1->INTENCLR = RTC_INTENSET_COMPARE0_Msk;
- NRF_RTC1->TASKS_STOP = 1;
- nrf_delay_us(MAX_RTC_TASKS_DELAY);
- NRF_RTC1->TASKS_CLEAR = 1;
- m_ticks_latest = 0;
- nrf_delay_us(MAX_RTC_TASKS_DELAY);
- m_rtc1_running = false;
- }
- static __INLINE uint32_t rtc1_counter_get(void)
- {
- return NRF_RTC1->COUNTER;
- }
- static __INLINE uint32_t ticks_diff_get(uint32_t ticks_now, uint32_t ticks_old)
- {
- return ((ticks_now - ticks_old) & MAX_RTC_COUNTER_VAL);
- }
- static __INLINE void rtc1_compare0_set(uint32_t value)
- {
- NRF_RTC1->CC[0] = value;
- }
- static void timer_list_insert(timer_node_t * p_timer)
- {
- if (mp_timer_id_head == NULL)
- {
- mp_timer_id_head = p_timer;
- }
- else
- {
- if (p_timer->ticks_to_expire <= mp_timer_id_head->ticks_to_expire)
- {
- mp_timer_id_head->ticks_to_expire -= p_timer->ticks_to_expire;
- p_timer->next = mp_timer_id_head;
- mp_timer_id_head = p_timer;
- }
- else
- {
- timer_node_t * p_previous;
- timer_node_t * p_current;
- uint32_t ticks_to_expire;
- ticks_to_expire = p_timer->ticks_to_expire;
- p_previous = mp_timer_id_head;
- p_current = mp_timer_id_head;
- while ((p_current != NULL) && (ticks_to_expire > p_current->ticks_to_expire))
- {
- ticks_to_expire -= p_current->ticks_to_expire;
- p_previous = p_current;
- p_current = p_current->next;
- }
- if (p_current != NULL)
- {
- p_current->ticks_to_expire -= ticks_to_expire;
- }
- p_timer->ticks_to_expire = ticks_to_expire;
- p_timer->next = p_current;
- p_previous->next = p_timer;
- }
- }
- }
- static bool timer_list_remove(timer_node_t * p_timer)
- {
- timer_node_t * p_old_head;
- timer_node_t * p_previous;
- timer_node_t * p_current;
- uint32_t timeout;
-
- p_old_head = mp_timer_id_head;
- p_previous = mp_timer_id_head;
- p_current = p_previous;
- while (p_current != NULL)
- {
- if (p_current == p_timer)
- {
- break;
- }
- p_previous = p_current;
- p_current = p_current->next;
- }
-
- if (p_current == NULL)
- {
- return false;
- }
-
- if (p_previous == p_current)
- {
- mp_timer_id_head = mp_timer_id_head->next;
-
- if (mp_timer_id_head == NULL)
- {
- NRF_RTC1->TASKS_CLEAR = 1;
- m_ticks_latest = 0;
- m_rtc1_reset = true;
- nrf_delay_us(MAX_RTC_TASKS_DELAY);
- }
- }
-
- timeout = p_current->ticks_to_expire;
-
- p_previous->next = p_current->next;
-
- p_current = p_previous->next;
- if (p_current != NULL)
- {
- p_current->ticks_to_expire += timeout;
- }
- return (p_old_head != mp_timer_id_head);
- }
- static void timer_timeouts_check_sched(void)
- {
- NVIC_SetPendingIRQ(RTC1_IRQn);
- }
- static void timer_list_handler_sched(void)
- {
- NVIC_SetPendingIRQ(SWI_IRQn);
- }
- #if APP_TIMER_CONFIG_USE_SCHEDULER
- static void timeout_handler_scheduled_exec(void * p_event_data, uint16_t event_size)
- {
- APP_ERROR_CHECK_BOOL(event_size == sizeof(app_timer_event_t));
- app_timer_event_t const * p_timer_event = (app_timer_event_t *)p_event_data;
- p_timer_event->timeout_handler(p_timer_event->p_context);
- }
- #endif
- static void timeout_handler_exec(timer_node_t * p_timer)
- {
- #if APP_TIMER_CONFIG_USE_SCHEDULER
- app_timer_event_t timer_event;
- timer_event.timeout_handler = p_timer->p_timeout_handler;
- timer_event.p_context = p_timer->p_context;
- uint32_t err_code = app_sched_event_put(&timer_event, sizeof(timer_event), timeout_handler_scheduled_exec);
- APP_ERROR_CHECK(err_code);
- #else
- p_timer->p_timeout_handler(p_timer->p_context);
- #endif
- }
- static void timer_timeouts_check(void)
- {
-
- if (mp_timer_id_head != NULL)
- {
- timer_node_t * p_timer;
- timer_node_t * p_previous_timer;
- uint32_t ticks_elapsed;
- uint32_t ticks_expired;
-
- ticks_expired = 0;
-
- ticks_elapsed = ticks_diff_get(rtc1_counter_get(), m_ticks_latest);
-
- p_timer = mp_timer_id_head;
-
- while (p_timer != NULL)
- {
-
- if (ticks_elapsed < p_timer->ticks_to_expire)
- {
- break;
- }
-
- ticks_elapsed -= p_timer->ticks_to_expire;
- ticks_expired += p_timer->ticks_to_expire;
-
- p_previous_timer = p_timer;
- p_timer = p_timer->next;
-
- if (p_previous_timer->is_running)
- {
- p_previous_timer->is_running = false;
- timeout_handler_exec(p_previous_timer);
- }
- }
-
- if (m_ticks_elapsed_q_read_ind == m_ticks_elapsed_q_write_ind)
- {
-
-
-
-
- if (++m_ticks_elapsed_q_write_ind == CONTEXT_QUEUE_SIZE_MAX)
- {
-
-
- m_ticks_elapsed_q_write_ind = 0;
- }
- }
-
- m_ticks_elapsed[m_ticks_elapsed_q_write_ind] = ticks_expired;
- timer_list_handler_sched();
- }
- }
- static bool elapsed_ticks_acquire(uint32_t * p_ticks_elapsed)
- {
-
- if (m_ticks_elapsed_q_read_ind != m_ticks_elapsed_q_write_ind)
- {
-
- m_ticks_elapsed_q_read_ind++;
- if (m_ticks_elapsed_q_read_ind == CONTEXT_QUEUE_SIZE_MAX)
- {
- m_ticks_elapsed_q_read_ind = 0;
- }
- *p_ticks_elapsed = m_ticks_elapsed[m_ticks_elapsed_q_read_ind];
- m_ticks_latest += *p_ticks_elapsed;
- m_ticks_latest &= MAX_RTC_COUNTER_VAL;
- return true;
- }
- else
- {
-
- *p_ticks_elapsed = 0;
- return false;
- }
- }
- static void expired_timers_handler(uint32_t ticks_elapsed,
- uint32_t ticks_previous,
- timer_node_t ** p_restart_list_head)
- {
- uint32_t ticks_expired = 0;
- while (mp_timer_id_head != NULL)
- {
- timer_node_t * p_timer;
- timer_node_t * p_timer_expired;
-
- p_timer = mp_timer_id_head;
-
- if (ticks_elapsed < p_timer->ticks_to_expire)
- {
- p_timer->ticks_to_expire -= ticks_elapsed;
- break;
- }
-
- ticks_elapsed -= p_timer->ticks_to_expire;
- ticks_expired += p_timer->ticks_to_expire;
-
- p_timer->ticks_to_expire = 0;
-
- p_timer_expired = mp_timer_id_head;
- mp_timer_id_head = p_timer->next;
-
- if (p_timer->ticks_periodic_interval != 0)
- {
- p_timer->ticks_at_start = (ticks_previous + ticks_expired) & MAX_RTC_COUNTER_VAL;
- p_timer->ticks_first_interval = p_timer->ticks_periodic_interval;
- p_timer->next = *p_restart_list_head;
- *p_restart_list_head = p_timer_expired;
- }
- }
- }
- static bool list_insertions_handler(timer_node_t * p_restart_list_head)
- {
- bool compare_update = false;
- timer_node_t * p_timer_id_old_head;
-
- p_timer_id_old_head = mp_timer_id_head;
-
- while ((p_restart_list_head != NULL) || (m_op_queue.first != m_op_queue.last))
- {
- timer_node_t * p_timer;
- if (p_restart_list_head != NULL)
- {
- p_timer = p_restart_list_head;
- p_restart_list_head = p_timer->next;
- }
- else
- {
- timer_user_op_t * p_user_op = &m_op_queue.user_op_queue[m_op_queue.first];
- m_op_queue.first++;
- if (m_op_queue.first == m_op_queue.size)
- {
- m_op_queue.first = 0;
- }
- p_timer = p_user_op->p_node;
- switch (p_user_op->op_type)
- {
- case TIMER_USER_OP_TYPE_STOP:
-
- if (timer_list_remove(p_user_op->p_node))
- {
- compare_update = true;
- }
- p_timer->is_running = false;
- continue;
- case TIMER_USER_OP_TYPE_STOP_ALL:
-
- while (mp_timer_id_head != NULL)
- {
- timer_node_t * p_head = mp_timer_id_head;
- p_head->is_running = false;
- mp_timer_id_head = p_head->next;
- }
- continue;
- case TIMER_USER_OP_TYPE_START:
- break;
- default:
-
- continue;
- }
- if (p_timer->is_running)
- {
- continue;
- }
- p_timer->ticks_at_start = p_user_op->params.start.ticks_at_start;
- p_timer->ticks_first_interval = p_user_op->params.start.ticks_first_interval;
- p_timer->ticks_periodic_interval = p_user_op->params.start.ticks_periodic_interval;
- p_timer->p_context = p_user_op->params.start.p_context;
- if (m_rtc1_reset)
- {
- p_timer->ticks_at_start = 0;
- }
- }
-
- if (
- ((p_timer->ticks_at_start - m_ticks_latest) & MAX_RTC_COUNTER_VAL)
- <
- (MAX_RTC_COUNTER_VAL / 2)
- )
- {
- p_timer->ticks_to_expire = ticks_diff_get(p_timer->ticks_at_start, m_ticks_latest) +
- p_timer->ticks_first_interval;
- }
- else
- {
- uint32_t delta_current_start;
- delta_current_start = ticks_diff_get(m_ticks_latest, p_timer->ticks_at_start);
- if (p_timer->ticks_first_interval > delta_current_start)
- {
- p_timer->ticks_to_expire = p_timer->ticks_first_interval - delta_current_start;
- }
- else
- {
- p_timer->ticks_to_expire = 0;
- }
- }
- p_timer->ticks_at_start = 0;
- p_timer->ticks_first_interval = 0;
- p_timer->is_running = true;
- p_timer->next = NULL;
-
- timer_list_insert(p_timer);
- }
- return (compare_update || (mp_timer_id_head != p_timer_id_old_head));
- }
- static void compare_reg_update(timer_node_t * p_timer_id_head_old)
- {
-
- if (mp_timer_id_head != NULL)
- {
- uint32_t ticks_to_expire = mp_timer_id_head->ticks_to_expire;
- uint32_t pre_counter_val = rtc1_counter_get();
- uint32_t cc = m_ticks_latest;
- uint32_t ticks_elapsed = ticks_diff_get(pre_counter_val, cc) + RTC_COMPARE_OFFSET_MIN;
- if (!m_rtc1_running)
- {
-
- rtc1_start();
- }
- cc += (ticks_elapsed < ticks_to_expire) ? ticks_to_expire : ticks_elapsed;
- cc &= MAX_RTC_COUNTER_VAL;
- rtc1_compare0_set(cc);
- uint32_t post_counter_val = rtc1_counter_get();
- if (
- (ticks_diff_get(post_counter_val, pre_counter_val) + RTC_COMPARE_OFFSET_MIN)
- >
- ticks_diff_get(cc, pre_counter_val)
- )
- {
-
-
-
-
-
- rtc1_compare0_set(rtc1_counter_get());
- nrf_delay_us(MAX_RTC_TASKS_DELAY);
- timer_timeouts_check_sched();
- }
- }
- else
- {
- #if (APP_TIMER_KEEPS_RTC_ACTIVE == 0)
-
- rtc1_stop();
- #endif
- }
- }
- static void timer_list_handler(void)
- {
- timer_node_t * p_restart_list_head = NULL;
- uint32_t ticks_elapsed;
- uint32_t ticks_previous;
- bool ticks_have_elapsed;
- bool compare_update = false;
- timer_node_t * p_timer_id_head_old;
- #if APP_TIMER_WITH_PROFILER
- {
- uint8_t size = m_op_queue.size;
- uint8_t first = m_op_queue.first;
- uint8_t last = m_op_queue.last;
- uint8_t utilization = (first <= last) ? (last - first) : (size + 1 - first + last);
- if (utilization > m_max_user_op_queue_utilization)
- {
- m_max_user_op_queue_utilization = utilization;
- }
- }
- #endif
-
- ticks_previous = m_ticks_latest;
- p_timer_id_head_old = mp_timer_id_head;
-
- ticks_have_elapsed = elapsed_ticks_acquire(&ticks_elapsed);
-
- if (ticks_have_elapsed)
- {
- expired_timers_handler(ticks_elapsed, ticks_previous, &p_restart_list_head);
- compare_update = true;
- }
-
- if (list_insertions_handler(p_restart_list_head))
- {
- compare_update = true;
- }
-
- if (compare_update)
- {
- compare_reg_update(p_timer_id_head_old);
- }
- m_rtc1_reset = false;
- }
- static void user_op_enque(uint8_t last_index)
- {
- m_op_queue.last = last_index;
- }
- static timer_user_op_t * user_op_alloc( uint8_t * p_last_index)
- {
- uint8_t last;
- timer_user_op_t * p_user_op;
- last = m_op_queue.last + 1;
- if (last == m_op_queue.size)
- {
-
- last = 0;
- }
- if (last == m_op_queue.first)
- {
-
- return NULL;
- }
- *p_last_index = last;
- p_user_op = &m_op_queue.user_op_queue[m_op_queue.last];
- return p_user_op;
- }
- static uint32_t timer_start_op_schedule(timer_node_t * p_node,
- uint32_t timeout_initial,
- uint32_t timeout_periodic,
- void * p_context)
- {
- uint8_t last_index;
- uint32_t err_code = NRF_SUCCESS;
- CRITICAL_REGION_ENTER();
- timer_user_op_t * p_user_op = user_op_alloc(&last_index);
- if (p_user_op == NULL)
- {
- err_code = NRF_ERROR_NO_MEM;
- }
- else
- {
- p_user_op->op_type = TIMER_USER_OP_TYPE_START;
- p_user_op->p_node = p_node;
- p_user_op->params.start.ticks_at_start = rtc1_counter_get();
- p_user_op->params.start.ticks_first_interval = timeout_initial;
- p_user_op->params.start.ticks_periodic_interval = timeout_periodic;
- p_user_op->params.start.p_context = p_context;
- user_op_enque(last_index);
- }
- CRITICAL_REGION_EXIT();
- if (err_code == NRF_SUCCESS)
- {
- timer_list_handler_sched();
- }
- return err_code;
- }
- static uint32_t timer_stop_op_schedule(timer_node_t * p_node,
- timer_user_op_type_t op_type)
- {
- uint8_t last_index;
- uint32_t err_code = NRF_SUCCESS;
- CRITICAL_REGION_ENTER();
- timer_user_op_t * p_user_op = user_op_alloc(&last_index);
- if (p_user_op == NULL)
- {
- err_code = NRF_ERROR_NO_MEM;
- }
- else
- {
- p_user_op->op_type = op_type;
- p_user_op->p_node = p_node;
- user_op_enque(last_index);
- }
- CRITICAL_REGION_EXIT();
- if (err_code == NRF_SUCCESS)
- {
- timer_list_handler_sched();
- }
- return err_code;
- }
- void RTC1_IRQHandler(void)
- {
-
- NRF_RTC1->EVENTS_COMPARE[0] = 0;
- NRF_RTC1->EVENTS_COMPARE[1] = 0;
- NRF_RTC1->EVENTS_COMPARE[2] = 0;
- NRF_RTC1->EVENTS_COMPARE[3] = 0;
- NRF_RTC1->EVENTS_TICK = 0;
- NRF_RTC1->EVENTS_OVRFLW = 0;
-
- timer_timeouts_check();
- }
- void SWI_IRQHandler(void)
- {
- timer_list_handler();
- }
- ret_code_t app_timer_init(void)
- {
-
- rtc1_stop();
-
- m_op_queue.first = 0;
- m_op_queue.last = 0;
- m_op_queue.size = APP_TIMER_CONFIG_OP_QUEUE_SIZE+1;
- mp_timer_id_head = NULL;
- m_ticks_elapsed_q_read_ind = 0;
- m_ticks_elapsed_q_write_ind = 0;
- #if APP_TIMER_WITH_PROFILER
- m_max_user_op_queue_utilization = 0;
- #endif
- NVIC_ClearPendingIRQ(SWI_IRQn);
- NVIC_SetPriority(SWI_IRQn, SWI_IRQ_PRI);
- NVIC_EnableIRQ(SWI_IRQn);
- rtc1_init(APP_TIMER_CONFIG_RTC_FREQUENCY);
- m_ticks_latest = rtc1_counter_get();
- 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)
- {
-
- VERIFY_MODULE_INITIALIZED();
- if (timeout_handler == NULL)
- {
- return NRF_ERROR_INVALID_PARAM;
- }
- if (p_timer_id == NULL)
- {
- return NRF_ERROR_INVALID_PARAM;
- }
- if (((timer_node_t*)*p_timer_id)->is_running)
- {
- return NRF_ERROR_INVALID_STATE;
- }
- timer_node_t * p_node = (timer_node_t *)*p_timer_id;
- p_node->is_running = false;
- p_node->mode = mode;
- p_node->p_timeout_handler = timeout_handler;
- return NRF_SUCCESS;
- }
- ret_code_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context)
- {
- uint32_t timeout_periodic;
- timer_node_t * p_node = (timer_node_t*)timer_id;
-
- VERIFY_MODULE_INITIALIZED();
- if (timer_id == 0)
- {
- return NRF_ERROR_INVALID_STATE;
- }
- if ((timeout_ticks < APP_TIMER_MIN_TIMEOUT_TICKS) || (timeout_ticks > MAX_RTC_COUNTER_VAL))
- {
- return NRF_ERROR_INVALID_PARAM;
- }
- if (p_node->p_timeout_handler == NULL)
- {
- return NRF_ERROR_INVALID_STATE;
- }
-
- timeout_periodic = (p_node->mode == APP_TIMER_MODE_REPEATED) ? timeout_ticks : 0;
- return timer_start_op_schedule(p_node,
- timeout_ticks,
- timeout_periodic,
- p_context);
- }
- ret_code_t app_timer_stop(app_timer_id_t timer_id)
- {
- timer_node_t * p_node = (timer_node_t*)timer_id;
-
- VERIFY_MODULE_INITIALIZED();
- if ((timer_id == NULL) || (p_node->p_timeout_handler == NULL))
- {
- return NRF_ERROR_INVALID_STATE;
- }
- p_node->is_running = false;
-
- return timer_stop_op_schedule(p_node, TIMER_USER_OP_TYPE_STOP);
- }
- ret_code_t app_timer_stop_all(void)
- {
-
- VERIFY_MODULE_INITIALIZED();
- return timer_stop_op_schedule(NULL, TIMER_USER_OP_TYPE_STOP_ALL);
- }
- uint32_t app_timer_cnt_get(void)
- {
- return rtc1_counter_get();
- }
- uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to,
- uint32_t ticks_from)
- {
- return ticks_diff_get(ticks_to, ticks_from);
- }
- #if APP_TIMER_WITH_PROFILER
- uint8_t app_timer_op_queue_utilization_get(void)
- {
- return m_max_user_op_queue_utilization;
- }
- #endif
- void app_timer_pause(void)
- {
- NRF_RTC1->TASKS_STOP = 1;
- }
- void app_timer_resume(void)
- {
- NRF_RTC1->TASKS_START = 1;
- }
- #endif
|