mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_Freescale/TARGET_K20D50M/us_ticker.c@304:89b9c3a9a045, 2014-08-29 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Aug 29 17:15:07 2014 +0100
- Revision:
- 304:89b9c3a9a045
- Parent:
- 255:20b371a9491b
Synchronized with git revision 734f365d7da26ef199751f4b0d91611479b495ea
Full URL: https://github.com/mbedmicro/mbed/commit/734f365d7da26ef199751f4b0d91611479b495ea/
1. timestamp_t as an abstraction for time values managed by
Ticker. Using uint64_t for timestamp_t allows a wraparound-free
Ticker. This change forces us to update the definitions of usTicker
for all platforms; but the changes beyond nRF51822 aren't major.
2. reduce power consumption on the nRF51822 by removing the need for
the high-frequency processor timer; and reimplementing it using the
RTC.
I've also replaced high-frequency clock with low-frequency external
clock during system startup of the nRF51822. This brings a major win
in power consumption.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 68:41613245dfd7 | 1 | /* mbed Microcontroller Library |
mbed_official | 68:41613245dfd7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 68:41613245dfd7 | 3 | * |
mbed_official | 68:41613245dfd7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 68:41613245dfd7 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 68:41613245dfd7 | 6 | * You may obtain a copy of the License at |
mbed_official | 68:41613245dfd7 | 7 | * |
mbed_official | 68:41613245dfd7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 68:41613245dfd7 | 9 | * |
mbed_official | 68:41613245dfd7 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 68:41613245dfd7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 68:41613245dfd7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 68:41613245dfd7 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 68:41613245dfd7 | 14 | * limitations under the License. |
mbed_official | 68:41613245dfd7 | 15 | */ |
mbed_official | 68:41613245dfd7 | 16 | #include <stddef.h> |
mbed_official | 68:41613245dfd7 | 17 | #include "us_ticker_api.h" |
mbed_official | 68:41613245dfd7 | 18 | #include "PeripheralNames.h" |
mbed_official | 73:299c67215126 | 19 | #include "clk_freqs.h" |
mbed_official | 68:41613245dfd7 | 20 | |
mbed_official | 161:09d8213f0000 | 21 | #define PIT_TIMER PIT->CHANNEL[0] |
mbed_official | 161:09d8213f0000 | 22 | #define PIT_TIMER_IRQ PIT0_IRQn |
mbed_official | 161:09d8213f0000 | 23 | #define PIT_TICKER PIT->CHANNEL[1] |
mbed_official | 161:09d8213f0000 | 24 | #define PIT_TICKER_IRQ PIT1_IRQn |
mbed_official | 161:09d8213f0000 | 25 | |
mbed_official | 161:09d8213f0000 | 26 | static void timer_init(void); |
mbed_official | 161:09d8213f0000 | 27 | static void ticker_init(void); |
mbed_official | 68:41613245dfd7 | 28 | |
mbed_official | 73:299c67215126 | 29 | |
mbed_official | 68:41613245dfd7 | 30 | static int us_ticker_inited = 0; |
mbed_official | 161:09d8213f0000 | 31 | static uint32_t clk_mhz; |
mbed_official | 68:41613245dfd7 | 32 | |
mbed_official | 68:41613245dfd7 | 33 | void us_ticker_init(void) { |
mbed_official | 68:41613245dfd7 | 34 | if (us_ticker_inited) |
mbed_official | 68:41613245dfd7 | 35 | return; |
mbed_official | 68:41613245dfd7 | 36 | us_ticker_inited = 1; |
mbed_official | 161:09d8213f0000 | 37 | |
mbed_official | 161:09d8213f0000 | 38 | SIM->SCGC6 |= SIM_SCGC6_PIT_MASK; // Clock PIT |
mbed_official | 161:09d8213f0000 | 39 | PIT->MCR = 0; // Enable PIT |
mbed_official | 161:09d8213f0000 | 40 | |
mbed_official | 161:09d8213f0000 | 41 | clk_mhz = bus_frequency() / 1000000; |
mbed_official | 68:41613245dfd7 | 42 | |
mbed_official | 161:09d8213f0000 | 43 | timer_init(); |
mbed_official | 161:09d8213f0000 | 44 | ticker_init(); |
mbed_official | 68:41613245dfd7 | 45 | } |
mbed_official | 68:41613245dfd7 | 46 | |
mbed_official | 68:41613245dfd7 | 47 | /****************************************************************************** |
mbed_official | 68:41613245dfd7 | 48 | * Timer for us timing. |
mbed_official | 161:09d8213f0000 | 49 | * |
mbed_official | 161:09d8213f0000 | 50 | * The K20D5M does not have a prescaler on its PIT timer nor the option |
mbed_official | 161:09d8213f0000 | 51 | * to chain timers, which is why a software timer is required to get 32-bit |
mbed_official | 161:09d8213f0000 | 52 | * word length. |
mbed_official | 68:41613245dfd7 | 53 | ******************************************************************************/ |
mbed_official | 161:09d8213f0000 | 54 | static volatile uint32_t msb_counter = 0; |
mbed_official | 161:09d8213f0000 | 55 | static uint32_t timer_ldval = 0; |
mbed_official | 73:299c67215126 | 56 | |
mbed_official | 161:09d8213f0000 | 57 | static void timer_isr(void) { |
mbed_official | 161:09d8213f0000 | 58 | msb_counter++; |
mbed_official | 161:09d8213f0000 | 59 | PIT_TIMER.TFLG = 1; |
mbed_official | 161:09d8213f0000 | 60 | } |
mbed_official | 68:41613245dfd7 | 61 | |
mbed_official | 161:09d8213f0000 | 62 | static void timer_init(void) { |
mbed_official | 161:09d8213f0000 | 63 | //CLZ counts the leading zeros, returning number of bits not used by clk_mhz |
mbed_official | 161:09d8213f0000 | 64 | timer_ldval = clk_mhz << __CLZ(clk_mhz); |
mbed_official | 161:09d8213f0000 | 65 | |
mbed_official | 161:09d8213f0000 | 66 | PIT_TIMER.LDVAL = timer_ldval; // 1us |
mbed_official | 161:09d8213f0000 | 67 | PIT_TIMER.TCTRL |= PIT_TCTRL_TIE_MASK; |
mbed_official | 161:09d8213f0000 | 68 | PIT_TIMER.TCTRL |= PIT_TCTRL_TEN_MASK; // Start timer 0 |
mbed_official | 161:09d8213f0000 | 69 | |
mbed_official | 161:09d8213f0000 | 70 | NVIC_SetVector(PIT_TIMER_IRQ, (uint32_t)timer_isr); |
mbed_official | 161:09d8213f0000 | 71 | NVIC_EnableIRQ(PIT_TIMER_IRQ); |
mbed_official | 68:41613245dfd7 | 72 | } |
mbed_official | 68:41613245dfd7 | 73 | |
mbed_official | 68:41613245dfd7 | 74 | uint32_t us_ticker_read() { |
mbed_official | 68:41613245dfd7 | 75 | if (!us_ticker_inited) |
mbed_official | 68:41613245dfd7 | 76 | us_ticker_init(); |
mbed_official | 161:09d8213f0000 | 77 | |
mbed_official | 161:09d8213f0000 | 78 | uint32_t retval; |
mbed_official | 161:09d8213f0000 | 79 | __disable_irq(); |
mbed_official | 161:09d8213f0000 | 80 | retval = (timer_ldval - PIT_TIMER.CVAL) / clk_mhz; //Hardware bits |
mbed_official | 161:09d8213f0000 | 81 | retval |= msb_counter << __CLZ(clk_mhz); //Software bits |
mbed_official | 161:09d8213f0000 | 82 | |
mbed_official | 161:09d8213f0000 | 83 | if (PIT_TIMER.TFLG == 1) { //If overflow bit is set, force it to be handled |
mbed_official | 161:09d8213f0000 | 84 | timer_isr(); //Handle IRQ, read again to make sure software/hardware bits are synced |
mbed_official | 161:09d8213f0000 | 85 | NVIC_ClearPendingIRQ(PIT_TIMER_IRQ); |
mbed_official | 161:09d8213f0000 | 86 | return us_ticker_read(); |
mbed_official | 161:09d8213f0000 | 87 | } |
mbed_official | 68:41613245dfd7 | 88 | |
mbed_official | 161:09d8213f0000 | 89 | __enable_irq(); |
mbed_official | 161:09d8213f0000 | 90 | return retval; |
mbed_official | 68:41613245dfd7 | 91 | } |
mbed_official | 68:41613245dfd7 | 92 | |
mbed_official | 68:41613245dfd7 | 93 | /****************************************************************************** |
mbed_official | 68:41613245dfd7 | 94 | * Timer Event |
mbed_official | 68:41613245dfd7 | 95 | * |
mbed_official | 68:41613245dfd7 | 96 | * It schedules interrupts at given (32bit)us interval of time. |
mbed_official | 161:09d8213f0000 | 97 | * It is implemented using PIT channel 1, since no prescaler is available, |
mbed_official | 161:09d8213f0000 | 98 | * some bits are implemented in software. |
mbed_official | 68:41613245dfd7 | 99 | ******************************************************************************/ |
mbed_official | 161:09d8213f0000 | 100 | static void ticker_isr(void); |
mbed_official | 73:299c67215126 | 101 | |
mbed_official | 161:09d8213f0000 | 102 | static void ticker_init(void) { |
mbed_official | 161:09d8213f0000 | 103 | /* Set interrupt handler */ |
mbed_official | 161:09d8213f0000 | 104 | NVIC_SetVector(PIT_TICKER_IRQ, (uint32_t)ticker_isr); |
mbed_official | 161:09d8213f0000 | 105 | NVIC_EnableIRQ(PIT_TICKER_IRQ); |
mbed_official | 68:41613245dfd7 | 106 | } |
mbed_official | 68:41613245dfd7 | 107 | |
mbed_official | 68:41613245dfd7 | 108 | void us_ticker_disable_interrupt(void) { |
mbed_official | 161:09d8213f0000 | 109 | PIT_TICKER.TCTRL &= ~PIT_TCTRL_TIE_MASK; |
mbed_official | 68:41613245dfd7 | 110 | } |
mbed_official | 68:41613245dfd7 | 111 | |
mbed_official | 68:41613245dfd7 | 112 | void us_ticker_clear_interrupt(void) { |
mbed_official | 68:41613245dfd7 | 113 | // we already clear interrupt in lptmr_isr |
mbed_official | 68:41613245dfd7 | 114 | } |
mbed_official | 68:41613245dfd7 | 115 | |
mbed_official | 68:41613245dfd7 | 116 | static uint32_t us_ticker_int_counter = 0; |
mbed_official | 68:41613245dfd7 | 117 | |
mbed_official | 161:09d8213f0000 | 118 | inline static void ticker_set(uint32_t count) { |
mbed_official | 161:09d8213f0000 | 119 | PIT_TICKER.TCTRL = 0; |
mbed_official | 161:09d8213f0000 | 120 | PIT_TICKER.LDVAL = count; |
mbed_official | 161:09d8213f0000 | 121 | PIT_TICKER.TCTRL = PIT_TCTRL_TIE_MASK | PIT_TCTRL_TEN_MASK; |
mbed_official | 68:41613245dfd7 | 122 | } |
mbed_official | 68:41613245dfd7 | 123 | |
mbed_official | 161:09d8213f0000 | 124 | static void ticker_isr(void) { |
mbed_official | 161:09d8213f0000 | 125 | // Clear IRQ flag |
mbed_official | 161:09d8213f0000 | 126 | PIT_TICKER.TFLG = 1; |
mbed_official | 68:41613245dfd7 | 127 | |
mbed_official | 68:41613245dfd7 | 128 | if (us_ticker_int_counter > 0) { |
mbed_official | 161:09d8213f0000 | 129 | ticker_set(0xFFFFFFFF); |
mbed_official | 68:41613245dfd7 | 130 | us_ticker_int_counter--; |
mbed_official | 68:41613245dfd7 | 131 | } else { |
mbed_official | 161:09d8213f0000 | 132 | // This function is going to disable the interrupts if there are |
mbed_official | 161:09d8213f0000 | 133 | // no other events in the queue |
mbed_official | 161:09d8213f0000 | 134 | us_ticker_irq_handler(); |
mbed_official | 68:41613245dfd7 | 135 | } |
mbed_official | 68:41613245dfd7 | 136 | } |
mbed_official | 68:41613245dfd7 | 137 | |
mbed_official | 304:89b9c3a9a045 | 138 | void us_ticker_set_interrupt(timestamp_t timestamp) { |
mbed_official | 304:89b9c3a9a045 | 139 | int delta = (int)((uint32_t)timestamp - us_ticker_read()); |
mbed_official | 68:41613245dfd7 | 140 | if (delta <= 0) { |
mbed_official | 68:41613245dfd7 | 141 | // This event was in the past: |
mbed_official | 68:41613245dfd7 | 142 | us_ticker_irq_handler(); |
mbed_official | 68:41613245dfd7 | 143 | return; |
mbed_official | 68:41613245dfd7 | 144 | } |
mbed_official | 68:41613245dfd7 | 145 | |
mbed_official | 161:09d8213f0000 | 146 | //Calculate how much falls outside the 32-bit after multiplying with clk_mhz |
mbed_official | 161:09d8213f0000 | 147 | //We shift twice 16-bit to keep everything within the 32-bit variable |
mbed_official | 161:09d8213f0000 | 148 | us_ticker_int_counter = (uint32_t)(delta >> 16); |
mbed_official | 161:09d8213f0000 | 149 | us_ticker_int_counter *= clk_mhz; |
mbed_official | 161:09d8213f0000 | 150 | us_ticker_int_counter >>= 16; |
mbed_official | 161:09d8213f0000 | 151 | |
mbed_official | 161:09d8213f0000 | 152 | uint32_t us_ticker_int_remainder = (uint32_t)delta * clk_mhz; |
mbed_official | 161:09d8213f0000 | 153 | if (us_ticker_int_remainder == 0) { |
mbed_official | 161:09d8213f0000 | 154 | ticker_set(0xFFFFFFFF); |
mbed_official | 68:41613245dfd7 | 155 | us_ticker_int_counter--; |
mbed_official | 68:41613245dfd7 | 156 | } else { |
mbed_official | 161:09d8213f0000 | 157 | ticker_set(us_ticker_int_remainder); |
mbed_official | 68:41613245dfd7 | 158 | } |
mbed_official | 68:41613245dfd7 | 159 | } |