42 #include <AppHardwareApi.h>
43 #include <PeripheralRegs.h>
46 #include "sys/clock.h"
49 #include "dev/watchdog.h"
55 #define PRINTF(...) printf(__VA_ARGS__)
60 #define CLOCK_TIMER E_AHI_TIMER_1
61 #define CLOCK_TIMER_ISR_DEV E_AHI_DEVICE_TIMER1
63 #define OVERFLOW_TIMER E_AHI_TIMER_0
64 #define OVERFLOW_TIMER_ISR_DEV E_AHI_DEVICE_TIMER0
67 #define CLOCK_PRESCALE 10
68 #define PRESCALED_TICKS_PER_SECOND 15625
70 #define CLOCK_INTERVAL 125
75 #define CLOCK_MAX_SCHEDULABLE_TICKS 520
77 #define CLOCK_RTIMER_GUARD_TIME US_TO_RTIMERTICKS(16)
79 #define CLOCK_TICK ((1 << CLOCK_PRESCALE) * CLOCK_INTERVAL)
81 #define RTIMER_OVERFLOW_PRESCALED 4194304
82 #define RTIMER_OVERFLOW_REMAINDER 54
85 #define CLOCK_LT(a, b) ((int32_t)((a)-(b)) < 0)
92 return u32AHI_TickTimerRead();
96 check_rtimer_overflow(rtimer_clock_t now)
98 static rtimer_clock_t last_rtimer_ticks;
99 static uint32_t clock_ticks_remainder;
100 static uint32_t clock_ticks_base;
102 if(last_rtimer_ticks > now) {
103 clock_ticks_base += RTIMER_OVERFLOW_PRESCALED / CLOCK_INTERVAL;
104 clock_ticks_remainder += RTIMER_OVERFLOW_REMAINDER;
105 if(clock_ticks_remainder > CLOCK_INTERVAL) {
106 clock_ticks_remainder -= CLOCK_INTERVAL;
107 clock_ticks_base += 1;
110 last_rtimer_ticks = now;
111 return clock_ticks_base;
127 clockTimerISR(uint32 u32Device, uint32 u32ItemBitmap)
129 if(u32Device != CLOCK_TIMER_ISR_DEV && u32Device != OVERFLOW_TIMER_ISR_DEV) {
133 ENERGEST_ON(ENERGEST_TYPE_IRQ);
135 if(u32Device == CLOCK_TIMER_ISR_DEV) {
139 if(u32Device == OVERFLOW_TIMER_ISR_DEV) {
140 check_rtimer_overflow(clock());
143 ENERGEST_OFF(ENERGEST_TYPE_IRQ);
147 clock_arch_calibrate(
void)
149 bAHI_SetClockRate(E_AHI_XTAL_32MHZ);
152 while(bAHI_GetClkSource() == 1) ;
153 while(bAHI_Clock32MHzStable() == 0) ;
155 vAHI_OptimiseWaitStates();
158 vREG_SysWrite(REG_SYS_PWR_CTRL, u32REG_SysRead(REG_SYS_PWR_CTRL)
159 | REG_SYSCTRL_PWRCTRL_SPIMEN_MASK);
163 clock_arch_init(
int is_reinitialization)
166 vAHI_TimerEnable(CLOCK_TIMER, CLOCK_PRESCALE, 0, 1, 0);
167 vAHI_TimerClockSelect(CLOCK_TIMER, 0, 0);
169 vAHI_TimerConfigureOutputs(CLOCK_TIMER, 0, 1);
170 vAHI_TimerDIOControl(CLOCK_TIMER, 0);
172 vAHI_Timer1RegisterCallback(clockTimerISR);
175 vAHI_TimerEnable(OVERFLOW_TIMER, CLOCK_PRESCALE, 0, 1, 0);
176 vAHI_TimerClockSelect(OVERFLOW_TIMER, 0, 0);
178 vAHI_TimerConfigureOutputs(OVERFLOW_TIMER, 0, 1);
179 vAHI_TimerDIOControl(OVERFLOW_TIMER, 0);
181 vAHI_Timer0RegisterCallback(clockTimerISR);
182 vAHI_TimerStartRepeat(OVERFLOW_TIMER, 0, PRESCALED_TICKS_PER_SECOND * 4);
184 if(is_reinitialization) {
186 check_rtimer_overflow(clock());
194 #ifdef JENNIC_CHIP_FAMILY_JN516x
196 *(
volatile uint32 *)0x020000a0 = 0;
199 clock_arch_calibrate();
208 uint32_t now = clock();
209 clock_time_t base = check_rtimer_overflow(now);
210 return base + now / CLOCK_TICK;
219 uint32_t end = clock() + dt;
221 while(CLOCK_LT(clock(), end));
230 uint32_t end = clock() + dt * 128;
231 while(CLOCK_LT(clock(), end)) {
256 clock_arch_time_to_etimer(
void)
258 clock_time_t time_to_etimer;
261 if((int32_t)time_to_etimer < 0) {
266 time_to_etimer = (clock_time_t)-1;
268 return time_to_etimer;
272 clock_arch_schedule_interrupt(clock_time_t time_to_etimer, rtimer_clock_t ticks_to_rtimer)
274 if(time_to_etimer > CLOCK_MAX_SCHEDULABLE_TICKS) {
275 time_to_etimer = CLOCK_MAX_SCHEDULABLE_TICKS;
278 time_to_etimer *= CLOCK_INTERVAL;
280 if(ticks_to_rtimer != (rtimer_clock_t)-1) {
282 rtimer_clock_t ticks_to_etimer = time_to_etimer * (1 << CLOCK_PRESCALE);
285 ticks_to_rtimer = (uint64_t)ticks_to_rtimer * (F_CPU / 2) / RTIMER_SECOND;
288 if(!CLOCK_LT(ticks_to_rtimer, ticks_to_etimer)
289 && CLOCK_LT(ticks_to_rtimer, ticks_to_etimer + CLOCK_RTIMER_GUARD_TIME)) {
296 if(time_to_etimer == 0) {
300 vAHI_TimerStartSingleShot(CLOCK_TIMER, 0, time_to_etimer);
clock_time_t etimer_next_expiration_time(void)
Get next event timer expiration time.
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
int etimer_pending(void)
Check if there are any non-expired event timers.
clock_time_t clock_time(void)
Get the current clock time.
Header file for the energy estimation mechanism
void clock_init()
Initialize the clock library.
void watchdog_periodic(void)
Writes the WDT clear sequence.
#define CLOCK_SECOND
A second, measured in system clock time.
void clock_delay(unsigned int i)
Obsolete delay function but we implement it here since some code still uses it.
void clock_wait(clock_time_t i)
Wait for a given number of ticks.
int process_nevents(void)
Number of events waiting to be processed.
unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Header file for NXP jn516x-specific rtimer code
void clock_delay_usec(uint16_t usec)
Delay a given number of microseconds.