蓝牙BLE芯片PHY6222之RTC计时
个人建议使用外部32K作为RTC的时钟源,精度相对6222内部32K高很多,内部32K看官方资料是±500ppm的时钟精度
RTC驱动
datetime_t ttdatetime;
#define UNIX_2000 946656000
UTCTimeStruct user_get_utc(void);
void user_set_utc(UTCTimeStruct *m_UtcStr);
void my_dump_byte_reverse(uint8_t* pData, int dlen);
void app_set_time(uint16 year, uint8 month, uint8 day, uint8 hour, uint8 minutes, uint8 seconds)
{UTCTimeStruct test_time;test_time.year = year;test_time.month = month - 1;test_time.day = day - 1;test_time.hour = hour;test_time.minutes = minutes;test_time.seconds = seconds;user_set_utc(&test_time);
}void get_current_systerm_time(datetime_t * rtc_time)
{UTCTimeStruct convet_timestamp_unit;UTCTimeStruct cnt;UTCTime utc_timecnt;
// »ñÈ¡utcʱ¼äcnt = user_get_utc();// ¸üÐÂʱ¼äµ½rtc_timeÖÐrtc_time->year = cnt.year;rtc_time->month = cnt.month;rtc_time->day = cnt.day;rtc_time->hour = cnt.hour;rtc_time->minutes = cnt.minutes;rtc_time->seconds = cnt.seconds;// ¸üÐÂʱ¼äµ½rtc_timeÖÐconvet_timestamp_unit.year = cnt.year;convet_timestamp_unit.month = cnt.month - 1;convet_timestamp_unit.day = cnt.day - 1;convet_timestamp_unit.hour = cnt.hour;convet_timestamp_unit.minutes = cnt.minutes;convet_timestamp_unit.seconds = cnt.seconds;utc_timecnt = osal_ConvertUTCSecs(&convet_timestamp_unit) + 946656000;LOG("#timestamp=%d\r\n", utc_timecnt);LOG("#%4d:%02d:%02d--%02d:%02d:%02d\r\n", rtc_time->year, rtc_time->month, rtc_time->day,rtc_time->hour, rtc_time->minutes, rtc_time->seconds);}UTCTime appUTC_time;
UTCTimeStruct appUTC_timeStruct;
//ʱ¼äת»»³ÉÃë²¢ÉèÖÃutc ʱ¼ä
void user_set_utc(UTCTimeStruct *m_UtcStr)
{appUTC_time = osal_ConvertUTCSecs(m_UtcStr);osal_setClock(appUTC_time - UNIX_2000);
}//»ñȡϵͳʱ¼ä
UTCTimeStruct user_get_utc(void)
{UTCTimeStruct tmp_utcStr;appUTC_time = osal_getClock();appUTC_time += UNIX_2000;osal_ConvertUTCTime(&appUTC_timeStruct, appUTC_time);tmp_utcStr = appUTC_timeStruct;tmp_utcStr.day += 1;tmp_utcStr.month += 1;return tmp_utcStr;
}
RTC初始化
app_set_time(2022, 4, 10, 12, 33, 56);
osal_set_event( simpleBLEPeripheral_TaskID, TIMER_DT_EVT );
定期获取RTC时间
if ( events & TIMER_DT_EVT ){get_current_systerm_time(&ttdatetime);osal_start_timerEx( simpleBLEPeripheral_TaskID, TIMER_DT_EVT, 30000 );return ( events ^ TIMER_DT_EVT );}
PS:个人学习笔记,想参考就参考,不参考划走下一篇,工程师还是要具备的调试,分析能力