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 "sys/clock.h"
00049 #include "sys/etimer.h"
00050 
00051 // The value that will be load in the SysTick value register.
00052 #define RELOAD_VALUE 24000-1   // 1 ms with a 24 MHz clock
00053 
00054 static volatile clock_time_t count;
00055 static volatile unsigned long current_seconds = 0;
00056 static unsigned int second_countdown = CLOCK_SECOND;
00057 
00058 /*---------------------------------------------------------------------------*/
00059 void SysTick_Handler(void)
00060 {
00061 
00062   count++;
00063 
00064   if(etimer_pending()) {
00065     etimer_request_poll();
00066   }
00067   
00068   if (--second_countdown == 0) {
00069     current_seconds++;
00070     second_countdown = CLOCK_SECOND;
00071   }
00072   
00073 }
00074 
00075 /*---------------------------------------------------------------------------*/
00076 
00077 void clock_init(void)
00078 { 
00079   
00080   INTERRUPTS_OFF();
00081   
00082   //Counts the number of ticks.  
00083   count = 0;
00084   
00085   SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
00086   SysTick_SetReload(RELOAD_VALUE);
00087   SysTick_ITConfig(ENABLE);
00088   SysTick_CounterCmd(SysTick_Counter_Enable);
00089   
00090   INTERRUPTS_ON();
00091 }
00092 
00093 /*---------------------------------------------------------------------------*/
00094 
00095 clock_time_t clock_time(void)
00096 {
00097   return count;
00098 }
00099 
00100 /*---------------------------------------------------------------------------*/
00101 /**
00102  * Delay the CPU for a multiple of TODO
00103  */
00104 void clock_delay(unsigned int i)
00105 {
00106   for (; i > 0; i--) {          /* Needs fixing XXX */
00107     unsigned j;
00108     for (j = 50; j > 0; j--)
00109       asm ("nop");
00110   }
00111 }
00112 
00113 /*---------------------------------------------------------------------------*/
00114 /**
00115  * Wait for a multiple of 1 ms.
00116  *
00117  */
00118 void clock_wait(int i)
00119 {
00120   clock_time_t start;
00121 
00122   start = clock_time();
00123   while(clock_time() - start < (clock_time_t)i);
00124 }
00125 /*---------------------------------------------------------------------------*/
00126 
00127 unsigned long clock_seconds(void)
00128 {
00129   return current_seconds;
00130 }

Generated on Mon Apr 11 14:23:33 2011 for Contiki 2.5 by  doxygen 1.6.1