2015-09-21 08:57:54 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014, SICS Swedish ICT.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* RTIMER for NXP jn516x
|
|
|
|
* \author
|
|
|
|
* Beshr Al Nahas <beshr@sics.se>
|
2015-12-02 09:25:32 +00:00
|
|
|
* Atis Elsts <atis.elsts@sics.se>
|
2015-09-21 08:57:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sys/rtimer.h"
|
|
|
|
#include "sys/clock.h"
|
|
|
|
#include <AppHardwareApi.h>
|
|
|
|
#include <PeripheralRegs.h>
|
2015-12-02 09:25:32 +00:00
|
|
|
#include <MicroSpecific.h>
|
2015-09-21 08:57:54 +00:00
|
|
|
#include "dev/watchdog.h"
|
|
|
|
#include "sys/energest.h"
|
2015-12-02 09:25:32 +00:00
|
|
|
#include "sys/process.h"
|
2015-09-21 08:57:54 +00:00
|
|
|
|
2015-12-02 09:25:32 +00:00
|
|
|
#if !RTIMER_USE_32KHZ
|
2015-09-21 08:57:54 +00:00
|
|
|
|
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
2015-12-02 09:25:32 +00:00
|
|
|
#define RTIMER_TIMER_ISR_DEV E_AHI_DEVICE_TICK_TIMER
|
|
|
|
|
|
|
|
static volatile rtimer_clock_t scheduled_time;
|
|
|
|
static volatile uint8_t has_next;
|
2015-09-21 08:57:54 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
rtimer_arch_run_next(uint32 u32DeviceId, uint32 u32ItemBitmap)
|
|
|
|
{
|
2015-12-02 09:25:32 +00:00
|
|
|
uint32_t delta;
|
|
|
|
|
2015-09-21 08:57:54 +00:00
|
|
|
if(u32DeviceId != RTIMER_TIMER_ISR_DEV) {
|
|
|
|
return;
|
|
|
|
}
|
2015-12-02 09:25:32 +00:00
|
|
|
|
2015-09-21 08:57:54 +00:00
|
|
|
ENERGEST_ON(ENERGEST_TYPE_IRQ);
|
|
|
|
vAHI_TickTimerIntPendClr();
|
|
|
|
vAHI_TickTimerIntEnable(0);
|
|
|
|
/*
|
|
|
|
* compare register is only 28bits wide so make sure the upper 4bits match
|
|
|
|
* the set compare point
|
|
|
|
*/
|
2015-12-02 09:25:32 +00:00
|
|
|
delta = u32AHI_TickTimerRead() - scheduled_time;
|
|
|
|
if(delta >> 28 == 0) {
|
2015-09-21 08:57:54 +00:00
|
|
|
/* run scheduled */
|
2015-12-02 09:25:32 +00:00
|
|
|
has_next = 0;
|
2015-09-21 08:57:54 +00:00
|
|
|
watchdog_start();
|
|
|
|
rtimer_run_next();
|
2015-12-02 09:25:32 +00:00
|
|
|
process_nevents();
|
2015-09-21 08:57:54 +00:00
|
|
|
} else {
|
|
|
|
/* No match. Schedule again. */
|
|
|
|
vAHI_TickTimerIntEnable(1);
|
2015-12-02 09:25:32 +00:00
|
|
|
vAHI_TickTimerInterval(scheduled_time);
|
2015-09-21 08:57:54 +00:00
|
|
|
}
|
|
|
|
ENERGEST_OFF(ENERGEST_TYPE_IRQ);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
rtimer_arch_init(void)
|
|
|
|
{
|
|
|
|
/* Initialise tick timer to run continuously */
|
|
|
|
vAHI_TickTimerConfigure(E_AHI_TICK_TIMER_DISABLE);
|
2015-12-02 09:25:32 +00:00
|
|
|
vAHI_TickTimerIntEnable(0);
|
|
|
|
vAHI_TickTimerRegisterCallback(rtimer_arch_run_next);
|
2015-09-21 08:57:54 +00:00
|
|
|
vAHI_TickTimerWrite(0);
|
2015-12-02 09:25:32 +00:00
|
|
|
vAHI_TickTimerConfigure(E_AHI_TICK_TIMER_CONT);
|
|
|
|
|
|
|
|
/* enable wakeup timers, but keep interrupts disabled */
|
|
|
|
vAHI_WakeTimerEnable(WAKEUP_TIMER, FALSE);
|
|
|
|
vAHI_WakeTimerEnable(TICK_TIMER, FALSE);
|
|
|
|
/* count down from zero (2, as values 0 and 1 must not be used) */
|
|
|
|
vAHI_WakeTimerStartLarge(TICK_TIMER, 2);
|
|
|
|
|
|
|
|
(void)u32AHI_Init();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
rtimer_arch_reinit(rtimer_clock_t sleep_start, rtimer_clock_t sleep_ticks)
|
|
|
|
{
|
|
|
|
uint64_t t;
|
|
|
|
/* Initialise tick timer to run continuously */
|
|
|
|
vAHI_TickTimerConfigure(E_AHI_TICK_TIMER_DISABLE);
|
|
|
|
vAHI_TickTimerIntEnable(0);
|
|
|
|
/* set the highest priority for the rtimer interrupt */
|
|
|
|
vAHI_InterruptSetPriority(MICRO_ISR_MASK_TICK_TMR, 15);
|
2015-09-21 08:57:54 +00:00
|
|
|
vAHI_TickTimerRegisterCallback(rtimer_arch_run_next);
|
2015-12-02 09:25:32 +00:00
|
|
|
WAIT_FOR_EDGE(t);
|
|
|
|
vAHI_TickTimerWrite(sleep_start + sleep_ticks);
|
2015-09-21 08:57:54 +00:00
|
|
|
vAHI_TickTimerConfigure(E_AHI_TICK_TIMER_CONT);
|
2015-12-02 09:25:32 +00:00
|
|
|
|
|
|
|
/* call pending interrupts */
|
|
|
|
u32AHI_Init();
|
|
|
|
|
|
|
|
if(has_next) {
|
|
|
|
vAHI_TickTimerIntPendClr();
|
|
|
|
vAHI_TickTimerIntEnable(1);
|
|
|
|
vAHI_TickTimerInterval(scheduled_time);
|
|
|
|
}
|
2015-09-21 08:57:54 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
rtimer_clock_t
|
|
|
|
rtimer_arch_now(void)
|
|
|
|
{
|
|
|
|
return u32AHI_TickTimerRead();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
rtimer_arch_schedule(rtimer_clock_t t)
|
|
|
|
{
|
|
|
|
PRINTF("rtimer_arch_schedule time %lu\n", t);
|
|
|
|
vAHI_TickTimerIntPendClr();
|
|
|
|
vAHI_TickTimerIntEnable(1);
|
|
|
|
vAHI_TickTimerInterval(t);
|
2015-12-02 09:25:32 +00:00
|
|
|
has_next = 1;
|
|
|
|
scheduled_time = t;
|
2015-09-21 08:57:54 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
rtimer_clock_t
|
2015-12-02 09:25:32 +00:00
|
|
|
rtimer_arch_time_to_rtimer(void)
|
2015-09-21 08:57:54 +00:00
|
|
|
{
|
|
|
|
rtimer_clock_t now = RTIMER_NOW();
|
2015-12-02 09:25:32 +00:00
|
|
|
if(has_next) {
|
|
|
|
return scheduled_time >= now ? scheduled_time - now : 0;
|
2015-09-21 08:57:54 +00:00
|
|
|
}
|
2015-12-02 09:25:32 +00:00
|
|
|
/* if no wakeup is scheduled yet return maximum time */
|
2015-09-21 08:57:54 +00:00
|
|
|
return (rtimer_clock_t)-1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-12-02 09:25:32 +00:00
|
|
|
#endif /* !RTIMER_USE_32KHZ */
|