41 #include "sys/clock.h"
42 #include <AppHardwareApi.h>
43 #include <PeripheralRegs.h>
44 #include <MicroSpecific.h>
45 #include "dev/watchdog.h"
54 #define PRINTF(...) printf(__VA_ARGS__)
59 #define RTIMER_TIMER_ISR_DEV E_AHI_DEVICE_SYSCTRL
61 #define MAX_VALUE 0xFFFFFFFF
63 #define START_VALUE (60 * RTIMER_ARCH_SECOND)
64 #define WRAPAROUND_VALUE ((uint64_t)0x1FFFFFFFFFF)
66 static volatile rtimer_clock_t scheduled_time;
67 static volatile uint8_t has_next;
71 timerISR(uint32 u32Device, uint32 u32ItemBitmap)
73 PRINTF(
"\ntimer isr %u %u\n", u32Device, u32ItemBitmap);
74 if(u32Device != RTIMER_TIMER_ISR_DEV) {
78 ENERGEST_ON(ENERGEST_TYPE_IRQ);
80 if(u32ItemBitmap & TICK_TIMER_MASK) {
82 uint32_t ticks_late = WRAPAROUND_VALUE - u64AHI_WakeTimerReadLarge(TICK_TIMER);
84 PRINTF(
"\nrtimer oflw, missed ticks %u\n", ticks_late);
86 vAHI_WakeTimerStartLarge(TICK_TIMER, MAX_VALUE - ticks_late);
89 if(u32ItemBitmap & WAKEUP_TIMER_MASK) {
97 if(RTIMER_CLOCK_LT(now + 1, scheduled_time)) {
98 vAHI_WakeTimerEnable(WAKEUP_TIMER,
TRUE);
99 vAHI_WakeTimerStartLarge(WAKEUP_TIMER, scheduled_time - now);
108 ENERGEST_OFF(ENERGEST_TYPE_IRQ);
115 vAHI_TickTimerIntEnable(0);
116 vAHI_TickTimerConfigure(E_AHI_TICK_TIMER_DISABLE);
117 vAHI_TickTimerWrite(0);
118 vAHI_TickTimerConfigure(E_AHI_TICK_TIMER_CONT);
120 vAHI_SysCtrlRegisterCallback(timerISR);
122 vAHI_InterruptSetPriority(MICRO_ISR_MASK_SYSCTRL, 15);
124 vAHI_WakeTimerEnable(WAKEUP_TIMER,
TRUE);
126 vAHI_WakeTimerEnable(TICK_TIMER,
TRUE);
128 vAHI_WakeTimerStartLarge(TICK_TIMER, START_VALUE);
134 rtimer_arch_reinit(rtimer_clock_t sleep_start, rtimer_clock_t sleep_ticks)
138 uint32_t wakeup_time = sleep_start + (uint64_t)sleep_ticks * (F_CPU / 2) / RTIMER_SECOND;
141 vAHI_TickTimerConfigure(E_AHI_TICK_TIMER_DISABLE);
142 vAHI_TickTimerIntEnable(0);
144 vAHI_TickTimerWrite(wakeup_time);
145 vAHI_TickTimerConfigure(E_AHI_TICK_TIMER_CONT);
159 return START_VALUE - (rtimer_clock_t)u64AHI_WakeTimerReadLarge(TICK_TIMER);
165 PRINTF(
"rtimer_arch_schedule time %lu\n", t);
166 vAHI_WakeTimerEnable(WAKEUP_TIMER,
TRUE);
173 rtimer_arch_time_to_rtimer(
void)
177 return scheduled_time >= now ? scheduled_time - now : 0;
180 return (rtimer_clock_t)-1;
#define TRUE
An alias for one, used for clarity.
void rtimer_arch_init(void)
We don't need to explicitly initialise anything but this routine is required by the API...
Header file for the real-time timer module.
void rtimer_arch_schedule(rtimer_clock_t t)
Schedules an rtimer task to be triggered at time t.
#define RTIMER_NOW()
Get the current clock time.
Header file for the energy estimation mechanism
#define rtimer_arch_now()
void rtimer_run_next(void)
Execute the next real-time task and schedule the next task, if any.
Header file for the Contiki process interface.
int process_nevents(void)
Number of events waiting to be processed.
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.