ProductPara.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. //IWDG_ReloadCounter();
  81. //1、读取主区参数
  82. ReadFlashData(MAIN_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
  83. if(sutProductPara.PSN < MIN_PSN_NUM || sutProductPara.PSN > MAX_PSN_NUM){
  84. printf("PSN invalid:%x\r\n",sutProductPara.PSN);
  85. sutProductPara.PSN=0;
  86. }
  87. if(sutProductPara.ProductVersion != PRODUCT_VERSION){
  88. sutProductPara.ProductVersion=PRODUCT_VERSION;
  89. needsave=1;
  90. }
  91. if(0!=strcmp(sutProductPara.ProductName, PRODUCT_NAME)){
  92. strcpy(sutProductPara.ProductName, PRODUCT_NAME);
  93. }
  94. if(sutProductPara.SleepFlag==1){
  95. needsave=1;
  96. needsleep=1;
  97. sutProductPara.SleepFlag=0;
  98. }
  99. if(needsave!=0) SaveProductParaToFlash();
  100. if(needsleep) Sleeping();
  101. }
  102. void GpsTimeUpdate(void)
  103. {
  104. if(GPS_TimeTable[newPara.gpsTimeIndex])//判断GPS是否已关,根据上传时间来算
  105. {
  106. //GPS_PWREN_HIGH;
  107. GPSRestart();
  108. g_uiGpsStat=1;
  109. }
  110. else{
  111. //GPS_PWREN_LOW;
  112. GPSStop();
  113. g_uiGpsStat=0;
  114. sutGpsInfo.isGpsWork=0;
  115. }
  116. }