00001 /* 00002 * Copyright (c) 2009, Swedish Institute of Computer Science. 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 copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the Institute nor the names of its contributors 00014 * may be used to endorse or promote products derived from this software 00015 * without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 00018 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 00021 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00023 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00024 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00025 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00026 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00027 * SUCH DAMAGE. 00028 * 00029 * This file is part of the Contiki operating system. 00030 * 00031 * $Id: clock.c,v 1.1 2009/09/08 20:07:35 zdshelby Exp $ 00032 */ 00033 00034 /** 00035 * \file 00036 * Implementation of the clock functions for the 8051 CPU 00037 * \author 00038 * Zach Shelby (zach@sensinode.com) 00039 */ 00040 00041 /** 00042 * TODO: Implement clock_fine() and clock_fine_max_ticks() using another timer? 00043 */ 00044 00045 #include <stdio.h> /*for debug printf*/ 00046 #include "sys/clock.h" 00047 #include "sys/etimer.h" 00048 #include "cc2430_sfr.h" 00049 00050 00051 /*Sleep timer runs on the 32k RC osc. */ 00052 /* One clock tick is 7.8 ms */ 00053 #define TICK_VAL (32768/128) 00054 00055 #define MAX_TICKS (~((clock_time_t)0) / 2) 00056 00057 /* Used in sleep timer interrupt for calculating the next interrupt time */ 00058 static unsigned long timer_value; 00059 /*starts calculating the ticks right after reset*/ 00060 static volatile clock_time_t count = 0; 00061 /*calculates seconds*/ 00062 static volatile clock_time_t seconds = 0; 00063 00064 /*---------------------------------------------------------------------------*/ 00065 /** 00066 * One delay is about 0.6 us, so this function delays for len * 0.6 us 00067 */ 00068 void 00069 clock_delay(unsigned int len) 00070 { 00071 unsigned int i; 00072 for(i = 0; i< len; i++) { 00073 __asm 00074 nop 00075 __endasm; 00076 } 00077 } 00078 /*---------------------------------------------------------------------------*/ 00079 /** 00080 * Wait for a multiple of ~8 ms (a tick) 00081 */ 00082 void 00083 clock_wait(int i) 00084 { 00085 clock_time_t start; 00086 00087 start = clock_time(); 00088 while(clock_time() - start < (clock_time_t)i); 00089 } 00090 /*---------------------------------------------------------------------------*/ 00091 clock_time_t 00092 clock_time(void) 00093 { 00094 return count; 00095 } 00096 /*---------------------------------------------------------------------------*/ 00097 CCIF unsigned long 00098 clock_seconds(void) 00099 { 00100 return seconds; 00101 } 00102 /*---------------------------------------------------------------------------*/ 00103 void 00104 clock_init(void) 00105 { 00106 CLKCON = OSC32K | TICKSPD2|TICKSPD1|TICKSPD0; /*tickspeed 250 kHz*/ 00107 00108 /*Initialize tick value*/ 00109 timer_value = ST0; /*sleep timer 0. low bits [7:0]*/ 00110 timer_value += ((unsigned long int)ST1) << 8; /*middle bits [15:8]*/ 00111 timer_value += ((unsigned long int)ST2) << 16; /*high bits [23:16]*/ 00112 timer_value += TICK_VAL; /*init value 256*/ 00113 ST2 = (unsigned char) (timer_value >> 16); 00114 ST1 = (unsigned char) (timer_value >> 8); 00115 ST0 = (unsigned char) timer_value; 00116 00117 IEN0 |= STIE; /*interrupt enable for sleep timers. STIE=Interrupt mask, CPU. */ 00118 } 00119 /*---------------------------------------------------------------------------*/ 00120 void 00121 cc2430_clock_ISR( void ) __interrupt (ST_VECTOR) 00122 { 00123 IEN0_EA = 0; /*interrupt disable*/ 00124 /* When using the cooperative scheduler the timer 2 ISR is only 00125 required to increment the RTOS tick count. */ 00126 00127 /*Read value of the ST0,ST1,ST2 and then add TICK_VAL and write it back. 00128 Next interrupt occurs after the current time + TICK_VAL*/ 00129 timer_value = ST0; 00130 timer_value += ((unsigned long int)ST1) << 8; 00131 timer_value += ((unsigned long int)ST2) << 16; 00132 timer_value += TICK_VAL; 00133 ST2 = (unsigned char) (timer_value >> 16); 00134 ST1 = (unsigned char) (timer_value >> 8); 00135 ST0 = (unsigned char) timer_value; 00136 00137 ++count; 00138 00139 /* Make sure the CLOCK_CONF_SECOND is a power of two, to ensure 00140 that the modulo operation below becomes a logical and and not 00141 an expensive divide. Algorithm from Wikipedia: 00142 http://en.wikipedia.org/wiki/Power_of_two */ 00143 #if (CLOCK_CONF_SECOND & (CLOCK_CONF_SECOND - 1)) != 0 00144 #error CLOCK_CONF_SECOND must be a power of two (i.e., 1, 2, 4, 8, 16, 32, 64, ...). 00145 #error Change CLOCK_CONF_SECOND in contiki-conf.h. 00146 #endif 00147 if(count % CLOCK_CONF_SECOND == 0) { 00148 ++seconds; 00149 } 00150 00151 if(etimer_pending() && 00152 (etimer_next_expiration_time() - count - 1) > MAX_TICKS) { /*core/sys/etimer.c*/ 00153 etimer_request_poll(); 00154 } 00155 00156 IRCON &= ~STIF; /*IRCON.STIF=Sleep timer interrupt flag. This flag called this interrupt func, now reset it*/ 00157 IEN0_EA = 1; /*interrupt enable*/ 00158 } 00159 /*---------------------------------------------------------------------------*/