PageRenderTime 156ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/cpu/cc2430/dev/clock.c

https://gitlab.com/CMP625/ContikiOS-3.0
C | 169 lines | 89 code | 11 blank | 69 comment | 5 complexity | d06b78432d1444469f35ac4e16831452 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 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 uint8_t sleep_flag;
  48. #endif
  49. /*---------------------------------------------------------------------------*/
  50. /* Used in sleep timer interrupt for calculating the next interrupt time */
  51. static unsigned long timer_value;
  52. static volatile CC_AT_DATA clock_time_t count = 0; /* Uptime in ticks */
  53. static volatile CC_AT_DATA clock_time_t seconds = 0; /* Uptime in secs */
  54. /*---------------------------------------------------------------------------*/
  55. /*
  56. * Each iteration is ~1.0xy usec, so this function delays for roughly len usec
  57. */
  58. void
  59. clock_delay_usec(uint16_t len)
  60. {
  61. DISABLE_INTERRUPTS();
  62. while(len--) {
  63. ASM(nop); ASM(nop);
  64. ASM(nop); ASM(nop);
  65. }
  66. ENABLE_INTERRUPTS();
  67. }
  68. /*---------------------------------------------------------------------------*/
  69. /*
  70. * Wait for a multiple of ~8 ms (a tick)
  71. */
  72. void
  73. clock_wait(clock_time_t i)
  74. {
  75. clock_time_t start;
  76. start = clock_time();
  77. while(clock_time() - start < (clock_time_t)i);
  78. }
  79. /*---------------------------------------------------------------------------*/
  80. CCIF clock_time_t
  81. clock_time(void)
  82. {
  83. return count;
  84. }
  85. /*---------------------------------------------------------------------------*/
  86. CCIF unsigned long
  87. clock_seconds(void)
  88. {
  89. return seconds;
  90. }
  91. /*---------------------------------------------------------------------------*/
  92. void
  93. clock_init(void)
  94. {
  95. CLKCON = OSC32K | TICKSPD2 | TICKSPD1; /* tickspeed 500 kHz for timers[1-4] */
  96. /* Initialize tick value */
  97. timer_value = ST0; /* ST low bits [7:0] */
  98. timer_value += ((unsigned long int)ST1) << 8; /* middle bits [15:8] */
  99. timer_value += ((unsigned long int)ST2) << 16; /* high bits [23:16] */
  100. timer_value += TICK_VAL; /* Init value 256 */
  101. ST2 = (unsigned char)(timer_value >> 16);
  102. ST1 = (unsigned char)(timer_value >> 8);
  103. ST0 = (unsigned char)timer_value;
  104. IEN0_STIE = 1; /* IEN0.STIE acknowledge Sleep Timer Interrupt */
  105. }
  106. /*---------------------------------------------------------------------------*/
  107. #pragma save
  108. #if CC_CONF_OPTIMIZE_STACK_SIZE
  109. #pragma exclude bits
  110. #endif
  111. void
  112. clock_ISR(void) __interrupt(ST_VECTOR)
  113. {
  114. DISABLE_INTERRUPTS();
  115. ENERGEST_ON(ENERGEST_TYPE_IRQ);
  116. /*
  117. * If the Sleep timer throws an interrupt while we are powering down to
  118. * PM1, we need to abort the power down. Clear SLEEP.MODE, this will signal
  119. * main() to abort the PM1 transition
  120. */
  121. SLEEP &= 0xFC;
  122. /*
  123. * Read value of the ST0:ST1:ST2, add TICK_VAL and write it back.
  124. * Next interrupt occurs after the current time + TICK_VAL
  125. */
  126. timer_value = ST0;
  127. timer_value += ((unsigned long int)ST1) << 8;
  128. timer_value += ((unsigned long int)ST2) << 16;
  129. timer_value += TICK_VAL;
  130. ST2 = (unsigned char)(timer_value >> 16);
  131. ST1 = (unsigned char)(timer_value >> 8);
  132. ST0 = (unsigned char)timer_value;
  133. ++count;
  134. /* Make sure the CLOCK_CONF_SECOND is a power of two, to ensure
  135. that the modulo operation below becomes a logical and and not
  136. an expensive divide. Algorithm from Wikipedia:
  137. http://en.wikipedia.org/wiki/Power_of_two */
  138. #if (CLOCK_CONF_SECOND & (CLOCK_CONF_SECOND - 1)) != 0
  139. #error CLOCK_CONF_SECOND must be a power of two (i.e., 1, 2, 4, 8, 16, 32, 64, ...).
  140. #error Change CLOCK_CONF_SECOND in contiki-conf.h.
  141. #endif
  142. if(count % CLOCK_CONF_SECOND == 0) {
  143. ++seconds;
  144. }
  145. #if CLOCK_CONF_STACK_FRIENDLY
  146. sleep_flag = 1;
  147. #else
  148. if(etimer_pending()
  149. && (etimer_next_expiration_time() - count - 1) > MAX_TICKS) {
  150. etimer_request_poll();
  151. }
  152. #endif
  153. IRCON_STIF = 0;
  154. ENERGEST_OFF(ENERGEST_TYPE_IRQ);
  155. ENABLE_INTERRUPTS();
  156. }
  157. #pragma restore
  158. /*---------------------------------------------------------------------------*/