osd-contiki/cpu/stm32w108/clock.c

135 lines
3.7 KiB
C
Raw Permalink Normal View History

/**
* \addtogroup mbxxx-platform
*
* @{
*/
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.
*
*/
2010-10-25 11:03:38 +02:00
/**
* \file
* Clock.
* \author
* Salvatore Pitrulli <salvopitru@users.sourceforge.net>
*/
#include PLATFORM_HEADER
#include <stdio.h>
2010-10-25 11:03:38 +02:00
#include "hal/error.h"
#include "hal/hal.h"
#include "dev/stm32w-systick.h"
2010-10-25 11:03:38 +02:00
#include "contiki.h"
#include "sys/clock.h"
#include "uart1.h"
#include "dev/leds.h"
#include "dev/stm32w-radio.h"
#define DEBUG DEBUG_NONE
#include "net/ip/uip-debug.h"
2010-10-25 11:03:38 +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;
/*---------------------------------------------------------------------------*/
void
SysTick_Handler(void)
2010-10-25 11:03:38 +02:00
{
count++;
if(etimer_pending()) {
etimer_request_poll();
}
if(--second_countdown == 0) {
2010-10-25 11:03:38 +02:00
current_seconds++;
second_countdown = CLOCK_SECOND;
}
2010-10-25 11:03:38 +02:00
}
/*---------------------------------------------------------------------------*/
void
clock_init(void)
{
ATOMIC(
/* 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
}
/*---------------------------------------------------------------------------*/
clock_time_t
clock_time(void)
2010-10-25 11:03:38 +02:00
{
return count;
}
/*---------------------------------------------------------------------------*/
/**
* Delay the CPU for a multiple of TODO
*/
void
clock_delay(unsigned int i)
2010-10-25 11:03:38 +02:00
{
for(; i > 0; i--) { /* Needs fixing XXX */
2010-10-25 11:03:38 +02:00
unsigned j;
for(j = 50; j > 0; j--) {
asm("nop");
}
2010-10-25 11:03:38 +02:00
}
}
/*---------------------------------------------------------------------------*/
/*
2010-10-25 11:03:38 +02:00
* Wait for a multiple of 1 ms.
*/
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);
2010-10-25 11:03:38 +02:00
}
/*---------------------------------------------------------------------------*/
unsigned long
clock_seconds(void)
2010-10-25 11:03:38 +02:00
{
return current_seconds;
}
/*--------------------------------------------------------------------------*/
/** @} */