mbed
Fork of mbed-dev by
targets/TARGET_Maxim/TARGET_MAX32630/us_ticker.c@182:57724642e740, 2018-02-16 (annotated)
- Committer:
- AnnaBridge
- Date:
- Fri Feb 16 16:09:33 2018 +0000
- Revision:
- 182:57724642e740
- Parent:
- 175:b96e65c34a4d
mbed-dev library. Release version 159.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 157:ff67d9f36b67 | 1 | /******************************************************************************* |
<> | 157:ff67d9f36b67 | 2 | * Copyright (c) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
<> | 157:ff67d9f36b67 | 3 | * |
<> | 157:ff67d9f36b67 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
<> | 157:ff67d9f36b67 | 5 | * copy of this software and associated documentation files (the "Software"), |
<> | 157:ff67d9f36b67 | 6 | * to deal in the Software without restriction, including without limitation |
<> | 157:ff67d9f36b67 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
<> | 157:ff67d9f36b67 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
<> | 157:ff67d9f36b67 | 9 | * Software is furnished to do so, subject to the following conditions: |
<> | 157:ff67d9f36b67 | 10 | * |
<> | 157:ff67d9f36b67 | 11 | * The above copyright notice and this permission notice shall be included |
<> | 157:ff67d9f36b67 | 12 | * in all copies or substantial portions of the Software. |
<> | 157:ff67d9f36b67 | 13 | * |
<> | 157:ff67d9f36b67 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
<> | 157:ff67d9f36b67 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
<> | 157:ff67d9f36b67 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
<> | 157:ff67d9f36b67 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
<> | 157:ff67d9f36b67 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
<> | 157:ff67d9f36b67 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
<> | 157:ff67d9f36b67 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
<> | 157:ff67d9f36b67 | 21 | * |
<> | 157:ff67d9f36b67 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
<> | 157:ff67d9f36b67 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
<> | 157:ff67d9f36b67 | 24 | * Products, Inc. Branding Policy. |
<> | 157:ff67d9f36b67 | 25 | * |
<> | 157:ff67d9f36b67 | 26 | * The mere transfer of this software does not imply any licenses |
<> | 157:ff67d9f36b67 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
<> | 157:ff67d9f36b67 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
<> | 157:ff67d9f36b67 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
<> | 157:ff67d9f36b67 | 30 | * ownership rights. |
<> | 157:ff67d9f36b67 | 31 | ******************************************************************************* |
<> | 157:ff67d9f36b67 | 32 | */ |
<> | 157:ff67d9f36b67 | 33 | |
<> | 157:ff67d9f36b67 | 34 | #include <stddef.h> |
<> | 157:ff67d9f36b67 | 35 | #include "mbed_error.h" |
<> | 157:ff67d9f36b67 | 36 | #include "us_ticker_api.h" |
<> | 157:ff67d9f36b67 | 37 | #include "PeripheralNames.h" |
<> | 157:ff67d9f36b67 | 38 | #include "tmr.h" |
<> | 157:ff67d9f36b67 | 39 | |
<> | 157:ff67d9f36b67 | 40 | #define US_TIMER MXC_TMR0 |
<> | 157:ff67d9f36b67 | 41 | #define US_TIMER_IRQn TMR0_0_IRQn |
<> | 157:ff67d9f36b67 | 42 | |
<> | 157:ff67d9f36b67 | 43 | static int us_ticker_inited = 0; |
<> | 157:ff67d9f36b67 | 44 | static uint32_t ticks_per_us; |
<> | 157:ff67d9f36b67 | 45 | static uint32_t tick_win; |
<> | 157:ff67d9f36b67 | 46 | static volatile uint64_t current_cnt; // Hold the current ticks |
<> | 157:ff67d9f36b67 | 47 | static volatile uint64_t event_cnt; // Holds the value of the next event |
<> | 157:ff67d9f36b67 | 48 | |
<> | 157:ff67d9f36b67 | 49 | #define MAX_TICK_VAL ((uint64_t)0xFFFFFFFF * ticks_per_us) |
<> | 157:ff67d9f36b67 | 50 | |
<> | 157:ff67d9f36b67 | 51 | //****************************************************************************** |
<> | 157:ff67d9f36b67 | 52 | static inline void inc_current_cnt(uint32_t inc) |
<> | 157:ff67d9f36b67 | 53 | { |
<> | 157:ff67d9f36b67 | 54 | // Overflow the ticker when the us ticker overflows |
<> | 157:ff67d9f36b67 | 55 | current_cnt += inc; |
<> | 157:ff67d9f36b67 | 56 | if (current_cnt > MAX_TICK_VAL) { |
<> | 157:ff67d9f36b67 | 57 | current_cnt -= (MAX_TICK_VAL + 1); |
<> | 157:ff67d9f36b67 | 58 | } |
<> | 157:ff67d9f36b67 | 59 | } |
<> | 157:ff67d9f36b67 | 60 | |
<> | 157:ff67d9f36b67 | 61 | //****************************************************************************** |
<> | 157:ff67d9f36b67 | 62 | static inline int event_passed(uint64_t current, uint64_t event) |
<> | 157:ff67d9f36b67 | 63 | { |
<> | 157:ff67d9f36b67 | 64 | // Determine if the event has already happened. |
<> | 157:ff67d9f36b67 | 65 | // If the event is behind the current ticker, within a window, |
<> | 157:ff67d9f36b67 | 66 | // then the event has already happened. |
<> | 157:ff67d9f36b67 | 67 | if (((current < tick_win) && ((event < current) || |
<> | 157:ff67d9f36b67 | 68 | (event > (MAX_TICK_VAL - (tick_win - current))))) || |
<> | 157:ff67d9f36b67 | 69 | ((event < current) && (event > (current - tick_win)))) { |
<> | 157:ff67d9f36b67 | 70 | return 1; |
<> | 157:ff67d9f36b67 | 71 | } |
<> | 157:ff67d9f36b67 | 72 | |
<> | 157:ff67d9f36b67 | 73 | return 0; |
<> | 157:ff67d9f36b67 | 74 | } |
<> | 157:ff67d9f36b67 | 75 | |
<> | 157:ff67d9f36b67 | 76 | //****************************************************************************** |
<> | 157:ff67d9f36b67 | 77 | static inline uint64_t event_diff(uint64_t current, uint64_t event) |
<> | 157:ff67d9f36b67 | 78 | { |
<> | 157:ff67d9f36b67 | 79 | // Check to see if the ticker will overflow before the event |
<> | 157:ff67d9f36b67 | 80 | if(current <= event) { |
<> | 157:ff67d9f36b67 | 81 | return (event - current); |
<> | 157:ff67d9f36b67 | 82 | } |
<> | 157:ff67d9f36b67 | 83 | |
<> | 157:ff67d9f36b67 | 84 | return ((MAX_TICK_VAL - current) + event); |
<> | 157:ff67d9f36b67 | 85 | } |
<> | 157:ff67d9f36b67 | 86 | |
<> | 157:ff67d9f36b67 | 87 | //****************************************************************************** |
<> | 157:ff67d9f36b67 | 88 | static void tmr_handler(void) |
<> | 157:ff67d9f36b67 | 89 | { |
<> | 157:ff67d9f36b67 | 90 | uint32_t cmp = TMR32_GetCompare(US_TIMER); |
<> | 157:ff67d9f36b67 | 91 | TMR32_SetCompare(US_TIMER, 0xFFFFFFFF); // reset to max value to prevent further interrupts |
<> | 157:ff67d9f36b67 | 92 | TMR32_ClearFlag(US_TIMER); |
<> | 157:ff67d9f36b67 | 93 | NVIC_ClearPendingIRQ(US_TIMER_IRQn); |
<> | 157:ff67d9f36b67 | 94 | |
<> | 157:ff67d9f36b67 | 95 | inc_current_cnt(cmp); |
<> | 157:ff67d9f36b67 | 96 | |
<> | 157:ff67d9f36b67 | 97 | if (event_passed(current_cnt + TMR32_GetCount(US_TIMER), event_cnt)) { |
<> | 157:ff67d9f36b67 | 98 | // the timestamp has expired |
<> | 157:ff67d9f36b67 | 99 | event_cnt = 0xFFFFFFFFFFFFFFFFULL; // reset to max value |
<> | 157:ff67d9f36b67 | 100 | us_ticker_irq_handler(); |
<> | 157:ff67d9f36b67 | 101 | } else { |
<> | 157:ff67d9f36b67 | 102 | uint64_t diff = event_diff(current_cnt, event_cnt); |
<> | 157:ff67d9f36b67 | 103 | if (diff < (uint64_t)0xFFFFFFFF) { |
<> | 157:ff67d9f36b67 | 104 | // the event occurs before the next overflow |
<> | 157:ff67d9f36b67 | 105 | TMR32_SetCompare(US_TIMER, diff); |
<> | 157:ff67d9f36b67 | 106 | |
<> | 157:ff67d9f36b67 | 107 | // Since the timer keeps counting after the terminal value is reached, it is possible that the new |
<> | 157:ff67d9f36b67 | 108 | // terminal value is in the past. |
<> | 157:ff67d9f36b67 | 109 | if (TMR32_GetCompare(US_TIMER) < TMR32_GetCount(US_TIMER)) { |
<> | 157:ff67d9f36b67 | 110 | // the timestamp has expired |
<> | 157:ff67d9f36b67 | 111 | TMR32_SetCompare(US_TIMER, 0xFFFFFFFF); // reset to max value to prevent further interrupts |
<> | 157:ff67d9f36b67 | 112 | TMR32_ClearFlag(US_TIMER); |
<> | 157:ff67d9f36b67 | 113 | NVIC_ClearPendingIRQ(US_TIMER_IRQn); |
<> | 157:ff67d9f36b67 | 114 | event_cnt = 0xFFFFFFFFFFFFFFFFULL; // reset to max value |
<> | 157:ff67d9f36b67 | 115 | us_ticker_irq_handler(); |
<> | 157:ff67d9f36b67 | 116 | } |
<> | 157:ff67d9f36b67 | 117 | } |
<> | 157:ff67d9f36b67 | 118 | } |
<> | 157:ff67d9f36b67 | 119 | } |
<> | 157:ff67d9f36b67 | 120 | |
<> | 157:ff67d9f36b67 | 121 | //****************************************************************************** |
<> | 157:ff67d9f36b67 | 122 | void us_ticker_init(void) |
<> | 157:ff67d9f36b67 | 123 | { |
<> | 157:ff67d9f36b67 | 124 | if (us_ticker_inited) { |
<> | 157:ff67d9f36b67 | 125 | return; |
<> | 157:ff67d9f36b67 | 126 | } |
<> | 157:ff67d9f36b67 | 127 | |
<> | 157:ff67d9f36b67 | 128 | us_ticker_inited = 1; |
<> | 157:ff67d9f36b67 | 129 | current_cnt = 0; |
<> | 157:ff67d9f36b67 | 130 | event_cnt = 0xFFFFFFFFFFFFFFFFULL; // reset to max value |
<> | 157:ff67d9f36b67 | 131 | ticks_per_us = SystemCoreClock / 1000000; |
<> | 157:ff67d9f36b67 | 132 | tick_win = SystemCoreClock / 100; // Set the tick window to 10ms |
<> | 157:ff67d9f36b67 | 133 | |
<> | 157:ff67d9f36b67 | 134 | int retval = TMR_Init(US_TIMER, TMR_PRESCALE_DIV_2_0, NULL); |
<> | 157:ff67d9f36b67 | 135 | MBED_ASSERT(retval == E_NO_ERROR); |
<> | 157:ff67d9f36b67 | 136 | |
<> | 157:ff67d9f36b67 | 137 | tmr32_cfg_t cfg; |
<> | 157:ff67d9f36b67 | 138 | cfg.mode = TMR32_MODE_CONTINUOUS; |
<> | 157:ff67d9f36b67 | 139 | cfg.polarity = TMR_POLARITY_UNUSED; |
<> | 157:ff67d9f36b67 | 140 | cfg.compareCount = 0xFFFFFFFF; |
<> | 157:ff67d9f36b67 | 141 | TMR32_Config(US_TIMER, &cfg); |
<> | 157:ff67d9f36b67 | 142 | |
AnnaBridge | 165:e614a9f1c9e2 | 143 | NVIC_SetVector(US_TIMER_IRQn, (uint32_t)tmr_handler); |
<> | 157:ff67d9f36b67 | 144 | NVIC_EnableIRQ(US_TIMER_IRQn); |
<> | 157:ff67d9f36b67 | 145 | TMR32_EnableINT(US_TIMER); |
<> | 157:ff67d9f36b67 | 146 | |
<> | 157:ff67d9f36b67 | 147 | TMR32_Start(US_TIMER); |
<> | 157:ff67d9f36b67 | 148 | } |
<> | 157:ff67d9f36b67 | 149 | |
<> | 157:ff67d9f36b67 | 150 | //****************************************************************************** |
<> | 157:ff67d9f36b67 | 151 | void us_ticker_deinit(void) |
<> | 157:ff67d9f36b67 | 152 | { |
<> | 157:ff67d9f36b67 | 153 | TMR32_Stop(US_TIMER); |
<> | 157:ff67d9f36b67 | 154 | TMR32_DisableINT(US_TIMER); |
<> | 157:ff67d9f36b67 | 155 | TMR32_ClearFlag(US_TIMER); |
<> | 157:ff67d9f36b67 | 156 | us_ticker_inited = 0; |
<> | 157:ff67d9f36b67 | 157 | } |
<> | 157:ff67d9f36b67 | 158 | |
<> | 157:ff67d9f36b67 | 159 | //****************************************************************************** |
<> | 157:ff67d9f36b67 | 160 | uint32_t us_ticker_read(void) |
<> | 157:ff67d9f36b67 | 161 | { |
<> | 157:ff67d9f36b67 | 162 | uint64_t current_cnt1, current_cnt2; |
<> | 157:ff67d9f36b67 | 163 | uint32_t cmp, cnt; |
<> | 157:ff67d9f36b67 | 164 | uint32_t flag1, flag2; |
<> | 157:ff67d9f36b67 | 165 | |
<> | 157:ff67d9f36b67 | 166 | if (!us_ticker_inited) { |
<> | 157:ff67d9f36b67 | 167 | us_ticker_init(); |
<> | 157:ff67d9f36b67 | 168 | } |
<> | 157:ff67d9f36b67 | 169 | |
<> | 157:ff67d9f36b67 | 170 | // Ensure coherency between current_cnt and TMR32_GetCount() |
<> | 157:ff67d9f36b67 | 171 | do { |
<> | 157:ff67d9f36b67 | 172 | current_cnt1 = current_cnt; |
<> | 157:ff67d9f36b67 | 173 | flag1 = TMR32_GetFlag(US_TIMER); |
<> | 157:ff67d9f36b67 | 174 | cmp = TMR32_GetCompare(US_TIMER); |
<> | 157:ff67d9f36b67 | 175 | cnt = TMR32_GetCount(US_TIMER); |
<> | 157:ff67d9f36b67 | 176 | flag2 = TMR32_GetFlag(US_TIMER); |
<> | 157:ff67d9f36b67 | 177 | current_cnt2 = current_cnt; |
<> | 157:ff67d9f36b67 | 178 | } while ((current_cnt1 != current_cnt2) || (flag1 != flag2)); |
<> | 157:ff67d9f36b67 | 179 | |
<> | 157:ff67d9f36b67 | 180 | // Account for an unserviced interrupt |
<> | 157:ff67d9f36b67 | 181 | if (flag1) { |
<> | 157:ff67d9f36b67 | 182 | current_cnt1 += cmp; |
<> | 157:ff67d9f36b67 | 183 | } |
<> | 157:ff67d9f36b67 | 184 | |
<> | 157:ff67d9f36b67 | 185 | current_cnt1 += cnt; |
<> | 157:ff67d9f36b67 | 186 | |
<> | 157:ff67d9f36b67 | 187 | return (current_cnt1 / ticks_per_us); |
<> | 157:ff67d9f36b67 | 188 | } |
<> | 157:ff67d9f36b67 | 189 | |
<> | 157:ff67d9f36b67 | 190 | //****************************************************************************** |
<> | 157:ff67d9f36b67 | 191 | void us_ticker_set_interrupt(timestamp_t timestamp) |
<> | 157:ff67d9f36b67 | 192 | { |
<> | 157:ff67d9f36b67 | 193 | // Note: interrupts are disabled before this function is called. |
<> | 157:ff67d9f36b67 | 194 | |
<> | 157:ff67d9f36b67 | 195 | TMR32_Stop(US_TIMER); |
<> | 157:ff67d9f36b67 | 196 | |
<> | 157:ff67d9f36b67 | 197 | if (TMR32_GetFlag(US_TIMER)) { |
<> | 157:ff67d9f36b67 | 198 | TMR32_ClearFlag(US_TIMER); |
<> | 157:ff67d9f36b67 | 199 | NVIC_ClearPendingIRQ(US_TIMER_IRQn); |
<> | 157:ff67d9f36b67 | 200 | inc_current_cnt(TMR32_GetCompare(US_TIMER)); |
<> | 157:ff67d9f36b67 | 201 | } |
<> | 157:ff67d9f36b67 | 202 | |
<> | 157:ff67d9f36b67 | 203 | // add and reset the current count value |
<> | 157:ff67d9f36b67 | 204 | inc_current_cnt(TMR32_GetCount(US_TIMER)); |
<> | 157:ff67d9f36b67 | 205 | TMR32_SetCount(US_TIMER, 0); |
<> | 157:ff67d9f36b67 | 206 | |
<> | 157:ff67d9f36b67 | 207 | // add the number of cycles that the timer is disabled here for |
<> | 157:ff67d9f36b67 | 208 | inc_current_cnt(200); |
<> | 157:ff67d9f36b67 | 209 | |
<> | 157:ff67d9f36b67 | 210 | event_cnt = (uint64_t)timestamp * ticks_per_us; |
<> | 157:ff67d9f36b67 | 211 | |
<> | 157:ff67d9f36b67 | 212 | // Check to see if the event has already passed |
<> | 157:ff67d9f36b67 | 213 | if (!event_passed(current_cnt, event_cnt)) { |
<> | 157:ff67d9f36b67 | 214 | uint64_t diff = event_diff(current_cnt, event_cnt); |
<> | 157:ff67d9f36b67 | 215 | if (diff < (uint64_t)0xFFFFFFFF) { |
<> | 157:ff67d9f36b67 | 216 | // the event occurs before the next overflow |
<> | 157:ff67d9f36b67 | 217 | TMR32_SetCompare(US_TIMER, diff); |
<> | 157:ff67d9f36b67 | 218 | } else { |
<> | 157:ff67d9f36b67 | 219 | // the event occurs after the next overflow |
<> | 157:ff67d9f36b67 | 220 | TMR32_SetCompare(US_TIMER, 0xFFFFFFFF); // set to max |
<> | 157:ff67d9f36b67 | 221 | } |
<> | 157:ff67d9f36b67 | 222 | } else { |
<> | 157:ff67d9f36b67 | 223 | // the requested timestamp occurs in the past |
<> | 157:ff67d9f36b67 | 224 | // set the timer up to immediately expire |
<> | 157:ff67d9f36b67 | 225 | TMR32_SetCompare(US_TIMER, 1); |
<> | 157:ff67d9f36b67 | 226 | } |
<> | 157:ff67d9f36b67 | 227 | |
<> | 157:ff67d9f36b67 | 228 | TMR32_Start(US_TIMER); |
<> | 157:ff67d9f36b67 | 229 | } |
<> | 157:ff67d9f36b67 | 230 | |
AnnaBridge | 175:b96e65c34a4d | 231 | void us_ticker_fire_interrupt(void) |
AnnaBridge | 175:b96e65c34a4d | 232 | { |
AnnaBridge | 175:b96e65c34a4d | 233 | NVIC_SetPendingIRQ(US_TIMER_IRQn); |
AnnaBridge | 175:b96e65c34a4d | 234 | } |
AnnaBridge | 175:b96e65c34a4d | 235 | |
<> | 157:ff67d9f36b67 | 236 | //****************************************************************************** |
<> | 157:ff67d9f36b67 | 237 | void us_ticker_disable_interrupt(void) |
<> | 157:ff67d9f36b67 | 238 | { |
<> | 157:ff67d9f36b67 | 239 | // There are no more events, set timer overflow to the max |
<> | 157:ff67d9f36b67 | 240 | TMR32_SetCompare(US_TIMER, 0xFFFFFFFF); |
<> | 157:ff67d9f36b67 | 241 | } |
<> | 157:ff67d9f36b67 | 242 | |
<> | 157:ff67d9f36b67 | 243 | //****************************************************************************** |
<> | 157:ff67d9f36b67 | 244 | void us_ticker_clear_interrupt(void) |
<> | 157:ff67d9f36b67 | 245 | { |
<> | 157:ff67d9f36b67 | 246 | // cleared in the local handler |
<> | 157:ff67d9f36b67 | 247 | } |
<> | 157:ff67d9f36b67 | 248 | |
<> | 157:ff67d9f36b67 | 249 | //****************************************************************************** |
<> | 157:ff67d9f36b67 | 250 | void us_ticker_set(timestamp_t timestamp) |
<> | 157:ff67d9f36b67 | 251 | { |
<> | 157:ff67d9f36b67 | 252 | TMR32_Stop(US_TIMER); |
<> | 157:ff67d9f36b67 | 253 | current_cnt = (uint64_t)timestamp * ticks_per_us; |
<> | 157:ff67d9f36b67 | 254 | TMR32_SetCount(US_TIMER, 0); |
<> | 157:ff67d9f36b67 | 255 | TMR32_SetCompare(US_TIMER, 0xFFFFFFFF); |
<> | 157:ff67d9f36b67 | 256 | TMR32_Start(US_TIMER); |
<> | 157:ff67d9f36b67 | 257 | |
<> | 157:ff67d9f36b67 | 258 | if (((uint64_t)timestamp * ticks_per_us) >= event_cnt) { |
<> | 157:ff67d9f36b67 | 259 | // The next timestamp has elapsed. Trigger the interrupt to handle it. |
<> | 157:ff67d9f36b67 | 260 | NVIC_SetPendingIRQ(US_TIMER_IRQn); |
<> | 157:ff67d9f36b67 | 261 | } |
<> | 157:ff67d9f36b67 | 262 | } |