• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #include "reg_timer.h"
17 #include "us_ticker_api.h"
18 #include "sysctrl_api.h"
19 
20 static bool us_ticker_inited = false;
21 
22 #define US_TICKER_TIMER         (&(CS_TIM0->TMR[2]))
23 #define US_TICKER_TIMER_IRQn    TIMER02_IRQn
24 #define US_TICKER_ISR_FUNC      TIMER02_IRQHandler
25 //#define US_TICKER_TIMER         (&(CS_TIM0->TMR[0]))
26 //#define US_TICKER_TIMER_IRQn    TIMER00_IRQn
27 #define US_TICKER_CLOCK_SOURCE  (26000000U)
28 #define US_TICKER_HZ            (1000000U)
29 
us_ticker_get_info()30 const ticker_info_t* us_ticker_get_info()
31 {
32     static const ticker_info_t info = {
33         1000000,    // 1 MHz
34              32     // 32 bit counter
35     };
36     return &info;
37 }
38 
39 /** US Ticker Interrupt Handler
40  *
41  * This function handles the us ticker interrupt.
42  */
US_TICKER_ISR_FUNC(void)43 void US_TICKER_ISR_FUNC(void)
44 {
45     uint32_t status = US_TICKER_TIMER->IS;
46     // wrap interrupt
47     if (status & (0x01UL << 2)) {
48         US_TICKER_TIMER->IC = (0x01UL << 17); // clear interrupt
49     }
50     // match interrupt
51     if (status & (0x01UL /*<< 0*/)) {
52         US_TICKER_TIMER->IC = (0x01UL << 16); // clear interrupt
53         us_ticker_irq_handler();
54     }
55 }
56 
us_ticker_init(void)57 void us_ticker_init(void) {
58     if (us_ticker_inited) {
59         /* calling init again should cancel current interrupt */
60         us_ticker_disable_interrupt();
61         return;
62     }
63     us_ticker_inited = true;
64 
65     // clk en
66     cpusysctrl_pclkme_set(CSC_PCLKME_TIMER0_EN_BIT);
67     cpusysctrl_oclkme_set(CSC_OCLKME_TIMER02_EN_BIT);
68     US_TICKER_TIMER->CTL &= ~0x01UL;    // disable us ticker
69     US_TICKER_TIMER->TD = 0x0CUL | (0x01UL << 4); // div13
70     US_TICKER_TIMER->PR = US_TICKER_CLOCK_SOURCE / 13 / US_TICKER_HZ - 1; // default to 1MHz (1 us ticks)
71     US_TICKER_TIMER->CTL |=  0x01UL;    // enable us ticker
72 
73     NVIC_SetPriority(US_TICKER_TIMER_IRQn, __NVIC_PRIO_LOWEST);
74     NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
75 }
76 
us_ticker_read()77 uint32_t us_ticker_read() {
78     return US_TICKER_TIMER->TC;
79 }
80 
us_ticker_set_interrupt(timestamp_t timestamp)81 void us_ticker_set_interrupt(timestamp_t timestamp) {
82     // set match value
83     US_TICKER_TIMER->MR = (uint32_t)timestamp;
84     // enable match interrupt
85     US_TICKER_TIMER->IC = 0x01UL;
86 }
87 
us_ticker_fire_interrupt(void)88 void us_ticker_fire_interrupt(void)
89 {
90     NVIC_SetPendingIRQ(US_TICKER_TIMER_IRQn);
91 }
92 
us_ticker_disable_interrupt(void)93 void us_ticker_disable_interrupt(void) {
94     US_TICKER_TIMER->IC = (0x01UL << 8);
95 }
96 
us_ticker_clear_interrupt(void)97 void us_ticker_clear_interrupt(void) {
98     US_TICKER_TIMER->IC = (0x01UL << 16);
99 }
100 
us_ticker_free(void)101 void us_ticker_free(void)
102 {
103     US_TICKER_TIMER->CTL &= ~0x01UL;    // disable us ticker
104 
105     US_TICKER_TIMER->IC = (0x01UL << 8);
106     NVIC_DisableIRQ(US_TICKER_TIMER_IRQn);
107 }
108