furi_hal_flash.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. void furi_hal_flash_erase(uint8_t page);
  79. /** Write double word (64 bits)
  80. *
  81. * @warning locking operation with critical section, stalls execution
  82. *
  83. * @param address destination address, must be double word aligned.
  84. * @param data data to write
  85. */
  86. void furi_hal_flash_write_dword(size_t address, uint64_t data);
  87. /** Write aligned page data (up to page size)
  88. *
  89. * @warning locking operation with critical section, stalls execution
  90. *
  91. * @param address destination address, must be page aligned.
  92. * @param data data to write
  93. * @param length data length
  94. */
  95. void furi_hal_flash_program_page(const uint8_t page, const uint8_t* data, uint16_t length);
  96. /** Get flash page number for address
  97. *
  98. * @return page number, -1 for invalid address
  99. */
  100. int16_t furi_hal_flash_get_page_number(size_t address);
  101. /** Writes OB word, using non-compl. index of register in Flash, OPTION_BYTE_BASE
  102. *
  103. * @warning locking operation with critical section, stalls execution
  104. *
  105. * @param word_idx OB word number
  106. * @param value data to write
  107. * @return true if value was written, false for read-only word
  108. */
  109. bool furi_hal_flash_ob_set_word(size_t word_idx, const uint32_t value);
  110. /** Forces a reload of OB data from flash to registers
  111. *
  112. * @warning Initializes system restart
  113. *
  114. */
  115. void furi_hal_flash_ob_apply();
  116. /** Get raw OB storage data
  117. *
  118. * @return pointer to read-only data of OB (raw + complementary values)
  119. */
  120. const FuriHalFlashRawOptionByteData* furi_hal_flash_ob_get_raw_ptr();
  121. #ifdef __cplusplus
  122. }
  123. #endif