ProductPara.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. unsigned char HardRevision[40];
  13. //读取指定地址的半字(16位数据)
  14. //faddr:读地址(此地址必须为2的倍数!!)
  15. //返回值:对应数据.
  16. u16 STMFLASH_ReadHalfWord(u32 faddr)
  17. {
  18. return *(vu16*)faddr;
  19. }
  20. //从指定地址开始读出指定长度的数据
  21. //ReadAddr:起始地址
  22. //pBuffer:数据指针
  23. //NumToWrite:半字(16位)数
  24. void STMFLASH_Read(u32 ReadAddr,u16 *pBuffer,u16 NumToRead)
  25. {
  26. u16 i;
  27. for(i=0;i<NumToRead;i++)
  28. {
  29. pBuffer[i]=STMFLASH_ReadHalfWord(ReadAddr);//读取2个字节.
  30. ReadAddr+=2;//偏移2个字节.
  31. }
  32. }
  33. /**********************************************************************************************************
  34. *写页数据,擦除整页2048字节,并写入Byte数据,数据可少于2048Btye,但必须是偶数。
  35. ************************************************************************************************************/
  36. void WritePageData(unsigned long WriteAddr,unsigned char *pData,unsigned short DataLen)
  37. {
  38. unsigned short i;
  39. unsigned short temp;
  40. FLASH_Status status;
  41. // FLASH_Status FLASHStatusTmp;
  42. // tsk_lock();
  43. if(WriteAddr<FLASH_BASE || (WriteAddr>=(FLASH_BASE+STM32_FLASH_SIZE*1024)))return;//非法地址
  44. if(WriteAddr&0x0000003ff){
  45. printf("addr ---erro\r\n");
  46. return;//非页首地址
  47. }
  48. FLASH_Unlock(); //解锁
  49. FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
  50. FLASH_WaitForLastBank1Operation(0xffffff);
  51. //FLASH->CR&= ~0x00000080;
  52. //FLASH->CR|= 0x00000080;
  53. status=1;
  54. printf("status1=%d\r\n",status);
  55. status=FLASH_ErasePage(WriteAddr);//擦除这个扇区
  56. printf("status2=%d\r\n",status);
  57. if(status!=FLASH_COMPLETE){
  58. //tsk_unlock();
  59. printf("erase erro-----%d\r\n",status);
  60. FLASH_Lock();
  61. return;
  62. }
  63. //
  64. //写入
  65. for(i = 0; i < DataLen; i+=2)
  66. {
  67. //数据8位融合为16位
  68. temp = (((unsigned short)pData[i+1])<<8) + ((unsigned short)pData[i]);
  69. FLASH_ProgramHalfWord((WriteAddr+i),temp);
  70. }
  71. FLASH_Lock();//上锁
  72. //tsk_unlock();
  73. }
  74. /**********************************************************************************************************
  75. ************************************************************************************************************/
  76. void ReadFlashData(unsigned long ReadAddr,unsigned char *pData,unsigned short DataLen)
  77. {
  78. int i,j;
  79. unsigned short temp;
  80. j=0;
  81. for(i=0;i<DataLen;i+=2){
  82. temp=STMFLASH_ReadHalfWord(ReadAddr+i);
  83. pData[j++]=(unsigned char)(temp&0xff);
  84. pData[j++]=(temp>>8)&0xff;
  85. }
  86. }
  87. /**********************************************************************************************************
  88. SaveProductParaToFlash
  89. ************************************************************************************************************/
  90. void SaveProductParaToFlash(char *recoreInfo)
  91. {
  92. static char *p="123456789";
  93. // uint32_t tick;
  94. char recBuf[60];
  95. PWR_EN_HIGH;
  96. // if(sutProductPara.takeNoteFlag)
  97. // {
  98. // //记录保存相关信息到SPI FLASH
  99. // tick=RTC_GetCounter();
  100. // //时间 位置 机型 序列号
  101. // snprintf(recBuf, sizeof(recBuf), "[rec]%d,%s,%s,%08x|\r\n",tick,recoreInfo,sutProductPara.ProductName,sutProductPara.PSN);
  102. // sFlash_Write(recBuf, RECORD_INFO_ADDRESS, sizeof(recBuf));
  103. // SlwTrace(INF, recBuf, 0);
  104. // }
  105. WritePageData(BACKUP_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
  106. DelayMs(100);
  107. WritePageData(MAIN_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
  108. //PWR_EN_LOW;
  109. }
  110. /********************************************************************
  111. *SaveParaToFlash
  112. 将sutNetPara重要参数保存到FLASH
  113. *********************************************************************/
  114. //主参数区:127K MAIN_CONFIG_ADDR
  115. //备份参数:126K BACKUP_CONFIG_ADDR
  116. //1、使用时主要使用主参数区参数
  117. //2、每次更新主参数区时,一并更新备份参数区
  118. //3、开机后读取主参数区以及备份区
  119. //3.1、如果两者的NAME和PSN都全为FF,则认为是第一次启动,初始化所有主备参数;
  120. //3.2、非3.1的情况下,如果主区的NAME不为PRODUCT_NAME(第一次初始化时设置)时,或者主区的PSN为非法值(1000000<PSN<4200000000)时
  121. // 认为参数区异常,如果此时的备份区的NAME不为PRODUCT_NAME,或者备份区PSN为非法值时,则执行刷新操作,即初始化所有主备参数
  122. // 否则copy备份区到主区,然后使用此时主区参数运行
  123. //3.3、如果主备区都合法,但不一致,以主区为准
  124. #define MIN_PSN_NUM 1000000
  125. #define MAX_PSN_NUM 4200000000
  126. void ReadProductParaFromFlash(void)
  127. {
  128. SUT_PRODUCT_PARA backupPara;
  129. char mainLogic,backupLogic;
  130. char saveType;
  131. char buf[40];
  132. //IWDG_ReloadCounter();
  133. saveType=0;//不用保存参数
  134. //1、读取主区参数
  135. ReadFlashData(MAIN_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
  136. printf("sutProductPara.LCDSetIndex=%d\r\n",sutProductPara.LCDSetIndex);
  137. //2、读取备份参数
  138. ReadFlashData(BACKUP_CONFIG_ADDR,(unsigned char *)&backupPara,sizeof(SUT_PRODUCT_PARA));
  139. printf("backupPara.LCDSetIndex=%d\r\n",backupPara.LCDSetIndex);
  140. //3.1、检测主区合法性
  141. mainLogic=0;//设置不合法
  142. if(0==strcmp(sutProductPara.ProductName,PRODUCT_NAME) && //产品名字相同
  143. (sutProductPara.PSN >= MIN_PSN_NUM && sutProductPara.PSN <= MAX_PSN_NUM))//PSN符合范围
  144. {
  145. mainLogic=1;//主区参数合法
  146. }
  147. //3.2、检测备份合法性
  148. backupLogic=0;//设置不合法
  149. if(0==strcmp(backupPara.ProductName,PRODUCT_NAME) && //产品名字相同
  150. (backupPara.PSN >= MIN_PSN_NUM && backupPara.PSN <= MAX_PSN_NUM)) //PSN符合范围
  151. {
  152. backupLogic=1;//备份参数合法
  153. }
  154. //3.3、如果主备都不合法,则进行刷机
  155. if(0==mainLogic && 0==backupLogic)
  156. {
  157. SlwTrace(INF, "[1]Rst all para!",1);
  158. memset((unsigned char *)&sutProductPara,0,sizeof(SUT_PRODUCT_PARA));
  159. sutProductPara.McuFlag =MCU_FLAG; //MCU标识
  160. sutProductPara.IapFlag =0x88; //IAP标志 0x00--无应用程序 0x55--需要空中升级 0x88--正常运行应用程序
  161. strcpy(sutProductPara.ProductName,PRODUCT_NAME);
  162. sutProductPara.ProductVersion=PRODUCT_VERSION;
  163. sutProductPara.PocPort =POC_SERVER_PORT;
  164. sutProductPara.GpsPort =TSGPS_SERVER_PORT;
  165. sutProductPara.PSN =MIN_PSN_NUM;//未写号情况下PSN为150000000
  166. sutProductPara.g_net=0; //网模自动
  167. strcpy(sutProductPara.PIP,POC_SERVER_IP);
  168. strcpy(sutProductPara.GIP,GPS_SERVER_IP);
  169. strcpy(sutProductPara.APNApn, APN_APN);
  170. strcpy(sutProductPara.APNName,APN_NAME);
  171. strcpy(sutProductPara.APNPass,APN_PASSWORD);
  172. sutProductPara.m_GPS_TimeIndex = DEFAULT_GPS_SEND_TIME_INDEX;
  173. strcpy(sutProductPara.Net_Mode,NET_MODE); //4G
  174. strcpy(sutProductPara.Language,LANGUAGE); //CN
  175. strcpy(sutProductPara.Local,LOCAL); //CN
  176. strcpy(sutProductPara.Channel,MODECHANNEL); //channel
  177. strcpy(sutProductPara.IMEI,"00000000000000"); //14位 最后一位为0
  178. strcpy(sutProductPara.Keysound,KEY_SOUND);
  179. sutProductPara.SendToneEnadble=1;
  180. sutProductPara.RecvToneEnadble=1;
  181. sutProductPara.PromptToneEnable=1;
  182. sutProductPara.FastInterface=0;
  183. sutProductPara.Aw8735=0;
  184. sutProductPara.Netmode=0;
  185. sutProductPara.LowConsumption=0;
  186. sutProductPara.LCDSetIndex=0;
  187. strcpy(sutProductPara.APNApn, APN_APN);
  188. sutProductPara.APNIndex=0;//默认使用资源文件中的第一个APN
  189. sutProductPara.VolLv=8;
  190. //然后保存参数
  191. saveType=1;//两个区都保存为:sutProductPara
  192. }
  193. //3.4、如果主合法,备非法,主copy到备
  194. else if(1==mainLogic && 0==backupLogic)
  195. {
  196. SlwTrace(INF, "[2]main-->backup",1);
  197. saveType=2;//仅保存:backupPara
  198. }
  199. //3.5、如果主非法,备合法,备copy到主
  200. else if(0==mainLogic && 1==backupLogic)
  201. {
  202. SlwTrace(INF, "[3]backup-->main",1);
  203. memcpy((unsigned char *)&sutProductPara, (unsigned char *)&backupPara, sizeof(SUT_PRODUCT_PARA));
  204. saveType=3;//仅保存:sutProductPara
  205. }
  206. //3.6、如果主备都合法,但不一致,主copy到备
  207. else if(1==mainLogic && 1==backupLogic && sutProductPara.PSN != backupPara.PSN)
  208. {
  209. SlwTrace(INF, "[4]main-->backup",1);
  210. saveType=2;//仅保存:backupPara
  211. }
  212. //3.7、如果主备都合法,但一致,不做任何操作
  213. else
  214. //{
  215. SlwTrace(INF, "[5]LogicOk",1);
  216. //检测是否版本发生了变更
  217. if(sutProductPara.ProductVersion != PRODUCT_VERSION)
  218. {//版本号发生了变化
  219. //FotaIpCheck();
  220. // sutProductPara.APNIndex=0;//默认使用资源文件中的第一个APN
  221. sutProductPara.ProductVersion=PRODUCT_VERSION;
  222. strcpy(sutProductPara.Keysound,KEY_SOUND);
  223. //1.1 GPS upload time --> 15 seconds
  224. sutProductPara.m_GPS_TimeIndex = DEFAULT_GPS_SEND_TIME_INDEX;//恢复GPS默认上传时间
  225. sutProductPara.FastInterface=0;
  226. sutProductPara.Aw8735=0;
  227. sutProductPara.Netmode=0;
  228. sutProductPara.LowConsumption=0;
  229. sutProductPara.LCDSetIndex=0;
  230. saveType=1;//两个区都保存为:sutProductPara
  231. SlwTrace(INF, "[6]Ver-Changed",1);
  232. }
  233. //}
  234. //检测GPS上传定位参数索引是否合法
  235. if(sutProductPara.m_GPS_TimeIndex >= sizeof(GPS_TimeTable))
  236. {
  237. sutProductPara.m_GPS_TimeIndex = DEFAULT_GPS_SEND_TIME_INDEX;
  238. saveType=1;
  239. }
  240. if(sutProductPara.SendToneEnadble>1)
  241. {
  242. sutProductPara.SendToneEnadble=1;
  243. saveType=1;
  244. }
  245. if(sutProductPara.RecvToneEnadble>1)
  246. {
  247. sutProductPara.RecvToneEnadble=1;
  248. saveType=1;
  249. }
  250. if(sutProductPara.PromptToneEnable>1)
  251. {
  252. sutProductPara.PromptToneEnable=1;
  253. saveType=1;
  254. }
  255. if(sutProductPara.FastInterface>2)
  256. {
  257. sutProductPara.SendToneEnadble=0;
  258. saveType=1;
  259. }
  260. if(sutProductPara.Aw8735>1)
  261. {
  262. sutProductPara.Aw8735=0;
  263. saveType=1;
  264. }
  265. if(sutProductPara.Netmode>3)
  266. {
  267. sutProductPara.Netmode=0;
  268. saveType=1;
  269. }
  270. if(sutProductPara.LowConsumption>1)
  271. {
  272. sutProductPara.LowConsumption=0;
  273. saveType=1;
  274. }
  275. if(sutProductPara.LCDSetIndex>4)
  276. {
  277. sutProductPara.LCDSetIndex=0;
  278. saveType=1;
  279. }
  280. if(sutProductPara.VolLv>11){
  281. sutProductPara.VolLv=11;
  282. }
  283. // GpsTimeUpdate();
  284. //执行参数保存
  285. switch(saveType)
  286. {
  287. case 1://两个区都保存为:sutProductPara
  288. case 2://仅保存:backupPara
  289. case 3://仅保存:sutProductPara
  290. snprintf(buf, sizeof(buf), "READ_PARA_%d",saveType);
  291. printf("%s\r\n",buf);
  292. SaveProductParaToFlash(buf);
  293. break;
  294. default:break;
  295. }
  296. //soc ID替代IMEI
  297. if(GetUniStringByID(sutProductPara.HARDID)==1){
  298. snprintf(buf, sizeof(buf), "[01]hardID:%s\r\n", sutProductPara.HARDID);
  299. SlwTrace(INF, buf,1);
  300. }
  301. SlwTrace(INF, "ParaRead_Done\r\n",1);
  302. printf("APN_INDEX:%d\r\n",sutProductPara.APNIndex);
  303. }
  304. void GpsTimeUpdate(void)
  305. {
  306. //sutGpsInfo.GPS_UploadTime = GPS_TimeTable[sutProductPara.m_GPS_TimeIndex];//20160825,这里再加载GPS时间,用户体验:在改了GPS值后,重启没放卡,去菜单看说GPS值没改到
  307. if(sutGpsInfo.GPS_UploadTime)//判断GPS是否已关,根据上传时间来算
  308. {
  309. GPS_PWREN_HIGH;
  310. g_uiGpsStat=1;
  311. }
  312. else{
  313. GPS_PWREN_LOW;
  314. g_uiGpsStat=0;
  315. //sutGpsInfo.isGpsWork=0;
  316. }
  317. }
  318. /************************************file begin*******************************/