EEPROM_store.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* this is the eeprom abstraction for the microbit v2.
  2. the eeprom will be mapped to a simple file in the file system, later.
  3. */
  4. #ifdef _MICROBIT_V2_
  5. const int STORESIZE = 256;
  6. byte program[STORESIZE];
  7. bool loaded = false;
  8. void load() {
  9. loaded = true;
  10. }
  11. byte readbyte(int addr) {
  12. if (!loaded) {
  13. load();
  14. }
  15. if ((addr >= 0) && (addr < STORESIZE)) {
  16. return program[addr];
  17. }
  18. return 0xFF;
  19. }
  20. void writebyte(int addr, byte value) {
  21. if ((addr >= 0) && (addr < STORESIZE)) {
  22. program[addr] = value;
  23. }
  24. }
  25. void store() {
  26. }
  27. #endif
  28. #ifdef ESP32
  29. #include <EEPROM.h>
  30. const int STORESIZE = 1024;
  31. byte program[STORESIZE];
  32. bool loaded = false;
  33. void load() {
  34. dbgOutLn("load prg from nvs");
  35. EEPROM.begin(STORESIZE);
  36. word readed = EEPROM.readBytes(0, program, STORESIZE);
  37. dbgOut("read:");
  38. dbgOut2(readed, HEX);
  39. dbgOutLn(" bytes");
  40. loaded = true;
  41. }
  42. byte readbyte(int addr) {
  43. if (!loaded) {
  44. load();
  45. }
  46. if ((addr >= 0) && (addr < STORESIZE)) {
  47. return program[addr];
  48. }
  49. return 0xFF;
  50. }
  51. void writebyte(int addr, byte value) {
  52. if ((addr >= 0) && (addr < STORESIZE)) {
  53. program[addr] = value;
  54. }
  55. }
  56. void store() {
  57. EEPROM.writeBytes(0, program, STORESIZE);
  58. EEPROM.commit();
  59. }
  60. #endif
  61. #if defined(__AVR_ATmega328P__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny861__) || defined(__AVR_ATtiny4313__)
  62. #include <EEPROM.h>
  63. #include <avr/eeprom.h>
  64. const int STORESIZE = E2END;
  65. byte readbyte(int addr) {
  66. return EEPROM.read(addr);
  67. }
  68. void writebyte(int addr, byte value) {
  69. EEPROM.write(addr, value);
  70. }
  71. void store() {
  72. }
  73. #endif