123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- /********************************************************************************
- * File Name: ProductPara.c
- * Function Describe:
- * Explain:
- * Writer: ShiLiangWen
- * Date: 2015-7-6
- *******************************************************************************/
- #define THIS_FILE_ID 6
- //-----------------------------------------------------------------------------
- #include "includes.h"
- SUT_PRODUCT_PARA sutProductPara;
- unsigned char HardRevision[40];
- //读取指定地址的半字(16位数据)
- //faddr:读地址(此地址必须为2的倍数!!)
- //返回值:对应数据.
- u16 STMFLASH_ReadHalfWord(u32 faddr)
- {
- return *(vu16*)faddr;
- }
- //从指定地址开始读出指定长度的数据
- //ReadAddr:起始地址
- //pBuffer:数据指针
- //NumToWrite:半字(16位)数
- void STMFLASH_Read(u32 ReadAddr,u16 *pBuffer,u16 NumToRead)
- {
- u16 i;
- for(i=0;i<NumToRead;i++)
- {
- pBuffer[i]=STMFLASH_ReadHalfWord(ReadAddr);//读取2个字节.
- ReadAddr+=2;//偏移2个字节.
- }
- }
- /**********************************************************************************************************
- *写页数据,擦除整页2048字节,并写入Byte数据,数据可少于2048Btye,但必须是偶数。
- ************************************************************************************************************/
- void WritePageData(unsigned long WriteAddr,unsigned char *pData,unsigned short DataLen)
- {
- unsigned short i;
- unsigned short temp;
- FLASH_Status status;
- // FLASH_Status FLASHStatusTmp;
- // tsk_lock();
- if(WriteAddr<FLASH_BASE || (WriteAddr>=(FLASH_BASE+STM32_FLASH_SIZE*1024)))return;//非法地址
- if(WriteAddr&0x0000003ff){
- printf("addr ---erro\r\n");
- return;//非页首地址
- }
- FLASH_Unlock(); //解锁
- FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
- FLASH_WaitForLastBank1Operation(0xffffff);
-
- //FLASH->CR&= ~0x00000080;
-
- //FLASH->CR|= 0x00000080;
- status=1;
- printf("status1=%d\r\n",status);
- status=FLASH_ErasePage(WriteAddr);//擦除这个扇区
- printf("status2=%d\r\n",status);
- if(status!=FLASH_COMPLETE){
- //tsk_unlock();
- printf("erase erro-----%d\r\n",status);
- FLASH_Lock();
- return;
- }
- //
- //写入
- for(i = 0; i < DataLen; i+=2)
- {
- //数据8位融合为16位
- temp = (((unsigned short)pData[i+1])<<8) + ((unsigned short)pData[i]);
- FLASH_ProgramHalfWord((WriteAddr+i),temp);
- }
- FLASH_Lock();//上锁
- //tsk_unlock();
- }
- /**********************************************************************************************************
- ************************************************************************************************************/
- void ReadFlashData(unsigned long ReadAddr,unsigned char *pData,unsigned short DataLen)
- {
- int i,j;
- unsigned short temp;
- j=0;
- for(i=0;i<DataLen;i+=2){
- temp=STMFLASH_ReadHalfWord(ReadAddr+i);
- pData[j++]=(unsigned char)(temp&0xff);
- pData[j++]=(temp>>8)&0xff;
- }
- }
- /**********************************************************************************************************
- SaveProductParaToFlash
- ************************************************************************************************************/
- void SaveProductParaToFlash(char *recoreInfo)
- {
- static char *p="123456789";
- // uint32_t tick;
- char recBuf[60];
- PWR_EN_HIGH;
- // if(sutProductPara.takeNoteFlag)
- // {
- // //记录保存相关信息到SPI FLASH
- // tick=RTC_GetCounter();
- // //时间 位置 机型 序列号
- // snprintf(recBuf, sizeof(recBuf), "[rec]%d,%s,%s,%08x|\r\n",tick,recoreInfo,sutProductPara.ProductName,sutProductPara.PSN);
- // sFlash_Write(recBuf, RECORD_INFO_ADDRESS, sizeof(recBuf));
- // SlwTrace(INF, recBuf, 0);
- // }
-
-
- WritePageData(BACKUP_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
- DelayMs(100);
- WritePageData(MAIN_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
- //PWR_EN_LOW;
- }
- /********************************************************************
- *SaveParaToFlash
- 将sutNetPara重要参数保存到FLASH
- *********************************************************************/
- //主参数区:127K MAIN_CONFIG_ADDR
- //备份参数:126K BACKUP_CONFIG_ADDR
- //1、使用时主要使用主参数区参数
- //2、每次更新主参数区时,一并更新备份参数区
- //3、开机后读取主参数区以及备份区
- //3.1、如果两者的NAME和PSN都全为FF,则认为是第一次启动,初始化所有主备参数;
- //3.2、非3.1的情况下,如果主区的NAME不为PRODUCT_NAME(第一次初始化时设置)时,或者主区的PSN为非法值(1000000<PSN<4200000000)时
- // 认为参数区异常,如果此时的备份区的NAME不为PRODUCT_NAME,或者备份区PSN为非法值时,则执行刷新操作,即初始化所有主备参数
- // 否则copy备份区到主区,然后使用此时主区参数运行
- //3.3、如果主备区都合法,但不一致,以主区为准
- #define MIN_PSN_NUM 1000000
- #define MAX_PSN_NUM 4200000000
- void ReadProductParaFromFlash(void)
- {
- SUT_PRODUCT_PARA backupPara;
- char mainLogic,backupLogic;
- char saveType;
- char buf[40];
- //IWDG_ReloadCounter();
- saveType=0;//不用保存参数
- //1、读取主区参数
- ReadFlashData(MAIN_CONFIG_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
- printf("sutProductPara.LCDSetIndex=%d\r\n",sutProductPara.LCDSetIndex);
- //2、读取备份参数
- ReadFlashData(BACKUP_CONFIG_ADDR,(unsigned char *)&backupPara,sizeof(SUT_PRODUCT_PARA));
- printf("backupPara.LCDSetIndex=%d\r\n",backupPara.LCDSetIndex);
-
- //3.1、检测主区合法性
- mainLogic=0;//设置不合法
- if(0==strcmp(sutProductPara.ProductName,PRODUCT_NAME) && //产品名字相同
- (sutProductPara.PSN >= MIN_PSN_NUM && sutProductPara.PSN <= MAX_PSN_NUM))//PSN符合范围
- {
- mainLogic=1;//主区参数合法
- }
- //3.2、检测备份合法性
- backupLogic=0;//设置不合法
- if(0==strcmp(backupPara.ProductName,PRODUCT_NAME) && //产品名字相同
- (backupPara.PSN >= MIN_PSN_NUM && backupPara.PSN <= MAX_PSN_NUM)) //PSN符合范围
- {
- backupLogic=1;//备份参数合法
- }
- //3.3、如果主备都不合法,则进行刷机
- if(0==mainLogic && 0==backupLogic)
- {
- SlwTrace(INF, "[1]Rst all para!",1);
- memset((unsigned char *)&sutProductPara,0,sizeof(SUT_PRODUCT_PARA));
- sutProductPara.McuFlag =MCU_FLAG; //MCU标识
- sutProductPara.IapFlag =0x88; //IAP标志 0x00--无应用程序 0x55--需要空中升级 0x88--正常运行应用程序
-
- strcpy(sutProductPara.ProductName,PRODUCT_NAME);
- sutProductPara.ProductVersion=PRODUCT_VERSION;
- sutProductPara.PocPort =POC_SERVER_PORT;
- sutProductPara.GpsPort =TSGPS_SERVER_PORT;
-
- sutProductPara.PSN =MIN_PSN_NUM;//未写号情况下PSN为150000000
- sutProductPara.g_net=0; //网模自动
-
- strcpy(sutProductPara.PIP,POC_SERVER_IP);
- strcpy(sutProductPara.GIP,GPS_SERVER_IP);
-
- strcpy(sutProductPara.APNApn, APN_APN);
- strcpy(sutProductPara.APNName,APN_NAME);
- strcpy(sutProductPara.APNPass,APN_PASSWORD);
- sutProductPara.m_GPS_TimeIndex = DEFAULT_GPS_SEND_TIME_INDEX;
-
- strcpy(sutProductPara.Net_Mode,NET_MODE); //4G
- strcpy(sutProductPara.Language,LANGUAGE); //CN
- strcpy(sutProductPara.Local,LOCAL); //CN
- strcpy(sutProductPara.Channel,MODECHANNEL); //channel
- strcpy(sutProductPara.IMEI,"00000000000000"); //14位 最后一位为0
- strcpy(sutProductPara.Keysound,KEY_SOUND);
- sutProductPara.SendToneEnadble=1;
- sutProductPara.RecvToneEnadble=1;
- sutProductPara.PromptToneEnable=1;
- sutProductPara.FastInterface=0;
- sutProductPara.Aw8735=0;
- sutProductPara.Netmode=0;
- sutProductPara.LowConsumption=0;
- sutProductPara.LCDSetIndex=0;
- strcpy(sutProductPara.APNApn, APN_APN);
- sutProductPara.APNIndex=0;//默认使用资源文件中的第一个APN
- sutProductPara.VolLv=8;
- //然后保存参数
- saveType=1;//两个区都保存为:sutProductPara
- }
- //3.4、如果主合法,备非法,主copy到备
- else if(1==mainLogic && 0==backupLogic)
- {
- SlwTrace(INF, "[2]main-->backup",1);
- saveType=2;//仅保存:backupPara
- }
- //3.5、如果主非法,备合法,备copy到主
- else if(0==mainLogic && 1==backupLogic)
- {
- SlwTrace(INF, "[3]backup-->main",1);
- memcpy((unsigned char *)&sutProductPara, (unsigned char *)&backupPara, sizeof(SUT_PRODUCT_PARA));
- saveType=3;//仅保存:sutProductPara
- }
- //3.6、如果主备都合法,但不一致,主copy到备
- else if(1==mainLogic && 1==backupLogic && sutProductPara.PSN != backupPara.PSN)
- {
- SlwTrace(INF, "[4]main-->backup",1);
- saveType=2;//仅保存:backupPara
- }
- //3.7、如果主备都合法,但一致,不做任何操作
- else
- //{
- SlwTrace(INF, "[5]LogicOk",1);
- //检测是否版本发生了变更
- if(sutProductPara.ProductVersion != PRODUCT_VERSION)
- {//版本号发生了变化
- //FotaIpCheck();
- // sutProductPara.APNIndex=0;//默认使用资源文件中的第一个APN
- sutProductPara.ProductVersion=PRODUCT_VERSION;
- strcpy(sutProductPara.Keysound,KEY_SOUND);
- //1.1 GPS upload time --> 15 seconds
- sutProductPara.m_GPS_TimeIndex = DEFAULT_GPS_SEND_TIME_INDEX;//恢复GPS默认上传时间
- sutProductPara.FastInterface=0;
- sutProductPara.Aw8735=0;
- sutProductPara.Netmode=0;
- sutProductPara.LowConsumption=0;
- sutProductPara.LCDSetIndex=0;
- saveType=1;//两个区都保存为:sutProductPara
- SlwTrace(INF, "[6]Ver-Changed",1);
- }
- //}
- //检测GPS上传定位参数索引是否合法
- if(sutProductPara.m_GPS_TimeIndex >= sizeof(GPS_TimeTable))
- {
- sutProductPara.m_GPS_TimeIndex = DEFAULT_GPS_SEND_TIME_INDEX;
- saveType=1;
- }
-
- if(sutProductPara.SendToneEnadble>1)
- {
- sutProductPara.SendToneEnadble=1;
- saveType=1;
- }
- if(sutProductPara.RecvToneEnadble>1)
- {
- sutProductPara.RecvToneEnadble=1;
- saveType=1;
- }
- if(sutProductPara.PromptToneEnable>1)
- {
- sutProductPara.PromptToneEnable=1;
- saveType=1;
- }
- if(sutProductPara.FastInterface>2)
- {
- sutProductPara.SendToneEnadble=0;
- saveType=1;
- }
- if(sutProductPara.Aw8735>1)
- {
- sutProductPara.Aw8735=0;
- saveType=1;
- }
- if(sutProductPara.Netmode>3)
- {
- sutProductPara.Netmode=0;
- saveType=1;
- }
- if(sutProductPara.LowConsumption>1)
- {
- sutProductPara.LowConsumption=0;
- saveType=1;
- }
-
- if(sutProductPara.LCDSetIndex>4)
- {
- sutProductPara.LCDSetIndex=0;
- saveType=1;
- }
-
- if(sutProductPara.VolLv>11){
- sutProductPara.VolLv=11;
- }
- // GpsTimeUpdate();
- //执行参数保存
- switch(saveType)
- {
- case 1://两个区都保存为:sutProductPara
- case 2://仅保存:backupPara
- case 3://仅保存:sutProductPara
- snprintf(buf, sizeof(buf), "READ_PARA_%d",saveType);
- printf("%s\r\n",buf);
- SaveProductParaToFlash(buf);
- break;
- default:break;
- }
-
- //soc ID替代IMEI
- if(GetUniStringByID(sutProductPara.HARDID)==1){
- snprintf(buf, sizeof(buf), "[01]hardID:%s\r\n", sutProductPara.HARDID);
- SlwTrace(INF, buf,1);
- }
- SlwTrace(INF, "ParaRead_Done\r\n",1);
- printf("APN_INDEX:%d\r\n",sutProductPara.APNIndex);
- }
- void GpsTimeUpdate(void)
- {
- //sutGpsInfo.GPS_UploadTime = GPS_TimeTable[sutProductPara.m_GPS_TimeIndex];//20160825,这里再加载GPS时间,用户体验:在改了GPS值后,重启没放卡,去菜单看说GPS值没改到
-
- if(sutGpsInfo.GPS_UploadTime)//判断GPS是否已关,根据上传时间来算
- {
- GPS_PWREN_HIGH;
- g_uiGpsStat=1;
- }
- else{
- GPS_PWREN_LOW;
- g_uiGpsStat=0;
- //sutGpsInfo.isGpsWork=0;
- }
- }
- /************************************file begin*******************************/
|