Nordic stack and drivers for the mbed BLE API
Dependents: BLE_Health_Thermometer2
Fork of nRF51822 by
nordic/app_common/app_timer.cpp@54:f3badab88f02, 2014-08-22 (annotated)
- Committer:
- todotani
- Date:
- Fri Aug 22 11:24:48 2014 +0000
- Revision:
- 54:f3badab88f02
- Parent:
- 37:c29c330d942c
BLE_Health_Thermometer for mbed HRM1017 with new BLE library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:eff01767de02 | 1 | /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved. |
bogdanm | 0:eff01767de02 | 2 | * |
bogdanm | 0:eff01767de02 | 3 | * The information contained herein is property of Nordic Semiconductor ASA. |
bogdanm | 0:eff01767de02 | 4 | * Terms and conditions of usage are described in detail in NORDIC |
bogdanm | 0:eff01767de02 | 5 | * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. |
bogdanm | 0:eff01767de02 | 6 | * |
bogdanm | 0:eff01767de02 | 7 | * Licensees are granted free, non-transferable use of the information. NO |
bogdanm | 0:eff01767de02 | 8 | * WARRANTY of ANY KIND is provided. This heading must NOT be removed from |
bogdanm | 0:eff01767de02 | 9 | * the file. |
bogdanm | 0:eff01767de02 | 10 | * |
bogdanm | 0:eff01767de02 | 11 | */ |
bogdanm | 0:eff01767de02 | 12 | |
bogdanm | 0:eff01767de02 | 13 | #include "app_timer.h" |
bogdanm | 0:eff01767de02 | 14 | #include <stdlib.h> |
bogdanm | 0:eff01767de02 | 15 | #include "nrf51.h" |
bogdanm | 0:eff01767de02 | 16 | #include "nrf51_bitfields.h" |
bogdanm | 0:eff01767de02 | 17 | #include "nrf_soc.h" |
bogdanm | 0:eff01767de02 | 18 | #include "app_error.h" |
bogdanm | 0:eff01767de02 | 19 | //#include "nrf_delay.h" |
bogdanm | 0:eff01767de02 | 20 | #include "mbed.h" |
bogdanm | 0:eff01767de02 | 21 | #include "app_util.h" |
Rohit Grover |
37:c29c330d942c | 22 | #include "app_util_platform.h" |
bogdanm | 0:eff01767de02 | 23 | |
bogdanm | 0:eff01767de02 | 24 | |
bogdanm | 0:eff01767de02 | 25 | #define RTC1_IRQ_PRI APP_IRQ_PRIORITY_LOW /**< Priority of the RTC1 interrupt (used for checking for timeouts and executing timeout handlers). */ |
bogdanm | 0:eff01767de02 | 26 | #define SWI0_IRQ_PRI APP_IRQ_PRIORITY_LOW /**< Priority of the SWI0 interrupt (used for updating the timer list). */ |
bogdanm | 0:eff01767de02 | 27 | |
bogdanm | 0:eff01767de02 | 28 | // The current design assumes that both interrupt handlers run at the same interrupt level. |
bogdanm | 0:eff01767de02 | 29 | // If this is to be changed, protection must be added to prevent them from interrupting each other |
bogdanm | 0:eff01767de02 | 30 | // (e.g. by using guard/trigger flags). |
bogdanm | 0:eff01767de02 | 31 | STATIC_ASSERT(RTC1_IRQ_PRI == SWI0_IRQ_PRI); |
bogdanm | 0:eff01767de02 | 32 | |
bogdanm | 0:eff01767de02 | 33 | #define MAX_RTC_COUNTER_VAL 0x00FFFFFF /**< Maximum value of the RTC counter. */ |
bogdanm | 0:eff01767de02 | 34 | |
bogdanm | 0:eff01767de02 | 35 | #define APP_HIGH_USER_ID 0 /**< User Id for the Application High "user". */ |
bogdanm | 0:eff01767de02 | 36 | #define APP_LOW_USER_ID 1 /**< User Id for the Application Low "user". */ |
bogdanm | 0:eff01767de02 | 37 | #define THREAD_MODE_USER_ID 2 /**< User Id for the Thread Mode "user". */ |
bogdanm | 0:eff01767de02 | 38 | |
bogdanm | 0:eff01767de02 | 39 | #define RTC_COMPARE_OFFSET_MIN 3 /**< Minimum offset between the current RTC counter value and the Capture Compare register. Although the nRF51 Series User Specification recommends this value to be 2, we use 3 to be safer.*/ |
bogdanm | 0:eff01767de02 | 40 | |
bogdanm | 0:eff01767de02 | 41 | #define MAX_RTC_TASKS_DELAY 47 /**< Maximum delay until an RTC task is executed. */ |
bogdanm | 0:eff01767de02 | 42 | |
bogdanm | 0:eff01767de02 | 43 | /**@brief Timer allocation state type. */ |
bogdanm | 0:eff01767de02 | 44 | typedef enum |
bogdanm | 0:eff01767de02 | 45 | { |
bogdanm | 0:eff01767de02 | 46 | STATE_FREE, /**< The timer node is available. */ |
bogdanm | 0:eff01767de02 | 47 | STATE_ALLOCATED /**< The timer node has been allocated. */ |
bogdanm | 0:eff01767de02 | 48 | } timer_alloc_state_t; |
bogdanm | 0:eff01767de02 | 49 | |
bogdanm | 0:eff01767de02 | 50 | /**@brief Timer node type. The nodes will be used form a linked list of running timers. */ |
bogdanm | 0:eff01767de02 | 51 | typedef struct |
bogdanm | 0:eff01767de02 | 52 | { |
bogdanm | 0:eff01767de02 | 53 | timer_alloc_state_t state; /**< Timer allocation state. */ |
bogdanm | 0:eff01767de02 | 54 | app_timer_mode_t mode; /**< Timer mode. */ |
bogdanm | 0:eff01767de02 | 55 | uint32_t ticks_to_expire; /**< Number of ticks from previous timer interrupt to timer expiry. */ |
bogdanm | 0:eff01767de02 | 56 | uint32_t ticks_at_start; /**< Current RTC counter value when the timer was started. */ |
bogdanm | 0:eff01767de02 | 57 | uint32_t ticks_first_interval; /**< Number of ticks in the first timer interval. */ |
bogdanm | 0:eff01767de02 | 58 | uint32_t ticks_periodic_interval; /**< Timer period (for repeating timers). */ |
bogdanm | 0:eff01767de02 | 59 | bool is_running; /**< True if timer is running, False otherwise. */ |
bogdanm | 0:eff01767de02 | 60 | app_timer_timeout_handler_t p_timeout_handler; /**< Pointer to function to be executed when the timer expires. */ |
bogdanm | 0:eff01767de02 | 61 | void * p_context; /**< General purpose pointer. Will be passed to the timeout handler when the timer expires. */ |
bogdanm | 0:eff01767de02 | 62 | app_timer_id_t next; /**< Id of next timer in list of running timers. */ |
bogdanm | 0:eff01767de02 | 63 | } timer_node_t; |
bogdanm | 0:eff01767de02 | 64 | |
bogdanm | 0:eff01767de02 | 65 | STATIC_ASSERT(sizeof(timer_node_t) <= APP_TIMER_NODE_SIZE); |
bogdanm | 0:eff01767de02 | 66 | STATIC_ASSERT(sizeof(timer_node_t) % 4 == 0); |
bogdanm | 0:eff01767de02 | 67 | |
bogdanm | 0:eff01767de02 | 68 | /**@brief Set of available timer operation types. */ |
bogdanm | 0:eff01767de02 | 69 | typedef enum |
bogdanm | 0:eff01767de02 | 70 | { |
bogdanm | 0:eff01767de02 | 71 | TIMER_USER_OP_TYPE_NONE, /**< Invalid timer operation type. */ |
bogdanm | 0:eff01767de02 | 72 | TIMER_USER_OP_TYPE_START, /**< Timer operation type Start. */ |
bogdanm | 0:eff01767de02 | 73 | TIMER_USER_OP_TYPE_STOP, /**< Timer operation type Stop. */ |
bogdanm | 0:eff01767de02 | 74 | TIMER_USER_OP_TYPE_STOP_ALL /**< Timer operation type Stop All. */ |
bogdanm | 0:eff01767de02 | 75 | } timer_user_op_type_t; |
bogdanm | 0:eff01767de02 | 76 | |
bogdanm | 0:eff01767de02 | 77 | /**@brief Structure describing a timer start operation. */ |
bogdanm | 0:eff01767de02 | 78 | typedef struct |
bogdanm | 0:eff01767de02 | 79 | { |
bogdanm | 0:eff01767de02 | 80 | uint32_t ticks_at_start; /**< Current RTC counter value when the timer was started. */ |
bogdanm | 0:eff01767de02 | 81 | uint32_t ticks_first_interval; /**< Number of ticks in the first timer interval. */ |
bogdanm | 0:eff01767de02 | 82 | uint32_t ticks_periodic_interval; /**< Timer period (for repeating timers). */ |
bogdanm | 0:eff01767de02 | 83 | void * p_context; /**< General purpose pointer. Will be passed to the timeout handler when the timer expires. */ |
bogdanm | 0:eff01767de02 | 84 | } timer_user_op_start_t; |
bogdanm | 0:eff01767de02 | 85 | |
bogdanm | 0:eff01767de02 | 86 | /**@brief Structure describing a timer operation. */ |
bogdanm | 0:eff01767de02 | 87 | typedef struct |
bogdanm | 0:eff01767de02 | 88 | { |
bogdanm | 0:eff01767de02 | 89 | timer_user_op_type_t op_type; /**< Timer operation type. */ |
bogdanm | 0:eff01767de02 | 90 | app_timer_id_t timer_id; /**< Id of timer on which the operation is to be performed. */ |
bogdanm | 0:eff01767de02 | 91 | union |
bogdanm | 0:eff01767de02 | 92 | { |
bogdanm | 0:eff01767de02 | 93 | timer_user_op_start_t start; /**< Structure describing a timer start operation. */ |
bogdanm | 0:eff01767de02 | 94 | } params; |
bogdanm | 0:eff01767de02 | 95 | } timer_user_op_t; |
bogdanm | 0:eff01767de02 | 96 | |
bogdanm | 0:eff01767de02 | 97 | STATIC_ASSERT(sizeof(timer_user_op_t) <= APP_TIMER_USER_OP_SIZE); |
bogdanm | 0:eff01767de02 | 98 | STATIC_ASSERT(sizeof(timer_user_op_t) % 4 == 0); |
bogdanm | 0:eff01767de02 | 99 | |
bogdanm | 0:eff01767de02 | 100 | /**@brief Structure describing a timer user. |
bogdanm | 0:eff01767de02 | 101 | * |
bogdanm | 0:eff01767de02 | 102 | * @details For each user of the timer module, there will be a timer operations queue. This queue |
bogdanm | 0:eff01767de02 | 103 | * will hold timer operations issued by this user until the timer interrupt handler |
bogdanm | 0:eff01767de02 | 104 | * processes these operations. For the current implementation, there will be one user for |
bogdanm | 0:eff01767de02 | 105 | * each interrupt level available to the application (APP_HIGH, APP_LOW and THREAD_MODE), |
bogdanm | 0:eff01767de02 | 106 | * but the module can easily be modified to e.g. have one queue per process when using an |
bogdanm | 0:eff01767de02 | 107 | * RTOS. The purpose of the queues is to be able to have a completely lockless timer |
bogdanm | 0:eff01767de02 | 108 | * implementation. |
bogdanm | 0:eff01767de02 | 109 | */ |
bogdanm | 0:eff01767de02 | 110 | typedef struct |
bogdanm | 0:eff01767de02 | 111 | { |
bogdanm | 0:eff01767de02 | 112 | uint8_t first; /**< Index of first entry to have been inserted in the queue (i.e. the next entry to be executed). */ |
bogdanm | 0:eff01767de02 | 113 | uint8_t last; /**< Index of last entry to have been inserted in the queue. */ |
bogdanm | 0:eff01767de02 | 114 | uint8_t user_op_queue_size; /**< Queue size. */ |
bogdanm | 0:eff01767de02 | 115 | timer_user_op_t * p_user_op_queue; /**< Queue buffer. */ |
bogdanm | 0:eff01767de02 | 116 | } timer_user_t; |
bogdanm | 0:eff01767de02 | 117 | |
bogdanm | 0:eff01767de02 | 118 | STATIC_ASSERT(sizeof(timer_user_t) == APP_TIMER_USER_SIZE); |
bogdanm | 0:eff01767de02 | 119 | STATIC_ASSERT(sizeof(timer_user_t) % 4 == 0); |
bogdanm | 0:eff01767de02 | 120 | |
bogdanm | 0:eff01767de02 | 121 | /**@brief User id type. |
bogdanm | 0:eff01767de02 | 122 | * |
bogdanm | 0:eff01767de02 | 123 | * @details In the current implementation, this will automatically be generated from the current |
bogdanm | 0:eff01767de02 | 124 | * interrupt level. |
bogdanm | 0:eff01767de02 | 125 | */ |
bogdanm | 0:eff01767de02 | 126 | typedef uint32_t timer_user_id_t; |
bogdanm | 0:eff01767de02 | 127 | |
bogdanm | 0:eff01767de02 | 128 | #define TIMER_NULL ((app_timer_id_t)(0 - 1)) /**< Invalid timer id. */ |
bogdanm | 0:eff01767de02 | 129 | #define CONTEXT_QUEUE_SIZE_MAX (2) /**< Timer internal elapsed ticks queue size. */ |
bogdanm | 0:eff01767de02 | 130 | |
bogdanm | 0:eff01767de02 | 131 | static uint8_t m_node_array_size; /**< Size of timer node array. */ |
bogdanm | 0:eff01767de02 | 132 | static timer_node_t * mp_nodes = NULL; /**< Array of timer nodes. */ |
bogdanm | 0:eff01767de02 | 133 | static uint8_t m_user_array_size; /**< Size of timer user array. */ |
bogdanm | 0:eff01767de02 | 134 | static timer_user_t * mp_users; /**< Array of timer users. */ |
bogdanm | 0:eff01767de02 | 135 | static app_timer_id_t m_timer_id_head; /**< First timer in list of running timers. */ |
bogdanm | 0:eff01767de02 | 136 | static uint32_t m_ticks_latest; /**< Last known RTC counter value. */ |
bogdanm | 0:eff01767de02 | 137 | static uint32_t m_ticks_elapsed[CONTEXT_QUEUE_SIZE_MAX]; /**< Timer internal elapsed ticks queue. */ |
bogdanm | 0:eff01767de02 | 138 | static uint8_t m_ticks_elapsed_q_read_ind; /**< Timer internal elapsed ticks queue read index. */ |
bogdanm | 0:eff01767de02 | 139 | static uint8_t m_ticks_elapsed_q_write_ind; /**< Timer internal elapsed ticks queue write index. */ |
bogdanm | 0:eff01767de02 | 140 | static app_timer_evt_schedule_func_t m_evt_schedule_func; /**< Pointer to function for propagating timeout events to the scheduler. */ |
bogdanm | 0:eff01767de02 | 141 | |
bogdanm | 0:eff01767de02 | 142 | |
bogdanm | 0:eff01767de02 | 143 | /**@brief Function for initializing the RTC1 counter. |
bogdanm | 0:eff01767de02 | 144 | * |
bogdanm | 0:eff01767de02 | 145 | * @param[in] prescaler Value of the RTC1 PRESCALER register. Set to 0 for no prescaling. |
bogdanm | 0:eff01767de02 | 146 | */ |
bogdanm | 0:eff01767de02 | 147 | static void rtc1_init(uint32_t prescaler) |
bogdanm | 0:eff01767de02 | 148 | { |
bogdanm | 0:eff01767de02 | 149 | NRF_RTC1->PRESCALER = prescaler; |
bogdanm | 0:eff01767de02 | 150 | NVIC_SetPriority(RTC1_IRQn, RTC1_IRQ_PRI); |
bogdanm | 0:eff01767de02 | 151 | } |
bogdanm | 0:eff01767de02 | 152 | |
bogdanm | 0:eff01767de02 | 153 | |
bogdanm | 0:eff01767de02 | 154 | /**@brief Function for starting the RTC1 timer. |
bogdanm | 0:eff01767de02 | 155 | */ |
bogdanm | 0:eff01767de02 | 156 | static void rtc1_start(void) |
bogdanm | 0:eff01767de02 | 157 | { |
bogdanm | 0:eff01767de02 | 158 | NRF_RTC1->EVTENSET = RTC_EVTEN_COMPARE0_Msk; |
bogdanm | 0:eff01767de02 | 159 | NRF_RTC1->INTENSET = RTC_INTENSET_COMPARE0_Msk; |
bogdanm | 0:eff01767de02 | 160 | |
bogdanm | 0:eff01767de02 | 161 | NVIC_ClearPendingIRQ(RTC1_IRQn); |
bogdanm | 0:eff01767de02 | 162 | NVIC_EnableIRQ(RTC1_IRQn); |
bogdanm | 0:eff01767de02 | 163 | |
bogdanm | 0:eff01767de02 | 164 | NRF_RTC1->TASKS_START = 1; |
bogdanm | 0:eff01767de02 | 165 | wait(0.0000001 * MAX_RTC_TASKS_DELAY); |
bogdanm | 0:eff01767de02 | 166 | } |
bogdanm | 0:eff01767de02 | 167 | |
bogdanm | 0:eff01767de02 | 168 | |
bogdanm | 0:eff01767de02 | 169 | /**@brief Function for stopping the RTC1 timer. |
bogdanm | 0:eff01767de02 | 170 | */ |
bogdanm | 0:eff01767de02 | 171 | static void rtc1_stop(void) |
bogdanm | 0:eff01767de02 | 172 | { |
bogdanm | 0:eff01767de02 | 173 | NVIC_DisableIRQ(RTC1_IRQn); |
bogdanm | 0:eff01767de02 | 174 | |
bogdanm | 0:eff01767de02 | 175 | NRF_RTC1->EVTENCLR = RTC_EVTEN_COMPARE0_Msk; |
bogdanm | 0:eff01767de02 | 176 | NRF_RTC1->INTENCLR = RTC_INTENSET_COMPARE0_Msk; |
bogdanm | 0:eff01767de02 | 177 | |
bogdanm | 0:eff01767de02 | 178 | NRF_RTC1->TASKS_STOP = 1; |
bogdanm | 0:eff01767de02 | 179 | wait(0.0000001 * MAX_RTC_TASKS_DELAY); |
bogdanm | 0:eff01767de02 | 180 | } |
bogdanm | 0:eff01767de02 | 181 | |
bogdanm | 0:eff01767de02 | 182 | |
bogdanm | 0:eff01767de02 | 183 | /**@brief Function for returning the current value of the RTC1 counter. |
bogdanm | 0:eff01767de02 | 184 | * |
bogdanm | 0:eff01767de02 | 185 | * @return Current value of the RTC1 counter. |
bogdanm | 0:eff01767de02 | 186 | */ |
bogdanm | 0:eff01767de02 | 187 | static __INLINE uint32_t rtc1_counter_get(void) |
bogdanm | 0:eff01767de02 | 188 | { |
bogdanm | 0:eff01767de02 | 189 | return NRF_RTC1->COUNTER; |
bogdanm | 0:eff01767de02 | 190 | } |
bogdanm | 0:eff01767de02 | 191 | |
bogdanm | 0:eff01767de02 | 192 | |
bogdanm | 0:eff01767de02 | 193 | /**@brief Function for computing the difference between two RTC1 counter values. |
bogdanm | 0:eff01767de02 | 194 | * |
bogdanm | 0:eff01767de02 | 195 | * @return Number of ticks elapsed from ticks_old to ticks_now. |
bogdanm | 0:eff01767de02 | 196 | */ |
bogdanm | 0:eff01767de02 | 197 | static __INLINE uint32_t ticks_diff_get(uint32_t ticks_now, uint32_t ticks_old) |
bogdanm | 0:eff01767de02 | 198 | { |
bogdanm | 0:eff01767de02 | 199 | return ((ticks_now - ticks_old) & MAX_RTC_COUNTER_VAL); |
bogdanm | 0:eff01767de02 | 200 | } |
bogdanm | 0:eff01767de02 | 201 | |
bogdanm | 0:eff01767de02 | 202 | |
bogdanm | 0:eff01767de02 | 203 | /**@brief Function for setting the RTC1 Capture Compare register 0, and enabling the corresponding |
bogdanm | 0:eff01767de02 | 204 | * event. |
bogdanm | 0:eff01767de02 | 205 | * |
bogdanm | 0:eff01767de02 | 206 | * @param[in] value New value of Capture Compare register 0. |
bogdanm | 0:eff01767de02 | 207 | */ |
bogdanm | 0:eff01767de02 | 208 | static __INLINE void rtc1_compare0_set(uint32_t value) |
bogdanm | 0:eff01767de02 | 209 | { |
bogdanm | 0:eff01767de02 | 210 | NRF_RTC1->CC[0] = value; |
bogdanm | 0:eff01767de02 | 211 | } |
bogdanm | 0:eff01767de02 | 212 | |
bogdanm | 0:eff01767de02 | 213 | |
bogdanm | 0:eff01767de02 | 214 | /**@brief Function for inserting a timer in the timer list. |
bogdanm | 0:eff01767de02 | 215 | * |
bogdanm | 0:eff01767de02 | 216 | * @param[in] timer_id Id of timer to insert. |
bogdanm | 0:eff01767de02 | 217 | */ |
bogdanm | 0:eff01767de02 | 218 | static void timer_list_insert(app_timer_id_t timer_id) |
bogdanm | 0:eff01767de02 | 219 | { |
bogdanm | 0:eff01767de02 | 220 | timer_node_t * p_timer = &mp_nodes[timer_id]; |
Rohit Grover |
37:c29c330d942c | 221 | |
bogdanm | 0:eff01767de02 | 222 | if (m_timer_id_head == TIMER_NULL) |
bogdanm | 0:eff01767de02 | 223 | { |
bogdanm | 0:eff01767de02 | 224 | m_timer_id_head = timer_id; |
bogdanm | 0:eff01767de02 | 225 | } |
bogdanm | 0:eff01767de02 | 226 | else |
bogdanm | 0:eff01767de02 | 227 | { |
bogdanm | 0:eff01767de02 | 228 | if (p_timer->ticks_to_expire <= mp_nodes[m_timer_id_head].ticks_to_expire) |
bogdanm | 0:eff01767de02 | 229 | { |
bogdanm | 0:eff01767de02 | 230 | mp_nodes[m_timer_id_head].ticks_to_expire -= p_timer->ticks_to_expire; |
Rohit Grover |
37:c29c330d942c | 231 | |
bogdanm | 0:eff01767de02 | 232 | p_timer->next = m_timer_id_head; |
bogdanm | 0:eff01767de02 | 233 | m_timer_id_head = timer_id; |
bogdanm | 0:eff01767de02 | 234 | } |
bogdanm | 0:eff01767de02 | 235 | else |
bogdanm | 0:eff01767de02 | 236 | { |
bogdanm | 0:eff01767de02 | 237 | app_timer_id_t previous; |
bogdanm | 0:eff01767de02 | 238 | app_timer_id_t current; |
bogdanm | 0:eff01767de02 | 239 | uint32_t ticks_to_expire; |
bogdanm | 0:eff01767de02 | 240 | |
bogdanm | 0:eff01767de02 | 241 | ticks_to_expire = p_timer->ticks_to_expire; |
bogdanm | 0:eff01767de02 | 242 | previous = m_timer_id_head; |
bogdanm | 0:eff01767de02 | 243 | current = m_timer_id_head; |
Rohit Grover |
37:c29c330d942c | 244 | |
bogdanm | 0:eff01767de02 | 245 | while ((current != TIMER_NULL) && (ticks_to_expire > mp_nodes[current].ticks_to_expire)) |
bogdanm | 0:eff01767de02 | 246 | { |
bogdanm | 0:eff01767de02 | 247 | ticks_to_expire -= mp_nodes[current].ticks_to_expire; |
bogdanm | 0:eff01767de02 | 248 | previous = current; |
bogdanm | 0:eff01767de02 | 249 | current = mp_nodes[current].next; |
bogdanm | 0:eff01767de02 | 250 | } |
bogdanm | 0:eff01767de02 | 251 | |
bogdanm | 0:eff01767de02 | 252 | if (current != TIMER_NULL) |
bogdanm | 0:eff01767de02 | 253 | { |
bogdanm | 0:eff01767de02 | 254 | mp_nodes[current].ticks_to_expire -= ticks_to_expire; |
bogdanm | 0:eff01767de02 | 255 | } |
bogdanm | 0:eff01767de02 | 256 | |
bogdanm | 0:eff01767de02 | 257 | p_timer->ticks_to_expire = ticks_to_expire; |
bogdanm | 0:eff01767de02 | 258 | p_timer->next = current; |
bogdanm | 0:eff01767de02 | 259 | mp_nodes[previous].next = timer_id; |
bogdanm | 0:eff01767de02 | 260 | } |
bogdanm | 0:eff01767de02 | 261 | } |
bogdanm | 0:eff01767de02 | 262 | } |
bogdanm | 0:eff01767de02 | 263 | |
bogdanm | 0:eff01767de02 | 264 | |
bogdanm | 0:eff01767de02 | 265 | /**@brief Function for removing a timer from the timer queue. |
bogdanm | 0:eff01767de02 | 266 | * |
bogdanm | 0:eff01767de02 | 267 | * @param[in] timer_id Id of timer to remove. |
bogdanm | 0:eff01767de02 | 268 | */ |
bogdanm | 0:eff01767de02 | 269 | static void timer_list_remove(app_timer_id_t timer_id) |
bogdanm | 0:eff01767de02 | 270 | { |
bogdanm | 0:eff01767de02 | 271 | app_timer_id_t previous; |
bogdanm | 0:eff01767de02 | 272 | app_timer_id_t current; |
bogdanm | 0:eff01767de02 | 273 | uint32_t timeout; |
bogdanm | 0:eff01767de02 | 274 | |
bogdanm | 0:eff01767de02 | 275 | // Find the timer's position in timer list |
bogdanm | 0:eff01767de02 | 276 | previous = m_timer_id_head; |
bogdanm | 0:eff01767de02 | 277 | current = previous; |
Rohit Grover |
37:c29c330d942c | 278 | |
bogdanm | 0:eff01767de02 | 279 | while (current != TIMER_NULL) |
bogdanm | 0:eff01767de02 | 280 | { |
bogdanm | 0:eff01767de02 | 281 | if (current == timer_id) |
bogdanm | 0:eff01767de02 | 282 | { |
bogdanm | 0:eff01767de02 | 283 | break; |
bogdanm | 0:eff01767de02 | 284 | } |
bogdanm | 0:eff01767de02 | 285 | previous = current; |
bogdanm | 0:eff01767de02 | 286 | current = mp_nodes[current].next; |
bogdanm | 0:eff01767de02 | 287 | } |
bogdanm | 0:eff01767de02 | 288 | |
bogdanm | 0:eff01767de02 | 289 | // Timer not in active list |
bogdanm | 0:eff01767de02 | 290 | if (current == TIMER_NULL) |
bogdanm | 0:eff01767de02 | 291 | { |
bogdanm | 0:eff01767de02 | 292 | return; |
bogdanm | 0:eff01767de02 | 293 | } |
bogdanm | 0:eff01767de02 | 294 | |
bogdanm | 0:eff01767de02 | 295 | // Timer is the first in the list |
bogdanm | 0:eff01767de02 | 296 | if (previous == current) |
bogdanm | 0:eff01767de02 | 297 | { |
bogdanm | 0:eff01767de02 | 298 | m_timer_id_head = mp_nodes[m_timer_id_head].next; |
bogdanm | 0:eff01767de02 | 299 | } |
bogdanm | 0:eff01767de02 | 300 | |
bogdanm | 0:eff01767de02 | 301 | // Remaining timeout between next timeout |
bogdanm | 0:eff01767de02 | 302 | timeout = mp_nodes[current].ticks_to_expire; |
bogdanm | 0:eff01767de02 | 303 | |
bogdanm | 0:eff01767de02 | 304 | // Link previous timer with next of this timer, i.e. removing the timer from list |
bogdanm | 0:eff01767de02 | 305 | mp_nodes[previous].next = mp_nodes[current].next; |
bogdanm | 0:eff01767de02 | 306 | |
bogdanm | 0:eff01767de02 | 307 | // If this is not the last timer, increment the next timer by this timer timeout |
bogdanm | 0:eff01767de02 | 308 | current = mp_nodes[previous].next; |
bogdanm | 0:eff01767de02 | 309 | if (current != TIMER_NULL) |
bogdanm | 0:eff01767de02 | 310 | { |
bogdanm | 0:eff01767de02 | 311 | mp_nodes[current].ticks_to_expire += timeout; |
bogdanm | 0:eff01767de02 | 312 | } |
bogdanm | 0:eff01767de02 | 313 | } |
bogdanm | 0:eff01767de02 | 314 | |
bogdanm | 0:eff01767de02 | 315 | |
bogdanm | 0:eff01767de02 | 316 | /**@brief Function for scheduling a check for timeouts by generating a RTC1 interrupt. |
bogdanm | 0:eff01767de02 | 317 | */ |
bogdanm | 0:eff01767de02 | 318 | static void timer_timeouts_check_sched(void) |
bogdanm | 0:eff01767de02 | 319 | { |
bogdanm | 0:eff01767de02 | 320 | NVIC_SetPendingIRQ(RTC1_IRQn); |
bogdanm | 0:eff01767de02 | 321 | } |
bogdanm | 0:eff01767de02 | 322 | |
bogdanm | 0:eff01767de02 | 323 | |
bogdanm | 0:eff01767de02 | 324 | /**@brief Function for scheduling a timer list update by generating a SWI0 interrupt. |
bogdanm | 0:eff01767de02 | 325 | */ |
bogdanm | 0:eff01767de02 | 326 | static void timer_list_handler_sched(void) |
bogdanm | 0:eff01767de02 | 327 | { |
bogdanm | 0:eff01767de02 | 328 | NVIC_SetPendingIRQ(SWI0_IRQn); |
bogdanm | 0:eff01767de02 | 329 | } |
bogdanm | 0:eff01767de02 | 330 | |
bogdanm | 0:eff01767de02 | 331 | |
bogdanm | 0:eff01767de02 | 332 | /**@brief Function for executing an application timeout handler, either by calling it directly, or |
bogdanm | 0:eff01767de02 | 333 | * by passing an event to the @ref app_scheduler. |
bogdanm | 0:eff01767de02 | 334 | * |
bogdanm | 0:eff01767de02 | 335 | * @param[in] p_timer Pointer to expired timer. |
bogdanm | 0:eff01767de02 | 336 | */ |
bogdanm | 0:eff01767de02 | 337 | static void timeout_handler_exec(timer_node_t * p_timer) |
bogdanm | 0:eff01767de02 | 338 | { |
bogdanm | 0:eff01767de02 | 339 | if (m_evt_schedule_func != NULL) |
bogdanm | 0:eff01767de02 | 340 | { |
bogdanm | 0:eff01767de02 | 341 | uint32_t err_code = m_evt_schedule_func(p_timer->p_timeout_handler, p_timer->p_context); |
bogdanm | 0:eff01767de02 | 342 | APP_ERROR_CHECK(err_code); |
bogdanm | 0:eff01767de02 | 343 | } |
bogdanm | 0:eff01767de02 | 344 | else |
bogdanm | 0:eff01767de02 | 345 | { |
bogdanm | 0:eff01767de02 | 346 | p_timer->p_timeout_handler(p_timer->p_context); |
bogdanm | 0:eff01767de02 | 347 | } |
bogdanm | 0:eff01767de02 | 348 | } |
bogdanm | 0:eff01767de02 | 349 | |
bogdanm | 0:eff01767de02 | 350 | |
bogdanm | 0:eff01767de02 | 351 | /**@brief Function for checking for expired timers. |
bogdanm | 0:eff01767de02 | 352 | */ |
bogdanm | 0:eff01767de02 | 353 | static void timer_timeouts_check(void) |
bogdanm | 0:eff01767de02 | 354 | { |
Rohit Grover |
37:c29c330d942c | 355 | // Handle expired of timer |
bogdanm | 0:eff01767de02 | 356 | if (m_timer_id_head != TIMER_NULL) |
bogdanm | 0:eff01767de02 | 357 | { |
bogdanm | 0:eff01767de02 | 358 | app_timer_id_t timer_id; |
bogdanm | 0:eff01767de02 | 359 | uint32_t ticks_elapsed; |
bogdanm | 0:eff01767de02 | 360 | uint32_t ticks_expired; |
bogdanm | 0:eff01767de02 | 361 | |
Rohit Grover |
37:c29c330d942c | 362 | // Initialize actual elapsed ticks being consumed to 0 |
bogdanm | 0:eff01767de02 | 363 | ticks_expired = 0; |
bogdanm | 0:eff01767de02 | 364 | |
bogdanm | 0:eff01767de02 | 365 | // ticks_elapsed is collected here, job will use it |
bogdanm | 0:eff01767de02 | 366 | ticks_elapsed = ticks_diff_get(rtc1_counter_get(), m_ticks_latest); |
bogdanm | 0:eff01767de02 | 367 | |
Rohit Grover |
37:c29c330d942c | 368 | // Auto variable containing the head of timers expiring |
bogdanm | 0:eff01767de02 | 369 | timer_id = m_timer_id_head; |
bogdanm | 0:eff01767de02 | 370 | |
Rohit Grover |
37:c29c330d942c | 371 | // Expire all timers within ticks_elapsed and collect ticks_expired |
bogdanm | 0:eff01767de02 | 372 | while (timer_id != TIMER_NULL) |
bogdanm | 0:eff01767de02 | 373 | { |
bogdanm | 0:eff01767de02 | 374 | timer_node_t * p_timer; |
bogdanm | 0:eff01767de02 | 375 | |
Rohit Grover |
37:c29c330d942c | 376 | // Auto variable for current timer node |
bogdanm | 0:eff01767de02 | 377 | p_timer = &mp_nodes[timer_id]; |
bogdanm | 0:eff01767de02 | 378 | |
Rohit Grover |
37:c29c330d942c | 379 | // Do nothing if timer did not expire |
bogdanm | 0:eff01767de02 | 380 | if (ticks_elapsed < p_timer->ticks_to_expire) |
bogdanm | 0:eff01767de02 | 381 | { |
bogdanm | 0:eff01767de02 | 382 | break; |
bogdanm | 0:eff01767de02 | 383 | } |
bogdanm | 0:eff01767de02 | 384 | |
Rohit Grover |
37:c29c330d942c | 385 | // Decrement ticks_elapsed and collect expired ticks |
bogdanm | 0:eff01767de02 | 386 | ticks_elapsed -= p_timer->ticks_to_expire; |
bogdanm | 0:eff01767de02 | 387 | ticks_expired += p_timer->ticks_to_expire; |
bogdanm | 0:eff01767de02 | 388 | |
Rohit Grover |
37:c29c330d942c | 389 | // Move to next timer |
bogdanm | 0:eff01767de02 | 390 | timer_id = p_timer->next; |
bogdanm | 0:eff01767de02 | 391 | |
Rohit Grover |
37:c29c330d942c | 392 | // Execute Task |
bogdanm | 0:eff01767de02 | 393 | timeout_handler_exec(p_timer); |
bogdanm | 0:eff01767de02 | 394 | } |
bogdanm | 0:eff01767de02 | 395 | |
bogdanm | 0:eff01767de02 | 396 | // Prepare to queue the ticks expired in the m_ticks_elapsed queue. |
bogdanm | 0:eff01767de02 | 397 | if (m_ticks_elapsed_q_read_ind == m_ticks_elapsed_q_write_ind) |
bogdanm | 0:eff01767de02 | 398 | { |
bogdanm | 0:eff01767de02 | 399 | // The read index of the queue is equal to the write index. This means the new |
bogdanm | 0:eff01767de02 | 400 | // value of ticks_expired should be stored at a new location in the m_ticks_elapsed |
bogdanm | 0:eff01767de02 | 401 | // queue (which is implemented as a double buffer). |
bogdanm | 0:eff01767de02 | 402 | |
bogdanm | 0:eff01767de02 | 403 | // Check if there will be a queue overflow. |
bogdanm | 0:eff01767de02 | 404 | if (++m_ticks_elapsed_q_write_ind == CONTEXT_QUEUE_SIZE_MAX) |
bogdanm | 0:eff01767de02 | 405 | { |
bogdanm | 0:eff01767de02 | 406 | // There will be a queue overflow. Hence the write index should point to the start |
bogdanm | 0:eff01767de02 | 407 | // of the queue. |
bogdanm | 0:eff01767de02 | 408 | m_ticks_elapsed_q_write_ind = 0; |
bogdanm | 0:eff01767de02 | 409 | } |
bogdanm | 0:eff01767de02 | 410 | } |
bogdanm | 0:eff01767de02 | 411 | |
bogdanm | 0:eff01767de02 | 412 | // Queue the ticks expired. |
bogdanm | 0:eff01767de02 | 413 | m_ticks_elapsed[m_ticks_elapsed_q_write_ind] = ticks_expired; |
bogdanm | 0:eff01767de02 | 414 | |
bogdanm | 0:eff01767de02 | 415 | timer_list_handler_sched(); |
bogdanm | 0:eff01767de02 | 416 | } |
bogdanm | 0:eff01767de02 | 417 | } |
bogdanm | 0:eff01767de02 | 418 | |
bogdanm | 0:eff01767de02 | 419 | |
bogdanm | 0:eff01767de02 | 420 | /**@brief Function for acquiring the number of ticks elapsed. |
bogdanm | 0:eff01767de02 | 421 | * |
bogdanm | 0:eff01767de02 | 422 | * @param[out] p_ticks_elapsed Number of ticks elapsed. |
bogdanm | 0:eff01767de02 | 423 | * |
bogdanm | 0:eff01767de02 | 424 | * @return TRUE if elapsed ticks was read from queue, FALSE otherwise. |
bogdanm | 0:eff01767de02 | 425 | */ |
bogdanm | 0:eff01767de02 | 426 | static bool elapsed_ticks_acquire(uint32_t * p_ticks_elapsed) |
bogdanm | 0:eff01767de02 | 427 | { |
Rohit Grover |
37:c29c330d942c | 428 | // Pick the elapsed value from queue |
bogdanm | 0:eff01767de02 | 429 | if (m_ticks_elapsed_q_read_ind != m_ticks_elapsed_q_write_ind) |
bogdanm | 0:eff01767de02 | 430 | { |
Rohit Grover |
37:c29c330d942c | 431 | // Dequeue elapsed value |
bogdanm | 0:eff01767de02 | 432 | m_ticks_elapsed_q_read_ind++; |
bogdanm | 0:eff01767de02 | 433 | if (m_ticks_elapsed_q_read_ind == CONTEXT_QUEUE_SIZE_MAX) |
bogdanm | 0:eff01767de02 | 434 | { |
bogdanm | 0:eff01767de02 | 435 | m_ticks_elapsed_q_read_ind = 0; |
bogdanm | 0:eff01767de02 | 436 | } |
bogdanm | 0:eff01767de02 | 437 | |
bogdanm | 0:eff01767de02 | 438 | *p_ticks_elapsed = m_ticks_elapsed[m_ticks_elapsed_q_read_ind]; |
bogdanm | 0:eff01767de02 | 439 | |
bogdanm | 0:eff01767de02 | 440 | m_ticks_latest += *p_ticks_elapsed; |
bogdanm | 0:eff01767de02 | 441 | m_ticks_latest &= MAX_RTC_COUNTER_VAL; |
bogdanm | 0:eff01767de02 | 442 | |
bogdanm | 0:eff01767de02 | 443 | return true; |
bogdanm | 0:eff01767de02 | 444 | } |
bogdanm | 0:eff01767de02 | 445 | else |
bogdanm | 0:eff01767de02 | 446 | { |
Rohit Grover |
37:c29c330d942c | 447 | // No elapsed value in queue |
bogdanm | 0:eff01767de02 | 448 | *p_ticks_elapsed = 0; |
bogdanm | 0:eff01767de02 | 449 | return false; |
bogdanm | 0:eff01767de02 | 450 | } |
bogdanm | 0:eff01767de02 | 451 | } |
bogdanm | 0:eff01767de02 | 452 | |
bogdanm | 0:eff01767de02 | 453 | |
bogdanm | 0:eff01767de02 | 454 | /**@brief Function for handling the timer list deletions. |
bogdanm | 0:eff01767de02 | 455 | * |
bogdanm | 0:eff01767de02 | 456 | * @return TRUE if Capture Compare register must be updated, FALSE otherwise. |
bogdanm | 0:eff01767de02 | 457 | */ |
bogdanm | 0:eff01767de02 | 458 | static bool list_deletions_handler(void) |
bogdanm | 0:eff01767de02 | 459 | { |
bogdanm | 0:eff01767de02 | 460 | app_timer_id_t timer_id_old_head; |
bogdanm | 0:eff01767de02 | 461 | uint8_t user_id; |
bogdanm | 0:eff01767de02 | 462 | |
bogdanm | 0:eff01767de02 | 463 | // Remember the old head, so as to decide if new compare needs to be set |
bogdanm | 0:eff01767de02 | 464 | timer_id_old_head = m_timer_id_head; |
bogdanm | 0:eff01767de02 | 465 | |
bogdanm | 0:eff01767de02 | 466 | user_id = m_user_array_size; |
bogdanm | 0:eff01767de02 | 467 | while (user_id--) |
bogdanm | 0:eff01767de02 | 468 | { |
bogdanm | 0:eff01767de02 | 469 | timer_user_t * p_user = &mp_users[user_id]; |
bogdanm | 0:eff01767de02 | 470 | uint8_t user_ops_first = p_user->first; |
Rohit Grover |
37:c29c330d942c | 471 | |
bogdanm | 0:eff01767de02 | 472 | while (user_ops_first != p_user->last) |
bogdanm | 0:eff01767de02 | 473 | { |
bogdanm | 0:eff01767de02 | 474 | timer_node_t * p_timer; |
bogdanm | 0:eff01767de02 | 475 | timer_user_op_t * p_user_op = &p_user->p_user_op_queue[user_ops_first]; |
bogdanm | 0:eff01767de02 | 476 | |
Rohit Grover |
37:c29c330d942c | 477 | // Traverse to next operation in queue |
bogdanm | 0:eff01767de02 | 478 | user_ops_first++; |
bogdanm | 0:eff01767de02 | 479 | if (user_ops_first == p_user->user_op_queue_size) |
bogdanm | 0:eff01767de02 | 480 | { |
bogdanm | 0:eff01767de02 | 481 | user_ops_first = 0; |
bogdanm | 0:eff01767de02 | 482 | } |
bogdanm | 0:eff01767de02 | 483 | |
bogdanm | 0:eff01767de02 | 484 | switch (p_user_op->op_type) |
bogdanm | 0:eff01767de02 | 485 | { |
bogdanm | 0:eff01767de02 | 486 | case TIMER_USER_OP_TYPE_STOP: |
bogdanm | 0:eff01767de02 | 487 | // Delete node if timer is running |
bogdanm | 0:eff01767de02 | 488 | p_timer = &mp_nodes[p_user_op->timer_id]; |
bogdanm | 0:eff01767de02 | 489 | if (p_timer->is_running) |
bogdanm | 0:eff01767de02 | 490 | { |
bogdanm | 0:eff01767de02 | 491 | timer_list_remove(p_user_op->timer_id); |
bogdanm | 0:eff01767de02 | 492 | p_timer->is_running = false; |
bogdanm | 0:eff01767de02 | 493 | } |
bogdanm | 0:eff01767de02 | 494 | break; |
Rohit Grover |
37:c29c330d942c | 495 | |
bogdanm | 0:eff01767de02 | 496 | case TIMER_USER_OP_TYPE_STOP_ALL: |
bogdanm | 0:eff01767de02 | 497 | // Delete list of running timers, and mark all timers as not running |
bogdanm | 0:eff01767de02 | 498 | while (m_timer_id_head != TIMER_NULL) |
bogdanm | 0:eff01767de02 | 499 | { |
bogdanm | 0:eff01767de02 | 500 | timer_node_t * p_head = &mp_nodes[m_timer_id_head]; |
bogdanm | 0:eff01767de02 | 501 | |
bogdanm | 0:eff01767de02 | 502 | p_head->is_running = false; |
bogdanm | 0:eff01767de02 | 503 | m_timer_id_head = p_head->next; |
bogdanm | 0:eff01767de02 | 504 | } |
bogdanm | 0:eff01767de02 | 505 | break; |
Rohit Grover |
37:c29c330d942c | 506 | |
bogdanm | 0:eff01767de02 | 507 | default: |
bogdanm | 0:eff01767de02 | 508 | // No implementation needed. |
bogdanm | 0:eff01767de02 | 509 | break; |
bogdanm | 0:eff01767de02 | 510 | } |
bogdanm | 0:eff01767de02 | 511 | } |
bogdanm | 0:eff01767de02 | 512 | } |
bogdanm | 0:eff01767de02 | 513 | |
bogdanm | 0:eff01767de02 | 514 | // Detect change in head of the list |
bogdanm | 0:eff01767de02 | 515 | return (m_timer_id_head != timer_id_old_head); |
bogdanm | 0:eff01767de02 | 516 | } |
bogdanm | 0:eff01767de02 | 517 | |
bogdanm | 0:eff01767de02 | 518 | |
bogdanm | 0:eff01767de02 | 519 | /**@brief Function for updating the timer list for expired timers. |
bogdanm | 0:eff01767de02 | 520 | * |
bogdanm | 0:eff01767de02 | 521 | * @param[in] ticks_elapsed Number of elapsed ticks. |
bogdanm | 0:eff01767de02 | 522 | * @param[in] ticks_previous Previous known value of the RTC counter. |
bogdanm | 0:eff01767de02 | 523 | * @param[out] p_restart_list_head List of repeating timers to be restarted. |
bogdanm | 0:eff01767de02 | 524 | */ |
bogdanm | 0:eff01767de02 | 525 | static void expired_timers_handler(uint32_t ticks_elapsed, |
bogdanm | 0:eff01767de02 | 526 | uint32_t ticks_previous, |
bogdanm | 0:eff01767de02 | 527 | app_timer_id_t * p_restart_list_head) |
bogdanm | 0:eff01767de02 | 528 | { |
bogdanm | 0:eff01767de02 | 529 | uint32_t ticks_expired = 0; |
bogdanm | 0:eff01767de02 | 530 | |
bogdanm | 0:eff01767de02 | 531 | while (m_timer_id_head != TIMER_NULL) |
bogdanm | 0:eff01767de02 | 532 | { |
bogdanm | 0:eff01767de02 | 533 | timer_node_t * p_timer; |
bogdanm | 0:eff01767de02 | 534 | app_timer_id_t id_expired; |
bogdanm | 0:eff01767de02 | 535 | |
Rohit Grover |
37:c29c330d942c | 536 | // Auto variable for current timer node |
bogdanm | 0:eff01767de02 | 537 | p_timer = &mp_nodes[m_timer_id_head]; |
bogdanm | 0:eff01767de02 | 538 | |
Rohit Grover |
37:c29c330d942c | 539 | // Do nothing if timer did not expire |
bogdanm | 0:eff01767de02 | 540 | if (ticks_elapsed < p_timer->ticks_to_expire) |
bogdanm | 0:eff01767de02 | 541 | { |
bogdanm | 0:eff01767de02 | 542 | p_timer->ticks_to_expire -= ticks_elapsed; |
bogdanm | 0:eff01767de02 | 543 | break; |
bogdanm | 0:eff01767de02 | 544 | } |
bogdanm | 0:eff01767de02 | 545 | |
Rohit Grover |
37:c29c330d942c | 546 | // Decrement ticks_elapsed and collect expired ticks |
bogdanm | 0:eff01767de02 | 547 | ticks_elapsed -= p_timer->ticks_to_expire; |
bogdanm | 0:eff01767de02 | 548 | ticks_expired += p_timer->ticks_to_expire; |
bogdanm | 0:eff01767de02 | 549 | |
bogdanm | 0:eff01767de02 | 550 | // Timer expired, set ticks_to_expire zero |
bogdanm | 0:eff01767de02 | 551 | p_timer->ticks_to_expire = 0; |
bogdanm | 0:eff01767de02 | 552 | p_timer->is_running = false; |
bogdanm | 0:eff01767de02 | 553 | |
Rohit Grover |
37:c29c330d942c | 554 | // Remove the expired timer from head |
bogdanm | 0:eff01767de02 | 555 | id_expired = m_timer_id_head; |
bogdanm | 0:eff01767de02 | 556 | m_timer_id_head = p_timer->next; |
bogdanm | 0:eff01767de02 | 557 | |
Rohit Grover |
37:c29c330d942c | 558 | // Timer will be restarted if periodic |
bogdanm | 0:eff01767de02 | 559 | if (p_timer->ticks_periodic_interval != 0) |
bogdanm | 0:eff01767de02 | 560 | { |
bogdanm | 0:eff01767de02 | 561 | p_timer->ticks_at_start = (ticks_previous + ticks_expired) & MAX_RTC_COUNTER_VAL; |
bogdanm | 0:eff01767de02 | 562 | p_timer->ticks_first_interval = p_timer->ticks_periodic_interval; |
bogdanm | 0:eff01767de02 | 563 | p_timer->next = *p_restart_list_head; |
bogdanm | 0:eff01767de02 | 564 | *p_restart_list_head = id_expired; |
bogdanm | 0:eff01767de02 | 565 | } |
bogdanm | 0:eff01767de02 | 566 | } |
bogdanm | 0:eff01767de02 | 567 | } |
bogdanm | 0:eff01767de02 | 568 | |
bogdanm | 0:eff01767de02 | 569 | |
bogdanm | 0:eff01767de02 | 570 | /**@brief Function for handling timer list insertions. |
bogdanm | 0:eff01767de02 | 571 | * |
bogdanm | 0:eff01767de02 | 572 | * @param[in] p_restart_list_head List of repeating timers to be restarted. |
bogdanm | 0:eff01767de02 | 573 | * |
bogdanm | 0:eff01767de02 | 574 | * @return TRUE if Capture Compare register must be updated, FALSE otherwise. |
bogdanm | 0:eff01767de02 | 575 | */ |
bogdanm | 0:eff01767de02 | 576 | static bool list_insertions_handler(app_timer_id_t restart_list_head) |
bogdanm | 0:eff01767de02 | 577 | { |
bogdanm | 0:eff01767de02 | 578 | app_timer_id_t timer_id_old_head; |
bogdanm | 0:eff01767de02 | 579 | uint8_t user_id; |
bogdanm | 0:eff01767de02 | 580 | |
bogdanm | 0:eff01767de02 | 581 | // Remember the old head, so as to decide if new compare needs to be set |
bogdanm | 0:eff01767de02 | 582 | timer_id_old_head = m_timer_id_head; |
bogdanm | 0:eff01767de02 | 583 | |
bogdanm | 0:eff01767de02 | 584 | user_id = m_user_array_size; |
bogdanm | 0:eff01767de02 | 585 | while (user_id--) |
bogdanm | 0:eff01767de02 | 586 | { |
bogdanm | 0:eff01767de02 | 587 | timer_user_t * p_user = &mp_users[user_id]; |
bogdanm | 0:eff01767de02 | 588 | |
Rohit Grover |
37:c29c330d942c | 589 | // Handle insertions of timers |
bogdanm | 0:eff01767de02 | 590 | while ((restart_list_head != TIMER_NULL) || (p_user->first != p_user->last)) |
bogdanm | 0:eff01767de02 | 591 | { |
bogdanm | 0:eff01767de02 | 592 | app_timer_id_t id_start; |
bogdanm | 0:eff01767de02 | 593 | timer_node_t * p_timer; |
bogdanm | 0:eff01767de02 | 594 | |
bogdanm | 0:eff01767de02 | 595 | if (restart_list_head != TIMER_NULL) |
bogdanm | 0:eff01767de02 | 596 | { |
bogdanm | 0:eff01767de02 | 597 | id_start = restart_list_head; |
bogdanm | 0:eff01767de02 | 598 | p_timer = &mp_nodes[id_start]; |
bogdanm | 0:eff01767de02 | 599 | restart_list_head = p_timer->next; |
bogdanm | 0:eff01767de02 | 600 | } |
bogdanm | 0:eff01767de02 | 601 | else |
bogdanm | 0:eff01767de02 | 602 | { |
bogdanm | 0:eff01767de02 | 603 | timer_user_op_t * p_user_op = &p_user->p_user_op_queue[p_user->first]; |
bogdanm | 0:eff01767de02 | 604 | |
bogdanm | 0:eff01767de02 | 605 | p_user->first++; |
bogdanm | 0:eff01767de02 | 606 | if (p_user->first == p_user->user_op_queue_size) |
bogdanm | 0:eff01767de02 | 607 | { |
bogdanm | 0:eff01767de02 | 608 | p_user->first = 0; |
bogdanm | 0:eff01767de02 | 609 | } |
bogdanm | 0:eff01767de02 | 610 | |
bogdanm | 0:eff01767de02 | 611 | id_start = p_user_op->timer_id; |
bogdanm | 0:eff01767de02 | 612 | p_timer = &mp_nodes[id_start]; |
bogdanm | 0:eff01767de02 | 613 | |
bogdanm | 0:eff01767de02 | 614 | if ((p_user_op->op_type != TIMER_USER_OP_TYPE_START) || p_timer->is_running) |
bogdanm | 0:eff01767de02 | 615 | { |
bogdanm | 0:eff01767de02 | 616 | continue; |
bogdanm | 0:eff01767de02 | 617 | } |
bogdanm | 0:eff01767de02 | 618 | |
bogdanm | 0:eff01767de02 | 619 | p_timer->ticks_at_start = p_user_op->params.start.ticks_at_start; |
bogdanm | 0:eff01767de02 | 620 | p_timer->ticks_first_interval = p_user_op->params.start.ticks_first_interval; |
bogdanm | 0:eff01767de02 | 621 | p_timer->ticks_periodic_interval = p_user_op->params.start.ticks_periodic_interval; |
bogdanm | 0:eff01767de02 | 622 | p_timer->p_context = p_user_op->params.start.p_context; |
bogdanm | 0:eff01767de02 | 623 | } |
bogdanm | 0:eff01767de02 | 624 | |
Rohit Grover |
37:c29c330d942c | 625 | // Prepare the node to be inserted |
bogdanm | 0:eff01767de02 | 626 | if ( |
bogdanm | 0:eff01767de02 | 627 | ((p_timer->ticks_at_start - m_ticks_latest) & MAX_RTC_COUNTER_VAL) |
bogdanm | 0:eff01767de02 | 628 | < |
bogdanm | 0:eff01767de02 | 629 | (MAX_RTC_COUNTER_VAL / 2) |
bogdanm | 0:eff01767de02 | 630 | ) |
bogdanm | 0:eff01767de02 | 631 | { |
Rohit Grover |
37:c29c330d942c | 632 | p_timer->ticks_to_expire = ticks_diff_get(p_timer->ticks_at_start, m_ticks_latest) + |
bogdanm | 0:eff01767de02 | 633 | p_timer->ticks_first_interval; |
bogdanm | 0:eff01767de02 | 634 | } |
bogdanm | 0:eff01767de02 | 635 | else |
bogdanm | 0:eff01767de02 | 636 | { |
bogdanm | 0:eff01767de02 | 637 | uint32_t delta_current_start; |
bogdanm | 0:eff01767de02 | 638 | |
bogdanm | 0:eff01767de02 | 639 | delta_current_start = ticks_diff_get(m_ticks_latest, p_timer->ticks_at_start); |
bogdanm | 0:eff01767de02 | 640 | if (p_timer->ticks_first_interval > delta_current_start) |
bogdanm | 0:eff01767de02 | 641 | { |
bogdanm | 0:eff01767de02 | 642 | p_timer->ticks_to_expire = p_timer->ticks_first_interval - delta_current_start; |
bogdanm | 0:eff01767de02 | 643 | } |
bogdanm | 0:eff01767de02 | 644 | else |
bogdanm | 0:eff01767de02 | 645 | { |
bogdanm | 0:eff01767de02 | 646 | p_timer->ticks_to_expire = 0; |
bogdanm | 0:eff01767de02 | 647 | } |
bogdanm | 0:eff01767de02 | 648 | } |
bogdanm | 0:eff01767de02 | 649 | |
bogdanm | 0:eff01767de02 | 650 | p_timer->ticks_at_start = 0; |
bogdanm | 0:eff01767de02 | 651 | p_timer->ticks_first_interval = 0; |
bogdanm | 0:eff01767de02 | 652 | p_timer->is_running = true; |
bogdanm | 0:eff01767de02 | 653 | p_timer->next = TIMER_NULL; |
bogdanm | 0:eff01767de02 | 654 | |
Rohit Grover |
37:c29c330d942c | 655 | // Insert into list |
bogdanm | 0:eff01767de02 | 656 | timer_list_insert(id_start); |
bogdanm | 0:eff01767de02 | 657 | } |
bogdanm | 0:eff01767de02 | 658 | } |
Rohit Grover |
37:c29c330d942c | 659 | |
bogdanm | 0:eff01767de02 | 660 | return (m_timer_id_head != timer_id_old_head); |
bogdanm | 0:eff01767de02 | 661 | } |
bogdanm | 0:eff01767de02 | 662 | |
bogdanm | 0:eff01767de02 | 663 | |
bogdanm | 0:eff01767de02 | 664 | /**@brief Function for updating the Capture Compare register. |
bogdanm | 0:eff01767de02 | 665 | */ |
bogdanm | 0:eff01767de02 | 666 | static void compare_reg_update(app_timer_id_t timer_id_head_old) |
bogdanm | 0:eff01767de02 | 667 | { |
Rohit Grover |
37:c29c330d942c | 668 | // Setup the timeout for timers on the head of the list |
bogdanm | 0:eff01767de02 | 669 | if (m_timer_id_head != TIMER_NULL) |
bogdanm | 0:eff01767de02 | 670 | { |
bogdanm | 0:eff01767de02 | 671 | uint32_t ticks_to_expire = mp_nodes[m_timer_id_head].ticks_to_expire; |
bogdanm | 0:eff01767de02 | 672 | uint32_t pre_counter_val = rtc1_counter_get(); |
bogdanm | 0:eff01767de02 | 673 | uint32_t cc = m_ticks_latest; |
bogdanm | 0:eff01767de02 | 674 | uint32_t ticks_elapsed = ticks_diff_get(pre_counter_val, cc) + RTC_COMPARE_OFFSET_MIN; |
bogdanm | 0:eff01767de02 | 675 | |
bogdanm | 0:eff01767de02 | 676 | if (timer_id_head_old == TIMER_NULL) |
bogdanm | 0:eff01767de02 | 677 | { |
bogdanm | 0:eff01767de02 | 678 | // No timers were already running, start RTC |
bogdanm | 0:eff01767de02 | 679 | rtc1_start(); |
bogdanm | 0:eff01767de02 | 680 | } |
bogdanm | 0:eff01767de02 | 681 | |
bogdanm | 0:eff01767de02 | 682 | cc += (ticks_elapsed < ticks_to_expire) ? ticks_to_expire : ticks_elapsed; |
bogdanm | 0:eff01767de02 | 683 | cc &= MAX_RTC_COUNTER_VAL; |
Rohit Grover |
37:c29c330d942c | 684 | |
bogdanm | 0:eff01767de02 | 685 | rtc1_compare0_set(cc); |
bogdanm | 0:eff01767de02 | 686 | |
bogdanm | 0:eff01767de02 | 687 | uint32_t post_counter_val = rtc1_counter_get(); |
bogdanm | 0:eff01767de02 | 688 | |
bogdanm | 0:eff01767de02 | 689 | if ( |
bogdanm | 0:eff01767de02 | 690 | (ticks_diff_get(post_counter_val, pre_counter_val) + RTC_COMPARE_OFFSET_MIN) |
bogdanm | 0:eff01767de02 | 691 | > |
bogdanm | 0:eff01767de02 | 692 | ticks_diff_get(cc, pre_counter_val) |
bogdanm | 0:eff01767de02 | 693 | ) |
bogdanm | 0:eff01767de02 | 694 | { |
bogdanm | 0:eff01767de02 | 695 | // When this happens the COMPARE event may not be triggered by the RTC. |
bogdanm | 0:eff01767de02 | 696 | // The nRF51 Series User Specification states that if the COUNTER value is N |
bogdanm | 0:eff01767de02 | 697 | // (i.e post_counter_val = N), writing N or N+1 to a CC register may not trigger a |
bogdanm | 0:eff01767de02 | 698 | // COMPARE event. Hence the RTC interrupt is forcefully pended by calling the following |
bogdanm | 0:eff01767de02 | 699 | // function. |
bogdanm | 0:eff01767de02 | 700 | timer_timeouts_check_sched(); |
bogdanm | 0:eff01767de02 | 701 | } |
bogdanm | 0:eff01767de02 | 702 | } |
bogdanm | 0:eff01767de02 | 703 | else |
bogdanm | 0:eff01767de02 | 704 | { |
bogdanm | 0:eff01767de02 | 705 | // No timers are running, stop RTC |
bogdanm | 0:eff01767de02 | 706 | rtc1_stop(); |
bogdanm | 0:eff01767de02 | 707 | } |
bogdanm | 0:eff01767de02 | 708 | } |
bogdanm | 0:eff01767de02 | 709 | |
bogdanm | 0:eff01767de02 | 710 | |
bogdanm | 0:eff01767de02 | 711 | /**@brief Function for handling changes to the timer list. |
bogdanm | 0:eff01767de02 | 712 | */ |
bogdanm | 0:eff01767de02 | 713 | static void timer_list_handler(void) |
bogdanm | 0:eff01767de02 | 714 | { |
bogdanm | 0:eff01767de02 | 715 | app_timer_id_t restart_list_head = TIMER_NULL; |
bogdanm | 0:eff01767de02 | 716 | uint32_t ticks_elapsed; |
bogdanm | 0:eff01767de02 | 717 | uint32_t ticks_previous; |
bogdanm | 0:eff01767de02 | 718 | bool ticks_have_elapsed; |
bogdanm | 0:eff01767de02 | 719 | bool compare_update; |
bogdanm | 0:eff01767de02 | 720 | app_timer_id_t timer_id_head_old; |
Rohit Grover |
37:c29c330d942c | 721 | |
bogdanm | 0:eff01767de02 | 722 | // Back up the previous known tick and previous list head |
bogdanm | 0:eff01767de02 | 723 | ticks_previous = m_ticks_latest; |
bogdanm | 0:eff01767de02 | 724 | timer_id_head_old = m_timer_id_head; |
Rohit Grover |
37:c29c330d942c | 725 | |
bogdanm | 0:eff01767de02 | 726 | // Get number of elapsed ticks |
bogdanm | 0:eff01767de02 | 727 | ticks_have_elapsed = elapsed_ticks_acquire(&ticks_elapsed); |
bogdanm | 0:eff01767de02 | 728 | |
bogdanm | 0:eff01767de02 | 729 | // Handle list deletions |
bogdanm | 0:eff01767de02 | 730 | compare_update = list_deletions_handler(); |
Rohit Grover |
37:c29c330d942c | 731 | |
bogdanm | 0:eff01767de02 | 732 | // Handle expired timers |
bogdanm | 0:eff01767de02 | 733 | if (ticks_have_elapsed) |
bogdanm | 0:eff01767de02 | 734 | { |
bogdanm | 0:eff01767de02 | 735 | expired_timers_handler(ticks_elapsed, ticks_previous, &restart_list_head); |
bogdanm | 0:eff01767de02 | 736 | compare_update = true; |
bogdanm | 0:eff01767de02 | 737 | } |
Rohit Grover |
37:c29c330d942c | 738 | |
bogdanm | 0:eff01767de02 | 739 | // Handle list insertions |
bogdanm | 0:eff01767de02 | 740 | if (list_insertions_handler(restart_list_head)) |
bogdanm | 0:eff01767de02 | 741 | { |
bogdanm | 0:eff01767de02 | 742 | compare_update = true; |
bogdanm | 0:eff01767de02 | 743 | } |
bogdanm | 0:eff01767de02 | 744 | |
bogdanm | 0:eff01767de02 | 745 | // Update compare register if necessary |
bogdanm | 0:eff01767de02 | 746 | if (compare_update) |
bogdanm | 0:eff01767de02 | 747 | { |
bogdanm | 0:eff01767de02 | 748 | compare_reg_update(timer_id_head_old); |
bogdanm | 0:eff01767de02 | 749 | } |
bogdanm | 0:eff01767de02 | 750 | } |
bogdanm | 0:eff01767de02 | 751 | |
bogdanm | 0:eff01767de02 | 752 | |
bogdanm | 0:eff01767de02 | 753 | /**@brief Function for enqueueing a new operations queue entry. |
bogdanm | 0:eff01767de02 | 754 | * |
bogdanm | 0:eff01767de02 | 755 | * @param[in] p_user User that the entry is to be enqueued for. |
bogdanm | 0:eff01767de02 | 756 | * @param[in] last_index Index of the next last index to be enqueued. |
bogdanm | 0:eff01767de02 | 757 | */ |
bogdanm | 0:eff01767de02 | 758 | static void user_op_enque(timer_user_t * p_user, app_timer_id_t last_index) |
bogdanm | 0:eff01767de02 | 759 | { |
bogdanm | 0:eff01767de02 | 760 | p_user->last = last_index; |
bogdanm | 0:eff01767de02 | 761 | } |
bogdanm | 0:eff01767de02 | 762 | |
bogdanm | 0:eff01767de02 | 763 | |
bogdanm | 0:eff01767de02 | 764 | /**@brief Function for allocating a new operations queue entry. |
bogdanm | 0:eff01767de02 | 765 | * |
bogdanm | 0:eff01767de02 | 766 | * @param[in] p_user User that the entry is to be allocated for. |
bogdanm | 0:eff01767de02 | 767 | * @param[out] p_last_index Index of the next last index to be enqueued. |
bogdanm | 0:eff01767de02 | 768 | * |
bogdanm | 0:eff01767de02 | 769 | * @return Pointer to allocated queue entry, or NULL if queue is full. |
bogdanm | 0:eff01767de02 | 770 | */ |
bogdanm | 0:eff01767de02 | 771 | static timer_user_op_t * user_op_alloc(timer_user_t * p_user, app_timer_id_t * p_last_index) |
Rohit Grover |
37:c29c330d942c | 772 | { |
bogdanm | 0:eff01767de02 | 773 | app_timer_id_t last; |
bogdanm | 0:eff01767de02 | 774 | timer_user_op_t * p_user_op; |
Rohit Grover |
37:c29c330d942c | 775 | |
bogdanm | 0:eff01767de02 | 776 | last = p_user->last + 1; |
bogdanm | 0:eff01767de02 | 777 | if (last == p_user->user_op_queue_size) |
bogdanm | 0:eff01767de02 | 778 | { |
bogdanm | 0:eff01767de02 | 779 | // Overflow case. |
bogdanm | 0:eff01767de02 | 780 | last = 0; |
bogdanm | 0:eff01767de02 | 781 | } |
bogdanm | 0:eff01767de02 | 782 | if (last == p_user->first) |
bogdanm | 0:eff01767de02 | 783 | { |
bogdanm | 0:eff01767de02 | 784 | // Queue is full. |
bogdanm | 0:eff01767de02 | 785 | return NULL; |
bogdanm | 0:eff01767de02 | 786 | } |
Rohit Grover |
37:c29c330d942c | 787 | |
Rohit Grover |
37:c29c330d942c | 788 | *p_last_index = last; |
bogdanm | 0:eff01767de02 | 789 | p_user_op = &p_user->p_user_op_queue[p_user->last]; |
Rohit Grover |
37:c29c330d942c | 790 | |
bogdanm | 0:eff01767de02 | 791 | return p_user_op; |
bogdanm | 0:eff01767de02 | 792 | } |
bogdanm | 0:eff01767de02 | 793 | |
bogdanm | 0:eff01767de02 | 794 | |
bogdanm | 0:eff01767de02 | 795 | /**@brief Function for scheduling a Timer Start operation. |
bogdanm | 0:eff01767de02 | 796 | * |
bogdanm | 0:eff01767de02 | 797 | * @param[in] user_id Id of user calling this function. |
bogdanm | 0:eff01767de02 | 798 | * @param[in] timer_id Id of timer to start. |
bogdanm | 0:eff01767de02 | 799 | * @param[in] timeout_initial Time (in ticks) to first timer expiry. |
bogdanm | 0:eff01767de02 | 800 | * @param[in] timeout_periodic Time (in ticks) between periodic expiries. |
bogdanm | 0:eff01767de02 | 801 | * @param[in] p_context General purpose pointer. Will be passed to the timeout handler when |
bogdanm | 0:eff01767de02 | 802 | * the timer expires. |
bogdanm | 0:eff01767de02 | 803 | * @return NRF_SUCCESS on success, otherwise an error code. |
bogdanm | 0:eff01767de02 | 804 | */ |
bogdanm | 0:eff01767de02 | 805 | static uint32_t timer_start_op_schedule(timer_user_id_t user_id, |
bogdanm | 0:eff01767de02 | 806 | app_timer_id_t timer_id, |
bogdanm | 0:eff01767de02 | 807 | uint32_t timeout_initial, |
bogdanm | 0:eff01767de02 | 808 | uint32_t timeout_periodic, |
bogdanm | 0:eff01767de02 | 809 | void * p_context) |
bogdanm | 0:eff01767de02 | 810 | { |
bogdanm | 0:eff01767de02 | 811 | app_timer_id_t last_index; |
Rohit Grover |
37:c29c330d942c | 812 | |
bogdanm | 0:eff01767de02 | 813 | timer_user_op_t * p_user_op = user_op_alloc(&mp_users[user_id], &last_index); |
bogdanm | 0:eff01767de02 | 814 | if (p_user_op == NULL) |
bogdanm | 0:eff01767de02 | 815 | { |
bogdanm | 0:eff01767de02 | 816 | return NRF_ERROR_NO_MEM; |
bogdanm | 0:eff01767de02 | 817 | } |
Rohit Grover |
37:c29c330d942c | 818 | |
bogdanm | 0:eff01767de02 | 819 | p_user_op->op_type = TIMER_USER_OP_TYPE_START; |
bogdanm | 0:eff01767de02 | 820 | p_user_op->timer_id = timer_id; |
bogdanm | 0:eff01767de02 | 821 | p_user_op->params.start.ticks_at_start = rtc1_counter_get(); |
bogdanm | 0:eff01767de02 | 822 | p_user_op->params.start.ticks_first_interval = timeout_initial; |
bogdanm | 0:eff01767de02 | 823 | p_user_op->params.start.ticks_periodic_interval = timeout_periodic; |
bogdanm | 0:eff01767de02 | 824 | p_user_op->params.start.p_context = p_context; |
Rohit Grover |
37:c29c330d942c | 825 | |
Rohit Grover |
37:c29c330d942c | 826 | user_op_enque(&mp_users[user_id], last_index); |
bogdanm | 0:eff01767de02 | 827 | |
bogdanm | 0:eff01767de02 | 828 | timer_list_handler_sched(); |
bogdanm | 0:eff01767de02 | 829 | |
bogdanm | 0:eff01767de02 | 830 | return NRF_SUCCESS; |
bogdanm | 0:eff01767de02 | 831 | } |
bogdanm | 0:eff01767de02 | 832 | |
bogdanm | 0:eff01767de02 | 833 | |
bogdanm | 0:eff01767de02 | 834 | /**@brief Function for scheduling a Timer Stop operation. |
bogdanm | 0:eff01767de02 | 835 | * |
bogdanm | 0:eff01767de02 | 836 | * @param[in] user_id Id of user calling this function. |
bogdanm | 0:eff01767de02 | 837 | * @param[in] timer_id Id of timer to stop. |
bogdanm | 0:eff01767de02 | 838 | * |
bogdanm | 0:eff01767de02 | 839 | * @return NRF_SUCCESS on successful scheduling a timer stop operation. NRF_ERROR_NO_MEM when there |
bogdanm | 0:eff01767de02 | 840 | * is no memory left to schedule the timer stop operation. |
bogdanm | 0:eff01767de02 | 841 | */ |
bogdanm | 0:eff01767de02 | 842 | static uint32_t timer_stop_op_schedule(timer_user_id_t user_id, app_timer_id_t timer_id) |
bogdanm | 0:eff01767de02 | 843 | { |
bogdanm | 0:eff01767de02 | 844 | app_timer_id_t last_index; |
Rohit Grover |
37:c29c330d942c | 845 | |
bogdanm | 0:eff01767de02 | 846 | timer_user_op_t * p_user_op = user_op_alloc(&mp_users[user_id], &last_index); |
bogdanm | 0:eff01767de02 | 847 | if (p_user_op == NULL) |
bogdanm | 0:eff01767de02 | 848 | { |
bogdanm | 0:eff01767de02 | 849 | return NRF_ERROR_NO_MEM; |
bogdanm | 0:eff01767de02 | 850 | } |
Rohit Grover |
37:c29c330d942c | 851 | |
bogdanm | 0:eff01767de02 | 852 | p_user_op->op_type = TIMER_USER_OP_TYPE_STOP; |
bogdanm | 0:eff01767de02 | 853 | p_user_op->timer_id = timer_id; |
Rohit Grover |
37:c29c330d942c | 854 | |
Rohit Grover |
37:c29c330d942c | 855 | user_op_enque(&mp_users[user_id], last_index); |
bogdanm | 0:eff01767de02 | 856 | |
bogdanm | 0:eff01767de02 | 857 | timer_list_handler_sched(); |
bogdanm | 0:eff01767de02 | 858 | |
bogdanm | 0:eff01767de02 | 859 | return NRF_SUCCESS; |
bogdanm | 0:eff01767de02 | 860 | } |
bogdanm | 0:eff01767de02 | 861 | |
bogdanm | 0:eff01767de02 | 862 | |
bogdanm | 0:eff01767de02 | 863 | /**@brief Function for scheduling a Timer Stop All operation. |
bogdanm | 0:eff01767de02 | 864 | * |
bogdanm | 0:eff01767de02 | 865 | * @param[in] user_id Id of user calling this function. |
bogdanm | 0:eff01767de02 | 866 | */ |
bogdanm | 0:eff01767de02 | 867 | static uint32_t timer_stop_all_op_schedule(timer_user_id_t user_id) |
bogdanm | 0:eff01767de02 | 868 | { |
bogdanm | 0:eff01767de02 | 869 | app_timer_id_t last_index; |
Rohit Grover |
37:c29c330d942c | 870 | |
bogdanm | 0:eff01767de02 | 871 | timer_user_op_t * p_user_op = user_op_alloc(&mp_users[user_id], &last_index); |
bogdanm | 0:eff01767de02 | 872 | if (p_user_op == NULL) |
bogdanm | 0:eff01767de02 | 873 | { |
bogdanm | 0:eff01767de02 | 874 | return NRF_ERROR_NO_MEM; |
bogdanm | 0:eff01767de02 | 875 | } |
Rohit Grover |
37:c29c330d942c | 876 | |
bogdanm | 0:eff01767de02 | 877 | p_user_op->op_type = TIMER_USER_OP_TYPE_STOP_ALL; |
bogdanm | 0:eff01767de02 | 878 | p_user_op->timer_id = TIMER_NULL; |
Rohit Grover |
37:c29c330d942c | 879 | |
Rohit Grover |
37:c29c330d942c | 880 | user_op_enque(&mp_users[user_id], last_index); |
bogdanm | 0:eff01767de02 | 881 | |
bogdanm | 0:eff01767de02 | 882 | timer_list_handler_sched(); |
bogdanm | 0:eff01767de02 | 883 | |
bogdanm | 0:eff01767de02 | 884 | return NRF_SUCCESS; |
bogdanm | 0:eff01767de02 | 885 | } |
bogdanm | 0:eff01767de02 | 886 | |
bogdanm | 0:eff01767de02 | 887 | |
bogdanm | 0:eff01767de02 | 888 | /**@brief Function for handling the RTC1 interrupt. |
bogdanm | 0:eff01767de02 | 889 | * |
bogdanm | 0:eff01767de02 | 890 | * @details Checks for timeouts, and executes timeout handlers for expired timers. |
bogdanm | 0:eff01767de02 | 891 | */ |
bogdanm | 0:eff01767de02 | 892 | extern "C" void RTC1_IRQHandler(void) |
bogdanm | 0:eff01767de02 | 893 | { |
bogdanm | 0:eff01767de02 | 894 | // Clear all events (also unexpected ones) |
bogdanm | 0:eff01767de02 | 895 | NRF_RTC1->EVENTS_COMPARE[0] = 0; |
bogdanm | 0:eff01767de02 | 896 | NRF_RTC1->EVENTS_COMPARE[1] = 0; |
bogdanm | 0:eff01767de02 | 897 | NRF_RTC1->EVENTS_COMPARE[2] = 0; |
bogdanm | 0:eff01767de02 | 898 | NRF_RTC1->EVENTS_COMPARE[3] = 0; |
bogdanm | 0:eff01767de02 | 899 | NRF_RTC1->EVENTS_TICK = 0; |
bogdanm | 0:eff01767de02 | 900 | NRF_RTC1->EVENTS_OVRFLW = 0; |
bogdanm | 0:eff01767de02 | 901 | |
bogdanm | 0:eff01767de02 | 902 | // Check for expired timers |
bogdanm | 0:eff01767de02 | 903 | timer_timeouts_check(); |
bogdanm | 0:eff01767de02 | 904 | } |
bogdanm | 0:eff01767de02 | 905 | |
bogdanm | 0:eff01767de02 | 906 | /**@brief Function for handling the SWI0 interrupt. |
bogdanm | 0:eff01767de02 | 907 | * |
bogdanm | 0:eff01767de02 | 908 | * @details Performs all updates to the timer list. |
bogdanm | 0:eff01767de02 | 909 | */ |
bogdanm | 0:eff01767de02 | 910 | extern "C" void SWI0_IRQHandler(void) |
bogdanm | 0:eff01767de02 | 911 | { |
bogdanm | 0:eff01767de02 | 912 | timer_list_handler(); |
bogdanm | 0:eff01767de02 | 913 | } |
bogdanm | 0:eff01767de02 | 914 | |
bogdanm | 0:eff01767de02 | 915 | uint32_t app_timer_init(uint32_t prescaler, |
bogdanm | 0:eff01767de02 | 916 | uint8_t max_timers, |
bogdanm | 0:eff01767de02 | 917 | uint8_t op_queues_size, |
bogdanm | 0:eff01767de02 | 918 | void * p_buffer, |
bogdanm | 0:eff01767de02 | 919 | app_timer_evt_schedule_func_t evt_schedule_func) |
bogdanm | 0:eff01767de02 | 920 | { |
bogdanm | 0:eff01767de02 | 921 | int i; |
bogdanm | 0:eff01767de02 | 922 | |
bogdanm | 0:eff01767de02 | 923 | // Check that buffer is correctly aligned |
bogdanm | 0:eff01767de02 | 924 | if (!is_word_aligned(p_buffer)) |
bogdanm | 0:eff01767de02 | 925 | { |
bogdanm | 0:eff01767de02 | 926 | return NRF_ERROR_INVALID_PARAM; |
bogdanm | 0:eff01767de02 | 927 | } |
bogdanm | 0:eff01767de02 | 928 | // Check for NULL buffer |
bogdanm | 0:eff01767de02 | 929 | if (p_buffer == NULL) |
bogdanm | 0:eff01767de02 | 930 | { |
bogdanm | 0:eff01767de02 | 931 | return NRF_ERROR_INVALID_PARAM; |
bogdanm | 0:eff01767de02 | 932 | } |
Rohit Grover |
37:c29c330d942c | 933 | |
bogdanm | 0:eff01767de02 | 934 | // Stop RTC to prevent any running timers from expiring (in case of reinitialization) |
bogdanm | 0:eff01767de02 | 935 | rtc1_stop(); |
Rohit Grover |
37:c29c330d942c | 936 | |
bogdanm | 0:eff01767de02 | 937 | m_evt_schedule_func = evt_schedule_func; |
bogdanm | 0:eff01767de02 | 938 | |
bogdanm | 0:eff01767de02 | 939 | // Initialize timer node array |
bogdanm | 0:eff01767de02 | 940 | m_node_array_size = max_timers; |
bogdanm | 0:eff01767de02 | 941 | mp_nodes = (timer_node_t *) p_buffer; |
Rohit Grover |
37:c29c330d942c | 942 | |
bogdanm | 0:eff01767de02 | 943 | for (i = 0; i < max_timers; i++) |
bogdanm | 0:eff01767de02 | 944 | { |
bogdanm | 0:eff01767de02 | 945 | mp_nodes[i].state = STATE_FREE; |
bogdanm | 0:eff01767de02 | 946 | mp_nodes[i].is_running = false; |
bogdanm | 0:eff01767de02 | 947 | } |
Rohit Grover |
37:c29c330d942c | 948 | |
bogdanm | 0:eff01767de02 | 949 | // Skip timer node array |
bogdanm | 0:eff01767de02 | 950 | p_buffer = &((uint8_t *)p_buffer)[max_timers * sizeof(timer_node_t)]; |
Rohit Grover |
37:c29c330d942c | 951 | |
bogdanm | 0:eff01767de02 | 952 | // Initialize users array |
bogdanm | 0:eff01767de02 | 953 | m_user_array_size = APP_TIMER_INT_LEVELS; |
bogdanm | 0:eff01767de02 | 954 | mp_users = (timer_user_t *) p_buffer; |
Rohit Grover |
37:c29c330d942c | 955 | |
bogdanm | 0:eff01767de02 | 956 | // Skip user array |
bogdanm | 0:eff01767de02 | 957 | p_buffer = &((uint8_t *)p_buffer)[APP_TIMER_INT_LEVELS * sizeof(timer_user_t)]; |
bogdanm | 0:eff01767de02 | 958 | |
bogdanm | 0:eff01767de02 | 959 | // Initialize operation queues |
bogdanm | 0:eff01767de02 | 960 | for (i = 0; i < APP_TIMER_INT_LEVELS; i++) |
bogdanm | 0:eff01767de02 | 961 | { |
bogdanm | 0:eff01767de02 | 962 | timer_user_t * p_user = &mp_users[i]; |
Rohit Grover |
37:c29c330d942c | 963 | |
bogdanm | 0:eff01767de02 | 964 | p_user->first = 0; |
bogdanm | 0:eff01767de02 | 965 | p_user->last = 0; |
bogdanm | 0:eff01767de02 | 966 | p_user->user_op_queue_size = op_queues_size; |
bogdanm | 0:eff01767de02 | 967 | p_user->p_user_op_queue = (timer_user_op_t *) p_buffer; |
Rohit Grover |
37:c29c330d942c | 968 | |
bogdanm | 0:eff01767de02 | 969 | // Skip operation queue |
bogdanm | 0:eff01767de02 | 970 | p_buffer = &((uint8_t *)p_buffer)[op_queues_size * sizeof(timer_user_op_t)]; |
bogdanm | 0:eff01767de02 | 971 | } |
bogdanm | 0:eff01767de02 | 972 | |
bogdanm | 0:eff01767de02 | 973 | m_timer_id_head = TIMER_NULL; |
bogdanm | 0:eff01767de02 | 974 | m_ticks_elapsed_q_read_ind = 0; |
bogdanm | 0:eff01767de02 | 975 | m_ticks_elapsed_q_write_ind = 0; |
bogdanm | 0:eff01767de02 | 976 | |
bogdanm | 0:eff01767de02 | 977 | NVIC_ClearPendingIRQ(SWI0_IRQn); |
bogdanm | 0:eff01767de02 | 978 | NVIC_SetPriority(SWI0_IRQn, SWI0_IRQ_PRI); |
bogdanm | 0:eff01767de02 | 979 | NVIC_EnableIRQ(SWI0_IRQn); |
bogdanm | 0:eff01767de02 | 980 | |
bogdanm | 0:eff01767de02 | 981 | rtc1_init(prescaler); |
bogdanm | 0:eff01767de02 | 982 | |
bogdanm | 0:eff01767de02 | 983 | m_ticks_latest = rtc1_counter_get(); |
Rohit Grover |
37:c29c330d942c | 984 | |
bogdanm | 0:eff01767de02 | 985 | return NRF_SUCCESS; |
bogdanm | 0:eff01767de02 | 986 | } |
bogdanm | 0:eff01767de02 | 987 | |
bogdanm | 0:eff01767de02 | 988 | |
bogdanm | 0:eff01767de02 | 989 | uint32_t app_timer_create(app_timer_id_t * p_timer_id, |
bogdanm | 0:eff01767de02 | 990 | app_timer_mode_t mode, |
bogdanm | 0:eff01767de02 | 991 | app_timer_timeout_handler_t timeout_handler) |
bogdanm | 0:eff01767de02 | 992 | { |
bogdanm | 0:eff01767de02 | 993 | int i; |
bogdanm | 0:eff01767de02 | 994 | |
bogdanm | 0:eff01767de02 | 995 | // Check state and parameters |
bogdanm | 0:eff01767de02 | 996 | if (mp_nodes == NULL) |
bogdanm | 0:eff01767de02 | 997 | { |
bogdanm | 0:eff01767de02 | 998 | return NRF_ERROR_INVALID_STATE; |
bogdanm | 0:eff01767de02 | 999 | } |
bogdanm | 0:eff01767de02 | 1000 | if (timeout_handler == NULL) |
bogdanm | 0:eff01767de02 | 1001 | { |
bogdanm | 0:eff01767de02 | 1002 | return NRF_ERROR_INVALID_PARAM; |
bogdanm | 0:eff01767de02 | 1003 | } |
bogdanm | 0:eff01767de02 | 1004 | if (p_timer_id == NULL) |
bogdanm | 0:eff01767de02 | 1005 | { |
bogdanm | 0:eff01767de02 | 1006 | return NRF_ERROR_INVALID_PARAM; |
Rohit Grover |
37:c29c330d942c | 1007 | } |
Rohit Grover |
37:c29c330d942c | 1008 | |
bogdanm | 0:eff01767de02 | 1009 | // Find free timer |
bogdanm | 0:eff01767de02 | 1010 | for (i = 0; i < m_node_array_size; i++) |
bogdanm | 0:eff01767de02 | 1011 | { |
bogdanm | 0:eff01767de02 | 1012 | if (mp_nodes[i].state == STATE_FREE) |
bogdanm | 0:eff01767de02 | 1013 | { |
bogdanm | 0:eff01767de02 | 1014 | mp_nodes[i].state = STATE_ALLOCATED; |
bogdanm | 0:eff01767de02 | 1015 | mp_nodes[i].mode = mode; |
bogdanm | 0:eff01767de02 | 1016 | mp_nodes[i].p_timeout_handler = timeout_handler; |
Rohit Grover |
37:c29c330d942c | 1017 | |
bogdanm | 0:eff01767de02 | 1018 | *p_timer_id = i; |
bogdanm | 0:eff01767de02 | 1019 | return NRF_SUCCESS; |
bogdanm | 0:eff01767de02 | 1020 | } |
bogdanm | 0:eff01767de02 | 1021 | } |
Rohit Grover |
37:c29c330d942c | 1022 | |
bogdanm | 0:eff01767de02 | 1023 | return NRF_ERROR_NO_MEM; |
bogdanm | 0:eff01767de02 | 1024 | } |
bogdanm | 0:eff01767de02 | 1025 | |
bogdanm | 0:eff01767de02 | 1026 | |
bogdanm | 0:eff01767de02 | 1027 | /**@brief Function for creating a timer user id from the current interrupt level. |
bogdanm | 0:eff01767de02 | 1028 | * |
bogdanm | 0:eff01767de02 | 1029 | * @return Timer user id. |
bogdanm | 0:eff01767de02 | 1030 | */ |
bogdanm | 0:eff01767de02 | 1031 | static timer_user_id_t user_id_get(void) |
bogdanm | 0:eff01767de02 | 1032 | { |
bogdanm | 0:eff01767de02 | 1033 | timer_user_id_t ret; |
bogdanm | 0:eff01767de02 | 1034 | |
bogdanm | 0:eff01767de02 | 1035 | STATIC_ASSERT(APP_TIMER_INT_LEVELS == 3); |
Rohit Grover |
37:c29c330d942c | 1036 | |
bogdanm | 0:eff01767de02 | 1037 | switch (current_int_priority_get()) |
bogdanm | 0:eff01767de02 | 1038 | { |
bogdanm | 0:eff01767de02 | 1039 | case APP_IRQ_PRIORITY_HIGH: |
bogdanm | 0:eff01767de02 | 1040 | ret = APP_HIGH_USER_ID; |
bogdanm | 0:eff01767de02 | 1041 | break; |
Rohit Grover |
37:c29c330d942c | 1042 | |
bogdanm | 0:eff01767de02 | 1043 | case APP_IRQ_PRIORITY_LOW: |
bogdanm | 0:eff01767de02 | 1044 | ret = APP_LOW_USER_ID; |
bogdanm | 0:eff01767de02 | 1045 | break; |
Rohit Grover |
37:c29c330d942c | 1046 | |
bogdanm | 0:eff01767de02 | 1047 | default: |
bogdanm | 0:eff01767de02 | 1048 | ret = THREAD_MODE_USER_ID; |
bogdanm | 0:eff01767de02 | 1049 | break; |
bogdanm | 0:eff01767de02 | 1050 | } |
Rohit Grover |
37:c29c330d942c | 1051 | |
bogdanm | 0:eff01767de02 | 1052 | return ret; |
bogdanm | 0:eff01767de02 | 1053 | } |
bogdanm | 0:eff01767de02 | 1054 | |
bogdanm | 0:eff01767de02 | 1055 | |
bogdanm | 0:eff01767de02 | 1056 | uint32_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context) |
bogdanm | 0:eff01767de02 | 1057 | { |
bogdanm | 0:eff01767de02 | 1058 | uint32_t timeout_periodic; |
Rohit Grover |
37:c29c330d942c | 1059 | |
bogdanm | 0:eff01767de02 | 1060 | // Check state and parameters |
bogdanm | 0:eff01767de02 | 1061 | if (mp_nodes == NULL) |
bogdanm | 0:eff01767de02 | 1062 | { |
bogdanm | 0:eff01767de02 | 1063 | return NRF_ERROR_INVALID_STATE; |
bogdanm | 0:eff01767de02 | 1064 | } |
bogdanm | 0:eff01767de02 | 1065 | if ((timer_id >= m_node_array_size) || (timeout_ticks < APP_TIMER_MIN_TIMEOUT_TICKS)) |
bogdanm | 0:eff01767de02 | 1066 | { |
bogdanm | 0:eff01767de02 | 1067 | return NRF_ERROR_INVALID_PARAM; |
bogdanm | 0:eff01767de02 | 1068 | } |
bogdanm | 0:eff01767de02 | 1069 | if (mp_nodes[timer_id].state != STATE_ALLOCATED) |
bogdanm | 0:eff01767de02 | 1070 | { |
bogdanm | 0:eff01767de02 | 1071 | return NRF_ERROR_INVALID_STATE; |
bogdanm | 0:eff01767de02 | 1072 | } |
Rohit Grover |
37:c29c330d942c | 1073 | |
bogdanm | 0:eff01767de02 | 1074 | // Schedule timer start operation |
bogdanm | 0:eff01767de02 | 1075 | timeout_periodic = (mp_nodes[timer_id].mode == APP_TIMER_MODE_REPEATED) ? timeout_ticks : 0; |
bogdanm | 0:eff01767de02 | 1076 | |
bogdanm | 0:eff01767de02 | 1077 | return timer_start_op_schedule(user_id_get(), |
bogdanm | 0:eff01767de02 | 1078 | timer_id, |
bogdanm | 0:eff01767de02 | 1079 | timeout_ticks, |
bogdanm | 0:eff01767de02 | 1080 | timeout_periodic, |
bogdanm | 0:eff01767de02 | 1081 | p_context); |
bogdanm | 0:eff01767de02 | 1082 | } |
bogdanm | 0:eff01767de02 | 1083 | |
bogdanm | 0:eff01767de02 | 1084 | |
bogdanm | 0:eff01767de02 | 1085 | uint32_t app_timer_stop(app_timer_id_t timer_id) |
bogdanm | 0:eff01767de02 | 1086 | { |
bogdanm | 0:eff01767de02 | 1087 | // Check state and parameters |
bogdanm | 0:eff01767de02 | 1088 | if (mp_nodes == NULL) |
bogdanm | 0:eff01767de02 | 1089 | { |
bogdanm | 0:eff01767de02 | 1090 | return NRF_ERROR_INVALID_STATE; |
bogdanm | 0:eff01767de02 | 1091 | } |
bogdanm | 0:eff01767de02 | 1092 | if (timer_id >= m_node_array_size) |
bogdanm | 0:eff01767de02 | 1093 | { |
bogdanm | 0:eff01767de02 | 1094 | return NRF_ERROR_INVALID_PARAM; |
bogdanm | 0:eff01767de02 | 1095 | } |
bogdanm | 0:eff01767de02 | 1096 | if (mp_nodes[timer_id].state != STATE_ALLOCATED) |
bogdanm | 0:eff01767de02 | 1097 | { |
bogdanm | 0:eff01767de02 | 1098 | return NRF_ERROR_INVALID_STATE; |
bogdanm | 0:eff01767de02 | 1099 | } |
Rohit Grover |
37:c29c330d942c | 1100 | |
bogdanm | 0:eff01767de02 | 1101 | // Schedule timer stop operation |
bogdanm | 0:eff01767de02 | 1102 | return timer_stop_op_schedule(user_id_get(), timer_id); |
bogdanm | 0:eff01767de02 | 1103 | } |
bogdanm | 0:eff01767de02 | 1104 | |
bogdanm | 0:eff01767de02 | 1105 | |
bogdanm | 0:eff01767de02 | 1106 | uint32_t app_timer_stop_all(void) |
bogdanm | 0:eff01767de02 | 1107 | { |
bogdanm | 0:eff01767de02 | 1108 | // Check state |
bogdanm | 0:eff01767de02 | 1109 | if (mp_nodes == NULL) |
bogdanm | 0:eff01767de02 | 1110 | { |
bogdanm | 0:eff01767de02 | 1111 | return NRF_ERROR_INVALID_STATE; |
bogdanm | 0:eff01767de02 | 1112 | } |
bogdanm | 0:eff01767de02 | 1113 | |
bogdanm | 0:eff01767de02 | 1114 | return timer_stop_all_op_schedule(user_id_get()); |
bogdanm | 0:eff01767de02 | 1115 | } |
bogdanm | 0:eff01767de02 | 1116 | |
bogdanm | 0:eff01767de02 | 1117 | |
bogdanm | 0:eff01767de02 | 1118 | uint32_t app_timer_cnt_get(uint32_t * p_ticks) |
bogdanm | 0:eff01767de02 | 1119 | { |
bogdanm | 0:eff01767de02 | 1120 | *p_ticks = rtc1_counter_get(); |
bogdanm | 0:eff01767de02 | 1121 | return NRF_SUCCESS; |
bogdanm | 0:eff01767de02 | 1122 | } |
bogdanm | 0:eff01767de02 | 1123 | |
bogdanm | 0:eff01767de02 | 1124 | |
bogdanm | 0:eff01767de02 | 1125 | uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to, |
bogdanm | 0:eff01767de02 | 1126 | uint32_t ticks_from, |
bogdanm | 0:eff01767de02 | 1127 | uint32_t * p_ticks_diff) |
bogdanm | 0:eff01767de02 | 1128 | { |
bogdanm | 0:eff01767de02 | 1129 | *p_ticks_diff = ticks_diff_get(ticks_to, ticks_from); |
bogdanm | 0:eff01767de02 | 1130 | return NRF_SUCCESS; |
bogdanm | 0:eff01767de02 | 1131 | } |