mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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