Nordic stack and drivers for the mbed BLE API

Dependents:   BLE_Health_Thermometer2

Fork of nRF51822 by Nordic Semiconductor

Committer:
Rohit Grover
Date:
Thu May 29 09:51:36 2014 +0100
Revision:
14:5ca08f962e4f
Parent:
0:eff01767de02
Child:
37:c29c330d942c
use accessors for GattCharacteristic

Who changed what in which revision?

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