Browse Source

数据测试

CHENJIE-PC\QiXiang_CHENJIE 3 years ago
parent
commit
f642610ad0
2 changed files with 125 additions and 6 deletions
  1. 15 0
      inc/app.h
  2. 110 6
      src/MainTask.c

+ 15 - 0
inc/app.h

@@ -40,6 +40,7 @@ extern "C" {
 
 #define APP_CONFIG_FILE_LATEST_VERSION 0
 #define APP_CONFIG_FILE_NAME  "qxappconfig.nvm"
+#define APP_DATAINFO_FILE_NAME  "qxappDataInfo.nvm"
 typedef struct AppNVMDataType
 {	
     BOOL   appDataModify;		//数据更改标志位
@@ -78,6 +79,20 @@ typedef enum
     APP_WAIT_STATE
 } appRunningState_t;
 //uint8_t* Uart_Receive_func(Uart_Receive_Type Uart_Receive_Msg,uint8_t* Uart_Rece_buffer,uint8_t Data_Len);
+//累计数据结构体
+typedef struct AppDataType
+{	
+    BOOL   appDataModify;		//数据更改标志位
+    UINT8  BmsChrgInfoSendFreq;
+    UINT8  BmsDisChrgInfoSendFreq;
+    UINT8  GpsChrgInfoSendFreq;
+    UINT8  GpsDisChrgInfoSendFreq;
+}AppDataBody;
+typedef struct _AppDataHeader
+{
+    UINT16 fileBodySize; //file body size, not include size of header;
+    UINT8  checkSum;
+}AppDataHeader;
 
 #ifdef __cplusplus
 }

+ 110 - 6
src/MainTask.c

@@ -41,8 +41,9 @@ volatile bool Sleep_flag = false;//睡眠标志位
 extern UINT32 TcpService;
 extern UINT8 BattChrgEndFlag;
 AppNVMDataType AppNVMData;
-
-
+AppDataBody AppDataInfo;
+AppConfigHeader   AppConfigHr;   //4 bytes
+AppDataHeader AppDataHr;
 //主线程堆栈声明区
 static StaticTask_t           gProcess_Main_Task_t;
 static UINT8                  gProcess_Main_TaskStack[PROC_MAIN_TASK_STACK_SIZE];
@@ -69,10 +70,6 @@ void appSaveConfig(void);
 static void appGetNVMSavedData(void);
 void appSaveNVMData(void);
 
-
-AppConfigHeader    AppConfigHr;   //4 bytes
-
-
 //主线程任务区
 static void MainTask(void* arg)
 {
@@ -600,6 +597,113 @@ static void setDefaultAppDataValue(void)
     return;
 }
 
+static void appSaveDataInfo(void)
+{
+    OSAFILE fp = PNULL;
+    UINT32  writeCount = 0;
+    AppDataHeader    AppDataHr;   //4 bytes
+    fp = OsaFopen(APP_DATAINFO_FILE_NAME, "wb");   //read & write
+    if(OsaFseek(fp, 0, SEEK_SET) != 0)
+	{
+        #ifdef USING_PRINTF
+				printf("Seek file failed [%d] \r\n",__LINE__);
+	    #endif
+	    OsaFclose(fp);
+        return;
+	}
+    if (fp == PNULL)
+    {
+	    #ifdef USING_PRINTF
+			printf(" NVM, can't open/create NVM: 'qxappconfig.nvm', save NVM failed\n");
+		#endif
+        return;
+    }
+    AppDataHr.fileBodySize   = sizeof(AppDataBody); 
+    AppDataHr.checkSum       = OsaCalcCrcValue((UINT8 *)&AppDataInfo, sizeof(AppDataInfo));
+
+    writeCount = OsaFwrite(&AppDataHr, sizeof(AppDataHeader), 1, fp);
+    if (writeCount != 1)
+    {
+        #ifdef USING_PRINTF
+                    printf(" NVM: 'qxappconfig.nvm', write the file header failed\n");
+        #endif
+        OsaFclose(fp);
+        return;
+    }
+    AppDataInfo.appDataModify = FALSE;
+    writeCount = OsaFwrite(&AppDataInfo, sizeof(AppDataInfo), 1, fp);
+    if (writeCount != 1)
+    {
+        #ifdef USING_PRINTF
+			printf(" NVM: 'qxappconfig.nvm', write the file body failed\n");
+		#endif
+    }
+    OsaFclose(fp);
+    return;
+}
+
+void LoadAppDataInfo(void)
+{
+    OSAFILE fp = PNULL;
+    UINT32  readCount = 0;
+    UINT8   crcCheck = 0;
+    void    *pReadAppConfig = (void *)&AppDataInfo;
+    fp = OsaFopen(APP_DATAINFO_FILE_NAME, "rb");   //read only
+    if (fp == PNULL)
+    {
+        #ifdef USING_PRINTF
+            printf(" NVM, can't open NVM: 'qxappData.nvm', use the defult value\n");
+        #endif
+        setDefaultAppDataInfo();
+        appSaveDataInfo();
+        return;
+    }
 
+    readCount = OsaFread(&AppDataHr, sizeof(AppDataHeader), 1, fp);
+    UINT8 readtimes=0;
+    while (readCount != 1 && readtimes<=5 )
+    {
+        readtimes++;
+        readCount = OsaFread(&AppDataHr, sizeof(AppDataHeader), 1, fp);
+        osDelay(10);
+    }
+    if(readtimes>5)
+    {
+        OsaFclose(fp);
+        setDefaultAppDataInfo();
+        appSaveDataInfo();
+        return;
+    }
+    if(AppDataHr.fileBodySize==sizeof(AppDataBody))//结构体数据没变动,可直接读出
+    {
+        readCount = OsaFread(pReadAppConfig, AppDataHr.fileBodySize, 1, fp);
+        //crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
+        readtimes = 0;
+        while (readtimes<=5 && readCount != 1)
+        {
+            readtimes++;
+            readCount = OsaFread(pReadAppConfig, AppConfigHr.fileBodySize, 1, fp);
+            //crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, sizeof(AppNVMData));
+        }
+    }
+    else
+    {
+
+    }
+    OsaFclose(fp);
+    return;
+}
+
+//无法找到文件时的第一次启动,后续不会启动此函数
+static void setDefaultAppDataInfo(void)
+{
+    memset(&AppDataInfo, 0x00, sizeof(AppDataBody));
+    AppDataInfo.appDataModify = false;
+    AppDataInfo.BmsChrgInfoSendFreq = 5;
+    AppDataInfo.BmsDisChrgInfoSendFreq = 30;
+    AppDataInfo.GpsChrgInfoSendFreq = 30;
+    AppDataInfo.GpsDisChrgInfoSendFreq = 5;
+    return;
+}