furi_hal_flash.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <stddef.h>
  5. #define FURI_HAL_FLASH_OB_RAW_SIZE_BYTES 0x80
  6. #define FURI_HAL_FLASH_OB_SIZE_WORDS (FURI_HAL_FLASH_OB_RAW_SIZE_BYTES / sizeof(uint32_t))
  7. #define FURI_HAL_FLASH_OB_TOTAL_VALUES (FURI_HAL_FLASH_OB_SIZE_WORDS / 2)
  8. typedef union {
  9. uint8_t bytes[FURI_HAL_FLASH_OB_RAW_SIZE_BYTES];
  10. union {
  11. struct {
  12. uint32_t base;
  13. uint32_t complementary_value;
  14. } values;
  15. uint64_t dword;
  16. } obs[FURI_HAL_FLASH_OB_TOTAL_VALUES];
  17. } FuriHalFlashRawOptionByteData;
  18. _Static_assert(
  19. sizeof(FuriHalFlashRawOptionByteData) == FURI_HAL_FLASH_OB_RAW_SIZE_BYTES,
  20. "UpdateManifestOptionByteData size error");
  21. /** Init flash, applying necessary workarounds
  22. */
  23. void furi_hal_flash_init();
  24. /** Get flash base address
  25. *
  26. * @return pointer to flash base
  27. */
  28. size_t furi_hal_flash_get_base();
  29. /** Get flash read block size
  30. *
  31. * @return size in bytes
  32. */
  33. size_t furi_hal_flash_get_read_block_size();
  34. /** Get flash write block size
  35. *
  36. * @return size in bytes
  37. */
  38. size_t furi_hal_flash_get_write_block_size();
  39. /** Get flash page size
  40. *
  41. * @return size in bytes
  42. */
  43. size_t furi_hal_flash_get_page_size();
  44. /** Get expected flash cycles count
  45. *
  46. * @return count of erase-write operations
  47. */
  48. size_t furi_hal_flash_get_cycles_count();
  49. /** Get free flash start address
  50. *
  51. * @return pointer to free region start
  52. */
  53. const void* furi_hal_flash_get_free_start_address();
  54. /** Get free flash end address
  55. *
  56. * @return pointer to free region end
  57. */
  58. const void* furi_hal_flash_get_free_end_address();
  59. /** Get first free page start address
  60. *
  61. * @return first free page memory address
  62. */
  63. size_t furi_hal_flash_get_free_page_start_address();
  64. /** Get free page count
  65. *
  66. * @return free page count
  67. */
  68. size_t furi_hal_flash_get_free_page_count();
  69. /** Erase Flash
  70. *
  71. * @warning locking operation with critical section, stalls execution
  72. *
  73. * @param page The page to erase
  74. *
  75. * @return true on success
  76. */
  77. bool furi_hal_flash_erase(uint8_t page);
  78. /** Write double word (64 bits)
  79. *
  80. * @warning locking operation with critical section, stalls execution
  81. *
  82. * @param address destination address, must be double word aligned.
  83. * @param data data to write
  84. *
  85. * @return true on success
  86. */
  87. bool furi_hal_flash_write_dword(size_t address, uint64_t data);
  88. /** Write aligned page data (up to page size)
  89. *
  90. * @warning locking operation with critical section, stalls execution
  91. *
  92. * @param address destination address, must be page aligned.
  93. * @param data data to write
  94. * @param length data length
  95. *
  96. * @return true on success
  97. */
  98. bool furi_hal_flash_program_page(const uint8_t page, const uint8_t* data, uint16_t length);
  99. /** Get flash page number for address
  100. *
  101. * @return page number, -1 for invalid address
  102. */
  103. int16_t furi_hal_flash_get_page_number(size_t address);
  104. /** Writes OB word, using non-compl. index of register in Flash, OPTION_BYTE_BASE
  105. *
  106. * @warning locking operation with critical section, stalls execution
  107. *
  108. * @param word_idx OB word number
  109. * @param value data to write
  110. * @return true if value was written, false for read-only word
  111. */
  112. bool furi_hal_flash_ob_set_word(size_t word_idx, const uint32_t value);
  113. /** Forces a reload of OB data from flash to registers
  114. *
  115. * @warning Initializes system restart
  116. *
  117. */
  118. void furi_hal_flash_ob_apply();
  119. /** Get raw OB storage data
  120. *
  121. * @return pointer to read-only data of OB (raw + complementary values)
  122. */
  123. const FuriHalFlashRawOptionByteData* furi_hal_flash_ob_get_raw_ptr();