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.

59 lines
1.7 KiB

  1. // Copyright 2022 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <stdbool.h>
  4. #include <hal.h>
  5. #include "timer.h"
  6. #include "wear_leveling.h"
  7. #include "wear_leveling_internal.h"
  8. #include "flash_stm32.h"
  9. bool backing_store_init(void) {
  10. bs_dprintf("Init\n");
  11. return true;
  12. }
  13. bool backing_store_unlock(void) {
  14. bs_dprintf("Unlock\n");
  15. FLASH_Unlock();
  16. return true;
  17. }
  18. bool backing_store_erase(void) {
  19. #ifdef WEAR_LEVELING_DEBUG_OUTPUT
  20. uint32_t start = timer_read32();
  21. #endif
  22. bool ret = true;
  23. FLASH_Status status;
  24. for (int i = 0; i < (WEAR_LEVELING_LEGACY_EMULATION_PAGE_COUNT); ++i) {
  25. status = FLASH_ErasePage(WEAR_LEVELING_LEGACY_EMULATION_BASE_PAGE_ADDRESS + (i * (WEAR_LEVELING_LEGACY_EMULATION_PAGE_SIZE)));
  26. if (status != FLASH_COMPLETE) {
  27. ret = false;
  28. }
  29. }
  30. bs_dprintf("Backing store erase took %ldms to complete\n", ((long)(timer_read32() - start)));
  31. return ret;
  32. }
  33. bool backing_store_write(uint32_t address, backing_store_int_t value) {
  34. uint32_t offset = ((WEAR_LEVELING_LEGACY_EMULATION_BASE_PAGE_ADDRESS) + address);
  35. bs_dprintf("Write ");
  36. wl_dump(offset, &value, sizeof(backing_store_int_t));
  37. return FLASH_ProgramHalfWord(offset, ~value) == FLASH_COMPLETE;
  38. }
  39. bool backing_store_lock(void) {
  40. bs_dprintf("Lock \n");
  41. FLASH_Lock();
  42. return true;
  43. }
  44. bool backing_store_read(uint32_t address, backing_store_int_t* value) {
  45. uint32_t offset = ((WEAR_LEVELING_LEGACY_EMULATION_BASE_PAGE_ADDRESS) + address);
  46. backing_store_int_t* loc = (backing_store_int_t*)offset;
  47. *value = ~(*loc);
  48. bs_dprintf("Read ");
  49. wl_dump(offset, loc, sizeof(backing_store_int_t));
  50. return true;
  51. }