1 /* mbed Microcontroller Library
2 *******************************************************************************
3 * Copyright (c) 2014, Realtek Semiconductor Corp.
4 * All rights reserved.
5 *
6 * This module is a confidential and proprietary property of RealTek and
7 * possession or use of this module requires written permission of RealTek.
8 *******************************************************************************
9 */
10 #include "objects.h"
11 #include <stddef.h>
12 #include "us_ticker_api.h"
13 #include "PeripheralNames.h"
14
15 #define TICK_READ_FROM_CPU 0 //1: read tick from CPU, 0: read tick from G-Timer
16 #define GTIMER_CLK_HZ (32768)
17 #define GTIMER_TICK_US (1000000/GTIMER_CLK_HZ)
18
19 #define SYS_TIM_ID 0 // the G-Timer ID for System
20 #define APP_TIM_ID 1 // the G-Timer ID for Application
21
22 static int us_ticker_inited = 0;
23
_us_ticker_irq_handler(void * Data)24 VOID _us_ticker_irq_handler(void *Data)
25 {
26 /* To avoid gcc warnings */
27 ( void ) Data;
28
29 us_ticker_irq_handler();
30 }
31
us_ticker_init(void)32 void us_ticker_init(void)
33 {
34 RTIM_TimeBaseInitTypeDef TIM_InitStruct;
35
36 if (us_ticker_inited)
37 return;
38 us_ticker_inited = 1;
39
40 RTIM_TimeBaseStructInit(&TIM_InitStruct);
41
42 TIM_InitStruct.TIM_Idx = APP_TIM_ID;
43 TIM_InitStruct.TIM_Prescaler = 0;
44 TIM_InitStruct.TIM_Period = 0xFFFFFFFF;
45
46 TIM_InitStruct.TIM_UpdateEvent = ENABLE; /* UEV enable */
47 TIM_InitStruct.TIM_UpdateSource = TIM_UpdateSource_Overflow;
48 TIM_InitStruct.TIM_ARRProtection = ENABLE;
49
50 RTIM_TimeBaseInit(TIMM01, &TIM_InitStruct, 0, (IRQ_FUN) _us_ticker_irq_handler, (u32)NULL);
51 RTIM_Cmd(TIMM01, ENABLE);
52
53 DBG_PRINTF(MODULE_TIMER, LEVEL_INFO, "%s: Timer_Id=%d\n", __FUNCTION__, APP_TIM_ID);
54 }
55
56 #if (!TICK_READ_FROM_CPU) || !defined(PLATFORM_FREERTOS)
us_ticker_read(void)57 uint32_t us_ticker_read(void)
58 {
59 uint32_t tick_cnt;
60 uint64_t us_tick;
61
62 tick_cnt = SYSTIMER_TickGet(); //up counter
63 us_tick = tick_cnt * (1000000/32768);
64
65 return ((uint32_t)us_tick);
66 }
67 #else
68 // if the system tick didn't be initialed, call delay function may got system hang
69 #define OS_CLOCK (200000000UL/6*5) // CPU clock = 166.66 MHz
70 #define OS_TICK 1000 // OS ticks 1000/sec
71 #define OS_TRV ((uint32_t)(((double)OS_CLOCK*(double)OS_TICK)/1E6)-1)
72 #define NVIC_ST_CTRL (*((volatile uint32_t *)0xE000E010))
73 #define NVIC_ST_RELOAD (*((volatile uint32_t *)0xE000E014))
74 #define NVIC_ST_CURRENT (*((volatile uint32_t *)0xE000E018))
75
76 extern uint32_t xTaskGetTickCount( void );
77
us_ticker_read(void)78 uint32_t us_ticker_read(void)
79 {
80 uint32_t tick_cnt;
81 uint32_t us_tick, ms;
82 static uint32_t last_us_tick=0;
83
84 ms = xTaskGetTickCount();
85 us_tick = (uint32_t)(ms*1000);
86
87 tick_cnt = OS_TRV - NVIC_ST_CURRENT;
88 us_tick += (uint32_t)((tick_cnt*1000)/(OS_TRV+1) );
89
90 if ( (last_us_tick > us_tick) && (last_us_tick < 0xFFFFFC00) ) {
91 us_tick += 1000;
92 }
93 last_us_tick = us_tick;
94
95 return us_tick;
96 }
97 #endif
98
us_ticker_set_interrupt(timestamp_t timestamp)99 void us_ticker_set_interrupt(timestamp_t timestamp)
100 {
101 uint32_t cur_time_us;
102 uint32_t duration_us;
103 uint32_t timer_tick = 0;
104
105 cur_time_us = us_ticker_read();
106 if ((uint32_t)timestamp >= cur_time_us) {
107 duration_us = (uint32_t)timestamp - cur_time_us;
108 }
109 else {
110 duration_us = 0xffffffff - cur_time_us + (uint32_t)timestamp;
111 }
112
113 if (duration_us < TIMER_TICK_US) {
114 duration_us = TIMER_TICK_US; // at least 1 tick
115 }
116
117 timer_tick = (uint32_t)((float)duration_us / 1000000 *32000);
118 RTIM_ChangePeriod(TIMx_LP[APP_TIM_ID], timer_tick);
119 }
120
us_ticker_disable_interrupt(void)121 void us_ticker_disable_interrupt(void)
122 {
123 RTIM_Cmd(TIMx_LP[APP_TIM_ID], DISABLE);
124 }
125
us_ticker_clear_interrupt(void)126 void us_ticker_clear_interrupt(void)
127 {
128 RTIM_INTClear(TIMx_LP[APP_TIM_ID]);//AembaD TODO
129 }
130