ProductPara.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. const unsigned char POC_SERVER_IP[4]={183,131,13,10};
  12. const unsigned char GPS_SERVER_IP[4]={183,131,13,6};
  13. const unsigned char DEFAULT_LOAD_IP[4]={122,224,240,106};
  14. const unsigned char DEFAULT_CONFIG_IP[4]={122,224,240,106};
  15. SUT_PRODUCT_PARA sutProductPara;
  16. SUT_NEW_SEGMENT_PARA sutNewSegmentPara;
  17. unsigned char startFlashPro=0;
  18. //HGS_PRODUCTDATA sutProductPara;
  19. //读取指定地址的半字(16位数据)
  20. //faddr:读地址(此地址必须为2的倍数!!)
  21. //返回值:对应数据.
  22. u16 STMFLASH_ReadHalfWord(u32 faddr)
  23. {
  24. return *(vu16*)faddr;
  25. }
  26. //从指定地址开始读出指定长度的数据
  27. //ReadAddr:起始地址
  28. //pBuffer:数据指针
  29. //NumToWrite:半字(16位)数
  30. void STMFLASH_Read(u32 ReadAddr,u16 *pBuffer,u16 NumToRead)
  31. {
  32. u16 i;
  33. for(i=0;i<NumToRead;i++)
  34. {
  35. pBuffer[i]=STMFLASH_ReadHalfWord(ReadAddr);//读取2个字节.
  36. ReadAddr+=2;//偏移2个字节.
  37. }
  38. }
  39. /**********************************************************************************************************
  40. *写页数据,擦除整页1024字节,并写入Byte数据,数据可少于1024Btye,但必须是偶数。
  41. ************************************************************************************************************/
  42. void WritePageData(unsigned long WriteAddr,unsigned char *pData,unsigned short DataLen)
  43. {
  44. unsigned short i;
  45. unsigned short temp;
  46. FLASH_Status status;
  47. RunMake(THIS_FILE_ID);
  48. if(WriteAddr<STM32_FLASH_BASE || (WriteAddr>=(STM32_FLASH_BASE+1024*STM32_FLASH_SIZE)))return;//非法地址
  49. if(WriteAddr&0x0000003ff)return;//非页首地址
  50. RunMake(THIS_FILE_ID);
  51. FLASH_Unlock(); //解锁
  52. RunMake(THIS_FILE_ID);
  53. status=FLASH_ErasePage(WriteAddr);//擦除这个扇区
  54. if(status!=FLASH_COMPLETE){
  55. RunMake(THIS_FILE_ID);
  56. return;
  57. }
  58. RunMake(THIS_FILE_ID);
  59. //写入
  60. for(i = 0; i < DataLen; i+=2)
  61. {
  62. //数据8位融合为16位
  63. temp = (((unsigned short)pData[i+1])<<8) + ((unsigned short)pData[i]);
  64. FLASH_ProgramHalfWord((WriteAddr+i),temp);
  65. }
  66. RunMake(THIS_FILE_ID);
  67. FLASH_Lock();//上锁
  68. RunMake(THIS_FILE_ID);
  69. }
  70. /**********************************************************************************************************
  71. ************************************************************************************************************/
  72. void ReadFlashData(unsigned long ReadAddr,unsigned char *pData,unsigned short DataLen)
  73. {
  74. #if 0
  75. int i,j;
  76. unsigned short temp;
  77. j=0;
  78. for(i=0;i<DataLen;i+=2){
  79. temp=STMFLASH_ReadHalfWord(ReadAddr+i);
  80. pData[j++]=(unsigned char)(temp&0xff);
  81. pData[j++]=(temp>>8)&0xff;
  82. }
  83. #else
  84. unsigned short temp;
  85. unsigned short i,j;
  86. j=0;
  87. for(i=0;i<DataLen;i+=2)
  88. {
  89. temp=*(vu16*)(ReadAddr+i);
  90. pData[j++]=(unsigned char)(temp&0xff);
  91. if(j<DataLen)
  92. pData[j++]=(temp>>8)&0xff;
  93. }
  94. #endif
  95. }
  96. /**********************************************************************************************************
  97. SaveProductParaToFlash
  98. ************************************************************************************************************/
  99. void SaveProductParaToFlash(void)
  100. {
  101. PWR_EN_HIGH;
  102. WritePageData(APP_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
  103. PWR_EN_LOW;
  104. }
  105. void SaveNewSegmentToFlash()
  106. {
  107. PWR_EN_HIGH;
  108. WritePageData(NEW_CONFIG_ADDR,(unsigned char *)&sutNewSegmentPara,sizeof(SUT_NEW_SEGMENT_PARA));
  109. PWR_EN_LOW;
  110. }
  111. /********************************************************************
  112. *SaveParaToFlash
  113. 将sutNetPara重要参数保存到FLASH
  114. *********************************************************************/
  115. void ReadProductParaFromFlash(void)
  116. {
  117. unsigned char Rand;
  118. Rand=GetRandBySTM32ID();
  119. ReadFlashData(APP_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
  120. ReadFlashData(NEW_CONFIG_ADDR,(unsigned char *)&sutNewSegmentPara,sizeof(SUT_NEW_SEGMENT_PARA));
  121. //soc ID替代IMEI
  122. if(MCU_FLAG!=sutProductPara.McuFlag || Rand!=sutProductPara.Rand \
  123. ){
  124. SlwTrace(INF,"Reset Config Param!",1);
  125. memset((unsigned char *)&sutProductPara,0,sizeof(SUT_PRODUCT_PARA));
  126. sutProductPara.McuFlag=MCU_FLAG; //MCU标识
  127. sutProductPara.ModemType=MODEM_TYPE_M9507;
  128. sutProductPara.GpsType=GPS_TYPE;
  129. sutProductPara.Rand=GetRandBySTM32ID(); //伪随机数,此数值和STM32硬件ID关联,每个不同的IC数值将不同
  130. strcpy(sutProductPara.ProductName,PRODUCT_NAME);
  131. sutProductPara.ProductVersion=PRODUCT_VERSION;
  132. sutProductPara.PSN=150000000;//未写号情况下PSN为150000000
  133. strcpy(sutProductPara.APNApn, APN_APN);
  134. strcpy(sutProductPara.MEID,"00000000000000");
  135. strcpy(sutProductPara.APNName,APN_NAME);
  136. strcpy(sutProductPara.APNPass,APN_PASSWORD);
  137. WritePageData(APP_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
  138. ////////////////////////////////////////////
  139. memset((unsigned char *)&sutNewSegmentPara,0,sizeof(SUT_NEW_SEGMENT_PARA));
  140. sutNewSegmentPara.flashCheckWord = FLASH_FIXED_WORD;
  141. memcpy(sutNewSegmentPara.ManagerIP,DEFAULT_LOAD_IP,4);
  142. sutNewSegmentPara.ManagerPort = DEFAULT_LOAD_PORT;
  143. ///////////////////////////////////////////
  144. memcpy(sutNewSegmentPara.ConfigSerIP,DEFAULT_CONFIG_IP,4);
  145. sutNewSegmentPara.ConfigSerPort = DEFAULT_CONFIG_PORT;
  146. //////////////////////////////////////
  147. memcpy(sutNewSegmentPara.GIP,GPS_SERVER_IP,sizeof(sutNewSegmentPara.GIP));
  148. sutNewSegmentPara.GpsPort=GPS_SERVER_PORT;
  149. memcpy(sutNewSegmentPara.PIP,POC_SERVER_IP,sizeof(sutNewSegmentPara.PIP));
  150. sutNewSegmentPara.PocPort=POC_SERVER_PORT;
  151. SaveNewSegmentToFlash();
  152. }
  153. else if(PRODUCT_VERSION!=sutProductPara.ProductVersion){
  154. strcpy(sutProductPara.ProductName,PRODUCT_NAME);// 每次都会去匹配产品名
  155. sutProductPara.ProductVersion=PRODUCT_VERSION;
  156. sutProductPara.g_net=0;
  157. SaveProductParaToFlash();
  158. if((sutNewSegmentPara.ManagerIP[0] == 0xFF &&
  159. sutNewSegmentPara.ManagerIP[1] == 0xFF &&
  160. sutNewSegmentPara.ManagerIP[2] == 0xFF &&
  161. sutNewSegmentPara.ManagerIP[3] == 0xFF) ||
  162. sutNewSegmentPara.ManagerPort == 0xFFFF)
  163. {
  164. memcpy(sutNewSegmentPara.ManagerIP,DEFAULT_LOAD_IP,4);
  165. sutNewSegmentPara.ManagerPort = DEFAULT_LOAD_PORT;
  166. }
  167. if((sutNewSegmentPara.ConfigSerIP[0] == 0xFF &&
  168. sutNewSegmentPara.ConfigSerIP[1] == 0xFF &&
  169. sutNewSegmentPara.ConfigSerIP[2] == 0xFF &&
  170. sutNewSegmentPara.ConfigSerIP[3] == 0xFF) ||
  171. sutNewSegmentPara.ConfigSerPort == 0xFFFF)
  172. {
  173. memcpy(sutNewSegmentPara.ConfigSerIP,DEFAULT_CONFIG_IP,4);
  174. sutNewSegmentPara.ConfigSerPort = DEFAULT_CONFIG_PORT;
  175. }
  176. sutNewSegmentPara.flashCheckWord = FLASH_FIXED_WORD;
  177. SaveNewSegmentToFlash();
  178. }
  179. startFlashPro=1;
  180. ReadFlashData(NEW_CONFIG_ADDR,(unsigned char *)&sutNewSegmentPara,sizeof(SUT_NEW_SEGMENT_PARA));
  181. GetUniStringByID(sutNewSegmentPara.HARDID);
  182. startFlashPro=0;
  183. if(FLASH_FIXED_WORD != sutNewSegmentPara.flashCheckWord)
  184. {
  185. while(1)
  186. {
  187. printf("FlashSizeCheck failed:%x\r\n",sutNewSegmentPara.flashCheckWord);
  188. IWDG_ReloadCounter();
  189. os_dly_wait(300);
  190. }
  191. }
  192. #if 0
  193. //166000014
  194. sutProductPara.AutH[0] = 0xd3;
  195. sutProductPara.AutH[1] = 0xe9;
  196. sutProductPara.AutH[2] = 0x67;
  197. sutProductPara.AutH[3] = 0x93;
  198. sutProductPara.AutH[4] = 0x4e;
  199. sutProductPara.AutH[5] = 0x90;
  200. sutProductPara.AutH[6] = 0xd6;
  201. #elif 0
  202. sutProductPara.AutH[0] = 0x5d;
  203. sutProductPara.AutH[1] = 0x8c;
  204. sutProductPara.AutH[2] = 0xef;
  205. sutProductPara.AutH[3] = 0xb3;
  206. sutProductPara.AutH[4] = 0xf2;
  207. sutProductPara.AutH[5] = 0x07;
  208. sutProductPara.AutH[6] = 0x5f;
  209. #endif
  210. }
  211. /************************************file begin*******************************/