2013-03-15 16:14:09 +01:00
|
|
|
/**
|
2013-07-12 09:53:53 +02:00
|
|
|
* \addtogroup mbxxx-platform
|
2013-03-15 16:14:09 +01:00
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
*/
|
2013-03-15 16:14:09 +01:00
|
|
|
|
2010-10-25 11:03:38 +02:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Clock.
|
|
|
|
* \author
|
|
|
|
* Salvatore Pitrulli <salvopitru@users.sourceforge.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include PLATFORM_HEADER
|
2013-03-15 16:14:09 +01:00
|
|
|
#include <stdio.h>
|
2010-10-25 11:03:38 +02:00
|
|
|
#include "hal/error.h"
|
|
|
|
#include "hal/hal.h"
|
2013-03-15 16:14:09 +01:00
|
|
|
#include "dev/stm32w-systick.h"
|
2010-10-25 11:03:38 +02:00
|
|
|
|
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
|
2013-11-22 09:17:54 +01:00
|
|
|
#include "net/ip/uip-debug.h"
|
2010-10-25 11:03:38 +02:00
|
|
|
|
2013-03-15 16:14:09 +01:00
|
|
|
/*--------------------------------------------------------------------------*/
|
2012-04-22 20:54:29 +02:00
|
|
|
/* The value that will be load in the SysTick value register. */
|
2013-03-15 16:14:09 +01:00
|
|
|
#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();
|
|
|
|
}
|
2013-03-15 16:14:09 +01:00
|
|
|
|
|
|
|
if(--second_countdown == 0) {
|
2010-10-25 11:03:38 +02:00
|
|
|
current_seconds++;
|
|
|
|
second_countdown = CLOCK_SECOND;
|
|
|
|
}
|
2013-03-15 16:14:09 +01:00
|
|
|
|
2010-10-25 11:03:38 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-04-22 20:54:29 +02:00
|
|
|
void
|
|
|
|
clock_init(void)
|
2013-03-15 16:14:09 +01:00
|
|
|
{
|
2011-04-08 13:38:16 +02:00
|
|
|
ATOMIC(
|
2013-03-15 16:14:09 +01:00
|
|
|
/* Counts the number of ticks. */
|
|
|
|
count = 0;
|
|
|
|
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
|
|
|
|
SysTick_SetReload(RELOAD_VALUE);
|
|
|
|
SysTick_ITConfig(ENABLE);
|
|
|
|
SysTick_CounterCmd(SysTick_Counter_Enable);)
|
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
|
|
|
{
|
2013-03-15 16:14:09 +01:00
|
|
|
for(; i > 0; i--) { /* Needs fixing XXX */
|
2010-10-25 11:03:38 +02:00
|
|
|
unsigned j;
|
2013-03-15 16:14:09 +01:00
|
|
|
for(j = 50; j > 0; j--) {
|
|
|
|
asm("nop");
|
|
|
|
}
|
2010-10-25 11:03:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* 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();
|
2013-03-15 16:14:09 +01:00
|
|
|
while(clock_time() - start < (clock_time_t) i);
|
2010-10-25 11:03:38 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-04-22 20:54:29 +02:00
|
|
|
unsigned long
|
|
|
|
clock_seconds(void)
|
2010-10-25 11:03:38 +02:00
|
|
|
{
|
|
|
|
return current_seconds;
|
|
|
|
}
|
2013-03-15 16:14:09 +01:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
/** @} */
|