102 lines
2.7 KiB
C
102 lines
2.7 KiB
C
#include "RtcTask.h"
|
|
#include "WatchDogTask.h"
|
|
#include "FileSysTask.h"
|
|
#include "TimerTask.h"
|
|
#include "UpcCommTask.h"
|
|
|
|
#include "ff.h"
|
|
|
|
#include <cpu.h>
|
|
#include <os.h>
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
/********************** 时钟同步任务的参数 ************************/
|
|
static int gTaskRtcSyncWatchDogId = -1; // RTC时钟同步任务的看门狗ID
|
|
static int gTaskRtcSyncEnable = 1; // 默认初次连接网络时,同步时钟
|
|
static unsigned short gTaskRtcSyncSendSerial = 0; // 请求时钟同步计数
|
|
static int gTaskRtcSyncFlag = 0; // 时钟是否已经同步过
|
|
static int gTaskRtcSyncTimerEventId = -1;
|
|
|
|
static void TaskRtcEnableSync(void);
|
|
static void TaskRtcDisableSync(void);
|
|
|
|
/* RTC时钟初始化 */
|
|
void TaskRtcInit(void)
|
|
{
|
|
/* 注册RTC时钟同步看门狗 */
|
|
gTaskRtcSyncWatchDogId = TaskWatchdogRegEvent("TaskRtcSync");
|
|
gTaskRtcSyncTimerEventId = TaskTimerRegEventId();
|
|
RtcDrvInit();
|
|
}
|
|
|
|
/* 时钟同步任务 */
|
|
void TaskRtcSync(void *arg)
|
|
{
|
|
unsigned int i = 0;
|
|
unsigned int RealWbyte;
|
|
int ret = 0;
|
|
/* 将时钟同步任务开启写入日志 */
|
|
do {
|
|
ret = TaskFileLogWrite("TaskRtcSync: Join in!!\n", strlen("TaskRtcSync: Join in!!\n"), &RealWbyte);
|
|
TaskWatchdogFreed(gTaskRtcSyncWatchDogId);
|
|
OSTimeDly(1);
|
|
} while (ret > 0);
|
|
TaskTimerStartEvent(&gTaskRtcSyncTimerEventId, 604800, 0xffffffff, TaskRtcEnableSync); // 7天同步一次时钟
|
|
while (1) {
|
|
if (gTaskRtcSyncEnable != 0) { // 如果同步时间被使能,则进行时间同步
|
|
/* 发送函数 */
|
|
i = TaskUpcCommSendMessage(TASK_RTC_SYNC_MESSGAE_ID, &gTaskRtcSyncSendSerial, NULL, 0);
|
|
if (i == 0) { // 发送失败的处理
|
|
OSTimeDly(1000);
|
|
TaskWatchdogFreed(gTaskRtcSyncWatchDogId);
|
|
continue;
|
|
}
|
|
/* 给60S的等待回应时间 */
|
|
i = 0;
|
|
while (gTaskRtcSyncEnable == 1) {
|
|
OSTimeDly(1000);
|
|
TaskWatchdogFreed(gTaskRtcSyncWatchDogId);
|
|
i++;
|
|
if (i > 60) {
|
|
break;
|
|
}
|
|
}
|
|
if (gTaskRtcSyncEnable != 1) {
|
|
gTaskRtcSyncSendSerial++;
|
|
}
|
|
}
|
|
TaskWatchdogFreed(gTaskRtcSyncWatchDogId);
|
|
OSTimeDly(1000);
|
|
}
|
|
}
|
|
|
|
/* 确认时钟同步函数 */
|
|
int TaskRtcSyncSet(DRV_RTC_TIME *RtcTime)
|
|
{
|
|
int ret;
|
|
ret = RtcSetTime(RtcTime);
|
|
if (ret < 0) {
|
|
return -1;
|
|
}
|
|
gTaskRtcSyncFlag = 1;
|
|
TaskRtcDisableSync();
|
|
return 0;
|
|
}
|
|
|
|
int TaskRtcGetSyncSta(void)
|
|
{
|
|
return gTaskRtcSyncFlag;
|
|
}
|
|
|
|
static void TaskRtcEnableSync(void)
|
|
{
|
|
gTaskRtcSyncEnable = 1;
|
|
}
|
|
|
|
static void TaskRtcDisableSync(void)
|
|
{
|
|
gTaskRtcSyncEnable = 0;
|
|
}
|