OSAL_Clock.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**************************************************************************************************
  2. Filename: OSAL_Clock.c
  3. Revised: $Date: 2008-12-15 15:42:47 -0800 (Mon, 15 Dec 2008) $
  4. Revision: $Revision: 18616 $
  5. Description: OSAL Clock definition and manipulation functions.
  6. Copyright 2004-2008 Texas Instruments Incorporated. All rights reserved.
  7. IMPORTANT: Your use of this Software is limited to those specific rights
  8. granted under the terms of a software license agreement between the user
  9. who downloaded the software, his/her employer (which must be your employer)
  10. and Texas Instruments Incorporated (the "License"). You may not use this
  11. Software unless you agree to abide by the terms of the License. The License
  12. limits your use, and you acknowledge, that the Software may not be modified,
  13. copied or distributed unless embedded on a Texas Instruments microcontroller
  14. or used solely and exclusively in conjunction with a Texas Instruments radio
  15. frequency transceiver, which is integrated into your product. Other than for
  16. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  17. works of, modify, distribute, perform, display or sell this Software and/or
  18. its documentation for any purpose.
  19. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  20. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  21. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  22. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  23. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  24. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  25. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  26. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  27. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  28. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  29. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  30. Should you have any questions regarding your right to use this Software,
  31. contact Texas Instruments Incorporated at www.TI.com.
  32. **************************************************************************************************/
  33. /*********************************************************************
  34. * INCLUDES
  35. */
  36. #include "comdef.h"
  37. #include "OnBoard.h"
  38. #include "OSAL.h"
  39. #include "OSAL_Clock.h"
  40. /*********************************************************************
  41. * MACROS
  42. */
  43. /*********************************************************************
  44. * CONSTANTS
  45. */
  46. // (MAXCALCTICKS * 8) + (max remainder) must be <= (uint16 max),
  47. // so: (8188 * 8) + 24 <= 65535
  48. #define MAXCALCTICKS ((uint16)(8188))
  49. #define BEGYEAR 2000 // 00:00:00 January 1, 2000
  50. #define DAY 86400UL // 24 hours * 60 minutes * 60 seconds
  51. #define IsLeapYear(yr) (!((yr) % 4) && (((yr) % 100) || !((yr) % 400)))
  52. #define YearLength(yr) (IsLeapYear(yr) ? 366 : 365)
  53. /*********************************************************************
  54. * TYPEDEFS
  55. */
  56. /*********************************************************************
  57. * GLOBAL VARIABLES
  58. */
  59. /*********************************************************************
  60. * EXTERNAL VARIABLES
  61. */
  62. /*********************************************************************
  63. * EXTERNAL FUNCTIONS
  64. */
  65. extern uint16 macMcuPrecisionCount(void);
  66. /*********************************************************************
  67. * LOCAL VARIABLES
  68. */
  69. static uint16 previousMacTimerTick = 0;
  70. static uint16 remUsTicks = 0;
  71. static uint16 timeMSec = 0;
  72. // number of seconds since 0 hrs, 0 minutes, 0 seconds, on the
  73. // 1st of January 2000 UTC
  74. UTCTime OSAL_timeSeconds = 0;
  75. /*********************************************************************
  76. * LOCAL FUNCTION PROTOTYPES
  77. */
  78. static uint8 monthLength( uint8 lpyr, uint8 mon );
  79. static void osalClockUpdate( uint16 elapsedMSec );
  80. /*********************************************************************
  81. * FUNCTIONS
  82. *********************************************************************/
  83. /*********************************************************************
  84. * @fn osalTimeUpdate
  85. *
  86. * @brief Uses the free running rollover count of the MAC backoff timer;
  87. * this timer runs freely with a constant 320 usec interval. The
  88. * count of 320-usec ticks is converted to msecs and used to update
  89. * the OSAL clock and Timers by invoking osalClockUpdate() and
  90. * osalTimerUpdate(). This function is intended to be invoked
  91. * from the background, not interrupt level.
  92. *
  93. * @param None.
  94. *
  95. * @return None.
  96. */
  97. void osalTimeUpdate( void )
  98. {
  99. uint16 tmp;
  100. uint16 ticks320us;
  101. uint16 elapsedMSec = 0;
  102. // Get the free-running count of 320us timer ticks
  103. tmp = macMcuPrecisionCount();
  104. if ( tmp != previousMacTimerTick )
  105. {
  106. // Calculate the elapsed ticks of the free-running timer.
  107. ticks320us = tmp - previousMacTimerTick;
  108. // Store the MAC Timer tick count for the next time through this function.
  109. previousMacTimerTick = tmp;
  110. /* It is necessary to loop to convert the usecs to msecs in increments so as
  111. * not to overflow the 16-bit variables.
  112. */
  113. while ( ticks320us > MAXCALCTICKS )
  114. {
  115. ticks320us -= MAXCALCTICKS;
  116. elapsedMSec += MAXCALCTICKS * 8 / 25;
  117. remUsTicks += MAXCALCTICKS * 8 % 25;
  118. }
  119. // update converted number with remaining ticks from loop and the
  120. // accumulated remainder from loop
  121. tmp = (ticks320us * 8) + remUsTicks;
  122. // Convert the 320 us ticks into milliseconds and a remainder
  123. elapsedMSec += tmp / 25;
  124. remUsTicks = tmp % 25;
  125. // Update OSAL Clock and Timers
  126. if ( elapsedMSec )
  127. {
  128. osalClockUpdate( elapsedMSec );
  129. osalTimerUpdate( elapsedMSec );
  130. }
  131. }
  132. }
  133. /*********************************************************************
  134. * @fn osalClockUpdate
  135. *
  136. * @brief Updates the OSAL Clock time with elapsed milliseconds.
  137. *
  138. * @param elapsedMSec - elapsed milliseconds
  139. *
  140. * @return none
  141. */
  142. static void osalClockUpdate( uint16 elapsedMSec )
  143. {
  144. // Add elapsed milliseconds to the saved millisecond portion of time
  145. timeMSec += elapsedMSec;
  146. // Roll up milliseconds to the number of seconds
  147. if ( timeMSec > 1000 )
  148. {
  149. OSAL_timeSeconds += timeMSec / 1000;
  150. timeMSec = timeMSec % 1000;
  151. }
  152. }
  153. /*********************************************************************
  154. * @fn osal_setClock
  155. *
  156. * @brief Set the new time. This will only set the seconds portion
  157. * of time and doesn't change the factional second counter.
  158. *
  159. * @param newTime - number of seconds since 0 hrs, 0 minutes,
  160. * 0 seconds, on the 1st of January 2000 UTC
  161. *
  162. * @return none
  163. */
  164. void osal_setClock( UTCTime newTime )
  165. {
  166. OSAL_timeSeconds = newTime;
  167. }
  168. /*********************************************************************
  169. * @fn osal_getClock
  170. *
  171. * @brief Gets the current time. This will only return the seconds
  172. * portion of time and doesn't include the factional second
  173. * counter.
  174. *
  175. * @param none
  176. *
  177. * @return number of seconds since 0 hrs, 0 minutes, 0 seconds,
  178. * on the 1st of January 2000 UTC
  179. */
  180. UTCTime osal_getClock( void )
  181. {
  182. return ( OSAL_timeSeconds );
  183. }
  184. /*********************************************************************
  185. * @fn osal_ConvertUTCTime
  186. *
  187. * @brief Converts UTCTime to UTCTimeStruct
  188. *
  189. * @param tm - pointer to breakdown struct
  190. *
  191. * @param secTime - number of seconds since 0 hrs, 0 minutes,
  192. * 0 seconds, on the 1st of January 2000 UTC
  193. *
  194. * @return none
  195. */
  196. void osal_ConvertUTCTime( UTCTimeStruct *tm, UTCTime secTime )
  197. {
  198. // calculate the time less than a day - hours, minutes, seconds
  199. {
  200. uint32 day = secTime % DAY;
  201. tm->seconds = day % 60UL;
  202. tm->minutes = (day % 3600UL) / 60;
  203. tm->hour = day / 3600UL;
  204. }
  205. // Fill in the calendar - day, month, year
  206. {
  207. uint16 numDays = secTime / DAY;
  208. tm->year = BEGYEAR;
  209. while ( numDays >= YearLength( tm->year ) )
  210. {
  211. numDays -= YearLength( tm->year );
  212. tm->year++;
  213. }
  214. tm->month = 0;
  215. while ( numDays >= monthLength( IsLeapYear( tm->year ), tm->month ) )
  216. {
  217. numDays -= monthLength( IsLeapYear( tm->year ), tm->month );
  218. tm->month++;
  219. }
  220. tm->day = numDays;
  221. }
  222. }
  223. /*********************************************************************
  224. * @fn monthLength
  225. *
  226. * @param lpyr - 1 for leap year, 0 if not
  227. *
  228. * @param mon - 0 - 11 (jan - dec)
  229. *
  230. * @return returns the number of days in a month
  231. */
  232. static uint8 monthLength( uint8 lpyr, uint8 mon )
  233. {
  234. uint8 days = 31;
  235. if ( mon == 1 ) // feb
  236. days = ( 28 + lpyr );
  237. else
  238. {
  239. if ( mon > 6 )
  240. mon--;
  241. if ( (mon % 2) == 1 )
  242. days = 30;
  243. }
  244. return ( days );
  245. }