ProductPara.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /********************************************************************************
  2. * File Name: ProductPara.c
  3. * Function Describe:
  4. * Explain:
  5. * Writer: ShiLiangWen
  6. * Date: 2015-7-6
  7. *******************************************************************************/
  8. #define THIS_FILE_ID 6
  9. //-----------------------------------------------------------------------------
  10. #include "includes.h"
  11. HGS_POC_DEF hgs_poc_type;
  12. SUT_PRODUCT_PARA sutProductPara;
  13. char DEV_HARDID[16];
  14. //读取指定地址的半字(16位数据)
  15. //faddr:读地址(此地址必须为2的倍数!!)
  16. //返回值:对应数据.
  17. u16 STMFLASH_ReadHalfWord(u32 faddr)
  18. {
  19. return *(vu16*)faddr;
  20. }
  21. //从指定地址开始读出指定长度的数据
  22. //ReadAddr:起始地址
  23. //pBuffer:数据指针
  24. //NumToWrite:半字(16位)数
  25. void STMFLASH_Read(u32 ReadAddr,u16 *pBuffer,u16 NumToRead)
  26. {
  27. u16 i;
  28. for(i=0;i<NumToRead;i++)
  29. {
  30. pBuffer[i]=STMFLASH_ReadHalfWord(ReadAddr);//读取2个字节.
  31. ReadAddr+=2;//偏移2个字节.
  32. }
  33. }
  34. /**********************************************************************************************************
  35. *写页数据,擦除整页2048字节,并写入Byte数据,数据可少于2048Btye,但必须是偶数。
  36. ************************************************************************************************************/
  37. void WritePageData(unsigned long WriteAddr,unsigned char *pData,unsigned short DataLen)
  38. {
  39. unsigned short i;
  40. unsigned short temp;
  41. FLASH_Status status;
  42. RunMake(THIS_FILE_ID);
  43. if(WriteAddr<STM32_FLASH_BASE || (WriteAddr>=(STM32_FLASH_BASE+1024*STM32_FLASH_SIZE)))return;//非法地址
  44. if(WriteAddr&0x0000003ff)return;//非页首地址
  45. RunMake(THIS_FILE_ID);
  46. FLASH_Unlock(); //解锁
  47. RunMake(THIS_FILE_ID);
  48. status=FLASH_ErasePage(WriteAddr);//擦除这个扇区
  49. if(status!=FLASH_COMPLETE){
  50. RunMake(THIS_FILE_ID);
  51. return;
  52. }
  53. RunMake(THIS_FILE_ID);
  54. //写入
  55. for(i = 0; i < DataLen; i+=2)
  56. {
  57. //数据8位融合为16位
  58. temp = (((unsigned short)pData[i+1])<<8) + ((unsigned short)pData[i]);
  59. FLASH_ProgramHalfWord((WriteAddr+i),temp);
  60. }
  61. RunMake(THIS_FILE_ID);
  62. FLASH_Lock();//上锁
  63. RunMake(THIS_FILE_ID);
  64. }
  65. /**********************************************************************************************************
  66. ************************************************************************************************************/
  67. void ReadFlashData(unsigned long ReadAddr,unsigned char *pData,unsigned short DataLen)
  68. {
  69. int i,j;
  70. unsigned short temp;
  71. j=0;
  72. for(i=0;i<DataLen;i+=2){
  73. temp=STMFLASH_ReadHalfWord(ReadAddr+i);
  74. pData[j++]=(unsigned char)(temp&0xff);
  75. pData[j++]=(temp>>8)&0xff;
  76. }
  77. }
  78. void printfATHCODE(void)
  79. {
  80. char buf[3], buf2[50];
  81. // unsigned char i;
  82. // for(i=0;i<7;i++)
  83. // {
  84. // sprintf(buf, "%02x", sutProductPara.AutH[i]);
  85. // strcat(buf2, buf);
  86. // }
  87. // strcat(buf2, "\r\n");
  88. // SlwTrace(INF , buf2);
  89. //snprintf(buf2, sizeof(buf2), "index=%d,time=%d",sutProductPara.m_GPS_TimeIndex,sutGpsInfo.GPS_UploadTime);
  90. //SlwTrace(INF, buf2,1);
  91. }
  92. /**********************************************************************************************************
  93. SaveProductParaToFlash
  94. ************************************************************************************************************/
  95. void SaveProductParaToFlash()
  96. {
  97. uint32_t tick;
  98. //char recBuf[60];
  99. PWR_EN_HIGH;
  100. // if(sutProductPara.takeNoteFlag)
  101. // {
  102. // //记录保存相关信息到SPI FLASH
  103. // tick=RTC_GetCounter();
  104. // //时间 位置 机型 序列号
  105. // snprintf(recBuf, sizeof(recBuf), "[rec]%d,%s,%s,%08x|\r\n",tick,recoreInfo,sutProductPara.ProductName,sutProductPara.PSN);
  106. // sFlash_Write(recBuf, RECORD_INFO_ADDRESS, sizeof(recBuf));
  107. // SlwTrace(INF, recBuf, 0);
  108. // }
  109. ////////////////////////
  110. WritePageData(MAIN_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
  111. //DelayMs(100);
  112. //WritePageData(BACKUP_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
  113. PWR_EN_LOW;
  114. }
  115. #if 0
  116. void ResetFip(void)
  117. {
  118. sutProductPara.MFotaIP[0]=FOTA_IAP_IP0;
  119. sutProductPara.MFotaIP[1]=FOTA_IAP_IP1;
  120. sutProductPara.MFotaIP[2]=FOTA_IAP_IP2;
  121. sutProductPara.MFotaIP[3]=FOTA_IAP_IP3;
  122. sutProductPara.MFotaPort=FOTA_IAP_PORT;
  123. }
  124. void FotaIpCheck(void)
  125. {
  126. char resetflag;
  127. resetflag=0;
  128. if(sutProductPara.ProductVersion != PRODUCT_VERSION)
  129. {
  130. if((sutProductPara.MFotaIP[0] == 0xFF &&
  131. sutProductPara.MFotaIP[1] == 0xFF &&
  132. sutProductPara.MFotaIP[2] == 0xFF &&
  133. sutProductPara.MFotaIP[3] == 0xFF) ||
  134. sutProductPara.MFotaPort == 0xFFFF)
  135. {
  136. resetflag=1;
  137. }
  138. if(sutProductPara.ProductVersion <= 107 && PRODUCT_VERSION >= 108)
  139. {
  140. resetflag=2;
  141. }
  142. if(resetflag) ResetFip();
  143. }
  144. }
  145. #endif
  146. /********************************************************************
  147. *SaveParaToFlash
  148. 将sutNetPara重要参数保存到FLASH
  149. *********************************************************************/
  150. #define MIN_PSN_NUM 1000000
  151. #define MAX_PSN_NUM 4200000000
  152. void ReadProductParaFromFlash(void)
  153. {
  154. char needsave=0;
  155. IWDG_ReloadCounter();
  156. //1、读取主区参数
  157. ReadFlashData(MAIN_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
  158. if(sutProductPara.PSN < MIN_PSN_NUM || sutProductPara.PSN > MAX_PSN_NUM){
  159. printf("PSN invalid:%x\r\n",sutProductPara.PSN);
  160. sutProductPara.PSN=0;
  161. }
  162. if(sutProductPara.ProductVersion != PRODUCT_VERSION){
  163. sutProductPara.ProductVersion=PRODUCT_VERSION;
  164. needsave=1;
  165. }
  166. if(0!=strcmp(sutProductPara.ProductName, PRODUCT_NAME)){
  167. strcpy(sutProductPara.ProductName, PRODUCT_NAME);
  168. needsave=1;
  169. }
  170. if(needsave!=0) SaveProductParaToFlash();
  171. }
  172. void GpsTimeUpdate(void)
  173. {
  174. if(GPS_TimeTable[newPara.gpsTimeIndex])//判断GPS是否已关,根据上传时间来算
  175. {
  176. GPS_PWREN_HIGH;
  177. printf("gpsOpening===%d\r\n",newPara.gpsTimeIndex);
  178. g_uiGpsStat=1;
  179. }
  180. else{
  181. printf("gpsDown====%d\r\n",newPara.gpsTimeIndex);
  182. GPS_PWREN_LOW;
  183. g_uiGpsStat=0;
  184. sutGpsInfo.isGpsWork=0;
  185. sutGpsInfo.isGpsValid=0;
  186. sutGpsInfo.GpsInactiveCt=0;
  187. g_usUart3RecvLen=0;
  188. }
  189. }
  190. /************************************file begin*******************************/