ProductPara.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. SUT_PRODUCT_PARA sutProductPara;
  12. SUT_DEVICE_CONFIG sutDeviceConfig;
  13. SUT_SERVER_IP sutServerIp;
  14. //SUT_DEVICE_INFO sutDeviceInfo;
  15. //读取指定地址的半字(16位数据)
  16. //faddr:读地址(此地址必须为2的倍数!!)
  17. //返回值:对应数据.
  18. u16 STMFLASH_ReadHalfWord(u32 faddr)
  19. {
  20. return *(vu16*)faddr;
  21. }
  22. //从指定地址开始读出指定长度的数据
  23. //ReadAddr:起始地址
  24. //pBuffer:数据指针
  25. //NumToWrite:半字(16位)数
  26. void STMFLASH_Read(u32 ReadAddr,u16 *pBuffer,u16 NumToRead)
  27. {
  28. u16 i;
  29. for(i=0;i<NumToRead;i++)
  30. {
  31. pBuffer[i]=STMFLASH_ReadHalfWord(ReadAddr);//读取2个字节.
  32. ReadAddr+=2;//偏移2个字节.
  33. }
  34. }
  35. /**********************************************************************************************************
  36. *写页数据,擦除整页1024字节,并写入Byte数据,数据可少于1024Btye,但必须是偶数。
  37. ************************************************************************************************************/
  38. void WritePageData(unsigned long WriteAddr,unsigned char *pData,unsigned short DataLen)
  39. {
  40. unsigned short i;
  41. unsigned short temp;
  42. FLASH_Status status;
  43. RunMake(THIS_FILE_ID);
  44. __disable_irq() ;
  45. if(WriteAddr<STM32_FLASH_BASE || (WriteAddr>=(STM32_FLASH_BASE+1024*STM32_FLASH_SIZE)))return;//非法地址
  46. if(WriteAddr&0x0000003ff)return;//非页首地址
  47. RunMake(THIS_FILE_ID);
  48. FLASH_Unlock(); //解锁
  49. RunMake(THIS_FILE_ID);
  50. status=FLASH_ErasePage(WriteAddr);//擦除这个扇区
  51. if(status!=FLASH_COMPLETE){
  52. RunMake(THIS_FILE_ID);
  53. __enable_irq() ;
  54. return;
  55. }
  56. RunMake(THIS_FILE_ID);
  57. //写入
  58. for(i = 0; i < DataLen; i+=2)
  59. {
  60. //数据8位融合为16位
  61. temp = (((unsigned short)pData[i+1])<<8) + ((unsigned short)pData[i]);
  62. FLASH_ProgramHalfWord((WriteAddr+i),temp);
  63. }
  64. RunMake(THIS_FILE_ID);
  65. FLASH_Lock();//上锁
  66. RunMake(THIS_FILE_ID);
  67. __enable_irq() ;
  68. }
  69. /**********************************************************************************************************
  70. ************************************************************************************************************/
  71. void ReadFlashData(unsigned long ReadAddr,unsigned char *pData,unsigned short DataLen)
  72. {
  73. int i,j;
  74. unsigned short temp;
  75. j=0;
  76. for(i=0;i<DataLen;i+=2){
  77. temp=STMFLASH_ReadHalfWord(ReadAddr+i);
  78. pData[j++]=(unsigned char)(temp&0xff);
  79. pData[j++]=(temp>>8)&0xff;
  80. }
  81. }
  82. /*****************************
  83. 根据PSN生成MAC
  84. *******************************/
  85. void getNetMacByPSN(unsigned char *MAC,unsigned long PSN)
  86. {
  87. SUTDL DL;
  88. DL.Data.ulData=PSN;
  89. MAC[0]=0xab;
  90. MAC[1]=0xcd;
  91. MAC[2]=DL.Data.ucData.b4;
  92. MAC[3]=DL.Data.ucData.b3;
  93. MAC[4]=DL.Data.ucData.b2;
  94. MAC[5]=DL.Data.ucData.b1;
  95. }
  96. /********************************************************
  97. 10进制转BCD
  98. ********************************************************/
  99. void DecToBCD(unsigned int Dec, unsigned char *Bcd, int length)
  100. {
  101. int i;
  102. int temp;
  103. for(i=length-1; i>=0; i--)
  104. {
  105. temp = Dec%100;
  106. Bcd[i] = ((temp/10)<<4) + ((temp%10) & 0x0F);
  107. Dec /= 100;
  108. }
  109. }
  110. /********************************************************************
  111. *ReadProductParaFromFlash
  112. *********************************************************************/
  113. void ReadProductParaFromFlash(void)
  114. {
  115. ReadFlashData(PRODUCT_PARA_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
  116. if(MCU_FLAG!=sutProductPara.McuFlag || PRODUCT_PARA_MARK!=sutProductPara.Mark)
  117. {
  118. memset((unsigned char *)&sutProductPara,0,sizeof(SUT_PRODUCT_PARA));
  119. sutProductPara.McuFlag=MCU_FLAG;
  120. snprintf(sutProductPara.ProductName,sizeof(sutProductPara.ProductName),"%s",PRODUCT_NAME);
  121. sutProductPara.SoftwareVer=SOFTWARE_VERSION;
  122. sutProductPara.HardwareVer=HARDWARE_VERSION;
  123. sutProductPara.ModemType=MODEM_TYPE;
  124. sutProductPara.GNSSType=GNSS_TYPE;
  125. sutProductPara.SensorType=SENSOR_TYPE;
  126. snprintf(sutProductPara.AppServer,sizeof(sutProductPara.AppServer),"%s",APP_SERVER);
  127. sutProductPara.AppPort=APP_SPORT;
  128. snprintf(sutProductPara.FotaServer,sizeof(sutProductPara.FotaServer),"%s",FOTA_SERVER);
  129. sutProductPara.FotaPort=FOTA_SPORT;
  130. snprintf(sutProductPara.CardAPN,sizeof(sutProductPara.CardAPN),"%s",CARD_APN);
  131. snprintf(sutProductPara.CardUsrName,sizeof(sutProductPara.CardUsrName),"%s",CARD_USER);
  132. snprintf(sutProductPara.CardUsrPass,sizeof(sutProductPara.CardUsrPass),"%s",CARD_PASS);
  133. sutProductPara.Mark=PRODUCT_PARA_MARK;
  134. WritePageData(PRODUCT_PARA_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
  135. strcpy(sutProductPara.proName, "ZRK");
  136. strcpy(sutProductPara.devName, "M20");
  137. }
  138. snprintf(sutProductPara.ProductName,sizeof(sutProductPara.ProductName),"%s",PRODUCT_NAME);
  139. sutProductPara.SoftwareVer=SOFTWARE_VERSION;
  140. sutProductPara.HardwareVer=HARDWARE_VERSION;
  141. }
  142. /**********************************************************************************************************
  143. SaveProductParaToFlash
  144. ************************************************************************************************************/
  145. void SaveProductParaToFlash(void)
  146. {
  147. //tsk_lock ();
  148. __set_PRIMASK(1);//关闭总中断
  149. WritePageData(PRODUCT_PARA_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
  150. __set_PRIMASK(0);//打开总中断
  151. //tsk_unlock ();
  152. }
  153. /********************************************************************
  154. *ReadDeviceConfigFromFlash
  155. *********************************************************************/
  156. void ReadDeviceConfigFromFlash(void)
  157. {
  158. if(DEVICE_CONFIG_ADDR<STM32_FLASH_BASE){//存放在外部FLASH
  159. SPI_Flash_Read((unsigned char *)&sutDeviceConfig,DEVICE_CONFIG_ADDR,sizeof(SUT_DEVICE_CONFIG));
  160. }else{//存放在MCU FLASH内
  161. ReadFlashData(DEVICE_CONFIG_ADDR,(unsigned char *)&sutDeviceConfig,sizeof(SUT_DEVICE_CONFIG));
  162. }
  163. if(sutDeviceConfig.Mark1!=DEVICE_CONFIG_MARK || sutDeviceConfig.Mark2!=DEVICE_CONFIG_MARK){
  164. SlwTrace(DEBUG,"Reset DeviceConfig!\r\n");
  165. memset((unsigned char *)&sutDeviceConfig,0,sizeof(SUT_DEVICE_CONFIG));
  166. sutDeviceConfig.Mark1=DEVICE_CONFIG_MARK;
  167. sutDeviceConfig.Mark2=DEVICE_CONFIG_MARK;
  168. sutDeviceConfig.AppExAddr=EXFLASH_FILE_DATA_ADDR;
  169. sutDeviceConfig.NetMode=1;
  170. sutDeviceConfig.SamplingInterval=5;//默认采样时间间隔 秒
  171. sutDeviceConfig.UploadInterval=60;//默认上传间隔 秒
  172. SaveDeviceConfigToFlash();
  173. }
  174. }
  175. /********************************************************************
  176. *SaveDeviceConfigToFlash
  177. *********************************************************************/
  178. void SaveDeviceConfigToFlash(void)
  179. {
  180. if(DEVICE_CONFIG_ADDR<STM32_FLASH_BASE){//存放在外部FLASH
  181. SPI_Flash_Write((unsigned char *)&sutDeviceConfig,DEVICE_CONFIG_ADDR,sizeof(SUT_DEVICE_CONFIG));
  182. }else{//存放在MCU FLASH内
  183. WritePageData(DEVICE_CONFIG_ADDR,(unsigned char *)&sutDeviceConfig,sizeof(SUT_DEVICE_CONFIG));
  184. }
  185. }
  186. void ReadServerIP(void)
  187. {
  188. SPI_Flash_Read((unsigned char *)&sutServerIp,EXFLASH_SERVER_CONFIG_ADDR,sizeof(SUT_SERVER_IP));
  189. }
  190. void SaveServerIP(void)
  191. {
  192. SPI_Flash_Write((unsigned char *)&sutServerIp,EXFLASH_SERVER_CONFIG_ADDR,sizeof(SUT_SERVER_IP));
  193. }
  194. /************************************file begin*******************************/