|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2006-2015 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 <stddef.h> |
| 17 | +#include "us_ticker_api.h" |
| 18 | +#include "PeripheralNames.h" |
| 19 | +#include "clk_freqs.h" |
| 20 | + |
| 21 | +static int us_ticker_inited = 0; |
| 22 | + |
| 23 | +void us_ticker_init(void) { |
| 24 | + if (us_ticker_inited) |
| 25 | + return; |
| 26 | + us_ticker_inited = 1; |
| 27 | + |
| 28 | + SIM->SCGC6 |= SIM_SCGC6_PIT_MASK; // Clock PIT |
| 29 | + PIT->MCR = 0; // Enable PIT |
| 30 | + |
| 31 | + //Timer on PIT0+1, ticker on PIT 2+3 |
| 32 | + //Init timer |
| 33 | + PIT->CHANNEL[1].LDVAL = 0xFFFFFFFF; |
| 34 | + PIT->CHANNEL[1].TCTRL = PIT_TCTRL_CHN_MASK | PIT_TCTRL_TEN_MASK; // Start timer 1, chained to timer 0 |
| 35 | + |
| 36 | + // Use channel 0 as a prescaler for channel 1 |
| 37 | + uint32_t ldval = (bus_frequency() + 500000) / 1000000 - 1; |
| 38 | + PIT->CHANNEL[0].LDVAL = ldval; |
| 39 | + PIT->CHANNEL[0].TCTRL = PIT_TCTRL_TEN_MASK; // Start timer |
| 40 | + |
| 41 | + //Init ticker |
| 42 | + PIT->CHANNEL[2].LDVAL = ldval; |
| 43 | + PIT->CHANNEL[2].TCTRL = PIT_TCTRL_TEN_MASK; // Start timer 2 as prescaler |
| 44 | + |
| 45 | + NVIC_SetVector(PIT3_IRQn, (uint32_t)us_ticker_irq_handler); |
| 46 | + NVIC_EnableIRQ(PIT3_IRQn); |
| 47 | +} |
| 48 | + |
| 49 | +/****************************************************************************** |
| 50 | + * Timer for us timing. |
| 51 | + ******************************************************************************/ |
| 52 | + |
| 53 | +uint32_t us_ticker_read() { |
| 54 | + if (!us_ticker_inited) |
| 55 | + us_ticker_init(); |
| 56 | + |
| 57 | + return ~(PIT->CHANNEL[1].CVAL); |
| 58 | +} |
| 59 | + |
| 60 | +/****************************************************************************** |
| 61 | + * Ticker Event |
| 62 | + ******************************************************************************/ |
| 63 | + |
| 64 | +void us_ticker_disable_interrupt(void) { |
| 65 | + PIT->CHANNEL[3].TCTRL &= ~PIT_TCTRL_TIE_MASK; |
| 66 | +} |
| 67 | + |
| 68 | +void us_ticker_clear_interrupt(void) { |
| 69 | + PIT->CHANNEL[3].TFLG = 1; |
| 70 | +} |
| 71 | + |
| 72 | +void us_ticker_set_interrupt(timestamp_t timestamp) { |
| 73 | + int delta = (int)((uint32_t)timestamp - us_ticker_read()); |
| 74 | + if (delta <= 0) { |
| 75 | + // This event was in the past: |
| 76 | + us_ticker_irq_handler(); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + PIT->CHANNEL[3].TCTRL = 0; |
| 81 | + PIT->CHANNEL[3].LDVAL = delta; |
| 82 | + PIT->CHANNEL[3].TCTRL = PIT_TCTRL_TIE_MASK | PIT_TCTRL_TEN_MASK | PIT_TCTRL_CHN_MASK; |
| 83 | + |
| 84 | +} |
0 commit comments