Contiki 2.6

clock.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, STMicroelectronics.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above
00011  *    copyright notice, this list of conditions and the following
00012  *    disclaimer in the documentation and/or other materials provided
00013  *    with the distribution.
00014  * 3. The name of the author may not be used to endorse or promote
00015  *    products derived from this software without specific prior
00016  *    written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00019  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00020  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00022  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00024  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * This file is part of the Contiki OS
00031  *
00032  * $Id: clock.c,v 1.1 2010/10/25 09:03:38 salvopitru Exp $
00033  */
00034 /*---------------------------------------------------------------------------*/
00035 /**
00036 * \file
00037 *                       Clock.
00038 * \author
00039 *                       Salvatore Pitrulli <salvopitru@users.sourceforge.net>
00040 */
00041 /*---------------------------------------------------------------------------*/
00042 
00043 #include PLATFORM_HEADER
00044 #include "hal/error.h"
00045 #include "hal/hal.h"
00046 #include "dev/stm32w_systick.h"
00047 
00048 #include "contiki.h"
00049 #include "sys/clock.h"
00050 
00051 #include "uart1.h"
00052 #include "dev/leds.h"
00053 #include "dev/stm32w-radio.h"
00054 
00055 #define DEBUG DEBUG_NONE
00056 #include "net/uip-debug.h"
00057 
00058 /* The value that will be load in the SysTick value register. */
00059 #define RELOAD_VALUE 24000-1   /* 1 ms with a 24 MHz clock */
00060 
00061 static volatile clock_time_t count;
00062 static volatile unsigned long current_seconds = 0;
00063 static unsigned int second_countdown = CLOCK_SECOND;
00064 
00065 /*---------------------------------------------------------------------------*/
00066 void
00067 SysTick_Handler(void)
00068 {
00069   count++;
00070 
00071   if(etimer_pending()) {
00072     etimer_request_poll();
00073   }
00074   
00075   if (--second_countdown == 0) {
00076     current_seconds++;
00077     second_countdown = CLOCK_SECOND;
00078   }
00079   
00080 }
00081 /*---------------------------------------------------------------------------*/
00082 void
00083 clock_init(void)
00084 { 
00085   
00086   ATOMIC(
00087 
00088   //Counts the number of ticks.
00089   count = 0;
00090   
00091   SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
00092   SysTick_SetReload(RELOAD_VALUE);
00093   SysTick_ITConfig(ENABLE);
00094   SysTick_CounterCmd(SysTick_Counter_Enable);
00095   
00096   )
00097 
00098 }
00099 /*---------------------------------------------------------------------------*/
00100 clock_time_t
00101 clock_time(void)
00102 {
00103   return count;
00104 }
00105 /*---------------------------------------------------------------------------*/
00106 /**
00107  * Delay the CPU for a multiple of TODO
00108  */
00109 void
00110 clock_delay(unsigned int i)
00111 {
00112   for (; i > 0; i--) {          /* Needs fixing XXX */
00113     unsigned j;
00114     for (j = 50; j > 0; j--)
00115       asm ("nop");
00116   }
00117 }
00118 /*---------------------------------------------------------------------------*/
00119 /**
00120  * Wait for a multiple of 1 ms.
00121  *
00122  */
00123 void
00124 clock_wait(clock_time_t i)
00125 {
00126   clock_time_t start;
00127 
00128   start = clock_time();
00129   while(clock_time() - start < (clock_time_t)i);
00130 }
00131 /*---------------------------------------------------------------------------*/
00132 unsigned long
00133 clock_seconds(void)
00134 {
00135   return current_seconds;
00136 }
00137 
00138 #include <stdio.h>
00139 
00140 void
00141 sleep_seconds(int seconds)
00142 {
00143   int32u quarter_seconds = seconds * 4;
00144   uint8_t radio_on;
00145 
00146   halPowerDown();
00147   radio_on = stm32w_radio_is_on();
00148   stm32w_radio_driver.off();
00149 
00150   halSleepForQsWithOptions(&quarter_seconds, 0);
00151 
00152 
00153   ATOMIC(
00154 
00155   halPowerUp();
00156 
00157   /* Update OS system ticks. */
00158   current_seconds += seconds - quarter_seconds / 4 ; /* Passed seconds */
00159   count += seconds * CLOCK_SECOND - quarter_seconds * CLOCK_SECOND / 4 ;
00160 
00161   if(etimer_pending()) {
00162     etimer_request_poll();
00163   }
00164 
00165   SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
00166   SysTick_SetReload(RELOAD_VALUE);
00167   SysTick_ITConfig(ENABLE);
00168   SysTick_CounterCmd(SysTick_Counter_Enable);
00169   )
00170 
00171   stm32w_radio_driver.init();
00172   if(radio_on) {
00173     stm32w_radio_driver.on();
00174   }
00175 
00176   uart1_init(115200);
00177   leds_init();
00178   rtimer_init();
00179 
00180   PRINTF("WakeInfo: %04x\r\n", halGetWakeInfo());
00181 
00182 }