2017-02-16 09:37:24 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* Copyright (c) 2013-2016 Realtek Semiconductor Corp.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "objects.h"
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "us_ticker_api.h"
|
|
|
|
#include "PeripheralNames.h"
|
|
|
|
|
|
|
|
#define TICK_READ_FROM_CPU 0 // 1: read tick from CPU, 0: read tick from G-Timer
|
|
|
|
#define SYS_TIM_ID 1 // the G-Timer ID for System
|
|
|
|
#define APP_TIM_ID 6 // the G-Timer ID for Application
|
|
|
|
|
|
|
|
static int us_ticker_inited = 0;
|
|
|
|
static TIMER_ADAPTER TimerAdapter;
|
|
|
|
|
|
|
|
extern HAL_TIMER_OP HalTimerOp;
|
2017-03-29 06:18:34 +00:00
|
|
|
extern HAL_TIMER_OP_EXT HalTimerOpExt;
|
2017-02-16 09:37:24 +00:00
|
|
|
|
|
|
|
VOID _us_ticker_irq_handler(IN VOID *Data)
|
|
|
|
{
|
|
|
|
us_ticker_irq_handler();
|
|
|
|
}
|
|
|
|
|
|
|
|
void us_ticker_init(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (us_ticker_inited) return;
|
|
|
|
us_ticker_inited = 1;
|
2017-04-07 11:29:38 +00:00
|
|
|
|
2017-02-16 09:37:24 +00:00
|
|
|
|
|
|
|
// Initial a G-Timer
|
|
|
|
TimerAdapter.IrqDis = 0; // Enable Irq @ initial
|
|
|
|
TimerAdapter.IrqHandle.IrqFun = (IRQ_FUN) _us_ticker_irq_handler;
|
|
|
|
TimerAdapter.IrqHandle.IrqNum = TIMER2_7_IRQ;
|
|
|
|
TimerAdapter.IrqHandle.Priority = 10;
|
|
|
|
TimerAdapter.IrqHandle.Data = (u32)NULL;
|
|
|
|
TimerAdapter.TimerId = APP_TIM_ID;
|
|
|
|
TimerAdapter.TimerIrqPriority = 0;
|
|
|
|
TimerAdapter.TimerLoadValueUs = 0xFFFFFFFF;
|
|
|
|
TimerAdapter.TimerMode = USER_DEFINED;
|
|
|
|
|
|
|
|
HalTimerOp.HalTimerInit((VOID*) &TimerAdapter);
|
|
|
|
|
|
|
|
DBG_TIMER_INFO("%s: Timer_Id=%d\n", __FUNCTION__, APP_TIM_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t us_ticker_read()
|
|
|
|
{
|
|
|
|
uint32_t tick_cnt;
|
|
|
|
uint32_t ticks_125ms;
|
|
|
|
uint32_t ticks_remain;
|
|
|
|
uint64_t us_tick;
|
|
|
|
|
|
|
|
tick_cnt = HalTimerOp.HalTimerReadCount(SYS_TIM_ID);
|
|
|
|
tick_cnt = 0xffffffff - tick_cnt; // it's a down counter
|
2017-04-10 10:16:14 +00:00
|
|
|
ticks_125ms = tick_cnt/(GTIMER_CLK_HZ/8); //use 125ms as a intermediate unit;
|
|
|
|
ticks_remain = tick_cnt - (ticks_125ms*(GTIMER_CLK_HZ/8)); //calculate the remainder
|
|
|
|
us_tick = ticks_125ms * 125000; //change unit to us, 125ms is 125000 us
|
|
|
|
us_tick += (ticks_remain * 1000000)/GTIMER_CLK_HZ; //also use us as unit
|
2017-02-16 09:37:24 +00:00
|
|
|
|
2017-04-10 10:16:14 +00:00
|
|
|
return ((uint32_t)us_tick); //return ticker value in micro-seconds (us)
|
2017-02-16 09:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void us_ticker_set_interrupt(timestamp_t timestamp)
|
|
|
|
{
|
|
|
|
uint32_t cur_time_us;
|
|
|
|
uint32_t time_def;
|
|
|
|
|
|
|
|
|
|
|
|
cur_time_us = us_ticker_read();
|
|
|
|
if ((uint32_t)timestamp >= cur_time_us) {
|
|
|
|
time_def = (uint32_t)timestamp - cur_time_us;
|
2017-04-07 11:29:38 +00:00
|
|
|
} else {
|
2017-02-16 09:37:24 +00:00
|
|
|
time_def = 0xffffffff - cur_time_us + (uint32_t)timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (time_def < TIMER_TICK_US) {
|
|
|
|
time_def = TIMER_TICK_US; // at least 1 tick
|
|
|
|
}
|
2017-04-07 11:29:38 +00:00
|
|
|
HalTimerOp.HalTimerDis((u32)TimerAdapter.TimerId);
|
|
|
|
HalTimerOpExt.HalTimerReLoad((u32)TimerAdapter.TimerId, time_def);
|
|
|
|
HalTimerOpExt.HalTimerIrqEn((u32)TimerAdapter.TimerId);
|
|
|
|
HalTimerOp.HalTimerEn((u32)TimerAdapter.TimerId);
|
2017-02-16 09:37:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
Ticker: add fire interrupt now function
fire_interrupt function should be used for events in the past. As we have now
64bit timestamp, we can figure out what is in the past, and ask a target to invoke
an interrupt immediately. The previous attemps in the target HAL tickers were not ideal, as it can wrap around easily (16 or 32 bit counters). This new
functionality should solve this problem.
set_interrupt for tickers in HAL code should not handle anything but the next match interrupt. If it was in the past is handled by the upper layer.
It is possible that we are setting next event to the close future, so once it is set it is already in the past. Therefore we add a check after set interrupt to verify it is in future.
If it is not, we fire interrupt immediately. This results in
two events - first one immediate, correct one. The second one might be scheduled in far future (almost entire ticker range),
that should be discarded.
The specification for the fire_interrupts are:
- should set pending bit for the ticker interrupt (as soon as possible),
the event we are scheduling is already in the past, and we do not want to skip
any events
- no arguments are provided, neither return value, not needed
- ticker should be initialized prior calling this function (no need to check if it is already initialized)
All our targets provide this new functionality, removing old misleading if (timestamp is in the past) checks.
2017-06-27 11:18:59 +00:00
|
|
|
void us_ticker_fire_interrupt(void)
|
|
|
|
{
|
|
|
|
NVIC_SetPendingIRQ(TIMER2_7_IRQ);
|
|
|
|
}
|
|
|
|
|
2017-02-16 09:37:24 +00:00
|
|
|
void us_ticker_disable_interrupt(void)
|
|
|
|
{
|
|
|
|
HalTimerOp.HalTimerDis((u32)TimerAdapter.TimerId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void us_ticker_clear_interrupt(void)
|
|
|
|
{
|
|
|
|
HalTimerOp.HalTimerIrqClear((u32)TimerAdapter.TimerId);
|
|
|
|
}
|