PageRenderTime 82ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/contiki-2.6/cpu/cc2430/dev/clock.c

https://bitbucket.org/ravwojdyla/rav_wismote
C | 165 lines | 85 code | 11 blank | 69 comment | 5 complexity | 797ccffbb6ba30141ba99bd1dd33c3f2 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  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 cc243x
  34. * \author
  35. * Zach Shelby (zach@sensinode.com) - original
  36. * George Oikonomou - <oikonomou@users.sourceforge.net>
  37. */
  38. #include "sys/clock.h"
  39. #include "sys/etimer.h"
  40. #include "cc2430_sfr.h"
  41. #include "sys/energest.h"
  42. /* Sleep timer runs on the 32k RC osc. */
  43. /* One clock tick is 7.8 ms */
  44. #define TICK_VAL (32768/128) /* 256 */
  45. /*---------------------------------------------------------------------------*/
  46. #if CLOCK_CONF_STACK_FRIENDLY
  47. volatile __bit sleep_flag;
  48. #else
  49. #endif
  50. /*---------------------------------------------------------------------------*/
  51. /* Used in sleep timer interrupt for calculating the next interrupt time */
  52. static unsigned long timer_value;
  53. static volatile __data clock_time_t count = 0; /* Uptime in ticks */
  54. static volatile __data clock_time_t seconds = 0; /* Uptime in secs */
  55. /*---------------------------------------------------------------------------*/
  56. /**
  57. * Each iteration is ~1.0xy usec, so this function delays for roughly len usec
  58. */
  59. void
  60. clock_delay_usec(uint16_t len)
  61. {
  62. DISABLE_INTERRUPTS();
  63. while(len--) {
  64. ASM(nop); ASM(nop);
  65. ASM(nop); 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. void
  94. clock_init(void)
  95. {
  96. CLKCON = OSC32K | TICKSPD2 | TICKSPD1; /* tickspeed 500 kHz for timers[1-4] */
  97. /* Initialize tick value */
  98. timer_value = ST0; /* ST low bits [7:0] */
  99. timer_value += ((unsigned long int) ST1) << 8; /* middle bits [15:8] */
  100. timer_value += ((unsigned long int) ST2) << 16; /* high bits [23:16] */
  101. timer_value += TICK_VAL; /* Init value 256 */
  102. ST2 = (unsigned char) (timer_value >> 16);
  103. ST1 = (unsigned char) (timer_value >> 8);
  104. ST0 = (unsigned char) timer_value;
  105. IEN0_STIE = 1; /* IEN0.STIE acknowledge Sleep Timer Interrupt */
  106. }
  107. /*---------------------------------------------------------------------------*/
  108. void
  109. clock_ISR(void) __interrupt(ST_VECTOR)
  110. {
  111. DISABLE_INTERRUPTS();
  112. ENERGEST_ON(ENERGEST_TYPE_IRQ);
  113. /*
  114. * If the Sleep timer throws an interrupt while we are powering down to
  115. * PM1, we need to abort the power down. Clear SLEEP.MODE, this will signal
  116. * main() to abort the PM1 transition
  117. */
  118. SLEEP &= 0xFC;
  119. /*
  120. * Read value of the ST0:ST1:ST2, add TICK_VAL and write it back.
  121. * Next interrupt occurs after the current time + TICK_VAL
  122. */
  123. timer_value = ST0;
  124. timer_value += ((unsigned long int) ST1) << 8;
  125. timer_value += ((unsigned long int) ST2) << 16;
  126. timer_value += TICK_VAL;
  127. ST2 = (unsigned char) (timer_value >> 16);
  128. ST1 = (unsigned char) (timer_value >> 8);
  129. ST0 = (unsigned char) timer_value;
  130. ++count;
  131. /* Make sure the CLOCK_CONF_SECOND is a power of two, to ensure
  132. that the modulo operation below becomes a logical and and not
  133. an expensive divide. Algorithm from Wikipedia:
  134. http://en.wikipedia.org/wiki/Power_of_two */
  135. #if (CLOCK_CONF_SECOND & (CLOCK_CONF_SECOND - 1)) != 0
  136. #error CLOCK_CONF_SECOND must be a power of two (i.e., 1, 2, 4, 8, 16, 32, 64, ...).
  137. #error Change CLOCK_CONF_SECOND in contiki-conf.h.
  138. #endif
  139. if(count % CLOCK_CONF_SECOND == 0) {
  140. ++seconds;
  141. }
  142. #if CLOCK_CONF_STACK_FRIENDLY
  143. sleep_flag = 1;
  144. #else
  145. if(etimer_pending()
  146. && (etimer_next_expiration_time() - count - 1) > MAX_TICKS) {
  147. etimer_request_poll();
  148. }
  149. #endif
  150. IRCON_STIF = 0;
  151. ENERGEST_OFF(ENERGEST_TYPE_IRQ);
  152. ENABLE_INTERRUPTS();
  153. }
  154. /*---------------------------------------------------------------------------*/