furi_hal_flash.h 3.6 KB

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