eeparams.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*********************************************************************
  2. *
  3. * Fraise eeprom stored user parameters library
  4. *
  5. *********************************************************************
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  18. * Copyright (c) Antoine Rousseau 2009-2013
  19. ********************************************************************/
  20. #include "eeparams.h"
  21. #define EEUSER 28 // first free eeprom address, after space reserved for ID, NAME and PREFIX.
  22. int eeaddress;
  23. char eeoperation;
  24. #define EE_RD 0
  25. #define EE_WR 1
  26. #define EE_READBYTE_NEXT() (eeReadByte((char)(eeaddress++)))
  27. #define EE_WRITEBYTE_NEXT(data) eeWriteByte((char)(eeaddress++),data)
  28. // ------- Loads :
  29. char EELoadChar()
  30. {
  31. return EE_READBYTE_NEXT();
  32. }
  33. int EELoadInt()
  34. {
  35. return (EE_READBYTE_NEXT()<<8)|EE_READBYTE_NEXT();
  36. }
  37. long EELoadLong() //untested
  38. {
  39. return ((unsigned long)EE_READBYTE_NEXT()<<24)|((unsigned long)EE_READBYTE_NEXT()<<16)|(EE_READBYTE_NEXT()<<8)|EE_READBYTE_NEXT();
  40. }
  41. // ------- Saves :
  42. void EESaveChar(unsigned char data)
  43. {
  44. EE_WRITEBYTE_NEXT(data);
  45. }
  46. void EESaveInt(int data)
  47. {
  48. EE_WRITEBYTE_NEXT(data>>8);
  49. EE_WRITEBYTE_NEXT(data&255);
  50. }
  51. void EESaveLong(long data) //untested
  52. {
  53. EE_WRITEBYTE_NEXT(data>>24);
  54. EE_WRITEBYTE_NEXT(data>>16);
  55. EE_WRITEBYTE_NEXT(data>>8);
  56. EE_WRITEBYTE_NEXT(data);
  57. }
  58. // ------- Declares :
  59. void EEdeclareChar(unsigned char *data)
  60. {
  61. if(eeoperation==EE_RD) *data=EELoadChar();
  62. else if(eeoperation==EE_WR) EESaveChar(*data);
  63. }
  64. void EEdeclareInt(unsigned int *data)
  65. {
  66. if(eeoperation==EE_RD) *data=EELoadInt();
  67. else if(eeoperation==EE_WR) EESaveInt(*data);
  68. }
  69. void EEdeclareLong(unsigned long *data)
  70. {
  71. if(eeoperation==EE_RD) *data=EELoadLong();
  72. else if(eeoperation==EE_WR) EESaveLong(*data);
  73. }
  74. // ------- Read/Write :
  75. void EEwriteMain()
  76. {
  77. eeoperation=EE_WR;
  78. eeaddress=EEUSER;
  79. #ifdef UD_EE
  80. EEdeclareMain();
  81. #endif
  82. }
  83. void EEreadMain()
  84. {
  85. eeoperation=EE_RD;
  86. eeaddress=EEUSER;
  87. #ifdef UD_EE
  88. EEdeclareMain();
  89. #endif
  90. }