DongEun Koak / mbed-src

Dependents:   Freedman_v2 Nucleo_i2c_OLED_BME280_copy

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Sep 23 08:30:06 2014 +0100
Revision:
329:e2a9f2c7ad2b
Parent:
324:406fd2029f23
Synchronized with git revision 347b41de66af15376c2e94c6e15c66a822fba2a6

Full URL: https://github.com/mbedmicro/mbed/commit/347b41de66af15376c2e94c6e15c66a822fba2a6/

[HAL] Freescale KSDK - Use PIT timer for ticker/timeout

Who changed what in which revision?

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