OSAL_Clock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /******************************************************************************
  2. Filename: OSAL_Clock.c
  3. Revised: $Date: 2012-03-02 15:52:01 -0800 (Fri, 02 Mar 2012) $
  4. Revision: $Revision: 29608 $
  5. Description: OSAL Clock definition and manipulation functions.
  6. Copyright 2008-2012 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. #define YearLength(yr) ((uint16)(IsLeapYear(yr) ? 366 : 365))
  44. /*********************************************************************
  45. * CONSTANTS
  46. */
  47. #define BEGYEAR 2000 // UTC started at 00:00:00 January 1, 2000
  48. #define DAY 86400UL // 24 hours * 60 minutes * 60 seconds
  49. /*********************************************************************
  50. * TYPEDEFS
  51. */
  52. /*********************************************************************
  53. * GLOBAL VARIABLES
  54. */
  55. /*********************************************************************
  56. * EXTERNAL VARIABLES
  57. */
  58. /*********************************************************************
  59. * EXTERNAL FUNCTIONS
  60. */
  61. extern uint32 macMcuPrecisionCount(void);
  62. #if (defined HAL_MCU_CC2430) || (defined HAL_MCU_CC2530) || (defined HAL_MCU_CC2533)
  63. /* This function is used to divide a 31 bit dividend by a 16 bit
  64. * divisor and return a packed 16 bit quotient and 16 bit
  65. * remainder.
  66. *
  67. * Note: This routine takes ~25.6us @32MHz. With C overhead, the
  68. * time is ~32us.
  69. *
  70. * dividend - 31 bit dividend.
  71. * divisor - 16 bit divisor.
  72. *
  73. * return - MSW divisor; LSW quotient
  74. */
  75. extern __near_func uint32 osalMcuDivide31By16To16( uint32 dividend, uint16 divisor );
  76. #define CONVERT_320US_TO_MS_ELAPSED_REMAINDER( x, y, z ) st( \
  77. \
  78. /* The 16 bit quotient is in MSW and */ \
  79. /* the 16 bit remainder is in LSW. */ \
  80. x = osalMcuDivide31By16To16( x, 25 ); \
  81. \
  82. /* Add quotient to y */ \
  83. y += (x >> 16); \
  84. \
  85. /* Copy remainder to z */ \
  86. z = (uint16)(x & 0x0FFFF); \
  87. )
  88. #else /* (defined HAL_MCU_CC2430) || (defined HAL_MCU_CC2530) || (defined HAL_MCU_CC2533) */
  89. #define CONVERT_320US_TO_MS_ELAPSED_REMAINDER( x, y, z ) st( \
  90. y += x / 25; \
  91. z = x % 25; \
  92. )
  93. #endif /* (defined HAL_MCU_CC2430) || (defined HAL_MCU_CC2530) || (defined HAL_MCU_CC2533) */
  94. /*********************************************************************
  95. * LOCAL VARIABLES
  96. */
  97. static uint32 previousMacTimerTick = 0;
  98. static uint16 remUsTicks = 0;
  99. static uint16 timeMSec = 0;
  100. // number of seconds since 0 hrs, 0 minutes, 0 seconds, on the
  101. // 1st of January 2000 UTC
  102. UTCTime OSAL_timeSeconds = 0;
  103. /*********************************************************************
  104. * LOCAL FUNCTION PROTOTYPES
  105. */
  106. static uint8 monthLength( uint8 lpyr, uint8 mon );
  107. static void osalClockUpdate( uint16 elapsedMSec );
  108. /*********************************************************************
  109. * FUNCTIONS
  110. *********************************************************************/
  111. /*********************************************************************
  112. * @fn osalTimeUpdate
  113. *
  114. * @brief Uses the free running rollover count of the MAC backoff timer;
  115. * this timer runs freely with a constant 320 usec interval. The
  116. * count of 320-usec ticks is converted to msecs and used to update
  117. * the OSAL clock and Timers by invoking osalClockUpdate() and
  118. * osalTimerUpdate(). This function is intended to be invoked
  119. * from the background, not interrupt level.
  120. *
  121. * @param None.
  122. *
  123. * @return None.
  124. */
  125. void osalTimeUpdate( void )
  126. {
  127. halIntState_t intState;
  128. uint32 tmp;
  129. uint32 ticks320us;
  130. uint16 elapsedMSec = 0;
  131. HAL_ENTER_CRITICAL_SECTION(intState);
  132. // Get the free-running count of 320us timer ticks
  133. tmp = macMcuPrecisionCount();
  134. HAL_EXIT_CRITICAL_SECTION(intState);
  135. if ( tmp != previousMacTimerTick )
  136. {
  137. // Calculate the elapsed ticks of the free-running timer.
  138. ticks320us = (tmp - previousMacTimerTick) & 0xffffffffu;
  139. // Store the MAC Timer tick count for the next time through this function.
  140. previousMacTimerTick = tmp;
  141. // update converted number with remaining ticks from loop and the
  142. // accumulated remainder from loop
  143. tmp = (ticks320us * 8) + remUsTicks;
  144. // Convert the 320 us ticks into milliseconds and a remainder
  145. CONVERT_320US_TO_MS_ELAPSED_REMAINDER( tmp, elapsedMSec, remUsTicks );
  146. // Update OSAL Clock and Timers
  147. if ( elapsedMSec )
  148. {
  149. osalClockUpdate( elapsedMSec );
  150. osalTimerUpdate( elapsedMSec );
  151. }
  152. }
  153. }
  154. /*********************************************************************
  155. * @fn osalClockUpdate
  156. *
  157. * @brief Updates the OSAL Clock time with elapsed milliseconds.
  158. *
  159. * @param elapsedMSec - elapsed milliseconds
  160. *
  161. * @return none
  162. */
  163. static void osalClockUpdate( uint16 elapsedMSec )
  164. {
  165. // Add elapsed milliseconds to the saved millisecond portion of time
  166. timeMSec += elapsedMSec;
  167. // Roll up milliseconds to the number of seconds
  168. if ( timeMSec >= 1000 )
  169. {
  170. OSAL_timeSeconds += timeMSec / 1000;
  171. timeMSec = timeMSec % 1000;
  172. }
  173. }
  174. /*********************************************************************
  175. * @fn osal_setClock
  176. *
  177. * @brief Set the new time. This will only set the seconds portion
  178. * of time and doesn't change the factional second counter.
  179. *
  180. * @param newTime - number of seconds since 0 hrs, 0 minutes,
  181. * 0 seconds, on the 1st of January 2000 UTC
  182. *
  183. * @return none
  184. */
  185. void osal_setClock( UTCTime newTime )
  186. {
  187. OSAL_timeSeconds = newTime;
  188. }
  189. /*********************************************************************
  190. * @fn osal_getClock
  191. *
  192. * @brief Gets the current time. This will only return the seconds
  193. * portion of time and doesn't include the factional second
  194. * counter.
  195. *
  196. * @param none
  197. *
  198. * @return number of seconds since 0 hrs, 0 minutes, 0 seconds,
  199. * on the 1st of January 2000 UTC
  200. */
  201. UTCTime osal_getClock( void )
  202. {
  203. return ( OSAL_timeSeconds );
  204. }
  205. /*********************************************************************
  206. * @fn osal_ConvertUTCTime
  207. *
  208. * @brief Converts UTCTime to UTCTimeStruct
  209. *
  210. * @param tm - pointer to breakdown struct
  211. *
  212. * @param secTime - number of seconds since 0 hrs, 0 minutes,
  213. * 0 seconds, on the 1st of January 2000 UTC
  214. *
  215. * @return none
  216. */
  217. void osal_ConvertUTCTime( UTCTimeStruct *tm, UTCTime secTime )
  218. {
  219. // calculate the time less than a day - hours, minutes, seconds
  220. {
  221. uint32 day = secTime % DAY;
  222. tm->seconds = day % 60UL;
  223. tm->minutes = (day % 3600UL) / 60UL;
  224. tm->hour = day / 3600UL;
  225. }
  226. // Fill in the calendar - day, month, year
  227. {
  228. uint16 numDays = secTime / DAY;
  229. tm->year = BEGYEAR;
  230. while ( numDays >= YearLength( tm->year ) )
  231. {
  232. numDays -= YearLength( tm->year );
  233. tm->year++;
  234. }
  235. tm->month = 0;
  236. while ( numDays >= monthLength( IsLeapYear( tm->year ), tm->month ) )
  237. {
  238. numDays -= monthLength( IsLeapYear( tm->year ), tm->month );
  239. tm->month++;
  240. }
  241. tm->day = numDays;
  242. }
  243. }
  244. /*********************************************************************
  245. * @fn monthLength
  246. *
  247. * @param lpyr - 1 for leap year, 0 if not
  248. *
  249. * @param mon - 0 - 11 (jan - dec)
  250. *
  251. * @return number of days in specified month
  252. */
  253. static uint8 monthLength( uint8 lpyr, uint8 mon )
  254. {
  255. uint8 days = 31;
  256. if ( mon == 1 ) // feb
  257. {
  258. days = ( 28 + lpyr );
  259. }
  260. else
  261. {
  262. if ( mon > 6 ) // aug-dec
  263. {
  264. mon--;
  265. }
  266. if ( mon & 1 )
  267. {
  268. days = 30;
  269. }
  270. }
  271. return ( days );
  272. }
  273. /*********************************************************************
  274. * @fn osal_ConvertUTCSecs
  275. *
  276. * @brief Converts a UTCTimeStruct to UTCTime
  277. *
  278. * @param tm - pointer to provided struct
  279. *
  280. * @return number of seconds since 00:00:00 on 01/01/2000 (UTC)
  281. */
  282. UTCTime osal_ConvertUTCSecs( UTCTimeStruct *tm )
  283. {
  284. uint32 seconds;
  285. /* Seconds for the partial day */
  286. seconds = (((tm->hour * 60UL) + tm->minutes) * 60UL) + tm->seconds;
  287. /* Account for previous complete days */
  288. {
  289. /* Start with complete days in current month */
  290. uint16 days = tm->day;
  291. /* Next, complete months in current year */
  292. {
  293. int8 month = tm->month;
  294. while ( --month >= 0 )
  295. {
  296. days += monthLength( IsLeapYear( tm->year ), month );
  297. }
  298. }
  299. /* Next, complete years before current year */
  300. {
  301. uint16 year = tm->year;
  302. while ( --year >= BEGYEAR )
  303. {
  304. days += YearLength( year );
  305. }
  306. }
  307. /* Add total seconds before partial day */
  308. seconds += (days * DAY);
  309. }
  310. return ( seconds );
  311. }