PageRenderTime 74ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/cpu/cc253x/dev/clock.c

https://bitbucket.org/sieben/contiki
C | 185 lines | 92 code | 13 blank | 80 comment | 6 complexity | 6469276e4d157d02599b3b08d0d941d2 MD5 | raw file
  1. /*
  2. * Copyright (c) 2009, Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the Institute nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * This file is part of the Contiki operating system.
  30. */
  31. /**
  32. * \file
  33. * Implementation of the clock functions for the cc253x.
  34. * Ported over from the cc243x original.
  35. * \author
  36. * Zach Shelby (zach@sensinode.com) - original (cc243x)
  37. * George Oikonomou - <oikonomou@users.sourceforge.net> - cc2530 port
  38. */
  39. #include "sfr-bits.h"
  40. #include "sys/clock.h"
  41. #include "sys/etimer.h"
  42. #include "cc253x.h"
  43. #include "sys/energest.h"
  44. /* Sleep timer runs on the 32k RC osc. */
  45. /* One clock tick is 7.8 ms */
  46. #define TICK_VAL (32768/128) /* 256 */
  47. /*---------------------------------------------------------------------------*/
  48. #if CLOCK_CONF_STACK_FRIENDLY
  49. volatile uint8_t sleep_flag;
  50. #endif
  51. /*---------------------------------------------------------------------------*/
  52. /* Do NOT remove the absolute address and do NOT remove the initialiser here */
  53. __xdata __at(0x0000) static unsigned long timer_value = 0;
  54. static volatile CC_AT_DATA clock_time_t count = 0; /* Uptime in ticks */
  55. static volatile CC_AT_DATA clock_time_t seconds = 0; /* Uptime in secs */
  56. /*---------------------------------------------------------------------------*/
  57. /**
  58. * Each iteration is ~1.0xy usec, so this function delays for roughly len usec
  59. */
  60. void
  61. clock_delay_usec(uint16_t len)
  62. {
  63. DISABLE_INTERRUPTS();
  64. while(len--) {
  65. ASM(nop);
  66. }
  67. ENABLE_INTERRUPTS();
  68. }
  69. /*---------------------------------------------------------------------------*/
  70. /**
  71. * Wait for a multiple of ~8 ms (a tick)
  72. */
  73. void
  74. clock_wait(clock_time_t i)
  75. {
  76. clock_time_t start;
  77. start = clock_time();
  78. while(clock_time() - start < (clock_time_t)i);
  79. }
  80. /*---------------------------------------------------------------------------*/
  81. CCIF clock_time_t
  82. clock_time(void)
  83. {
  84. return count;
  85. }
  86. /*---------------------------------------------------------------------------*/
  87. CCIF unsigned long
  88. clock_seconds(void)
  89. {
  90. return seconds;
  91. }
  92. /*---------------------------------------------------------------------------*/
  93. /*
  94. * There is some ambiguity between TI cc2530 software examples and information
  95. * in the datasheet.
  96. *
  97. * TI examples appear to be writing to SLEEPCMD, initialising hardware in a
  98. * fashion semi-similar to cc2430
  99. *
  100. * However, the datasheet claims that those bits in SLEEPCMD are reserved
  101. *
  102. * The code here goes by the datasheet (ignore TI examples) and seems to work.
  103. */
  104. void
  105. clock_init(void)
  106. {
  107. /* Make sure we know where we stand */
  108. CLKCONCMD = CLKCONCMD_OSC32K | CLKCONCMD_OSC;
  109. /* Stay with 32 KHz RC OSC, Chance System Clock to 32 MHz */
  110. CLKCONCMD &= ~CLKCONCMD_OSC;
  111. while(CLKCONSTA & CLKCONCMD_OSC);
  112. /* Tickspeed 500 kHz for timers[1-4] */
  113. CLKCONCMD |= CLKCONCMD_TICKSPD2 | CLKCONCMD_TICKSPD1;
  114. while(CLKCONSTA != CLKCONCMD);
  115. /* Initialize tick value */
  116. timer_value = ST0;
  117. timer_value += ((unsigned long int)ST1) << 8;
  118. timer_value += ((unsigned long int)ST2) << 16;
  119. timer_value += TICK_VAL;
  120. ST2 = (unsigned char)(timer_value >> 16);
  121. ST1 = (unsigned char)(timer_value >> 8);
  122. ST0 = (unsigned char)timer_value;
  123. STIE = 1; /* IEN0.STIE interrupt enable */
  124. }
  125. /*---------------------------------------------------------------------------*/
  126. /* avoid referencing bits, we don't call code which use them */
  127. #pragma save
  128. #if CC_CONF_OPTIMIZE_STACK_SIZE
  129. #pragma exclude bits
  130. #endif
  131. void
  132. clock_isr(void) __interrupt(ST_VECTOR)
  133. {
  134. DISABLE_INTERRUPTS();
  135. ENERGEST_ON(ENERGEST_TYPE_IRQ);
  136. /*
  137. * Read value of the ST0:ST1:ST2, add TICK_VAL and write it back.
  138. * Next interrupt occurs after the current time + TICK_VAL
  139. */
  140. timer_value = ST0;
  141. timer_value += ((unsigned long int)ST1) << 8;
  142. timer_value += ((unsigned long int)ST2) << 16;
  143. timer_value += TICK_VAL;
  144. ST2 = (unsigned char)(timer_value >> 16);
  145. ST1 = (unsigned char)(timer_value >> 8);
  146. ST0 = (unsigned char)timer_value;
  147. ++count;
  148. /* Make sure the CLOCK_CONF_SECOND is a power of two, to ensure
  149. that the modulo operation below becomes a logical and and not
  150. an expensive divide. Algorithm from Wikipedia:
  151. http://en.wikipedia.org/wiki/Power_of_two */
  152. #if (CLOCK_CONF_SECOND & (CLOCK_CONF_SECOND - 1)) != 0
  153. #error CLOCK_CONF_SECOND must be a power of two (i.e., 1, 2, 4, 8, 16, 32, 64, ...).
  154. #error Change CLOCK_CONF_SECOND in contiki-conf.h.
  155. #endif
  156. if(count % CLOCK_CONF_SECOND == 0) {
  157. ++seconds;
  158. }
  159. #if CLOCK_CONF_STACK_FRIENDLY
  160. sleep_flag = 1;
  161. #else
  162. if(etimer_pending()
  163. && (etimer_next_expiration_time() - count - 1) > MAX_TICKS) {
  164. etimer_request_poll();
  165. }
  166. #endif
  167. STIF = 0; /* IRCON.STIF */
  168. ENERGEST_OFF(ENERGEST_TYPE_IRQ);
  169. ENABLE_INTERRUPTS();
  170. }
  171. #pragma restore
  172. /*---------------------------------------------------------------------------*/