/************************************************************************ Grt-Mars, Inc. 240 Sheffield Str. Mountainside, New Jersey 07092 PROJECT : ProEX Author : Peter Strezev ******************************************************************************* Functions to handle storage and retrieval of Irrigation programs in FLASH *******************************************************************************/ #include #include #include "EE_Dataset.h" #include "FlashEE.h" /* --- Table of 16-bit virtual addresses (VA) for all variables. Each VA consists of 2 parts: Program Number (0..5) in the --- */ /* --- upper byte, variable number (0..76) in the lower byte. --- */ /* 1k的空间,最大存储地址是512,即0、2...1024 将地址分成8部分,则: 0~128: 0\2\4...\128 + 0x000 64byte 130~258: 0\2\4...\128 + 0x100 64byte ... 896~1024: 0\2\4...\128 + 0x800 64byte */ const unsigned short VirtAddVarTab[NumbOfVar] = { //52byte -> (0~51)*2 0, //Net+DNSFlag 2, 4, //ComGateway[4] --2个地址 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, //SoctType+SysIP[31] --16个地址 26, 28, 30, 32, 34, 36, 38, 40, //DNSPort[4] --2个地址 42, //CTime+Cir1Probe 44, //Cir2Probe+Cir3Probe 46, //Cir4Probe+AppAddr 48, //UpDateStep+WorkMode 50, //u16 GYMAX; 52, //u16 QYMAX; 54 //u16 GLMAX; }; void EEprom_Init ( void ) { FLASH_Unlock(); EE_Init(); FLASH_Lock(); } void LoadDtuConfig ( unsigned short addr ) { char ConfigBuffer[sizeof ( SysPara )]; unsigned short Temp, VAddr; int i; tEEstat ErrCode; FLASH_Unlock(); for ( i = 0; i <= sizeof ( SysPara ) - 2; i += 2 ) { VAddr = addr + i; ErrCode = ( tEEstat ) EE_ReadVariable ( VAddr, &Temp ); if ( ErrCode == EE_DONE ) { ConfigBuffer[i] = ( char ) ( Temp >> 8 ); ConfigBuffer[i + 1] = ( char ) ( Temp & 0xFF ); //todo What if we overflow the buffer (if size is odd?) } } FLASH_Lock(); memcpy ( &SysPara, &ConfigBuffer, sizeof ( SysPara ) ); } void SaveDtuConfig ( unsigned short addr ) { char ConfigBuffer[ ( sizeof ( SysPara ) + 1 )]; unsigned short Temp, VAddr, Data; int i; tEEstat ErrCode; memcpy ( &ConfigBuffer, &SysPara, sizeof ( SysPara ) ); FLASH_Unlock(); for ( i = 0; i <= sizeof ( SysPara ) - 2; i += 2 ) { VAddr = addr + i; Data = ( unsigned short ) ( ConfigBuffer[i] << 8 ); Data |= ( unsigned short ) ( ConfigBuffer[i + 1] ); ErrCode = ( tEEstat ) EE_ReadVariable ( VAddr, &Temp ); if ( ( ErrCode == EE_BADVAR ) || ( Data != Temp ) ) //Only save new variables or if data is different { __disable_interrupt(); ErrCode = ( tEEstat ) EE_WriteVariable ( VAddr, Data ); __enable_interrupt(); } } FLASH_Lock(); }