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