123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- #include "comdef.h"
- #include "OnBoard.h"
- #include "OSAL.h"
- #include "OSAL_Clock.h"
- #define YearLength(yr) ((uint16)(IsLeapYear(yr) ? 366 : 365))
- #define BEGYEAR 2000
- #define DAY 86400UL
- extern uint32 macMcuPrecisionCount(void);
- #if (defined HAL_MCU_CC2430) || (defined HAL_MCU_CC2530) || (defined HAL_MCU_CC2533)
-
- extern __near_func uint32 osalMcuDivide31By16To16( uint32 dividend, uint16 divisor );
- #define CONVERT_320US_TO_MS_ELAPSED_REMAINDER( x, y, z ) st( \
- \
- \
- \
- x = osalMcuDivide31By16To16( x, 25 ); \
- \
- \
- y += (x >> 16); \
- \
- \
- z = (uint16)(x & 0x0FFFF); \
- )
- #else
- #define CONVERT_320US_TO_MS_ELAPSED_REMAINDER( x, y, z ) st( \
- y += x / 25; \
- z = x % 25; \
- )
- #endif
- static uint32 previousMacTimerTick = 0;
- static uint16 remUsTicks = 0;
- static uint16 timeMSec = 0;
- UTCTime OSAL_timeSeconds = 0;
- static uint8 monthLength( uint8 lpyr, uint8 mon );
- static void osalClockUpdate( uint16 elapsedMSec );
- void osalTimeUpdate( void )
- {
- halIntState_t intState;
- uint32 tmp;
- uint32 ticks320us;
- uint16 elapsedMSec = 0;
- HAL_ENTER_CRITICAL_SECTION(intState);
-
- tmp = macMcuPrecisionCount();
- HAL_EXIT_CRITICAL_SECTION(intState);
-
- if ( tmp != previousMacTimerTick )
- {
-
- ticks320us = (tmp - previousMacTimerTick) & 0xffffffffu;
-
- previousMacTimerTick = tmp;
-
-
-
- tmp = (ticks320us * 8) + remUsTicks;
-
- CONVERT_320US_TO_MS_ELAPSED_REMAINDER( tmp, elapsedMSec, remUsTicks );
-
- if ( elapsedMSec )
- {
- osalClockUpdate( elapsedMSec );
- osalTimerUpdate( elapsedMSec );
- }
- }
- }
- static void osalClockUpdate( uint16 elapsedMSec )
- {
-
- timeMSec += elapsedMSec;
-
- if ( timeMSec >= 1000 )
- {
- OSAL_timeSeconds += timeMSec / 1000;
- timeMSec = timeMSec % 1000;
- }
- }
- void osal_setClock( UTCTime newTime )
- {
- OSAL_timeSeconds = newTime;
- }
- UTCTime osal_getClock( void )
- {
- return ( OSAL_timeSeconds );
- }
- void osal_ConvertUTCTime( UTCTimeStruct *tm, UTCTime secTime )
- {
-
- {
- uint32 day = secTime % DAY;
- tm->seconds = day % 60UL;
- tm->minutes = (day % 3600UL) / 60UL;
- tm->hour = day / 3600UL;
- }
-
- {
- uint16 numDays = secTime / DAY;
- tm->year = BEGYEAR;
- while ( numDays >= YearLength( tm->year ) )
- {
- numDays -= YearLength( tm->year );
- tm->year++;
- }
- tm->month = 0;
- while ( numDays >= monthLength( IsLeapYear( tm->year ), tm->month ) )
- {
- numDays -= monthLength( IsLeapYear( tm->year ), tm->month );
- tm->month++;
- }
- tm->day = numDays;
- }
- }
- static uint8 monthLength( uint8 lpyr, uint8 mon )
- {
- uint8 days = 31;
- if ( mon == 1 )
- {
- days = ( 28 + lpyr );
- }
- else
- {
- if ( mon > 6 )
- {
- mon--;
- }
- if ( mon & 1 )
- {
- days = 30;
- }
- }
- return ( days );
- }
- UTCTime osal_ConvertUTCSecs( UTCTimeStruct *tm )
- {
- uint32 seconds;
-
- seconds = (((tm->hour * 60UL) + tm->minutes) * 60UL) + tm->seconds;
-
- {
-
- uint16 days = tm->day;
-
- {
- int8 month = tm->month;
- while ( --month >= 0 )
- {
- days += monthLength( IsLeapYear( tm->year ), month );
- }
- }
-
- {
- uint16 year = tm->year;
- while ( --year >= BEGYEAR )
- {
- days += YearLength( year );
- }
- }
-
- seconds += (days * DAY);
- }
- return ( seconds );
- }
|