user.c 3.28 KB
#include "user.h"
//保存固定参数
#define FLASH_CONST_DATA_SAVE_ADDRESS   ADDR_FLASH_SECTOR_10
#define CONST_DATA_SAVE_LEN             16
SystemInfoUnion        sysInfoUnion;


//系统信息保存到FLASH
//输入:起始地址、起始校验结果
//返回:数据保存后的结束地址
//校验结果更新到CheckSum
unsigned long SystemInfo_WriteFlash(unsigned long startAddress,unsigned long *CheckSum)
{
    unsigned long i;
    unsigned long address;
    unsigned long *pData;

    FLASH_Unlock();

		address = 0;
    pData = (unsigned long*)(&sysInfoUnion);
    for(i = 0; i < sizeof(SystemInfoUnion)/4; address+=4,i++)
    {
		FLASH_ProgramWord(startAddress+address, pData[i]);
        *CheckSum ^= pData[i];
    }
    
    FLASH_Lock();
    return startAddress + address;
}

//从FLASH读取系统信息
unsigned long SystemInfo_ReadFlash(unsigned long startAddress)
{
    unsigned long i;
    unsigned long *pData;
    unsigned long *pFlash;

    pFlash  = (unsigned long*)(startAddress);
    pData  = (unsigned long*)(&sysInfoUnion);
    for(i = 0; i < sizeof(SystemInfoUnion)/4; i++)
    {
		pData[i] = pFlash[i];
    }
    return startAddress + sizeof(SystemInfoUnion);
}

//清除系统参数
void PowerOff_ClearConstFlash()
{
    __set_PRIMASK(1);  		/* 关中断 */
    FLASH_Unlock();
    FLASH_EraseSector(bsp_GetSector(FLASH_CONST_DATA_SAVE_ADDRESS),VoltageRange_3);
	FLASH_Lock();
    __set_PRIMASK(0);  		/* 开中断 */
}

void PowerOff_HeadInfo_WriteInfo(unsigned long startAddress,unsigned long len,unsigned long checkSum)
{
    FLASH_Unlock();    
    FLASH_ProgramWord(startAddress+0, len);                                     
    FLASH_ProgramWord(startAddress+4, checkSum);
    FLASH_Lock();    
}

unsigned char PowerOn_ReadFlash_ConstData()//读取系统参数
{
    unsigned long i;
    unsigned long CheckSum = 0;
    unsigned long address;
    unsigned long *pFlash;

    pFlash  = (unsigned long*)FLASH_CONST_DATA_SAVE_ADDRESS;
    
    if(pFlash[0] > 0x4000)
    {
        return 0;
    }
    
    for(i = 0; i < pFlash[0]/4; i++)
    {
        CheckSum ^= pFlash[2+i];
    }
    
    if(CheckSum != pFlash[1])
    {
        return 0;
    }
    
    address = FLASH_CONST_DATA_SAVE_ADDRESS + 8;
    address = SystemInfo_ReadFlash(address);
    
    return 1;
}

void PowerOn_WriteFlash_ConstData()//写系统FLASH
{
    unsigned long CheckSum = 0;
    unsigned long address;

    address = FLASH_CONST_DATA_SAVE_ADDRESS + 8;
    address = SystemInfo_WriteFlash(address,&CheckSum);
    address -= FLASH_CONST_DATA_SAVE_ADDRESS;
    address -= 8;    
    PowerOff_HeadInfo_WriteInfo(FLASH_CONST_DATA_SAVE_ADDRESS,address,CheckSum);
}

void proccessSetupInfo(u8 *RecvBuffer,u16 BufferLength)
{	
	if(strncmp("$para", (const char*)RecvBuffer, 5) == 0)//地标动作命令
	{
		Setup_Process();
	}
}

void ProccessSetupInfo(u8 Res)
{
	static char Buffer[120];
	static u16 iii = 0;

	if(iii > 100)
	{
		iii = 0;
	}
	if(Res == 0x24)  					// $
	{
		iii = 0;
	}
	Buffer[iii++] = Res;
	if(Res == 0x23)   					// # 
	{
		proccessSetupInfo((u8 *)Buffer,iii);
		iii = 0;
	}
}

void initFactoryParam()
{
	initSetup(sysInfoUnion.systemInfo.SystemParameter,FACTORYPARAMNUM);//设置参数
	PowerOn_ReadFlash_ConstData();
	while(1)
	{
		static int countTime = 0;
		u8 buff;
		if(ReadUart(COM1,&buff,1))
		{
			ProccessSetupInfo(buff);
		}
		bsp_DelayMS(1);
		
		if(countTime++ > 5000)
		{
			break;
		}
	}
}