123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- /********************************************************************************
- * 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;
- SUT_DEVICE_CONFIG sutDeviceConfig;
- SUT_SERVER_IP sutServerIp;
- //SUT_DEVICE_INFO sutDeviceInfo;
- //读取指定地址的半字(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个字节.
- }
- }
- /**********************************************************************************************************
- *写页数据,擦除整页1024字节,并写入Byte数据,数据可少于1024Btye,但必须是偶数。
- ************************************************************************************************************/
- void WritePageData(unsigned long WriteAddr,unsigned char *pData,unsigned short DataLen)
- {
- unsigned short i;
- unsigned short temp;
- FLASH_Status status;
-
- RunMake(THIS_FILE_ID);
- __disable_irq() ;
- if(WriteAddr<STM32_FLASH_BASE || (WriteAddr>=(STM32_FLASH_BASE+1024*STM32_FLASH_SIZE)))return;//非法地址
- if(WriteAddr&0x0000003ff)return;//非页首地址
- RunMake(THIS_FILE_ID);
- FLASH_Unlock(); //解锁
- RunMake(THIS_FILE_ID);
- status=FLASH_ErasePage(WriteAddr);//擦除这个扇区
- if(status!=FLASH_COMPLETE){
- RunMake(THIS_FILE_ID);
- __enable_irq() ;
- return;
- }
- RunMake(THIS_FILE_ID);
- //写入
- 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);
- }
- RunMake(THIS_FILE_ID);
- FLASH_Lock();//上锁
- RunMake(THIS_FILE_ID);
- __enable_irq() ;
- }
- /**********************************************************************************************************
- ************************************************************************************************************/
- 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;
- }
- }
- /*****************************
- 根据PSN生成MAC
- *******************************/
- void getNetMacByPSN(unsigned char *MAC,unsigned long PSN)
- {
- SUTDL DL;
- DL.Data.ulData=PSN;
- MAC[0]=0xab;
- MAC[1]=0xcd;
- MAC[2]=DL.Data.ucData.b4;
- MAC[3]=DL.Data.ucData.b3;
- MAC[4]=DL.Data.ucData.b2;
- MAC[5]=DL.Data.ucData.b1;
- }
- /********************************************************
- 10进制转BCD
- ********************************************************/
- void DecToBCD(unsigned int Dec, unsigned char *Bcd, int length)
- {
- int i;
- int temp;
- for(i=length-1; i>=0; i--)
- {
- temp = Dec%100;
- Bcd[i] = ((temp/10)<<4) + ((temp%10) & 0x0F);
- Dec /= 100;
- }
- }
- /********************************************************************
- *ReadProductParaFromFlash
- *********************************************************************/
- void ReadProductParaFromFlash(void)
- {
- ReadFlashData(PRODUCT_PARA_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
- if(MCU_FLAG!=sutProductPara.McuFlag || PRODUCT_PARA_MARK!=sutProductPara.Mark)
- {
- memset((unsigned char *)&sutProductPara,0,sizeof(SUT_PRODUCT_PARA));
- sutProductPara.McuFlag=MCU_FLAG;
- snprintf(sutProductPara.ProductName,sizeof(sutProductPara.ProductName),"%s",PRODUCT_NAME);
- sutProductPara.SoftwareVer=SOFTWARE_VERSION;
- sutProductPara.HardwareVer=HARDWARE_VERSION;
- sutProductPara.ModemType=MODEM_TYPE;
- sutProductPara.GNSSType=GNSS_TYPE;
- sutProductPara.SensorType=SENSOR_TYPE;
- snprintf(sutProductPara.AppServer,sizeof(sutProductPara.AppServer),"%s",APP_SERVER);
- sutProductPara.AppPort=APP_SPORT;
- snprintf(sutProductPara.FotaServer,sizeof(sutProductPara.FotaServer),"%s",FOTA_SERVER);
- sutProductPara.FotaPort=FOTA_SPORT;
- snprintf(sutProductPara.CardAPN,sizeof(sutProductPara.CardAPN),"%s",CARD_APN);
- snprintf(sutProductPara.CardUsrName,sizeof(sutProductPara.CardUsrName),"%s",CARD_USER);
- snprintf(sutProductPara.CardUsrPass,sizeof(sutProductPara.CardUsrPass),"%s",CARD_PASS);
- sutProductPara.Mark=PRODUCT_PARA_MARK;
- WritePageData(PRODUCT_PARA_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
- strcpy(sutProductPara.proName, "ZRK");
- strcpy(sutProductPara.devName, "M20");
- }
- snprintf(sutProductPara.ProductName,sizeof(sutProductPara.ProductName),"%s",PRODUCT_NAME);
- sutProductPara.SoftwareVer=SOFTWARE_VERSION;
- sutProductPara.HardwareVer=HARDWARE_VERSION;
- }
- /**********************************************************************************************************
- SaveProductParaToFlash
- ************************************************************************************************************/
- void SaveProductParaToFlash(void)
- {
- //tsk_lock ();
- __set_PRIMASK(1);//关闭总中断
- WritePageData(PRODUCT_PARA_ADDR,(unsigned char *)&sutProductPara,sizeof(SUT_PRODUCT_PARA));
- __set_PRIMASK(0);//打开总中断
- //tsk_unlock ();
- }
- /********************************************************************
- *ReadDeviceConfigFromFlash
- *********************************************************************/
- void ReadDeviceConfigFromFlash(void)
- {
- if(DEVICE_CONFIG_ADDR<STM32_FLASH_BASE){//存放在外部FLASH
- SPI_Flash_Read((unsigned char *)&sutDeviceConfig,DEVICE_CONFIG_ADDR,sizeof(SUT_DEVICE_CONFIG));
- }else{//存放在MCU FLASH内
- ReadFlashData(DEVICE_CONFIG_ADDR,(unsigned char *)&sutDeviceConfig,sizeof(SUT_DEVICE_CONFIG));
- }
- if(sutDeviceConfig.Mark1!=DEVICE_CONFIG_MARK || sutDeviceConfig.Mark2!=DEVICE_CONFIG_MARK){
- SlwTrace(DEBUG,"Reset DeviceConfig!\r\n");
- memset((unsigned char *)&sutDeviceConfig,0,sizeof(SUT_DEVICE_CONFIG));
-
- sutDeviceConfig.Mark1=DEVICE_CONFIG_MARK;
- sutDeviceConfig.Mark2=DEVICE_CONFIG_MARK;
- sutDeviceConfig.AppExAddr=EXFLASH_FILE_DATA_ADDR;
-
- sutDeviceConfig.NetMode=1;
- sutDeviceConfig.SamplingInterval=5;//默认采样时间间隔 秒
- sutDeviceConfig.UploadInterval=60;//默认上传间隔 秒
- SaveDeviceConfigToFlash();
- }
-
- }
- /********************************************************************
- *SaveDeviceConfigToFlash
- *********************************************************************/
- void SaveDeviceConfigToFlash(void)
- {
- if(DEVICE_CONFIG_ADDR<STM32_FLASH_BASE){//存放在外部FLASH
- SPI_Flash_Write((unsigned char *)&sutDeviceConfig,DEVICE_CONFIG_ADDR,sizeof(SUT_DEVICE_CONFIG));
- }else{//存放在MCU FLASH内
- WritePageData(DEVICE_CONFIG_ADDR,(unsigned char *)&sutDeviceConfig,sizeof(SUT_DEVICE_CONFIG));
- }
- }
- void ReadServerIP(void)
- {
- SPI_Flash_Read((unsigned char *)&sutServerIp,EXFLASH_SERVER_CONFIG_ADDR,sizeof(SUT_SERVER_IP));
- }
- void SaveServerIP(void)
- {
- SPI_Flash_Write((unsigned char *)&sutServerIp,EXFLASH_SERVER_CONFIG_ADDR,sizeof(SUT_SERVER_IP));
- }
- /************************************file begin*******************************/
|