mbed library sources

Dependents:   FRDM-KL46Z_LCD_Test FRDM-KL46Z_LCD_Test FRDM-KL46Z_Plantilla FRDM-KL46Z_Plantilla ... more

Committer:
ebrus
Date:
Thu Jul 28 15:56:34 2016 +0000
Revision:
0:6bc4ac881c8e
1;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ebrus 0:6bc4ac881c8e 1 /* mbed Microcontroller Library
ebrus 0:6bc4ac881c8e 2 * Copyright (c) 2006-2013 ARM Limited
ebrus 0:6bc4ac881c8e 3 *
ebrus 0:6bc4ac881c8e 4 * Licensed under the Apache License, Version 2.0 (the "License");
ebrus 0:6bc4ac881c8e 5 * you may not use this file except in compliance with the License.
ebrus 0:6bc4ac881c8e 6 * You may obtain a copy of the License at
ebrus 0:6bc4ac881c8e 7 *
ebrus 0:6bc4ac881c8e 8 * http://www.apache.org/licenses/LICENSE-2.0
ebrus 0:6bc4ac881c8e 9 *
ebrus 0:6bc4ac881c8e 10 * Unless required by applicable law or agreed to in writing, software
ebrus 0:6bc4ac881c8e 11 * distributed under the License is distributed on an "AS IS" BASIS,
ebrus 0:6bc4ac881c8e 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ebrus 0:6bc4ac881c8e 13 * See the License for the specific language governing permissions and
ebrus 0:6bc4ac881c8e 14 * limitations under the License.
ebrus 0:6bc4ac881c8e 15 */
ebrus 0:6bc4ac881c8e 16 #include <stddef.h>
ebrus 0:6bc4ac881c8e 17 #include "us_ticker_api.h"
ebrus 0:6bc4ac881c8e 18 #include "PeripheralNames.h"
ebrus 0:6bc4ac881c8e 19 #include "fsl_pit_hal.h"
ebrus 0:6bc4ac881c8e 20 #include "fsl_sim_hal.h"
ebrus 0:6bc4ac881c8e 21 #include "fsl_clock_manager.h"
ebrus 0:6bc4ac881c8e 22
ebrus 0:6bc4ac881c8e 23 static int us_ticker_inited = 0;
ebrus 0:6bc4ac881c8e 24
ebrus 0:6bc4ac881c8e 25 void us_ticker_init(void) {
ebrus 0:6bc4ac881c8e 26 if (us_ticker_inited) {
ebrus 0:6bc4ac881c8e 27 return;
ebrus 0:6bc4ac881c8e 28 }
ebrus 0:6bc4ac881c8e 29 us_ticker_inited = 1;
ebrus 0:6bc4ac881c8e 30
ebrus 0:6bc4ac881c8e 31 //Common for ticker/timer
ebrus 0:6bc4ac881c8e 32 uint32_t busClock;
ebrus 0:6bc4ac881c8e 33 CLOCK_SYS_EnablePitClock(0);
ebrus 0:6bc4ac881c8e 34 PIT_HAL_Enable(PIT_BASE);
ebrus 0:6bc4ac881c8e 35 CLOCK_SYS_GetFreq(kBusClock, &busClock);
ebrus 0:6bc4ac881c8e 36
ebrus 0:6bc4ac881c8e 37 //Timer
ebrus 0:6bc4ac881c8e 38 PIT_HAL_SetTimerPeriodByCount(PIT_BASE, 0, busClock / 1000000 - 1);
ebrus 0:6bc4ac881c8e 39 PIT_HAL_SetTimerPeriodByCount(PIT_BASE, 1, 0xFFFFFFFF);
ebrus 0:6bc4ac881c8e 40 PIT_HAL_SetTimerChainCmd(PIT_BASE, 1, true);
ebrus 0:6bc4ac881c8e 41 PIT_HAL_StartTimer(PIT_BASE, 0);
ebrus 0:6bc4ac881c8e 42 PIT_HAL_StartTimer(PIT_BASE, 1);
ebrus 0:6bc4ac881c8e 43
ebrus 0:6bc4ac881c8e 44 //Ticker
ebrus 0:6bc4ac881c8e 45 PIT_HAL_SetTimerPeriodByCount(PIT_BASE, 2, busClock / 1000000 - 1);
ebrus 0:6bc4ac881c8e 46 PIT_HAL_SetTimerChainCmd(PIT_BASE, 3, true);
ebrus 0:6bc4ac881c8e 47 NVIC_SetVector(PIT3_IRQn, (uint32_t)us_ticker_irq_handler);
ebrus 0:6bc4ac881c8e 48 NVIC_EnableIRQ(PIT3_IRQn);
ebrus 0:6bc4ac881c8e 49 }
ebrus 0:6bc4ac881c8e 50
ebrus 0:6bc4ac881c8e 51
ebrus 0:6bc4ac881c8e 52 uint32_t us_ticker_read() {
ebrus 0:6bc4ac881c8e 53 if (!us_ticker_inited) {
ebrus 0:6bc4ac881c8e 54 us_ticker_init();
ebrus 0:6bc4ac881c8e 55 }
ebrus 0:6bc4ac881c8e 56
ebrus 0:6bc4ac881c8e 57 return ~(PIT_HAL_ReadTimerCount(PIT_BASE, 1));
ebrus 0:6bc4ac881c8e 58 }
ebrus 0:6bc4ac881c8e 59
ebrus 0:6bc4ac881c8e 60 void us_ticker_disable_interrupt(void) {
ebrus 0:6bc4ac881c8e 61 PIT_HAL_SetIntCmd(PIT_BASE, 3, false);
ebrus 0:6bc4ac881c8e 62 }
ebrus 0:6bc4ac881c8e 63
ebrus 0:6bc4ac881c8e 64 void us_ticker_clear_interrupt(void) {
ebrus 0:6bc4ac881c8e 65 PIT_HAL_ClearIntFlag(PIT_BASE, 3);
ebrus 0:6bc4ac881c8e 66 }
ebrus 0:6bc4ac881c8e 67
ebrus 0:6bc4ac881c8e 68 void us_ticker_set_interrupt(timestamp_t timestamp) {
ebrus 0:6bc4ac881c8e 69 int delta = (int)(timestamp - us_ticker_read());
ebrus 0:6bc4ac881c8e 70 if (delta <= 0) {
ebrus 0:6bc4ac881c8e 71 // This event was in the past:
ebrus 0:6bc4ac881c8e 72 us_ticker_irq_handler();
ebrus 0:6bc4ac881c8e 73 return;
ebrus 0:6bc4ac881c8e 74 }
ebrus 0:6bc4ac881c8e 75
ebrus 0:6bc4ac881c8e 76 PIT_HAL_StopTimer(PIT_BASE, 3);
ebrus 0:6bc4ac881c8e 77 PIT_HAL_StopTimer(PIT_BASE, 2);
ebrus 0:6bc4ac881c8e 78 PIT_HAL_SetTimerPeriodByCount(PIT_BASE, 3, (uint32_t)delta);
ebrus 0:6bc4ac881c8e 79 PIT_HAL_SetIntCmd(PIT_BASE, 3, true);
ebrus 0:6bc4ac881c8e 80 PIT_HAL_StartTimer(PIT_BASE, 3);
ebrus 0:6bc4ac881c8e 81 PIT_HAL_StartTimer(PIT_BASE, 2);
ebrus 0:6bc4ac881c8e 82 }