Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.5 KiB

Rework settings (#2282) * wip based on early draft. todo benchmarking * fixup eraser, assume keys are unique * fix cursor copy, test removal at random * small benchmark via permutations. todo lambdas and novirtual * fix empty condition / reset * overwrite optimizations, fix move offsets overflows * ...erase using 0xff instead of 0 * test the theory with code, different length kv were bugged * try to check for out-of-bounds writes / reads * style * trying to fix mover again * clarify length, defend against reading len on edge * fix uncommited rewind change * prove space assumptions * more concise traces, fix move condition (agrh!!!) * slightly more internal knowledge (estimates API?) * make sure cursor is only valid within the range * ensure 0 does not blow things * go back up * cursor comments * comments * rewrite writes through cursor * in del too * estimate kv storage requirements, return available size * move raw erase / move into a method, allow ::set to avoid scanning storage twice * refactor naming, use in code * amend storage slicing test * fix crash handler offsets, cleanup configuration * start -> begin * eeprom readiness * dependencies * unused * SPI_FLASH constants for older Core * vtables -> templates * less include dependencies * gcov help, move estimate outside of the class * writer position can never match, use begin + offset * tweak save_crash to trigger only once in a serious crash * doh, header function should be inline * foreach api, tweak structs for public api * use test helper class * when not using foreach, move cursor reset closer to the loop using read_kv * coverage comments, fix typo in tests decltype * ensure set() does not break with offset * make codacy happy again
4 years ago
Rework settings (#2282) * wip based on early draft. todo benchmarking * fixup eraser, assume keys are unique * fix cursor copy, test removal at random * small benchmark via permutations. todo lambdas and novirtual * fix empty condition / reset * overwrite optimizations, fix move offsets overflows * ...erase using 0xff instead of 0 * test the theory with code, different length kv were bugged * try to check for out-of-bounds writes / reads * style * trying to fix mover again * clarify length, defend against reading len on edge * fix uncommited rewind change * prove space assumptions * more concise traces, fix move condition (agrh!!!) * slightly more internal knowledge (estimates API?) * make sure cursor is only valid within the range * ensure 0 does not blow things * go back up * cursor comments * comments * rewrite writes through cursor * in del too * estimate kv storage requirements, return available size * move raw erase / move into a method, allow ::set to avoid scanning storage twice * refactor naming, use in code * amend storage slicing test * fix crash handler offsets, cleanup configuration * start -> begin * eeprom readiness * dependencies * unused * SPI_FLASH constants for older Core * vtables -> templates * less include dependencies * gcov help, move estimate outside of the class * writer position can never match, use begin + offset * tweak save_crash to trigger only once in a serious crash * doh, header function should be inline * foreach api, tweak structs for public api * use test helper class * when not using foreach, move cursor reset closer to the loop using read_kv * coverage comments, fix typo in tests decltype * ensure set() does not break with offset * make codacy happy again
4 years ago
Rework settings (#2282) * wip based on early draft. todo benchmarking * fixup eraser, assume keys are unique * fix cursor copy, test removal at random * small benchmark via permutations. todo lambdas and novirtual * fix empty condition / reset * overwrite optimizations, fix move offsets overflows * ...erase using 0xff instead of 0 * test the theory with code, different length kv were bugged * try to check for out-of-bounds writes / reads * style * trying to fix mover again * clarify length, defend against reading len on edge * fix uncommited rewind change * prove space assumptions * more concise traces, fix move condition (agrh!!!) * slightly more internal knowledge (estimates API?) * make sure cursor is only valid within the range * ensure 0 does not blow things * go back up * cursor comments * comments * rewrite writes through cursor * in del too * estimate kv storage requirements, return available size * move raw erase / move into a method, allow ::set to avoid scanning storage twice * refactor naming, use in code * amend storage slicing test * fix crash handler offsets, cleanup configuration * start -> begin * eeprom readiness * dependencies * unused * SPI_FLASH constants for older Core * vtables -> templates * less include dependencies * gcov help, move estimate outside of the class * writer position can never match, use begin + offset * tweak save_crash to trigger only once in a serious crash * doh, header function should be inline * foreach api, tweak structs for public api * use test helper class * when not using foreach, move cursor reset closer to the loop using read_kv * coverage comments, fix typo in tests decltype * ensure set() does not break with offset * make codacy happy again
4 years ago
  1. /*
  2. EEPROM MODULE
  3. */
  4. #pragma once
  5. #include <Arduino.h>
  6. #include <EEPROM_Rotate.h>
  7. // "The library uses 3 bytes to track last valid sector, so there must be at least 3"
  8. // Reserve addresses 11, 12 and 13 for EEPROM_Rotate
  9. constexpr int EepromRotateOffset = 11;
  10. constexpr size_t EepromRotateReservedSize = 3;
  11. // Backwards compatibility requires us to reserve up to the offset + it's size
  12. // *The* main reason right now is EEPROM_Rotate crc bytes, the rest is not used
  13. // (but, might be in the future?)
  14. constexpr size_t EepromReservedSize = 14;
  15. constexpr size_t EepromSize = SPI_FLASH_SEC_SIZE;
  16. bool eepromReady();
  17. void eepromSectorsDebug();
  18. void eepromRotate(bool value);
  19. uint32_t eepromCurrent();
  20. String eepromSectors();
  21. unsigned long eepromSpace();
  22. void eepromClear();
  23. void eepromBackup(uint32_t index);
  24. void eepromForceCommit();
  25. void eepromCommit();
  26. void eepromSetup();
  27. // Implementation is inline right here, since we want to avoid chaining too much functions to simply access the EEPROM object
  28. // (which is already hidden via the subclassing...)
  29. // TODO: note that EEPROM is a `char` storage, but we have some helper methods to write up to 4 bytes at once
  30. // TODO: note that SPI flash is an `int32_t` storage, might want to optimize for that? write will happen in bulk anyway, perhaps does not matter much
  31. extern EEPROM_Rotate EEPROMr;
  32. inline unsigned long eepromSpace() {
  33. return EEPROMr.reserved() * SPI_FLASH_SEC_SIZE;
  34. }
  35. inline void eepromClear() {
  36. auto* ptr = EEPROMr.getDataPtr();
  37. std::fill(ptr + EepromReservedSize, ptr + EepromSize, 0xFF);
  38. EEPROMr.commit();
  39. }
  40. inline uint8_t eepromRead(int address) {
  41. return EEPROMr.read(address);
  42. }
  43. inline void eepromWrite(int address, unsigned char value) {
  44. EEPROMr.write(address, value);
  45. }
  46. inline void eepromGet(int address, unsigned char& value) {
  47. EEPROMr.get(address, value);
  48. }
  49. inline void eepromGet(int address, unsigned short& value) {
  50. EEPROMr.get(address, value);
  51. }
  52. inline void eepromGet(int address, unsigned int& value) {
  53. EEPROMr.get(address, value);
  54. }
  55. inline void eepromGet(int address, unsigned long& value) {
  56. EEPROMr.get(address, value);
  57. }
  58. inline void eepromPut(int address, unsigned char value) {
  59. EEPROMr.put(address, value);
  60. }
  61. inline void eepromPut(int address, unsigned short value) {
  62. EEPROMr.put(address, value);
  63. }
  64. inline void eepromPut(int address, unsigned int value) {
  65. EEPROMr.put(address, value);
  66. }
  67. inline void eepromPut(int address, unsigned long value) {
  68. EEPROMr.put(address, value);
  69. }