1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "us_ticker_api.h"
18 #include "PeripheralNames.h"
22 * "us_ticker_init", "us_ticker_read"
23 * "us_ticker_set_interrupt", "us_ticker_disable_interrupt", "us_ticker_clear_interrupt"
25 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
26 #define US_TICKER_TIMER ((LPC_TIM_TypeDef *)LPC_TIM3_BASE)
27 #define US_TICKER_TIMER_IRQn TIMER3_IRQn
29 #elif defined(TARGET_LPC11U24)
30 #define US_TICKER_TIMER ((LPC_CTxxBx_Type *)LPC_CT32B1_BASE)
31 #define US_TICKER_TIMER_IRQn TIMER_32_1_IRQn
35 static int us_ticker_running
= 0;
37 static inline void us_ticker_init(void) {
38 us_ticker_running
= 1;
39 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
40 LPC_SC
->PCONP
|= 1 << 23; // Clock TIMER_3
42 US_TICKER_TIMER
->CTCR
= 0x0; // timer mode
43 uint32_t PCLK
= SystemCoreClock
/ 4;
45 #elif defined(TARGET_LPC11U24)
46 LPC_SYSCON
->SYSAHBCLKCTRL
|= (1<<10); // Clock TIMER_1
47 uint32_t PCLK
= SystemCoreClock
;
50 US_TICKER_TIMER
->TCR
= 0x2; // reset
52 uint32_t prescale
= PCLK
/ 1000000; // default to 1MHz (1 us ticks)
53 US_TICKER_TIMER
->PR
= prescale
- 1;
54 US_TICKER_TIMER
->TCR
= 1; // enable = 1, reset = 0
57 uint32_t us_ticker_read() {
58 if (!us_ticker_running
)
61 return US_TICKER_TIMER
->TC
;
64 static inline void us_ticker_set_interrupt(unsigned int timestamp
) {
66 US_TICKER_TIMER
->MR0
= timestamp
;
67 // enable match interrupt
68 US_TICKER_TIMER
->MCR
|= 1;
71 static inline void us_ticker_disable_interrupt(void) {
72 US_TICKER_TIMER
->MCR
&= ~1;
75 static inline void us_ticker_clear_interrupt(void) {
76 US_TICKER_TIMER
->IR
= 1;
79 static ticker_event_handler event_handler
;
80 static ticker_event_t
*head
= NULL
;
82 void irq_handler(void) {
83 us_ticker_clear_interrupt();
85 /* Go through all the pending TimerEvents */
88 // There are no more TimerEvents left, so disable matches.
89 us_ticker_disable_interrupt();
93 if ((int)(head
->timestamp
- us_ticker_read()) <= 0) {
94 // This event was in the past:
95 // point to the following one and execute its handler
96 ticker_event_t
*p
= head
;
99 event_handler(p
->id
); // NOTE: the handler can set new events
102 // This event and the following ones in the list are in the future:
103 // set it as next interrupt and return
104 us_ticker_set_interrupt(head
->timestamp
);
110 void us_ticker_set_handler(ticker_event_handler handler
) {
111 event_handler
= handler
;
113 NVIC_SetVector(US_TICKER_TIMER_IRQn
, (uint32_t)irq_handler
);
114 NVIC_EnableIRQ(US_TICKER_TIMER_IRQn
);
117 void us_ticker_insert_event(ticker_event_t
*obj
, unsigned int timestamp
, uint32_t id
) {
118 /* disable interrupts for the duration of the function */
121 // initialise our data
122 obj
->timestamp
= timestamp
;
125 /* Go through the list until we either reach the end, or find
126 an element this should come before (which is possibly the
128 ticker_event_t
*prev
= NULL
, *p
= head
;
130 /* check if we come before p */
131 if ((int)(timestamp
- p
->timestamp
) <= 0) {
134 /* go to the next element */
138 /* if prev is NULL we're at the head */
141 us_ticker_set_interrupt(timestamp
);
145 /* if we're at the end p will be NULL, which is correct */
151 void us_ticker_remove_event(ticker_event_t
*obj
) {
154 // remove this object from the list
155 if (head
== obj
) { // first in the list, so just drop me
157 if (obj
->next
!= NULL
) {
158 us_ticker_set_interrupt(head
->timestamp
);
160 } else { // find the object before me, then drop me
161 ticker_event_t
* p
= head
;
163 if (p
->next
== obj
) {