added prescaler for 16 bit pwm in LPC1347 target
Fork of mbed-dev by
targets/hal/TARGET_Freescale/TARGET_KSDK2_MCUS/TARGET_KL27Z/us_ticker.c@147:ba84b7dc41a7, 2016-09-10 (annotated)
- Committer:
- JojoS
- Date:
- Sat Sep 10 15:32:04 2016 +0000
- Revision:
- 147:ba84b7dc41a7
- Parent:
- 121:7f86b4238bec
added prescaler for 16 bit timers (solution as in LPC11xx), default prescaler 31 for max 28 ms period time
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 | |
mbed_official | 121:7f86b4238bec | 25 | void us_ticker_init(void) { |
mbed_official | 121:7f86b4238bec | 26 | if (us_ticker_inited) { |
mbed_official | 121:7f86b4238bec | 27 | return; |
mbed_official | 121:7f86b4238bec | 28 | } |
mbed_official | 121:7f86b4238bec | 29 | us_ticker_inited = 1; |
mbed_official | 121:7f86b4238bec | 30 | |
mbed_official | 121:7f86b4238bec | 31 | // Need to initialize the clocks here as ticker init gets called before mbed_sdk_init |
mbed_official | 121:7f86b4238bec | 32 | if (SystemCoreClock == DEFAULT_SYSTEM_CLOCK) |
mbed_official | 121:7f86b4238bec | 33 | BOARD_BootClockRUN(); |
mbed_official | 121:7f86b4238bec | 34 | |
mbed_official | 121:7f86b4238bec | 35 | //Timer uses PIT |
mbed_official | 121:7f86b4238bec | 36 | //Common for ticker/timer |
mbed_official | 121:7f86b4238bec | 37 | uint32_t busClock; |
mbed_official | 121:7f86b4238bec | 38 | |
mbed_official | 121:7f86b4238bec | 39 | // Structure to initialize PIT |
mbed_official | 121:7f86b4238bec | 40 | pit_config_t pitConfig; |
mbed_official | 121:7f86b4238bec | 41 | |
mbed_official | 121:7f86b4238bec | 42 | PIT_GetDefaultConfig(&pitConfig); |
mbed_official | 121:7f86b4238bec | 43 | PIT_Init(PIT, &pitConfig); |
mbed_official | 121:7f86b4238bec | 44 | |
mbed_official | 121:7f86b4238bec | 45 | busClock = CLOCK_GetFreq(kCLOCK_BusClk); |
mbed_official | 121:7f86b4238bec | 46 | |
mbed_official | 121:7f86b4238bec | 47 | PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, busClock / 1000000 - 1); |
mbed_official | 121:7f86b4238bec | 48 | PIT_SetTimerPeriod(PIT, kPIT_Chnl_1, 0xFFFFFFFF); |
mbed_official | 121:7f86b4238bec | 49 | PIT_SetTimerChainMode(PIT, kPIT_Chnl_1, true); |
mbed_official | 121:7f86b4238bec | 50 | PIT_StartTimer(PIT, kPIT_Chnl_0); |
mbed_official | 121:7f86b4238bec | 51 | PIT_StartTimer(PIT, kPIT_Chnl_1); |
mbed_official | 121:7f86b4238bec | 52 | |
mbed_official | 121:7f86b4238bec | 53 | //Ticker uses LPTMR |
mbed_official | 121:7f86b4238bec | 54 | lptmr_config_t lptmrConfig; |
mbed_official | 121:7f86b4238bec | 55 | LPTMR_GetDefaultConfig(&lptmrConfig); |
mbed_official | 121:7f86b4238bec | 56 | lptmrConfig.prescalerClockSource = kLPTMR_PrescalerClock_0; |
mbed_official | 121:7f86b4238bec | 57 | LPTMR_Init(LPTMR0, &lptmrConfig); |
mbed_official | 121:7f86b4238bec | 58 | |
mbed_official | 121:7f86b4238bec | 59 | busClock = CLOCK_GetFreq(kCLOCK_McgInternalRefClk); |
mbed_official | 121:7f86b4238bec | 60 | LPTMR_SetTimerPeriod(LPTMR0, busClock / 1000000 - 1); |
mbed_official | 121:7f86b4238bec | 61 | /* Set interrupt handler */ |
mbed_official | 121:7f86b4238bec | 62 | NVIC_SetVector(LPTMR0_IRQn, (uint32_t)us_ticker_irq_handler); |
mbed_official | 121:7f86b4238bec | 63 | NVIC_EnableIRQ(LPTMR0_IRQn); |
mbed_official | 121:7f86b4238bec | 64 | } |
mbed_official | 121:7f86b4238bec | 65 | |
mbed_official | 121:7f86b4238bec | 66 | |
mbed_official | 121:7f86b4238bec | 67 | uint32_t us_ticker_read() { |
mbed_official | 121:7f86b4238bec | 68 | if (!us_ticker_inited) { |
mbed_official | 121:7f86b4238bec | 69 | us_ticker_init(); |
mbed_official | 121:7f86b4238bec | 70 | } |
mbed_official | 121:7f86b4238bec | 71 | |
mbed_official | 121:7f86b4238bec | 72 | return ~(PIT_GetCurrentTimerCount(PIT, kPIT_Chnl_1)); |
mbed_official | 121:7f86b4238bec | 73 | } |
mbed_official | 121:7f86b4238bec | 74 | |
mbed_official | 121:7f86b4238bec | 75 | void us_ticker_disable_interrupt(void) { |
mbed_official | 121:7f86b4238bec | 76 | LPTMR_DisableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable); |
mbed_official | 121:7f86b4238bec | 77 | } |
mbed_official | 121:7f86b4238bec | 78 | |
mbed_official | 121:7f86b4238bec | 79 | void us_ticker_clear_interrupt(void) { |
mbed_official | 121:7f86b4238bec | 80 | LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag); |
mbed_official | 121:7f86b4238bec | 81 | } |
mbed_official | 121:7f86b4238bec | 82 | |
mbed_official | 121:7f86b4238bec | 83 | void us_ticker_set_interrupt(timestamp_t timestamp) { |
mbed_official | 121:7f86b4238bec | 84 | int delta = (int)(timestamp - us_ticker_read()); |
mbed_official | 121:7f86b4238bec | 85 | if (delta <= 0) { |
mbed_official | 121:7f86b4238bec | 86 | // This event was in the past. |
mbed_official | 121:7f86b4238bec | 87 | // Set the interrupt as pending, but don't process it here. |
mbed_official | 121:7f86b4238bec | 88 | // This prevents a recurive loop under heavy load |
mbed_official | 121:7f86b4238bec | 89 | // which can lead to a stack overflow. |
mbed_official | 121:7f86b4238bec | 90 | NVIC_SetPendingIRQ(LPTMR0_IRQn); |
mbed_official | 121:7f86b4238bec | 91 | return; |
mbed_official | 121:7f86b4238bec | 92 | } |
mbed_official | 121:7f86b4238bec | 93 | |
mbed_official | 121:7f86b4238bec | 94 | LPTMR_StopTimer(LPTMR0); |
mbed_official | 121:7f86b4238bec | 95 | LPTMR_SetTimerPeriod(LPTMR0, (uint32_t)delta); |
mbed_official | 121:7f86b4238bec | 96 | LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable); |
mbed_official | 121:7f86b4238bec | 97 | LPTMR_StartTimer(LPTMR0); |
mbed_official | 121:7f86b4238bec | 98 | } |