293 lines
8.6 KiB
C
293 lines
8.6 KiB
C
/*
|
||
* rtc.c
|
||
*
|
||
* Created on: Jul 29, 2025
|
||
* Author: cc
|
||
*/
|
||
#include "includes.h"
|
||
#include <string.h>
|
||
#include <time.h>
|
||
|
||
S_RTC RTC_Raw_Data = {
|
||
.year = 0,
|
||
.month = 1,
|
||
.day = 1,
|
||
.week = 0,
|
||
.hour = 0,
|
||
.minute = 0,
|
||
.second = 0,
|
||
};
|
||
|
||
S_RTC MCU_RTC_Data = {
|
||
.year = 0,
|
||
.month = 1,
|
||
.day = 1,
|
||
.week = 0,
|
||
.hour = 0,
|
||
.minute = 0,
|
||
.second = 0,
|
||
};
|
||
|
||
S_RTC Net_RTC_Data; //2024-08-03 网络时间
|
||
TIME_INFO_T g_time_info;
|
||
|
||
uint32_t Mcu_GetTime_tick = 0;
|
||
uint32_t Log_Time_ms = 0;
|
||
|
||
/*******************************************************************************
|
||
* Function Name : RTC_Init
|
||
* Description : RTC初始化 - 注意BLV-C1P没有RTC功能,只是使用系统定时器模拟RTC计时
|
||
* Return : None
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) void RTC_Init(void)
|
||
{
|
||
memset(&RTC_Raw_Data,0,sizeof(S_RTC));
|
||
memset(&MCU_RTC_Data,0,sizeof(S_RTC));
|
||
memset(&Net_RTC_Data,0,sizeof(S_RTC));
|
||
memset(&g_time_info,0,sizeof(TIME_INFO_T));
|
||
|
||
RTC_Raw_Data.year = 0x00;
|
||
RTC_Raw_Data.month = 0x01;
|
||
RTC_Raw_Data.day = 0x01;
|
||
RTC_Raw_Data.week = 0x00;
|
||
RTC_Raw_Data.hour = 0x00;
|
||
RTC_Raw_Data.minute = 0x00;
|
||
RTC_Raw_Data.second = 0x00;
|
||
|
||
MCU_RTC_Data.year = 0x00;
|
||
MCU_RTC_Data.month = 0x01;
|
||
MCU_RTC_Data.day = 0x01;
|
||
MCU_RTC_Data.week = 0x00;
|
||
MCU_RTC_Data.hour = 0x00;
|
||
MCU_RTC_Data.minute = 0x00;
|
||
MCU_RTC_Data.second = 0x00;
|
||
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : HEX_data_conversion_to_DEC
|
||
* Description : 将十六进制表达的十进制数据转换为实际的十进制数据 0x20 -> 20
|
||
* Return : None
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) uint8_t HEX_Conversion_To_DEC(uint8_t c_num)
|
||
{
|
||
uint8_t rev_num = 0;
|
||
|
||
rev_num = (c_num/16)*10 + (c_num%16);
|
||
|
||
return rev_num;
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : HEX_data_conversion_to_DEC
|
||
* Description : 将十进制数据转换为十六进制方式表达数据 20 -> 0x20
|
||
* Return : None
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) uint8_t DEV_Conversion_To_HEX(uint8_t c_num)
|
||
{
|
||
uint8_t rev_num = 0;
|
||
|
||
rev_num = (c_num/10)*16 + (c_num%10);
|
||
|
||
return rev_num;
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : RTC_Conversion_To_UTC
|
||
* Description : 将RTC时间转化为UTC时间
|
||
* Return : None
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) uint32_t RTC_Conversion_To_Unix(S_RTC *rtc_time)
|
||
{
|
||
// uint32_t timestamp = 0;
|
||
// struct tm test_time;
|
||
//
|
||
// test_time.tm_year = HEX_Conversion_To_DEC(rtc_time->year) + 2000 - 1900;
|
||
// if(rtc_time->month != 0x00)
|
||
// {
|
||
// test_time.tm_mon = HEX_Conversion_To_DEC(rtc_time->month) - 1;
|
||
// }else {
|
||
// test_time.tm_mon = 1;
|
||
// }
|
||
//
|
||
// test_time.tm_mday = HEX_Conversion_To_DEC(rtc_time->day);
|
||
// test_time.tm_hour = HEX_Conversion_To_DEC(rtc_time->hour);
|
||
// test_time.tm_min = HEX_Conversion_To_DEC(rtc_time->minute);
|
||
// test_time.tm_sec = HEX_Conversion_To_DEC(rtc_time->second);
|
||
// test_time.tm_isdst = -1;
|
||
//
|
||
// timestamp = mktime(&test_time); //这转化的标志的UTC时间戳
|
||
//
|
||
// /*北京时间还需要减去8小时*/
|
||
// timestamp -= 8*3600;
|
||
//
|
||
// return timestamp;
|
||
|
||
return 0x00;
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : UTC_Conversion_To_RTC
|
||
* Description : 将UTC时间转化为RTC时间
|
||
* Return : None
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) void Unix_Conversion_To_RTC(S_RTC *rtc_time,uint32_t utc_tick)
|
||
{
|
||
// uint8_t temp = 0;
|
||
// time_t temp_tick = utc_tick + 8*3600; /*北京时间换算成标准还需要加上8小时*/
|
||
// struct tm *test_time;
|
||
//
|
||
// test_time = localtime(&temp_tick);
|
||
//
|
||
// temp = ( 1900 + test_time->tm_year ) - 2000;
|
||
// rtc_time->year = DEV_Conversion_To_HEX(temp);
|
||
// temp = 1 + test_time->tm_mon;
|
||
// rtc_time->month = DEV_Conversion_To_HEX(temp);
|
||
// temp = test_time->tm_mday;
|
||
// rtc_time->day = DEV_Conversion_To_HEX(temp);
|
||
//
|
||
// temp = test_time->tm_hour;
|
||
// rtc_time->hour = DEV_Conversion_To_HEX(temp);
|
||
// temp = test_time->tm_min;
|
||
// rtc_time->minute = DEV_Conversion_To_HEX(temp);
|
||
// temp = test_time->tm_sec;
|
||
// rtc_time->second = DEV_Conversion_To_HEX(temp);
|
||
//
|
||
// temp = test_time->tm_wday;
|
||
// rtc_time->week = DEV_Conversion_To_HEX(temp);
|
||
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : RTC_ReadDate
|
||
* Description : RTC时间获取 - BLV_C1P中没有RTC功能,只好手动计时了
|
||
* Return : None
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) uint8_t RTC_ReadDate(S_RTC *psRTC)
|
||
{
|
||
|
||
if(g_time_info.time_select == 0x02){
|
||
/* CSIO时间读取 开始*/
|
||
|
||
psRTC->year = MCU_RTC_Data.year;
|
||
psRTC->month = MCU_RTC_Data.month;
|
||
psRTC->day = MCU_RTC_Data.day;
|
||
psRTC->hour = MCU_RTC_Data.hour;
|
||
psRTC->minute = MCU_RTC_Data.minute;
|
||
psRTC->second = MCU_RTC_Data.second;
|
||
psRTC->week = MCU_RTC_Data.week;
|
||
|
||
/* CSIO时间读取 结束*/
|
||
}else{
|
||
/* 软件计时 开始 */
|
||
|
||
//服务器的当前时间+本地计数
|
||
uint32_t rtc_tick = 0;
|
||
|
||
rtc_tick = RTC_Conversion_To_Unix(&MCU_RTC_Data);
|
||
//rtc_tick += rtc_hour*3600+rtc_min*60+rtc_sec;
|
||
rtc_tick += SysTick_1s - Mcu_GetTime_tick;
|
||
Unix_Conversion_To_RTC(psRTC,rtc_tick);
|
||
|
||
/* 软件计时 结束 */
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : RTC_WriteDate
|
||
* Description : RTC时间设置 - BLV_C1P中没有RTC功能,只好手动计时了
|
||
* Return : None
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) uint8_t RTC_WriteDate(S_RTC SetRTC)
|
||
{
|
||
//变量保存2个小时向服务器询问的当前时间,将本地计数清零
|
||
MCU_RTC_Data.year = SetRTC.year;
|
||
MCU_RTC_Data.month = SetRTC.month;
|
||
MCU_RTC_Data.day = SetRTC.day;
|
||
MCU_RTC_Data.hour = SetRTC.hour;
|
||
MCU_RTC_Data.minute = SetRTC.minute;
|
||
MCU_RTC_Data.second = SetRTC.second;
|
||
|
||
Mcu_GetTime_tick = SysTick_1s; //记录当前设置时间
|
||
|
||
return 0;
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : NetRTC_WriteDate
|
||
* Description : 网络设置 RTC时间
|
||
* Return : None
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) uint8_t NetRTC_WriteDate(S_RTC SetRTC)
|
||
{
|
||
Net_RTC_Data.year = SetRTC.year;
|
||
Net_RTC_Data.month = SetRTC.month;
|
||
Net_RTC_Data.day = SetRTC.day;
|
||
Net_RTC_Data.hour = SetRTC.hour;
|
||
Net_RTC_Data.minute = SetRTC.minute;
|
||
Net_RTC_Data.second = SetRTC.second;
|
||
Net_RTC_Data.week = SetRTC.week;
|
||
|
||
g_time_info.Mcu_GetTime_tick = SysTick_1s; //记录当前设置时间
|
||
|
||
return 0;
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : RTC_TASK
|
||
* Description : RTC任务 - BLV_C1P中没有RTC功能,只好手动计时了
|
||
* Return : None
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) void RTC_TASK(void)
|
||
{
|
||
static uint32_t RTC_Tick = 0;
|
||
uint8_t r_minute = 0;
|
||
if(SysTick_1ms - RTC_Tick >= 1000)
|
||
{
|
||
r_minute = RTC_Raw_Data.minute;
|
||
RTC_Tick = SysTick_1ms;
|
||
RTC_ReadDate(&RTC_Raw_Data);
|
||
|
||
if(r_minute != RTC_Raw_Data.minute)
|
||
{
|
||
Log_Time_ms = SysTick_1ms; //每个分钟里的时间戳
|
||
}
|
||
if(server_info.sync_tick==0x01)
|
||
{
|
||
server_info.sync_tick = 0x02;
|
||
}
|
||
}
|
||
}
|
||
|
||
/*******************************************************************************
|
||
* Function Name : RTC_TimeDate_Correct_Figure
|
||
* Description : RTC时间数据是否为有效数据内容 不允许出现 A~F
|
||
* Return : 0x00:数据内容正常;0x01:数据内容不正确
|
||
*******************************************************************************/
|
||
__attribute__((section(".non_0_wait"))) uint8_t RTC_TimeDate_Correct_Figure(uint8_t data)
|
||
{
|
||
uint8_t temp_num = data;
|
||
|
||
if( ((temp_num & 0x0F) < 0x0A) ){
|
||
temp_num >>= 4;
|
||
if( ((temp_num & 0x0F) < 0x0A) ){
|
||
return 0x00;
|
||
}
|
||
}
|
||
|
||
return 0x01;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|