Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-dev by
targets/TARGET_Freescale/TARGET_KSDK2_MCUS/TARGET_KL43Z/us_ticker.c@153:9398a535854b, 2016-12-22 (annotated)
- Committer:
- fwndz
- Date:
- Thu Dec 22 05:12:40 2016 +0000
- Revision:
- 153:9398a535854b
- Parent:
- 152:9a67f0b066fc
device target maximize
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 121:7f86b4238bec | 1 | /* mbed Microcontroller Library |
mbed_official | 121:7f86b4238bec | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 121:7f86b4238bec | 3 | * |
mbed_official | 121:7f86b4238bec | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 121:7f86b4238bec | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 121:7f86b4238bec | 6 | * You may obtain a copy of the License at |
mbed_official | 121:7f86b4238bec | 7 | * |
mbed_official | 121:7f86b4238bec | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 121:7f86b4238bec | 9 | * |
mbed_official | 121:7f86b4238bec | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 121:7f86b4238bec | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 121:7f86b4238bec | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 121:7f86b4238bec | 13 | * See the License for the specific language governing permissions and |
mbed_official | 121:7f86b4238bec | 14 | * limitations under the License. |
mbed_official | 121:7f86b4238bec | 15 | */ |
mbed_official | 121:7f86b4238bec | 16 | #include <stddef.h> |
mbed_official | 121:7f86b4238bec | 17 | #include "us_ticker_api.h" |
mbed_official | 121:7f86b4238bec | 18 | #include "PeripheralNames.h" |
mbed_official | 121:7f86b4238bec | 19 | #include "fsl_pit.h" |
mbed_official | 121:7f86b4238bec | 20 | #include "fsl_lptmr.h" |
mbed_official | 121:7f86b4238bec | 21 | #include "fsl_clock_config.h" |
mbed_official | 121:7f86b4238bec | 22 | |
mbed_official | 121:7f86b4238bec | 23 | static int us_ticker_inited = 0; |
mbed_official | 121:7f86b4238bec | 24 | |
<> | 152:9a67f0b066fc | 25 | void us_ticker_init(void) |
<> | 152:9a67f0b066fc | 26 | { |
mbed_official | 121:7f86b4238bec | 27 | if (us_ticker_inited) { |
mbed_official | 121:7f86b4238bec | 28 | return; |
mbed_official | 121:7f86b4238bec | 29 | } |
mbed_official | 121:7f86b4238bec | 30 | us_ticker_inited = 1; |
mbed_official | 121:7f86b4238bec | 31 | |
mbed_official | 121:7f86b4238bec | 32 | //Timer uses PIT |
mbed_official | 121:7f86b4238bec | 33 | //Common for ticker/timer |
mbed_official | 121:7f86b4238bec | 34 | uint32_t busClock; |
mbed_official | 121:7f86b4238bec | 35 | |
mbed_official | 121:7f86b4238bec | 36 | // Structure to initialize PIT |
mbed_official | 121:7f86b4238bec | 37 | pit_config_t pitConfig; |
mbed_official | 121:7f86b4238bec | 38 | |
mbed_official | 121:7f86b4238bec | 39 | PIT_GetDefaultConfig(&pitConfig); |
mbed_official | 121:7f86b4238bec | 40 | PIT_Init(PIT, &pitConfig); |
mbed_official | 121:7f86b4238bec | 41 | |
mbed_official | 121:7f86b4238bec | 42 | busClock = CLOCK_GetFreq(kCLOCK_BusClk); |
mbed_official | 121:7f86b4238bec | 43 | |
mbed_official | 121:7f86b4238bec | 44 | PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, busClock / 1000000 - 1); |
mbed_official | 121:7f86b4238bec | 45 | PIT_SetTimerPeriod(PIT, kPIT_Chnl_1, 0xFFFFFFFF); |
mbed_official | 121:7f86b4238bec | 46 | PIT_SetTimerChainMode(PIT, kPIT_Chnl_1, true); |
mbed_official | 121:7f86b4238bec | 47 | PIT_StartTimer(PIT, kPIT_Chnl_0); |
mbed_official | 121:7f86b4238bec | 48 | PIT_StartTimer(PIT, kPIT_Chnl_1); |
mbed_official | 121:7f86b4238bec | 49 | |
mbed_official | 121:7f86b4238bec | 50 | //Ticker uses LPTMR |
mbed_official | 121:7f86b4238bec | 51 | lptmr_config_t lptmrConfig; |
mbed_official | 121:7f86b4238bec | 52 | LPTMR_GetDefaultConfig(&lptmrConfig); |
mbed_official | 121:7f86b4238bec | 53 | lptmrConfig.prescalerClockSource = kLPTMR_PrescalerClock_0; |
mbed_official | 121:7f86b4238bec | 54 | LPTMR_Init(LPTMR0, &lptmrConfig); |
mbed_official | 121:7f86b4238bec | 55 | |
mbed_official | 121:7f86b4238bec | 56 | busClock = CLOCK_GetFreq(kCLOCK_McgInternalRefClk); |
mbed_official | 121:7f86b4238bec | 57 | LPTMR_SetTimerPeriod(LPTMR0, busClock / 1000000 - 1); |
mbed_official | 121:7f86b4238bec | 58 | /* Set interrupt handler */ |
mbed_official | 121:7f86b4238bec | 59 | NVIC_SetVector(LPTMR0_IRQn, (uint32_t)us_ticker_irq_handler); |
mbed_official | 121:7f86b4238bec | 60 | NVIC_EnableIRQ(LPTMR0_IRQn); |
mbed_official | 121:7f86b4238bec | 61 | } |
mbed_official | 121:7f86b4238bec | 62 | |
mbed_official | 121:7f86b4238bec | 63 | |
<> | 152:9a67f0b066fc | 64 | uint32_t us_ticker_read() |
<> | 152:9a67f0b066fc | 65 | { |
mbed_official | 121:7f86b4238bec | 66 | if (!us_ticker_inited) { |
mbed_official | 121:7f86b4238bec | 67 | us_ticker_init(); |
mbed_official | 121:7f86b4238bec | 68 | } |
mbed_official | 121:7f86b4238bec | 69 | |
mbed_official | 121:7f86b4238bec | 70 | return ~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1)); |
mbed_official | 121:7f86b4238bec | 71 | } |
mbed_official | 121:7f86b4238bec | 72 | |
<> | 152:9a67f0b066fc | 73 | void us_ticker_disable_interrupt(void) |
<> | 152:9a67f0b066fc | 74 | { |
mbed_official | 121:7f86b4238bec | 75 | LPTMR_DisableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable); |
mbed_official | 121:7f86b4238bec | 76 | } |
mbed_official | 121:7f86b4238bec | 77 | |
<> | 152:9a67f0b066fc | 78 | void us_ticker_clear_interrupt(void) |
<> | 152:9a67f0b066fc | 79 | { |
mbed_official | 121:7f86b4238bec | 80 | LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag); |
mbed_official | 121:7f86b4238bec | 81 | } |
mbed_official | 121:7f86b4238bec | 82 | |
<> | 152:9a67f0b066fc | 83 | void us_ticker_set_interrupt(timestamp_t timestamp) |
<> | 152:9a67f0b066fc | 84 | { |
mbed_official | 121:7f86b4238bec | 85 | int delta = (int)(timestamp - us_ticker_read()); |
mbed_official | 121:7f86b4238bec | 86 | if (delta <= 0) { |
mbed_official | 121:7f86b4238bec | 87 | // This event was in the past. |
mbed_official | 121:7f86b4238bec | 88 | // Set the interrupt as pending, but don't process it here. |
mbed_official | 121:7f86b4238bec | 89 | // This prevents a recurive loop under heavy load |
mbed_official | 121:7f86b4238bec | 90 | // which can lead to a stack overflow. |
mbed_official | 121:7f86b4238bec | 91 | NVIC_SetPendingIRQ(LPTMR0_IRQn); |
mbed_official | 121:7f86b4238bec | 92 | return; |
mbed_official | 121:7f86b4238bec | 93 | } |
mbed_official | 121:7f86b4238bec | 94 | |
mbed_official | 121:7f86b4238bec | 95 | LPTMR_StopTimer(LPTMR0); |
mbed_official | 121:7f86b4238bec | 96 | LPTMR_SetTimerPeriod(LPTMR0, (uint32_t)delta); |
mbed_official | 121:7f86b4238bec | 97 | LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable); |
mbed_official | 121:7f86b4238bec | 98 | LPTMR_StartTimer(LPTMR0); |
mbed_official | 121:7f86b4238bec | 99 | } |