2010-10-25 11:03:38 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, STMicroelectronics.
|
|
|
|
* 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. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 OS
|
|
|
|
*
|
|
|
|
* $Id: clock.c,v 1.1 2010/10/25 09:03:38 salvopitru Exp $
|
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Clock.
|
|
|
|
* \author
|
|
|
|
* Salvatore Pitrulli <salvopitru@users.sourceforge.net>
|
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#include PLATFORM_HEADER
|
|
|
|
#include "hal/error.h"
|
|
|
|
#include "hal/hal.h"
|
|
|
|
#include "dev/stm32w_systick.h"
|
|
|
|
|
2011-04-08 13:38:16 +02:00
|
|
|
#include "contiki.h"
|
2012-04-09 21:49:53 +02:00
|
|
|
#include "sys/clock.h"
|
2011-04-08 13:38:16 +02:00
|
|
|
|
|
|
|
#include "uart1.h"
|
|
|
|
#include "dev/leds.h"
|
|
|
|
#include "dev/stm32w-radio.h"
|
|
|
|
|
|
|
|
#define DEBUG DEBUG_NONE
|
|
|
|
#include "net/uip-debug.h"
|
2010-10-25 11:03:38 +02:00
|
|
|
|
2012-04-22 20:54:29 +02:00
|
|
|
/* The value that will be load in the SysTick value register. */
|
|
|
|
#define RELOAD_VALUE 24000-1 /* 1 ms with a 24 MHz clock */
|
2010-10-25 11:03:38 +02:00
|
|
|
|
|
|
|
static volatile clock_time_t count;
|
|
|
|
static volatile unsigned long current_seconds = 0;
|
|
|
|
static unsigned int second_countdown = CLOCK_SECOND;
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-04-22 20:54:29 +02:00
|
|
|
void
|
|
|
|
SysTick_Handler(void)
|
2010-10-25 11:03:38 +02:00
|
|
|
{
|
|
|
|
count++;
|
|
|
|
|
|
|
|
if(etimer_pending()) {
|
|
|
|
etimer_request_poll();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (--second_countdown == 0) {
|
|
|
|
current_seconds++;
|
|
|
|
second_countdown = CLOCK_SECOND;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-04-22 20:54:29 +02:00
|
|
|
void
|
|
|
|
clock_init(void)
|
2010-10-25 11:03:38 +02:00
|
|
|
{
|
|
|
|
|
2011-04-08 13:38:16 +02:00
|
|
|
ATOMIC(
|
|
|
|
|
|
|
|
//Counts the number of ticks.
|
2010-10-25 11:03:38 +02:00
|
|
|
count = 0;
|
|
|
|
|
|
|
|
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
|
|
|
|
SysTick_SetReload(RELOAD_VALUE);
|
|
|
|
SysTick_ITConfig(ENABLE);
|
|
|
|
SysTick_CounterCmd(SysTick_Counter_Enable);
|
|
|
|
|
2011-04-08 13:38:16 +02:00
|
|
|
)
|
|
|
|
|
2010-10-25 11:03:38 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-04-22 20:54:29 +02:00
|
|
|
clock_time_t
|
|
|
|
clock_time(void)
|
2010-10-25 11:03:38 +02:00
|
|
|
{
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* Delay the CPU for a multiple of TODO
|
|
|
|
*/
|
2012-04-22 20:54:29 +02:00
|
|
|
void
|
|
|
|
clock_delay(unsigned int i)
|
2010-10-25 11:03:38 +02:00
|
|
|
{
|
|
|
|
for (; i > 0; i--) { /* Needs fixing XXX */
|
|
|
|
unsigned j;
|
|
|
|
for (j = 50; j > 0; j--)
|
|
|
|
asm ("nop");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* Wait for a multiple of 1 ms.
|
|
|
|
*
|
|
|
|
*/
|
2012-04-22 20:54:29 +02:00
|
|
|
void
|
|
|
|
clock_wait(clock_time_t i)
|
2010-10-25 11:03:38 +02:00
|
|
|
{
|
|
|
|
clock_time_t start;
|
|
|
|
|
|
|
|
start = clock_time();
|
|
|
|
while(clock_time() - start < (clock_time_t)i);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-04-22 20:54:29 +02:00
|
|
|
unsigned long
|
|
|
|
clock_seconds(void)
|
2010-10-25 11:03:38 +02:00
|
|
|
{
|
|
|
|
return current_seconds;
|
|
|
|
}
|
2011-04-08 13:38:16 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2012-04-22 20:54:29 +02:00
|
|
|
void
|
|
|
|
sleep_seconds(int seconds)
|
2011-04-08 13:38:16 +02:00
|
|
|
{
|
|
|
|
int32u quarter_seconds = seconds * 4;
|
|
|
|
uint8_t radio_on;
|
|
|
|
|
|
|
|
halPowerDown();
|
|
|
|
radio_on = stm32w_radio_is_on();
|
|
|
|
stm32w_radio_driver.off();
|
|
|
|
|
|
|
|
halSleepForQsWithOptions(&quarter_seconds, 0);
|
|
|
|
|
|
|
|
|
|
|
|
ATOMIC(
|
|
|
|
|
|
|
|
halPowerUp();
|
|
|
|
|
2012-04-22 20:54:29 +02:00
|
|
|
/* Update OS system ticks. */
|
|
|
|
current_seconds += seconds - quarter_seconds / 4 ; /* Passed seconds */
|
2011-04-08 13:38:16 +02:00
|
|
|
count += seconds * CLOCK_SECOND - quarter_seconds * CLOCK_SECOND / 4 ;
|
|
|
|
|
|
|
|
if(etimer_pending()) {
|
|
|
|
etimer_request_poll();
|
|
|
|
}
|
|
|
|
|
|
|
|
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
|
|
|
|
SysTick_SetReload(RELOAD_VALUE);
|
|
|
|
SysTick_ITConfig(ENABLE);
|
|
|
|
SysTick_CounterCmd(SysTick_Counter_Enable);
|
|
|
|
)
|
|
|
|
|
|
|
|
stm32w_radio_driver.init();
|
2012-04-22 20:54:29 +02:00
|
|
|
if(radio_on) {
|
|
|
|
stm32w_radio_driver.on();
|
2011-04-08 13:38:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uart1_init(115200);
|
|
|
|
leds_init();
|
|
|
|
rtimer_init();
|
|
|
|
|
|
|
|
PRINTF("WakeInfo: %04x\r\n", halGetWakeInfo());
|
|
|
|
|
|
|
|
}
|