EE_Dataset.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /************************************************************************
  2. Grt-Mars, Inc.
  3. 240 Sheffield Str.
  4. Mountainside, New Jersey 07092
  5. PROJECT : ProEX
  6. Author : Peter Strezev
  7. *******************************************************************************
  8. Functions to handle storage and retrieval of Irrigation programs in FLASH
  9. *******************************************************************************/
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "EE_Dataset.h"
  13. #include "FlashEE.h"
  14. /* --- Table of 16-bit virtual addresses (VA) for all variables. Each VA consists of 2 parts: Program Number (0..5) in the --- */
  15. /* --- upper byte, variable number (0..76) in the lower byte. --- */
  16. /*
  17. 1k的空间,最大存储地址是512,即0、2...1024
  18. 将地址分成8部分,则:
  19. 0~128: 0\2\4...\128 + 0x000 64byte
  20. 130~258: 0\2\4...\128 + 0x100 64byte
  21. ...
  22. 896~1024: 0\2\4...\128 + 0x800 64byte
  23. */
  24. const unsigned short VirtAddVarTab[NumbOfVar] =
  25. {
  26. //52byte -> (0~51)*2
  27. 0, //Net+DNSFlag
  28. 2, 4, //ComGateway[4] --2个地址
  29. 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, //SoctType+SysIP[31] --16个地址
  30. 26, 28, 30, 32, 34, 36,
  31. 38, 40, //DNSPort[4] --2个地址
  32. 42, //CTime+Cir1Probe
  33. 44, //Cir2Probe+Cir3Probe
  34. 46, //Cir4Probe+AppAddr
  35. 48, //UpDateStep+WorkMode
  36. 50, //u16 GYMAX;
  37. 52, //u16 QYMAX;
  38. 54 //u16 GLMAX;
  39. };
  40. void EEprom_Init ( void )
  41. {
  42. FLASH_Unlock();
  43. EE_Init();
  44. FLASH_Lock();
  45. }
  46. void LoadDtuConfig ( unsigned short addr )
  47. {
  48. char ConfigBuffer[sizeof ( SysPara )];
  49. unsigned short Temp, VAddr;
  50. int i;
  51. tEEstat ErrCode;
  52. FLASH_Unlock();
  53. for ( i = 0; i <= sizeof ( SysPara ) - 2; i += 2 )
  54. {
  55. VAddr = addr + i;
  56. ErrCode = ( tEEstat ) EE_ReadVariable ( VAddr, &Temp );
  57. if ( ErrCode == EE_DONE )
  58. {
  59. ConfigBuffer[i] = ( char ) ( Temp >> 8 );
  60. ConfigBuffer[i + 1] = ( char ) ( Temp & 0xFF ); //todo What if we overflow the buffer (if size is odd?)
  61. }
  62. }
  63. FLASH_Lock();
  64. memcpy ( &SysPara, &ConfigBuffer, sizeof ( SysPara ) );
  65. }
  66. void SaveDtuConfig ( unsigned short addr )
  67. {
  68. char ConfigBuffer[ ( sizeof ( SysPara ) + 1 )];
  69. unsigned short Temp, VAddr, Data;
  70. int i;
  71. tEEstat ErrCode;
  72. memcpy ( &ConfigBuffer, &SysPara, sizeof ( SysPara ) );
  73. FLASH_Unlock();
  74. for ( i = 0; i <= sizeof ( SysPara ) - 2; i += 2 )
  75. {
  76. VAddr = addr + i;
  77. Data = ( unsigned short ) ( ConfigBuffer[i] << 8 );
  78. Data |= ( unsigned short ) ( ConfigBuffer[i + 1] );
  79. ErrCode = ( tEEstat ) EE_ReadVariable ( VAddr, &Temp );
  80. if ( ( ErrCode == EE_BADVAR ) || ( Data != Temp ) ) //Only save new variables or if data is different
  81. {
  82. __disable_interrupt();
  83. ErrCode = ( tEEstat ) EE_WriteVariable ( VAddr, Data );
  84. __enable_interrupt();
  85. }
  86. }
  87. FLASH_Lock();
  88. }