forked

Committer:
AnnaBridge
Date:
Wed Jun 21 17:46:44 2017 +0100
Revision:
167:e84263d55307
This updates the lib to the mbed lib v 145

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 167:e84263d55307 1 /* mbed Microcontroller Library
AnnaBridge 167:e84263d55307 2 * Copyright (c) 2013-2016 Realtek Semiconductor Corp.
AnnaBridge 167:e84263d55307 3 *
AnnaBridge 167:e84263d55307 4 * Licensed under the Apache License, Version 2.0 (the "License");
AnnaBridge 167:e84263d55307 5 * you may not use this file except in compliance with the License.
AnnaBridge 167:e84263d55307 6 * You may obtain a copy of the License at
AnnaBridge 167:e84263d55307 7 *
AnnaBridge 167:e84263d55307 8 * http://www.apache.org/licenses/LICENSE-2.0
AnnaBridge 167:e84263d55307 9 *
AnnaBridge 167:e84263d55307 10 * Unless required by applicable law or agreed to in writing, software
AnnaBridge 167:e84263d55307 11 * distributed under the License is distributed on an "AS IS" BASIS,
AnnaBridge 167:e84263d55307 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AnnaBridge 167:e84263d55307 13 * See the License for the specific language governing permissions and
AnnaBridge 167:e84263d55307 14 * limitations under the License.
AnnaBridge 167:e84263d55307 15 */
AnnaBridge 167:e84263d55307 16
AnnaBridge 167:e84263d55307 17 #include "objects.h"
AnnaBridge 167:e84263d55307 18 #include <stddef.h>
AnnaBridge 167:e84263d55307 19 #include "us_ticker_api.h"
AnnaBridge 167:e84263d55307 20 #include "PeripheralNames.h"
AnnaBridge 167:e84263d55307 21
AnnaBridge 167:e84263d55307 22 #define TICK_READ_FROM_CPU 0 // 1: read tick from CPU, 0: read tick from G-Timer
AnnaBridge 167:e84263d55307 23 #define SYS_TIM_ID 1 // the G-Timer ID for System
AnnaBridge 167:e84263d55307 24 #define APP_TIM_ID 6 // the G-Timer ID for Application
AnnaBridge 167:e84263d55307 25
AnnaBridge 167:e84263d55307 26 static int us_ticker_inited = 0;
AnnaBridge 167:e84263d55307 27 static TIMER_ADAPTER TimerAdapter;
AnnaBridge 167:e84263d55307 28
AnnaBridge 167:e84263d55307 29 extern HAL_TIMER_OP HalTimerOp;
AnnaBridge 167:e84263d55307 30 extern HAL_TIMER_OP_EXT HalTimerOpExt;
AnnaBridge 167:e84263d55307 31
AnnaBridge 167:e84263d55307 32 VOID _us_ticker_irq_handler(IN VOID *Data)
AnnaBridge 167:e84263d55307 33 {
AnnaBridge 167:e84263d55307 34 us_ticker_irq_handler();
AnnaBridge 167:e84263d55307 35 }
AnnaBridge 167:e84263d55307 36
AnnaBridge 167:e84263d55307 37 void us_ticker_init(void)
AnnaBridge 167:e84263d55307 38 {
AnnaBridge 167:e84263d55307 39
AnnaBridge 167:e84263d55307 40 if (us_ticker_inited) return;
AnnaBridge 167:e84263d55307 41 us_ticker_inited = 1;
AnnaBridge 167:e84263d55307 42
AnnaBridge 167:e84263d55307 43
AnnaBridge 167:e84263d55307 44 // Initial a G-Timer
AnnaBridge 167:e84263d55307 45 TimerAdapter.IrqDis = 0; // Enable Irq @ initial
AnnaBridge 167:e84263d55307 46 TimerAdapter.IrqHandle.IrqFun = (IRQ_FUN) _us_ticker_irq_handler;
AnnaBridge 167:e84263d55307 47 TimerAdapter.IrqHandle.IrqNum = TIMER2_7_IRQ;
AnnaBridge 167:e84263d55307 48 TimerAdapter.IrqHandle.Priority = 10;
AnnaBridge 167:e84263d55307 49 TimerAdapter.IrqHandle.Data = (u32)NULL;
AnnaBridge 167:e84263d55307 50 TimerAdapter.TimerId = APP_TIM_ID;
AnnaBridge 167:e84263d55307 51 TimerAdapter.TimerIrqPriority = 0;
AnnaBridge 167:e84263d55307 52 TimerAdapter.TimerLoadValueUs = 0xFFFFFFFF;
AnnaBridge 167:e84263d55307 53 TimerAdapter.TimerMode = USER_DEFINED;
AnnaBridge 167:e84263d55307 54
AnnaBridge 167:e84263d55307 55 HalTimerOp.HalTimerInit((VOID*) &TimerAdapter);
AnnaBridge 167:e84263d55307 56
AnnaBridge 167:e84263d55307 57 DBG_TIMER_INFO("%s: Timer_Id=%d\n", __FUNCTION__, APP_TIM_ID);
AnnaBridge 167:e84263d55307 58 }
AnnaBridge 167:e84263d55307 59
AnnaBridge 167:e84263d55307 60 uint32_t us_ticker_read()
AnnaBridge 167:e84263d55307 61 {
AnnaBridge 167:e84263d55307 62 uint32_t tick_cnt;
AnnaBridge 167:e84263d55307 63 uint32_t ticks_125ms;
AnnaBridge 167:e84263d55307 64 uint32_t ticks_remain;
AnnaBridge 167:e84263d55307 65 uint64_t us_tick;
AnnaBridge 167:e84263d55307 66
AnnaBridge 167:e84263d55307 67 tick_cnt = HalTimerOp.HalTimerReadCount(SYS_TIM_ID);
AnnaBridge 167:e84263d55307 68 tick_cnt = 0xffffffff - tick_cnt; // it's a down counter
AnnaBridge 167:e84263d55307 69 ticks_125ms = tick_cnt/(GTIMER_CLK_HZ/8); //use 125ms as a intermediate unit;
AnnaBridge 167:e84263d55307 70 ticks_remain = tick_cnt - (ticks_125ms*(GTIMER_CLK_HZ/8)); //calculate the remainder
AnnaBridge 167:e84263d55307 71 us_tick = ticks_125ms * 125000; //change unit to us, 125ms is 125000 us
AnnaBridge 167:e84263d55307 72 us_tick += (ticks_remain * 1000000)/GTIMER_CLK_HZ; //also use us as unit
AnnaBridge 167:e84263d55307 73
AnnaBridge 167:e84263d55307 74 return ((uint32_t)us_tick); //return ticker value in micro-seconds (us)
AnnaBridge 167:e84263d55307 75 }
AnnaBridge 167:e84263d55307 76
AnnaBridge 167:e84263d55307 77 void us_ticker_set_interrupt(timestamp_t timestamp)
AnnaBridge 167:e84263d55307 78 {
AnnaBridge 167:e84263d55307 79 uint32_t cur_time_us;
AnnaBridge 167:e84263d55307 80 uint32_t time_def;
AnnaBridge 167:e84263d55307 81
AnnaBridge 167:e84263d55307 82
AnnaBridge 167:e84263d55307 83 cur_time_us = us_ticker_read();
AnnaBridge 167:e84263d55307 84 if ((uint32_t)timestamp >= cur_time_us) {
AnnaBridge 167:e84263d55307 85 time_def = (uint32_t)timestamp - cur_time_us;
AnnaBridge 167:e84263d55307 86 } else {
AnnaBridge 167:e84263d55307 87 time_def = 0xffffffff - cur_time_us + (uint32_t)timestamp;
AnnaBridge 167:e84263d55307 88 }
AnnaBridge 167:e84263d55307 89
AnnaBridge 167:e84263d55307 90 if (time_def < TIMER_TICK_US) {
AnnaBridge 167:e84263d55307 91 time_def = TIMER_TICK_US; // at least 1 tick
AnnaBridge 167:e84263d55307 92 }
AnnaBridge 167:e84263d55307 93 HalTimerOp.HalTimerDis((u32)TimerAdapter.TimerId);
AnnaBridge 167:e84263d55307 94 HalTimerOpExt.HalTimerReLoad((u32)TimerAdapter.TimerId, time_def);
AnnaBridge 167:e84263d55307 95 HalTimerOpExt.HalTimerIrqEn((u32)TimerAdapter.TimerId);
AnnaBridge 167:e84263d55307 96 HalTimerOp.HalTimerEn((u32)TimerAdapter.TimerId);
AnnaBridge 167:e84263d55307 97
AnnaBridge 167:e84263d55307 98 }
AnnaBridge 167:e84263d55307 99
AnnaBridge 167:e84263d55307 100 void us_ticker_disable_interrupt(void)
AnnaBridge 167:e84263d55307 101 {
AnnaBridge 167:e84263d55307 102 HalTimerOp.HalTimerDis((u32)TimerAdapter.TimerId);
AnnaBridge 167:e84263d55307 103 }
AnnaBridge 167:e84263d55307 104
AnnaBridge 167:e84263d55307 105 void us_ticker_clear_interrupt(void)
AnnaBridge 167:e84263d55307 106 {
AnnaBridge 167:e84263d55307 107 HalTimerOp.HalTimerIrqClear((u32)TimerAdapter.TimerId);
AnnaBridge 167:e84263d55307 108 }