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  */
00033 /*---------------------------------------------------------------------------*/
00034 /**
00035 * \file
00036 *                       Clock for STM32W.
00037 * \author
00038 *                       Salvatore Pitrulli <salvopitru@users.sourceforge.net>
00039 */
00040 /*---------------------------------------------------------------------------*/
00041 
00042 /*
00043  * File customized for mbxxx platform. It uses systick timer to control button
00044  * status without interrupts, as well as for system clock.
00045  */
00046 
00047 #include PLATFORM_HEADER
00048 #include "hal/error.h"
00049 #include "hal/hal.h"
00050 #include "dev/stm32w_systick.h"
00051 
00052 #include "sys/clock.h"
00053 #include "sys/etimer.h"
00054 #include "dev/button-sensor.h"
00055 #include "uart1.h"
00056 #include "dev/leds.h"
00057 #include "dev/stm32w-radio.h"
00058 
00059 #define DEBUG DEBUG_NONE
00060 #include "net/uip-debug.h"
00061 
00062 // The value that will be load in the SysTick value register.
00063 #define RELOAD_VALUE 24000-1   // 1 ms with a 24 MHz clock
00064 
00065 static volatile clock_time_t count;
00066 static volatile unsigned long current_seconds = 0;
00067 static unsigned int second_countdown = CLOCK_SECOND;
00068 
00069 /*---------------------------------------------------------------------------*/
00070 void SysTick_Handler(void)
00071 {
00072 
00073   count++;
00074 
00075   if(button_sensor.status(SENSORS_READY)){
00076     button_sensor.value(0); // sensors_changed is called inside this function.
00077   }
00078 
00079   if(etimer_pending()) {
00080     etimer_request_poll();
00081   }
00082   
00083   if (--second_countdown == 0) {
00084     current_seconds++;
00085     second_countdown = CLOCK_SECOND;
00086   }
00087   
00088 }
00089 
00090 /*---------------------------------------------------------------------------*/
00091 
00092 void clock_init(void)
00093 { 
00094   
00095   ATOMIC(
00096   
00097   //Counts the number of ticks.  
00098   count = 0;
00099   
00100   SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
00101   SysTick_SetReload(RELOAD_VALUE);
00102   SysTick_ITConfig(ENABLE);
00103   SysTick_CounterCmd(SysTick_Counter_Enable);
00104   
00105   )
00106 }
00107 
00108 /*---------------------------------------------------------------------------*/
00109 
00110 clock_time_t clock_time(void)
00111 {
00112   return count;
00113 }
00114 
00115 /*---------------------------------------------------------------------------*/
00116 /**
00117  * Delay the CPU for a multiple of TODO
00118  */
00119 void clock_delay(unsigned int i)
00120 {
00121   for (; i > 0; i--) {          /* Needs fixing XXX */
00122     unsigned j;
00123     for (j = 50; j > 0; j--)
00124       asm ("nop");
00125   }
00126 }
00127 
00128 /*---------------------------------------------------------------------------*/
00129 /**
00130  * Wait for a multiple of 1 ms.
00131  *
00132  */
00133 void clock_wait(clock_time_t i)
00134 {
00135   clock_time_t start;
00136 
00137   start = clock_time();
00138   while(clock_time() - start < (clock_time_t)i);
00139 }
00140 /*---------------------------------------------------------------------------*/
00141 
00142 unsigned long clock_seconds(void)
00143 {
00144   return current_seconds;
00145 }
00146 
00147 void sleep_seconds(int seconds)
00148 {
00149   int32u quarter_seconds = seconds * 4;
00150   uint8_t radio_on;
00151 
00152 
00153   halPowerDown();
00154   radio_on = stm32w_radio_is_on();
00155   stm32w_radio_driver.off();
00156 
00157   halSleepForQsWithOptions(&quarter_seconds, 0);
00158 
00159 
00160   ATOMIC(
00161 
00162   halPowerUp();
00163 
00164   // Update OS system ticks.
00165   current_seconds += seconds - quarter_seconds / 4 ; // Passed seconds
00166   count += seconds * CLOCK_SECOND - quarter_seconds * CLOCK_SECOND / 4 ;
00167 
00168   if(etimer_pending()) {
00169     etimer_request_poll();
00170   }
00171 
00172   SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
00173   SysTick_SetReload(RELOAD_VALUE);
00174   SysTick_ITConfig(ENABLE);
00175   SysTick_CounterCmd(SysTick_Counter_Enable);
00176 
00177   )
00178 
00179   stm32w_radio_driver.init();
00180   if(radio_on){
00181           stm32w_radio_driver.on();
00182   }
00183 
00184   uart1_init(115200);
00185   leds_init();
00186   rtimer_init();
00187 
00188   PRINTF("WakeInfo: %04x\r\n", halGetWakeInfo());
00189 
00190 
00191 }