1 /*******************************************************************************
2 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
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:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
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.
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.
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
31 *******************************************************************************
34 #include "mbed_error.h"
35 #include "us_ticker_api.h"
36 #include "PeripheralNames.h"
39 #define US_TIMER MXC_TMR0
40 #define US_TIMER_IRQn TMR0_IRQn
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
48 #define ticks_to_us(ticks) ((ticks) / ticks_per_us);
49 #define MAX_TICK_VAL ((uint64_t)0xFFFFFFFF * ticks_per_us)
51 //******************************************************************************
52 static inline void inc_current_cnt(uint32_t inc
) {
54 // Overflow the ticker when the us ticker overflows
56 if(current_cnt
> MAX_TICK_VAL
) {
57 current_cnt
-= (MAX_TICK_VAL
+ 1);
61 //******************************************************************************
62 static inline int event_passed(uint64_t current
, uint64_t event
) {
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
)))) {
76 //******************************************************************************
77 static inline uint64_t event_diff(uint64_t current
, uint64_t event
) {
79 // Check to see if the ticker will overflow before the event
80 if(current
<= event
) {
81 return (event
- current
);
84 return ((MAX_TICK_VAL
- current
) + event
);
87 //******************************************************************************
88 static void tmr_handler(void)
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
);
95 inc_current_cnt(term_cnt32
);
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();
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
;
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();
122 //******************************************************************************
123 void us_ticker_init(void)
125 if (us_ticker_inited
)
127 us_ticker_inited
= 1;
130 event_cnt
= 0xFFFFFFFFFFFFFFFFULL
; // reset to max value
132 if (SystemCoreClock
<= 1000000) {
133 error("us_ticker cannot operate at this SystemCoreClock");
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;
141 // Set the tick window to 10ms
142 tick_win
= SystemCoreClock
/100;
144 // Set timer overflow to the max
145 US_TIMER
->term_cnt32
= 0xFFFFFFFF;
146 US_TIMER
->pwm_cap32
= 0xFFFFFFFF;
147 US_TIMER
->count32
= 0;
149 US_TIMER
->intfl
= (MXC_F_TMR_INTFL_TIMER0
| MXC_F_TMR_INTFL_TIMER1
); // clear pending interrupts
151 NVIC_SetVector(US_TIMER_IRQn
, (uint32_t)tmr_handler
);
152 NVIC_EnableIRQ(US_TIMER_IRQn
);
154 US_TIMER
->inten
|= MXC_F_TMR_INTEN_TIMER0
; // enable interrupts
155 US_TIMER
->ctrl
|= MXC_F_TMR_CTRL_ENABLE0
; // enable timer
158 //******************************************************************************
159 void us_ticker_deinit(void)
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;
167 //******************************************************************************
168 uint32_t us_ticker_read(void)
170 uint64_t current_cnt1
, current_cnt2
;
171 uint32_t term_cnt
, tmr_cnt
;
174 if (!us_ticker_inited
)
177 // Ensure coherency between current_cnt and US_TIMER->count32
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
));
188 current_cnt1
+= term_cnt
;
191 current_cnt1
+= tmr_cnt
;
193 return (current_cnt1
/ ticks_per_us
);
196 //******************************************************************************
197 void us_ticker_set_interrupt(timestamp_t timestamp
)
199 // Note: interrupts are disabled before this function is called.
200 US_TIMER
->ctrl
&= ~MXC_F_TMR_CTRL_ENABLE0
; // disable timer
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
);
208 // add and reset the current count value
209 inc_current_cnt(US_TIMER
->count32
);
210 US_TIMER
->count32
= 0;
212 // add the number of cycles that the timer is disabled here for
213 inc_current_cnt(200);
215 event_cnt
= (uint64_t)timestamp
* ticks_per_us
;
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
;
224 // the event occurs after the next overflow
225 US_TIMER
->term_cnt32
= 0xFFFFFFFF; // set to max
228 // the requested timestamp occurs in the past
229 // set the timer up to immediately expire
230 US_TIMER
->term_cnt32
= 1;
232 US_TIMER
->ctrl
|= MXC_F_TMR_CTRL_ENABLE0
; // enable timer
235 //******************************************************************************
236 void us_ticker_disable_interrupt(void)
238 // There are no more events, set timer overflow to the max
239 US_TIMER
->term_cnt32
= 0xFFFFFFFF;
242 //******************************************************************************
243 void us_ticker_clear_interrupt(void)
245 // cleared in the local handler
248 //******************************************************************************
249 void us_ticker_set(timestamp_t timestamp
)
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
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
);