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