mbed library sources: Modified to operate FRDM-KL25Z at 48MHz from internal 32kHz oscillator (nothing else changed).
Fork of mbed-src by
The only file that changed is: mbed-src-FLL48/targets/cmsis/TARGET_Freescale/TARGET_KL25Z/system_MKL25Z4.h
targets/hal/TARGET_Freescale/TARGET_KL05Z/us_ticker.c@30:7ca55132b805, 2013-10-26 (annotated)
- Committer:
- icenyne
- Date:
- Sat Oct 26 13:59:14 2013 +0000
- Revision:
- 30:7ca55132b805
- Parent:
- 20:4263a77256ae
mbed library source for KL25Z updated to operate using internal oscillator at 48MHz as the default.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 20:4263a77256ae | 1 | /* mbed Microcontroller Library |
bogdanm | 20:4263a77256ae | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 20:4263a77256ae | 3 | * |
bogdanm | 20:4263a77256ae | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 20:4263a77256ae | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 20:4263a77256ae | 6 | * You may obtain a copy of the License at |
bogdanm | 20:4263a77256ae | 7 | * |
bogdanm | 20:4263a77256ae | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 20:4263a77256ae | 9 | * |
bogdanm | 20:4263a77256ae | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 20:4263a77256ae | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 20:4263a77256ae | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 20:4263a77256ae | 13 | * See the License for the specific language governing permissions and |
bogdanm | 20:4263a77256ae | 14 | * limitations under the License. |
bogdanm | 20:4263a77256ae | 15 | */ |
bogdanm | 20:4263a77256ae | 16 | #include <stddef.h> |
bogdanm | 20:4263a77256ae | 17 | #include "us_ticker_api.h" |
bogdanm | 20:4263a77256ae | 18 | #include "PeripheralNames.h" |
bogdanm | 20:4263a77256ae | 19 | |
bogdanm | 20:4263a77256ae | 20 | /* Prototypes */ |
bogdanm | 20:4263a77256ae | 21 | static void pit_init(void); |
bogdanm | 20:4263a77256ae | 22 | static void lptmr_init(void); |
bogdanm | 20:4263a77256ae | 23 | static void lptmr_isr(void); |
bogdanm | 20:4263a77256ae | 24 | |
bogdanm | 20:4263a77256ae | 25 | /* Global variables */ |
bogdanm | 20:4263a77256ae | 26 | static uint32_t us_ticker_inited = 0; |
bogdanm | 20:4263a77256ae | 27 | static uint32_t us_ticker_int_counter = 0; |
bogdanm | 20:4263a77256ae | 28 | static uint16_t us_ticker_int_remainder = 0; |
bogdanm | 20:4263a77256ae | 29 | |
bogdanm | 20:4263a77256ae | 30 | |
bogdanm | 20:4263a77256ae | 31 | void us_ticker_init(void) { |
bogdanm | 20:4263a77256ae | 32 | if (us_ticker_inited) { |
bogdanm | 20:4263a77256ae | 33 | return; |
bogdanm | 20:4263a77256ae | 34 | } |
bogdanm | 20:4263a77256ae | 35 | us_ticker_inited = 1; |
bogdanm | 20:4263a77256ae | 36 | |
bogdanm | 20:4263a77256ae | 37 | pit_init(); |
bogdanm | 20:4263a77256ae | 38 | lptmr_init(); |
bogdanm | 20:4263a77256ae | 39 | } |
bogdanm | 20:4263a77256ae | 40 | |
bogdanm | 20:4263a77256ae | 41 | static void pit_init(void) { |
bogdanm | 20:4263a77256ae | 42 | SIM->SCGC6 |= SIM_SCGC6_PIT_MASK; // Clock PIT |
bogdanm | 20:4263a77256ae | 43 | PIT->MCR = 0; // Enable PIT |
bogdanm | 20:4263a77256ae | 44 | |
bogdanm | 20:4263a77256ae | 45 | // Channel 1 |
bogdanm | 20:4263a77256ae | 46 | PIT->CHANNEL[1].LDVAL = 0xFFFFFFFF; |
bogdanm | 20:4263a77256ae | 47 | PIT->CHANNEL[1].TCTRL = PIT_TCTRL_CHN_MASK; // Chain to timer 0, disable Interrupts |
bogdanm | 20:4263a77256ae | 48 | PIT->CHANNEL[1].TCTRL |= PIT_TCTRL_TEN_MASK; // Start timer 1 |
bogdanm | 20:4263a77256ae | 49 | |
bogdanm | 20:4263a77256ae | 50 | // Use channel 0 as a prescaler for channel 1 |
bogdanm | 20:4263a77256ae | 51 | PIT->CHANNEL[0].LDVAL = 23; |
bogdanm | 20:4263a77256ae | 52 | PIT->CHANNEL[0].TCTRL = PIT_TCTRL_TEN_MASK; // Start timer 0, disable interrupts |
bogdanm | 20:4263a77256ae | 53 | } |
bogdanm | 20:4263a77256ae | 54 | |
bogdanm | 20:4263a77256ae | 55 | uint32_t us_ticker_read() { |
bogdanm | 20:4263a77256ae | 56 | if (!us_ticker_inited) { |
bogdanm | 20:4263a77256ae | 57 | us_ticker_init(); |
bogdanm | 20:4263a77256ae | 58 | } |
bogdanm | 20:4263a77256ae | 59 | |
bogdanm | 20:4263a77256ae | 60 | // The PIT is a countdown timer |
bogdanm | 20:4263a77256ae | 61 | return ~(PIT->CHANNEL[1].CVAL); |
bogdanm | 20:4263a77256ae | 62 | } |
bogdanm | 20:4263a77256ae | 63 | |
bogdanm | 20:4263a77256ae | 64 | static void lptmr_init(void) { |
bogdanm | 20:4263a77256ae | 65 | SIM->SCGC5 |= SIM_SCGC5_LPTMR_MASK; |
bogdanm | 20:4263a77256ae | 66 | |
bogdanm | 20:4263a77256ae | 67 | LPTMR0->CSR = 0; |
bogdanm | 20:4263a77256ae | 68 | |
bogdanm | 20:4263a77256ae | 69 | NVIC_SetVector(LPTimer_IRQn, (uint32_t)lptmr_isr); |
bogdanm | 20:4263a77256ae | 70 | NVIC_EnableIRQ(LPTimer_IRQn); |
bogdanm | 20:4263a77256ae | 71 | |
bogdanm | 20:4263a77256ae | 72 | // Clock at (1)MHz -> (1)tick/us |
bogdanm | 20:4263a77256ae | 73 | LPTMR0->PSR = LPTMR_PSR_PCS(0); // MCGIRCLK -> 2MHz / presc 2 = 1MHz |
bogdanm | 20:4263a77256ae | 74 | } |
bogdanm | 20:4263a77256ae | 75 | |
bogdanm | 20:4263a77256ae | 76 | void us_ticker_disable_interrupt(void) { |
bogdanm | 20:4263a77256ae | 77 | LPTMR0->CSR &= ~LPTMR_CSR_TIE_MASK; |
bogdanm | 20:4263a77256ae | 78 | } |
bogdanm | 20:4263a77256ae | 79 | |
bogdanm | 20:4263a77256ae | 80 | void us_ticker_clear_interrupt(void) { |
bogdanm | 20:4263a77256ae | 81 | // we've already cleared interrupt in lptmr_isr |
bogdanm | 20:4263a77256ae | 82 | } |
bogdanm | 20:4263a77256ae | 83 | |
bogdanm | 20:4263a77256ae | 84 | static void lptmr_set(unsigned short count) { |
bogdanm | 20:4263a77256ae | 85 | // Reset |
bogdanm | 20:4263a77256ae | 86 | LPTMR0->CSR = 0; |
bogdanm | 20:4263a77256ae | 87 | |
bogdanm | 20:4263a77256ae | 88 | // Set the compare register |
bogdanm | 20:4263a77256ae | 89 | LPTMR0->CMR = count; |
bogdanm | 20:4263a77256ae | 90 | |
bogdanm | 20:4263a77256ae | 91 | // Enable interrupt |
bogdanm | 20:4263a77256ae | 92 | LPTMR0->CSR |= LPTMR_CSR_TIE_MASK; |
bogdanm | 20:4263a77256ae | 93 | |
bogdanm | 20:4263a77256ae | 94 | // Start the timer |
bogdanm | 20:4263a77256ae | 95 | LPTMR0->CSR |= LPTMR_CSR_TEN_MASK; |
bogdanm | 20:4263a77256ae | 96 | } |
bogdanm | 20:4263a77256ae | 97 | |
bogdanm | 20:4263a77256ae | 98 | static void lptmr_isr(void) { |
bogdanm | 20:4263a77256ae | 99 | // write 1 to TCF to clear the LPT timer compare flag |
bogdanm | 20:4263a77256ae | 100 | LPTMR0->CSR |= LPTMR_CSR_TCF_MASK; |
bogdanm | 20:4263a77256ae | 101 | |
bogdanm | 20:4263a77256ae | 102 | if (us_ticker_int_counter > 0) { |
bogdanm | 20:4263a77256ae | 103 | lptmr_set(0xFFFF); |
bogdanm | 20:4263a77256ae | 104 | us_ticker_int_counter--; |
bogdanm | 20:4263a77256ae | 105 | } else { |
bogdanm | 20:4263a77256ae | 106 | if (us_ticker_int_remainder > 0) { |
bogdanm | 20:4263a77256ae | 107 | lptmr_set(us_ticker_int_remainder); |
bogdanm | 20:4263a77256ae | 108 | us_ticker_int_remainder = 0; |
bogdanm | 20:4263a77256ae | 109 | } else { |
bogdanm | 20:4263a77256ae | 110 | // This function is going to disable the interrupts if there are |
bogdanm | 20:4263a77256ae | 111 | // no other events in the queue |
bogdanm | 20:4263a77256ae | 112 | us_ticker_irq_handler(); |
bogdanm | 20:4263a77256ae | 113 | } |
bogdanm | 20:4263a77256ae | 114 | } |
bogdanm | 20:4263a77256ae | 115 | } |
bogdanm | 20:4263a77256ae | 116 | |
bogdanm | 20:4263a77256ae | 117 | void us_ticker_set_interrupt(uint32_t timestamp) { |
bogdanm | 20:4263a77256ae | 118 | int32_t delta = (int32_t)(timestamp - us_ticker_read()); |
bogdanm | 20:4263a77256ae | 119 | if (delta <= 0) { |
bogdanm | 20:4263a77256ae | 120 | // This event was in the past: |
bogdanm | 20:4263a77256ae | 121 | us_ticker_irq_handler(); |
bogdanm | 20:4263a77256ae | 122 | return; |
bogdanm | 20:4263a77256ae | 123 | } |
bogdanm | 20:4263a77256ae | 124 | |
bogdanm | 20:4263a77256ae | 125 | us_ticker_int_counter = (uint32_t)(delta >> 16); |
bogdanm | 20:4263a77256ae | 126 | us_ticker_int_remainder = (uint16_t)(0xFFFF & delta); |
bogdanm | 20:4263a77256ae | 127 | if (us_ticker_int_counter > 0) { |
bogdanm | 20:4263a77256ae | 128 | lptmr_set(0xFFFF); |
bogdanm | 20:4263a77256ae | 129 | us_ticker_int_counter--; |
bogdanm | 20:4263a77256ae | 130 | } else { |
bogdanm | 20:4263a77256ae | 131 | lptmr_set(us_ticker_int_remainder); |
bogdanm | 20:4263a77256ae | 132 | us_ticker_int_remainder = 0; |
bogdanm | 20:4263a77256ae | 133 | } |
bogdanm | 20:4263a77256ae | 134 | } |