settings.c 905 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <string.h>
  2. #include "settings.h"
  3. #include "flash.h"
  4. #define RAM_SETTINGS (0x20012000)
  5. #define FLASH_SETTINGS (0x0E0000)
  6. #define SETTINGS_PART_SIZE (0x20000) /* 128 KB */
  7. struct settings *Settings = (struct settings *)(RAM_SETTINGS);
  8. void settings_save(void)
  9. {
  10. flash_unlock();
  11. flash_erase(FLASH_SETTINGS, SETTINGS_PART_SIZE);
  12. flash_write(FLASH_SETTINGS, (const void *)(RAM_SETTINGS), sizeof(struct settings));
  13. flash_lock();
  14. }
  15. static void restore_defaults(void)
  16. {
  17. memset(Settings, 0, sizeof(struct settings));
  18. Settings->storage_hdr = STORAGE_HDR;
  19. Settings->bpm = 125;
  20. settings_save();
  21. }
  22. void settings_load(void)
  23. {
  24. struct settings *s = (struct settings *)FLASH_SETTINGS;
  25. if (s->storage_hdr != STORAGE_HDR) {
  26. restore_defaults();
  27. return;
  28. }
  29. memcpy((void *)RAM_SETTINGS, (const void *)FLASH_SETTINGS, sizeof(struct settings));
  30. }