2006-06-18 00:41:10 +02:00
|
|
|
/*
|
2011-09-23 15:01:13 +02:00
|
|
|
* Copyright (c) 2011, Swedish Institute of Computer Science
|
2007-03-25 23:51:31 +02:00
|
|
|
* All rights reserved.
|
2006-06-18 00:41:10 +02:00
|
|
|
*
|
2007-03-25 23:51:31 +02:00
|
|
|
* 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.
|
2006-06-18 00:41:10 +02:00
|
|
|
*
|
2007-03-25 23:51:31 +02:00
|
|
|
* 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.
|
2006-06-18 00:41:10 +02:00
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*/
|
|
|
|
|
2011-05-25 17:21:51 +02:00
|
|
|
#include "contiki.h"
|
2008-07-04 01:59:20 +02:00
|
|
|
#include "sys/energest.h"
|
2006-06-18 00:41:10 +02:00
|
|
|
#include "sys/clock.h"
|
|
|
|
#include "sys/etimer.h"
|
2008-12-02 13:44:48 +01:00
|
|
|
#include "rtimer-arch.h"
|
2011-09-21 20:02:04 +02:00
|
|
|
#include "dev/watchdog.h"
|
2012-03-07 01:14:54 +01:00
|
|
|
#include "isr_compat.h"
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2008-12-02 13:44:48 +01:00
|
|
|
#define INTERVAL (RTIMER_ARCH_SECOND / CLOCK_SECOND)
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2006-08-17 17:25:46 +02:00
|
|
|
#define MAX_TICKS (~((clock_time_t)0) / 2)
|
|
|
|
|
2014-08-21 18:09:54 +02:00
|
|
|
#define CLOCK_LT(a, b) ((int16_t)((a)-(b)) < 0)
|
|
|
|
|
2008-10-10 14:36:58 +02:00
|
|
|
static volatile unsigned long seconds;
|
2008-07-08 01:38:09 +02:00
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
static volatile clock_time_t count = 0;
|
2011-09-23 15:01:13 +02:00
|
|
|
/* last_tar is used for calculating clock_fine, last_ccr might be better? */
|
2010-12-16 23:50:21 +01:00
|
|
|
static volatile uint16_t last_tar = 0;
|
2006-06-18 00:41:10 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2014-08-21 18:09:54 +02:00
|
|
|
static inline uint16_t
|
|
|
|
read_tar(void)
|
|
|
|
{
|
|
|
|
/* Same as clock_counter(), but can be inlined */
|
|
|
|
uint16_t t1, t2;
|
|
|
|
do {
|
|
|
|
t1 = TA1R;
|
|
|
|
t2 = TA1R;
|
|
|
|
} while(t1 != t2);
|
|
|
|
return t1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-03-07 01:14:54 +01:00
|
|
|
ISR(TIMER1_A1, timera1)
|
2011-09-21 20:02:04 +02:00
|
|
|
{
|
|
|
|
ENERGEST_ON(ENERGEST_TYPE_IRQ);
|
|
|
|
|
2011-09-23 15:01:13 +02:00
|
|
|
/* watchdog_start(); */
|
2011-09-21 20:02:04 +02:00
|
|
|
|
2011-09-23 15:01:13 +02:00
|
|
|
if(TA1IV == 2) {
|
2011-09-21 20:02:04 +02:00
|
|
|
|
2011-09-23 15:01:13 +02:00
|
|
|
/* HW timer bug fix: Interrupt handler called before TR==CCR.
|
|
|
|
* Occurs when timer state is toggled between STOP and CONT. */
|
2011-09-21 20:02:04 +02:00
|
|
|
while(TA1CTL & MC1 && TA1CCR1 - TA1R == 1);
|
|
|
|
|
2014-08-21 18:09:54 +02:00
|
|
|
last_tar = read_tar();
|
2011-09-23 15:01:13 +02:00
|
|
|
/* Make sure interrupt time is future */
|
2014-08-21 18:09:54 +02:00
|
|
|
while(!CLOCK_LT(last_tar, TA1CCR1)) {
|
2011-09-21 20:02:04 +02:00
|
|
|
TA1CCR1 += INTERVAL;
|
|
|
|
++count;
|
|
|
|
|
|
|
|
/* Make sure the CLOCK_CONF_SECOND is a power of two, to ensure
|
2011-09-23 15:01:13 +02:00
|
|
|
that the modulo operation below becomes a logical and and not
|
|
|
|
an expensive divide. Algorithm from Wikipedia:
|
|
|
|
http://en.wikipedia.org/wiki/Power_of_two */
|
2011-09-21 20:02:04 +02:00
|
|
|
#if (CLOCK_CONF_SECOND & (CLOCK_CONF_SECOND - 1)) != 0
|
|
|
|
#error CLOCK_CONF_SECOND must be a power of two (i.e., 1, 2, 4, 8, 16, 32, 64, ...).
|
|
|
|
#error Change CLOCK_CONF_SECOND in contiki-conf.h.
|
|
|
|
#endif
|
|
|
|
if(count % CLOCK_CONF_SECOND == 0) {
|
2011-09-23 15:01:13 +02:00
|
|
|
++seconds;
|
2011-09-21 20:02:04 +02:00
|
|
|
energest_flush();
|
|
|
|
}
|
2014-08-21 18:09:54 +02:00
|
|
|
last_tar = read_tar();
|
|
|
|
}
|
2011-09-21 20:02:04 +02:00
|
|
|
|
|
|
|
if(etimer_pending() &&
|
|
|
|
(etimer_next_expiration_time() - count - 1) > MAX_TICKS) {
|
|
|
|
etimer_request_poll();
|
|
|
|
LPM4_EXIT;
|
|
|
|
}
|
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2009-12-09 00:40:14 +01:00
|
|
|
/* if(process_nevents() >= 0) {
|
|
|
|
LPM4_EXIT;
|
|
|
|
}*/
|
2010-12-16 23:50:21 +01:00
|
|
|
|
2011-09-23 15:01:13 +02:00
|
|
|
/* watchdog_stop(); */
|
|
|
|
|
2007-05-22 22:59:11 +02:00
|
|
|
ENERGEST_OFF(ENERGEST_TYPE_IRQ);
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
clock_time_t
|
|
|
|
clock_time(void)
|
|
|
|
{
|
2010-02-23 19:41:20 +01:00
|
|
|
clock_time_t t1, t2;
|
|
|
|
do {
|
|
|
|
t1 = count;
|
|
|
|
t2 = count;
|
|
|
|
} while(t1 != t2);
|
|
|
|
return t1;
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
clock_set(clock_time_t clock, clock_time_t fclock)
|
2007-03-25 23:51:31 +02:00
|
|
|
{
|
2011-09-21 20:02:04 +02:00
|
|
|
TA1R = fclock;
|
|
|
|
TA1CCR1 = fclock + INTERVAL;
|
2006-06-18 00:41:10 +02:00
|
|
|
count = clock;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-25 23:51:31 +02:00
|
|
|
int
|
|
|
|
clock_fine_max(void)
|
|
|
|
{
|
|
|
|
return INTERVAL;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
unsigned short
|
|
|
|
clock_fine(void)
|
|
|
|
{
|
|
|
|
unsigned short t;
|
2007-10-04 22:45:29 +02:00
|
|
|
/* Assign last_tar to local varible that can not be changed by interrupt */
|
|
|
|
t = last_tar;
|
|
|
|
/* perform calc based on t, TAR will not be changed during interrupt */
|
2011-09-21 20:02:04 +02:00
|
|
|
return (unsigned short) (TA1R - t);
|
2007-03-25 23:51:31 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
void
|
|
|
|
clock_init(void)
|
|
|
|
{
|
|
|
|
dint();
|
2011-09-23 15:01:13 +02:00
|
|
|
|
2011-09-21 20:02:04 +02:00
|
|
|
/* Select SMCLK (2.4576MHz), clear TAR */
|
2011-09-23 15:01:13 +02:00
|
|
|
/* TACTL = TASSEL1 | TACLR | ID_3; */
|
2011-09-21 20:02:04 +02:00
|
|
|
|
2011-09-23 15:01:13 +02:00
|
|
|
/* Select ACLK 32768Hz clock, divide by 2 */
|
|
|
|
/* TA1CTL = TASSEL0 | TACLR | ID_1; */
|
2011-09-21 20:02:04 +02:00
|
|
|
|
|
|
|
#if INTERVAL==32768/CLOCK_SECOND
|
|
|
|
TA1CTL = TASSEL0 | TACLR;
|
|
|
|
#elif INTERVAL==16384/CLOCK_SECOND
|
|
|
|
TA1CTL = TASSEL0 | TACLR | ID_1;
|
|
|
|
#else
|
|
|
|
#error NEED TO UPDATE clock.c to match interval!
|
|
|
|
#endif
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2011-09-21 20:02:04 +02:00
|
|
|
/* Initialize ccr1 to create the X ms interval. */
|
|
|
|
/* CCR1 interrupt enabled, interrupt occurs when timer equals CCR1. */
|
|
|
|
TA1CCTL1 = CCIE;
|
|
|
|
|
|
|
|
/* Interrupt after X ms. */
|
|
|
|
TA1CCR1 = INTERVAL;
|
|
|
|
|
|
|
|
/* Start Timer_A in continuous mode. */
|
|
|
|
TA1CTL |= MC1;
|
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
count = 0;
|
2007-04-02 18:32:20 +02:00
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
/* Enable interrupts. */
|
2007-03-25 23:51:31 +02:00
|
|
|
eint();
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
2007-03-25 23:51:31 +02:00
|
|
|
* Delay the CPU for a multiple of 2.83 us.
|
2006-06-18 00:41:10 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
clock_delay(unsigned int i)
|
|
|
|
{
|
2011-09-23 13:22:18 +02:00
|
|
|
while(i--) {
|
2011-10-06 14:05:57 +02:00
|
|
|
_NOP();
|
2011-09-23 13:22:18 +02:00
|
|
|
}
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* Wait for a multiple of 10 ms.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
2012-04-09 21:49:53 +02:00
|
|
|
clock_wait(clock_time_t i)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
|
|
|
clock_time_t start;
|
|
|
|
|
|
|
|
start = clock_time();
|
|
|
|
while(clock_time() - start < (clock_time_t)i);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
clock_set_seconds(unsigned long sec)
|
|
|
|
{
|
2011-09-23 15:01:13 +02:00
|
|
|
int s;
|
|
|
|
s = splhigh();
|
|
|
|
seconds = sec;
|
|
|
|
splx(s);
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2007-11-12 22:07:39 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
unsigned long
|
|
|
|
clock_seconds(void)
|
|
|
|
{
|
2008-10-10 14:36:58 +02:00
|
|
|
unsigned long t1, t2;
|
|
|
|
do {
|
|
|
|
t1 = seconds;
|
|
|
|
t2 = seconds;
|
|
|
|
} while(t1 != t2);
|
|
|
|
return t1;
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2007-11-12 22:07:39 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
rtimer_clock_t
|
|
|
|
clock_counter(void)
|
|
|
|
{
|
2011-09-23 15:01:13 +02:00
|
|
|
rtimer_clock_t t1, t2;
|
|
|
|
do {
|
|
|
|
t1 = TA1R;
|
|
|
|
t2 = TA1R;
|
|
|
|
} while(t1 != t2);
|
|
|
|
return t1;
|
2007-11-12 22:07:39 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|