mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
targets/hal/TARGET_ARM_SSG/TARGET_MPS2/us_ticker.c@0:9b334a45a8ff, 2015-10-01 (annotated)
- Committer:
- bogdanm
- Date:
- Thu Oct 01 15:25:22 2015 +0300
- Revision:
- 0:9b334a45a8ff
- Child:
- 80:bdf1132a57cf
Initial commit on mbed-dev
Replaces mbed-src (now inactive)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:9b334a45a8ff | 1 | /* mbed Microcontroller Library |
bogdanm | 0:9b334a45a8ff | 2 | * Copyright (c) 2006-2015 ARM Limited |
bogdanm | 0:9b334a45a8ff | 3 | * |
bogdanm | 0:9b334a45a8ff | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 0:9b334a45a8ff | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 0:9b334a45a8ff | 6 | * You may obtain a copy of the License at |
bogdanm | 0:9b334a45a8ff | 7 | * |
bogdanm | 0:9b334a45a8ff | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 0:9b334a45a8ff | 9 | * |
bogdanm | 0:9b334a45a8ff | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 0:9b334a45a8ff | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 0:9b334a45a8ff | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 0:9b334a45a8ff | 13 | * See the License for the specific language governing permissions and |
bogdanm | 0:9b334a45a8ff | 14 | * limitations under the License. |
bogdanm | 0:9b334a45a8ff | 15 | */ |
bogdanm | 0:9b334a45a8ff | 16 | #include <stddef.h> |
bogdanm | 0:9b334a45a8ff | 17 | #include "us_ticker_api.h" |
bogdanm | 0:9b334a45a8ff | 18 | #include "PeripheralNames.h" |
bogdanm | 0:9b334a45a8ff | 19 | |
bogdanm | 0:9b334a45a8ff | 20 | #define US_TICKER_TIMER1 CMSDK_DUALTIMER1 |
bogdanm | 0:9b334a45a8ff | 21 | #define US_TICKER_TIMER2 CMSDK_DUALTIMER2 |
bogdanm | 0:9b334a45a8ff | 22 | #define US_TICKER_TIMER_IRQn DUALTIMER_IRQn |
bogdanm | 0:9b334a45a8ff | 23 | |
bogdanm | 0:9b334a45a8ff | 24 | int us_ticker_inited = 0; |
bogdanm | 0:9b334a45a8ff | 25 | |
bogdanm | 0:9b334a45a8ff | 26 | void us_ticker_init(void) { |
bogdanm | 0:9b334a45a8ff | 27 | if (us_ticker_inited) return; |
bogdanm | 0:9b334a45a8ff | 28 | us_ticker_inited = 1; |
bogdanm | 0:9b334a45a8ff | 29 | |
bogdanm | 0:9b334a45a8ff | 30 | US_TICKER_TIMER1->TimerControl = 0x0; // disable timer |
bogdanm | 0:9b334a45a8ff | 31 | US_TICKER_TIMER2->TimerControl = 0x00; // disable timer |
bogdanm | 0:9b334a45a8ff | 32 | US_TICKER_TIMER1->TimerLoad = 0xFFFFFFFF; |
bogdanm | 0:9b334a45a8ff | 33 | US_TICKER_TIMER2->TimerLoad = 0xFFFFFFFF; |
bogdanm | 0:9b334a45a8ff | 34 | |
bogdanm | 0:9b334a45a8ff | 35 | US_TICKER_TIMER1->TimerControl = 0x62; // enable interrupt and set to 32 bit counter and set to periodic mode |
bogdanm | 0:9b334a45a8ff | 36 | US_TICKER_TIMER2->TimerControl = 0x42; // enable interrupt and set to 32 bit counter |
bogdanm | 0:9b334a45a8ff | 37 | |
bogdanm | 0:9b334a45a8ff | 38 | US_TICKER_TIMER1->TimerControl |= 0x80; // enable counter |
bogdanm | 0:9b334a45a8ff | 39 | US_TICKER_TIMER2->TimerControl |= 0x80; // enable counter |
bogdanm | 0:9b334a45a8ff | 40 | |
bogdanm | 0:9b334a45a8ff | 41 | NVIC_SetVector(US_TICKER_TIMER_IRQn, (uint32_t)us_ticker_irq_handler); |
bogdanm | 0:9b334a45a8ff | 42 | NVIC_EnableIRQ(US_TICKER_TIMER_IRQn); |
bogdanm | 0:9b334a45a8ff | 43 | } |
bogdanm | 0:9b334a45a8ff | 44 | |
bogdanm | 0:9b334a45a8ff | 45 | uint32_t us_ticker_read() { |
bogdanm | 0:9b334a45a8ff | 46 | uint32_t return_value = 0; |
bogdanm | 0:9b334a45a8ff | 47 | if (!us_ticker_inited) |
bogdanm | 0:9b334a45a8ff | 48 | us_ticker_init(); |
bogdanm | 0:9b334a45a8ff | 49 | return_value = ((~US_TICKER_TIMER2->TimerValue)/25); |
bogdanm | 0:9b334a45a8ff | 50 | return return_value; |
bogdanm | 0:9b334a45a8ff | 51 | } |
bogdanm | 0:9b334a45a8ff | 52 | |
bogdanm | 0:9b334a45a8ff | 53 | void us_ticker_set_interrupt(timestamp_t timestamp) { |
bogdanm | 0:9b334a45a8ff | 54 | uint32_t timer_value = 0; |
bogdanm | 0:9b334a45a8ff | 55 | int delta = 0; |
bogdanm | 0:9b334a45a8ff | 56 | if (!us_ticker_inited) |
bogdanm | 0:9b334a45a8ff | 57 | us_ticker_init(); |
bogdanm | 0:9b334a45a8ff | 58 | delta = (int)(timestamp - us_ticker_read()); |
bogdanm | 0:9b334a45a8ff | 59 | if (delta <= 0) { |
bogdanm | 0:9b334a45a8ff | 60 | // This event was in the past: |
bogdanm | 0:9b334a45a8ff | 61 | us_ticker_irq_handler(); |
bogdanm | 0:9b334a45a8ff | 62 | return; |
bogdanm | 0:9b334a45a8ff | 63 | } |
bogdanm | 0:9b334a45a8ff | 64 | timer_value = (delta)*25; |
bogdanm | 0:9b334a45a8ff | 65 | // enable interrupt |
bogdanm | 0:9b334a45a8ff | 66 | US_TICKER_TIMER1->TimerControl = 0x0; // disable timer |
bogdanm | 0:9b334a45a8ff | 67 | US_TICKER_TIMER1->TimerControl = 0x62; // enable interrupt and set to 32 bit counter and set to periodic mode |
bogdanm | 0:9b334a45a8ff | 68 | US_TICKER_TIMER1->TimerLoad = (delta)*25; //initialise the timer value |
bogdanm | 0:9b334a45a8ff | 69 | US_TICKER_TIMER1->TimerControl |= 0x80; //enable timer |
bogdanm | 0:9b334a45a8ff | 70 | } |
bogdanm | 0:9b334a45a8ff | 71 | |
bogdanm | 0:9b334a45a8ff | 72 | void us_ticker_disable_interrupt(void) { |
bogdanm | 0:9b334a45a8ff | 73 | |
bogdanm | 0:9b334a45a8ff | 74 | US_TICKER_TIMER1->TimerControl &= 0xDF; |
bogdanm | 0:9b334a45a8ff | 75 | US_TICKER_TIMER2->TimerControl &= 0xDF; |
bogdanm | 0:9b334a45a8ff | 76 | |
bogdanm | 0:9b334a45a8ff | 77 | } |
bogdanm | 0:9b334a45a8ff | 78 | |
bogdanm | 0:9b334a45a8ff | 79 | void us_ticker_clear_interrupt(void) { |
bogdanm | 0:9b334a45a8ff | 80 | |
bogdanm | 0:9b334a45a8ff | 81 | US_TICKER_TIMER1->TimerIntClr = 0x1; |
bogdanm | 0:9b334a45a8ff | 82 | US_TICKER_TIMER2->TimerIntClr = 0x1; |
bogdanm | 0:9b334a45a8ff | 83 | |
bogdanm | 0:9b334a45a8ff | 84 | } |