test

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?

UserRevisionLine numberNew contents of line
elijahsj 1:8a094db1347f 1 /*******************************************************************************
elijahsj 1:8a094db1347f 2 * Copyright (C) 2015 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 "mbed_error.h"
elijahsj 1:8a094db1347f 35 #include "us_ticker_api.h"
elijahsj 1:8a094db1347f 36 #include "PeripheralNames.h"
elijahsj 1:8a094db1347f 37 #include "tmr_regs.h"
elijahsj 1:8a094db1347f 38
elijahsj 1:8a094db1347f 39 #define US_TIMER MXC_TMR0
elijahsj 1:8a094db1347f 40 #define US_TIMER_IRQn TMR0_IRQn
elijahsj 1:8a094db1347f 41
elijahsj 1:8a094db1347f 42 static int us_ticker_inited = 0;
elijahsj 1:8a094db1347f 43 static uint32_t ticks_per_us;
elijahsj 1:8a094db1347f 44 static uint32_t tick_win;
elijahsj 1:8a094db1347f 45 static volatile uint64_t current_cnt; // Hold the current ticks
elijahsj 1:8a094db1347f 46 static volatile uint64_t event_cnt; // Holds the value of the next event
elijahsj 1:8a094db1347f 47
elijahsj 1:8a094db1347f 48 #define ticks_to_us(ticks) ((ticks) / ticks_per_us);
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 term_cnt32 = US_TIMER->term_cnt32;
elijahsj 1:8a094db1347f 91 US_TIMER->term_cnt32 = 0xFFFFFFFF; // reset to max value to prevent further interrupts
elijahsj 1:8a094db1347f 92 US_TIMER->intfl = (MXC_F_TMR_INTFL_TIMER0 | MXC_F_TMR_INTFL_TIMER1); // clear interrupt
elijahsj 1:8a094db1347f 93 NVIC_ClearPendingIRQ(US_TIMER_IRQn);
elijahsj 1:8a094db1347f 94
elijahsj 1:8a094db1347f 95 inc_current_cnt(term_cnt32);
elijahsj 1:8a094db1347f 96
elijahsj 1:8a094db1347f 97 if (event_passed(current_cnt + US_TIMER->count32, 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
elijahsj 1:8a094db1347f 103 uint64_t diff = event_diff(current_cnt, event_cnt);
elijahsj 1:8a094db1347f 104 if (diff < (uint64_t)0xFFFFFFFF) {
elijahsj 1:8a094db1347f 105 // the event occurs before the next overflow
elijahsj 1:8a094db1347f 106 US_TIMER->term_cnt32 = diff;
elijahsj 1:8a094db1347f 107
elijahsj 1:8a094db1347f 108 // Since the timer keeps counting after the terminal value is reached, it is possible that the new
elijahsj 1:8a094db1347f 109 // terminal value is in the past.
elijahsj 1:8a094db1347f 110 if (US_TIMER->term_cnt32 < US_TIMER->count32) {
elijahsj 1:8a094db1347f 111 // the timestamp has expired
elijahsj 1:8a094db1347f 112 US_TIMER->term_cnt32 = 0xFFFFFFFF; // reset to max value to prevent further interrupts
elijahsj 1:8a094db1347f 113 US_TIMER->intfl = (MXC_F_TMR_INTFL_TIMER0 | MXC_F_TMR_INTFL_TIMER1); // clear interrupt
elijahsj 1:8a094db1347f 114 NVIC_ClearPendingIRQ(US_TIMER_IRQn);
elijahsj 1:8a094db1347f 115 event_cnt = 0xFFFFFFFFFFFFFFFFULL; // reset to max value
elijahsj 1:8a094db1347f 116 us_ticker_irq_handler();
elijahsj 1:8a094db1347f 117 }
elijahsj 1:8a094db1347f 118 }
elijahsj 1:8a094db1347f 119 }
elijahsj 1:8a094db1347f 120 }
elijahsj 1:8a094db1347f 121
elijahsj 1:8a094db1347f 122 //******************************************************************************
elijahsj 1:8a094db1347f 123 void us_ticker_init(void)
elijahsj 1:8a094db1347f 124 {
elijahsj 1:8a094db1347f 125 if (us_ticker_inited)
elijahsj 1:8a094db1347f 126 return;
elijahsj 1:8a094db1347f 127 us_ticker_inited = 1;
elijahsj 1:8a094db1347f 128
elijahsj 1:8a094db1347f 129 current_cnt = 0;
elijahsj 1:8a094db1347f 130 event_cnt = 0xFFFFFFFFFFFFFFFFULL; // reset to max value
elijahsj 1:8a094db1347f 131
elijahsj 1:8a094db1347f 132 if (SystemCoreClock <= 1000000) {
elijahsj 1:8a094db1347f 133 error("us_ticker cannot operate at this SystemCoreClock");
elijahsj 1:8a094db1347f 134 return;
elijahsj 1:8a094db1347f 135 }
elijahsj 1:8a094db1347f 136
elijahsj 1:8a094db1347f 137 // Configure timer for 32-bit continuous mode with /1 prescaler
elijahsj 1:8a094db1347f 138 US_TIMER->ctrl = MXC_E_TMR_MODE_CONTINUOUS << MXC_F_TMR_CTRL_MODE_POS | (0 << MXC_F_TMR_CTRL_PRESCALE_POS);
elijahsj 1:8a094db1347f 139 ticks_per_us = SystemCoreClock / 1000000;
elijahsj 1:8a094db1347f 140
elijahsj 1:8a094db1347f 141 // Set the tick window to 10ms
elijahsj 1:8a094db1347f 142 tick_win = SystemCoreClock/100;
elijahsj 1:8a094db1347f 143
elijahsj 1:8a094db1347f 144 // Set timer overflow to the max
elijahsj 1:8a094db1347f 145 US_TIMER->term_cnt32 = 0xFFFFFFFF;
elijahsj 1:8a094db1347f 146 US_TIMER->pwm_cap32 = 0xFFFFFFFF;
elijahsj 1:8a094db1347f 147 US_TIMER->count32 = 0;
elijahsj 1:8a094db1347f 148
elijahsj 1:8a094db1347f 149 US_TIMER->intfl = (MXC_F_TMR_INTFL_TIMER0 | MXC_F_TMR_INTFL_TIMER1); // clear pending interrupts
elijahsj 1:8a094db1347f 150
elijahsj 1:8a094db1347f 151 NVIC_SetVector(US_TIMER_IRQn, (uint32_t)tmr_handler);
elijahsj 1:8a094db1347f 152 NVIC_EnableIRQ(US_TIMER_IRQn);
elijahsj 1:8a094db1347f 153
elijahsj 1:8a094db1347f 154 US_TIMER->inten |= MXC_F_TMR_INTEN_TIMER0; // enable interrupts
elijahsj 1:8a094db1347f 155 US_TIMER->ctrl |= MXC_F_TMR_CTRL_ENABLE0; // enable timer
elijahsj 1:8a094db1347f 156 }
elijahsj 1:8a094db1347f 157
elijahsj 1:8a094db1347f 158 //******************************************************************************
elijahsj 1:8a094db1347f 159 void us_ticker_deinit(void)
elijahsj 1:8a094db1347f 160 {
elijahsj 1:8a094db1347f 161 US_TIMER->ctrl = 0; // disable timer
elijahsj 1:8a094db1347f 162 US_TIMER->inten = 0; // disable interrupts
elijahsj 1:8a094db1347f 163 US_TIMER->intfl = (MXC_F_TMR_INTFL_TIMER0 | MXC_F_TMR_INTFL_TIMER1); // clear interrupts
elijahsj 1:8a094db1347f 164 us_ticker_inited = 0;
elijahsj 1:8a094db1347f 165 }
elijahsj 1:8a094db1347f 166
elijahsj 1:8a094db1347f 167 //******************************************************************************
elijahsj 1:8a094db1347f 168 uint32_t us_ticker_read(void)
elijahsj 1:8a094db1347f 169 {
elijahsj 1:8a094db1347f 170 uint64_t current_cnt1, current_cnt2;
elijahsj 1:8a094db1347f 171 uint32_t term_cnt, tmr_cnt;
elijahsj 1:8a094db1347f 172 uint32_t intfl1, intfl2;
elijahsj 1:8a094db1347f 173
elijahsj 1:8a094db1347f 174 if (!us_ticker_inited)
elijahsj 1:8a094db1347f 175 us_ticker_init();
elijahsj 1:8a094db1347f 176
elijahsj 1:8a094db1347f 177 // Ensure coherency between current_cnt and US_TIMER->count32
elijahsj 1:8a094db1347f 178 do {
elijahsj 1:8a094db1347f 179 current_cnt1 = current_cnt;
elijahsj 1:8a094db1347f 180 intfl1 = US_TIMER->intfl;
elijahsj 1:8a094db1347f 181 term_cnt = US_TIMER->term_cnt32;
elijahsj 1:8a094db1347f 182 tmr_cnt = US_TIMER->count32;
elijahsj 1:8a094db1347f 183 intfl2 = US_TIMER->intfl;
elijahsj 1:8a094db1347f 184 current_cnt2 = current_cnt;
elijahsj 1:8a094db1347f 185 } while ((current_cnt1 != current_cnt2) || (intfl1 != intfl2));
elijahsj 1:8a094db1347f 186
elijahsj 1:8a094db1347f 187 // Account for an unserviced interrupt
elijahsj 1:8a094db1347f 188 if (intfl1) {
elijahsj 1:8a094db1347f 189 current_cnt1 += term_cnt;
elijahsj 1:8a094db1347f 190 }
elijahsj 1:8a094db1347f 191
elijahsj 1:8a094db1347f 192 current_cnt1 += tmr_cnt;
elijahsj 1:8a094db1347f 193
elijahsj 1:8a094db1347f 194 return (current_cnt1 / ticks_per_us);
elijahsj 1:8a094db1347f 195 }
elijahsj 1:8a094db1347f 196
elijahsj 1:8a094db1347f 197 //******************************************************************************
elijahsj 1:8a094db1347f 198 void us_ticker_set_interrupt(timestamp_t timestamp)
elijahsj 1:8a094db1347f 199 {
elijahsj 1:8a094db1347f 200 // Note: interrupts are disabled before this function is called.
elijahsj 1:8a094db1347f 201
elijahsj 1:8a094db1347f 202 US_TIMER->ctrl &= ~MXC_F_TMR_CTRL_ENABLE0; // disable timer
elijahsj 1:8a094db1347f 203
elijahsj 1:8a094db1347f 204 if (US_TIMER->intfl) {
elijahsj 1:8a094db1347f 205 US_TIMER->intfl = (MXC_F_TMR_INTFL_TIMER0 | MXC_F_TMR_INTFL_TIMER1); // clear interrupt
elijahsj 1:8a094db1347f 206 NVIC_ClearPendingIRQ(US_TIMER_IRQn);
elijahsj 1:8a094db1347f 207 inc_current_cnt(US_TIMER->term_cnt32);
elijahsj 1:8a094db1347f 208 }
elijahsj 1:8a094db1347f 209
elijahsj 1:8a094db1347f 210 // add and reset the current count value
elijahsj 1:8a094db1347f 211 inc_current_cnt(US_TIMER->count32);
elijahsj 1:8a094db1347f 212 US_TIMER->count32 = 0;
elijahsj 1:8a094db1347f 213
elijahsj 1:8a094db1347f 214 // add the number of cycles that the timer is disabled here for
elijahsj 1:8a094db1347f 215 inc_current_cnt(200);
elijahsj 1:8a094db1347f 216
elijahsj 1:8a094db1347f 217 event_cnt = (uint64_t)timestamp * ticks_per_us;
elijahsj 1:8a094db1347f 218
elijahsj 1:8a094db1347f 219 // Check to see if the event has already passed
elijahsj 1:8a094db1347f 220 if (!event_passed(current_cnt, event_cnt)) {
elijahsj 1:8a094db1347f 221 uint64_t diff = event_diff(current_cnt, event_cnt);
elijahsj 1:8a094db1347f 222 if (diff < (uint64_t)0xFFFFFFFF) {
elijahsj 1:8a094db1347f 223 // the event occurs before the next overflow
elijahsj 1:8a094db1347f 224 US_TIMER->term_cnt32 = diff;
elijahsj 1:8a094db1347f 225 } else {
elijahsj 1:8a094db1347f 226 // the event occurs after the next overflow
elijahsj 1:8a094db1347f 227 US_TIMER->term_cnt32 = 0xFFFFFFFF; // set to max
elijahsj 1:8a094db1347f 228 }
elijahsj 1:8a094db1347f 229 } else {
elijahsj 1:8a094db1347f 230 // the requested timestamp occurs in the past
elijahsj 1:8a094db1347f 231 // set the timer up to immediately expire
elijahsj 1:8a094db1347f 232 US_TIMER->term_cnt32 = 1;
elijahsj 1:8a094db1347f 233 }
elijahsj 1:8a094db1347f 234 US_TIMER->ctrl |= MXC_F_TMR_CTRL_ENABLE0; // enable timer
elijahsj 1:8a094db1347f 235 }
elijahsj 1:8a094db1347f 236
elijahsj 1:8a094db1347f 237 void us_ticker_fire_interrupt(void)
elijahsj 1:8a094db1347f 238 {
elijahsj 1:8a094db1347f 239 NVIC_SetPendingIRQ(US_TIMER_IRQn);
elijahsj 1:8a094db1347f 240 }
elijahsj 1:8a094db1347f 241
elijahsj 1:8a094db1347f 242 //******************************************************************************
elijahsj 1:8a094db1347f 243 void us_ticker_disable_interrupt(void)
elijahsj 1:8a094db1347f 244 {
elijahsj 1:8a094db1347f 245 // There are no more events, set timer overflow to the max
elijahsj 1:8a094db1347f 246 US_TIMER->term_cnt32 = 0xFFFFFFFF;
elijahsj 1:8a094db1347f 247 }
elijahsj 1:8a094db1347f 248
elijahsj 1:8a094db1347f 249 //******************************************************************************
elijahsj 1:8a094db1347f 250 void us_ticker_clear_interrupt(void)
elijahsj 1:8a094db1347f 251 {
elijahsj 1:8a094db1347f 252 // cleared in the local handler
elijahsj 1:8a094db1347f 253 }
elijahsj 1:8a094db1347f 254
elijahsj 1:8a094db1347f 255 //******************************************************************************
elijahsj 1:8a094db1347f 256 void us_ticker_set(timestamp_t timestamp)
elijahsj 1:8a094db1347f 257 {
elijahsj 1:8a094db1347f 258 US_TIMER->ctrl &= ~MXC_F_TMR_CTRL_ENABLE0; // disable timer
elijahsj 1:8a094db1347f 259 current_cnt = (uint64_t)timestamp * ticks_per_us;
elijahsj 1:8a094db1347f 260 US_TIMER->count32 = 0;
elijahsj 1:8a094db1347f 261 US_TIMER->term_cnt32 = 0xFFFFFFFF;
elijahsj 1:8a094db1347f 262 US_TIMER->ctrl |= MXC_F_TMR_CTRL_ENABLE0; // enable timer
elijahsj 1:8a094db1347f 263
elijahsj 1:8a094db1347f 264 if (((uint64_t)timestamp * ticks_per_us) >= event_cnt) {
elijahsj 1:8a094db1347f 265 // The next timestamp has elapsed. Trigger the interrupt to handle it.
elijahsj 1:8a094db1347f 266 NVIC_SetPendingIRQ(US_TIMER_IRQn);
elijahsj 1:8a094db1347f 267 }
elijahsj 1:8a094db1347f 268 }