ProductPara.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "includes.h"
  2. HGS_POC_DEF hgs_poc_type;
  3. SUT_PRODUCT_PARA sutProductPara;
  4. char DEV_HARDID[16];
  5. //读取指定地址的半字(16位数据)
  6. //faddr:读地址(此地址必须为2的倍数!!)
  7. //返回值:对应数据
  8. u16 STMFLASH_ReadHalfWord(u32 faddr)
  9. {
  10. return *(vu16*)faddr;
  11. }
  12. /**********************************************************************************************************
  13. *写页数据,擦除整页2048字节,并写入Byte数据,数据可少于2048Btye,但必须是偶数。
  14. ************************************************************************************************************/
  15. void WritePageData(unsigned long WriteAddr,unsigned char *pData,unsigned short DataLen)
  16. {
  17. unsigned short i;
  18. unsigned short temp;
  19. FLASH_Status status;
  20. if(WriteAddr<FLASH_BASE || (WriteAddr>=(FLASH_BASE+1024*STM32_FLASH_SIZE)))return;//非法地址
  21. if(WriteAddr&0x0000003ff)return;//非页首地址
  22. FLASH_Unlock(); //解锁
  23. status=FLASH_ErasePage(WriteAddr);//擦除这个扇区
  24. if(status!=FLASH_COMPLETE){
  25. return;
  26. }
  27. //写入
  28. for(i = 0; i < DataLen; i+=2)
  29. {
  30. //数据8位融合为16位
  31. temp = (((unsigned short)pData[i+1])<<8) + ((unsigned short)pData[i]);
  32. FLASH_ProgramHalfWord((WriteAddr+i),temp);
  33. }
  34. FLASH_Lock();//上锁
  35. }
  36. void ReadFlashData(unsigned long ReadAddr,unsigned char *pData,unsigned short DataLen)
  37. {
  38. int i,j;
  39. unsigned short temp;
  40. j=0;
  41. for(i=0;i<DataLen;i+=2){
  42. temp=STMFLASH_ReadHalfWord(ReadAddr+i);
  43. pData[j++]=(unsigned char)(temp&0xff);
  44. pData[j++]=(temp>>8)&0xff;
  45. }
  46. }
  47. /**********************************************************************************************************
  48. SaveProductParaToFlash
  49. ************************************************************************************************************/
  50. void SaveProductParaToFlash()
  51. {
  52. uint32_t tick;
  53. //char recBuf[60];
  54. PWR_EN_HIGH;
  55. // if(sutProductPara.takeNoteFlag)
  56. // {
  57. // //记录保存相关信息到SPI FLASH
  58. // tick=RTC_GetCounter();
  59. // //时间 位置 机型 序列号
  60. // snprintf(recBuf, sizeof(recBuf), "[rec]%d,%s,%s,%08x|\r\n",tick,recoreInfo,sutProductPara.ProductName,sutProductPara.PSN);
  61. // sFlash_Write(recBuf, RECORD_INFO_ADDRESS, sizeof(recBuf));
  62. // SlwTrace(INF, recBuf, 0);
  63. // }
  64. ////////////////////////
  65. WritePageData(MAIN_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
  66. //DelayMs(100);
  67. //WritePageData(BACKUP_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
  68. //PWR_EN_LOW;//软按键不需要
  69. }
  70. /********************************************************************
  71. *SaveParaToFlash
  72. 将sutNetPara重要参数保存到FLASH
  73. *********************************************************************/
  74. #define MIN_PSN_NUM 1000000
  75. #define MAX_PSN_NUM 4200000000
  76. void ReadProductParaFromFlash(void)
  77. {
  78. char needsave=0;
  79. char needsleep=0;
  80. //1、读取主区参数
  81. ReadFlashData(MAIN_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
  82. if(sutProductPara.PSN < MIN_PSN_NUM || sutProductPara.PSN > MAX_PSN_NUM){
  83. printf("PSN invalid:%x\r\n",sutProductPara.PSN);
  84. sutProductPara.PSN=1990595007;
  85. }
  86. if(sutProductPara.ProductVersion != PRODUCT_VERSION){
  87. sutProductPara.ProductVersion=PRODUCT_VERSION;
  88. needsave=1;
  89. }
  90. if(0!=strcmp(sutProductPara.ProductName, PRODUCT_NAME)){
  91. strcpy(sutProductPara.ProductName, PRODUCT_NAME);
  92. needsave=1;
  93. }
  94. if(newPara.PF1Status>1){
  95. newPara.PF1Status=0;
  96. needsave=1;
  97. }
  98. if(sutProductPara.SleepFlag==1){
  99. needsave=1;
  100. needsleep=1;
  101. sutProductPara.SleepFlag=0;
  102. }
  103. if(needsave!=0) SaveProductParaToFlash();
  104. if(needsleep) Sleeping();
  105. }
  106. void GpsTimeUpdate(void)
  107. {
  108. newPara.gpsTimeIndex=0;// ZT260不支持GPS
  109. if(GPS_TimeTable[newPara.gpsTimeIndex])//判断GPS是否已关,根据上传时间来算
  110. {
  111. //GPS_PWREN_HIGH;
  112. g_uiGpsStat=1;
  113. }
  114. else{
  115. //GPS_PWREN_LOW;
  116. g_uiGpsStat=0;
  117. sutGpsInfo.isGpsWork=0;
  118. }
  119. printf("newPara.gpsTimeIndex===%d,%d\r\n",newPara.gpsTimeIndex,g_uiGpsStat);
  120. }