RCBControllerでモータを制御します。うおーるぼっとも動かせました。

Dependencies:   BLE_API_Native_IRC TB6612FNG2 mbed

Fork of BLE_RCBController by Junichi Katsu

  • 古いBLEライブラリを使っているのでプラットフォームは”Nordic nRF51822”を選択してください。
  • ライブラリ類はUpdateしないでください。コンパイルエラーになります。

うまく接続できない時は、iPhone/iPadのBluetoothをOFF->ONしてキャッシュをクリアしてみてください。

RCBControllerでうおーるぼっとを操縦する例 /media/uploads/robo8080/img_1671.jpg

RCBControllerでの操縦は次の4種類あります。 それぞれうおーるぼっとの動きが異なりますので試してみてください。

  • 左十字ボタン
  • 左のみアナログ
  • 右のみアナログ
  • 両方アナログ

うおーるぼっと(LPC1768のソケット)とHRM1017の接続はこれです。

LPC1768 ー HRM1017

p11 ーーー P0_0

p12 ーーー P0_1

p13 ーーー P0_28

p14 ーーー P0_29

p21 ーーー P0_30

p22 ーーー P0_25

GND ーーー GND

HRM1017の電源はうおーるぼっとのUSBコネクタからとります。 /media/uploads/robo8080/img_1674.jpg

Committer:
jksoft
Date:
Thu Jul 10 14:21:52 2014 +0000
Revision:
0:8c643bfe55b7
??

Who changed what in which revision?

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