furi_hal_flash.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #include <furi_hal_flash.h>
  2. #include <furi_hal_bt.h>
  3. #include <furi.h>
  4. #include <ble.h>
  5. #include <shci.h>
  6. #include <stm32wbxx.h>
  7. #define FURI_HAL_TAG "FuriHalFlash"
  8. #define FURI_HAL_CRITICAL_MSG "Critical flash operation fail"
  9. #define FURI_HAL_FLASH_READ_BLOCK 8
  10. #define FURI_HAL_FLASH_WRITE_BLOCK 8
  11. #define FURI_HAL_FLASH_PAGE_SIZE 4096
  12. #define FURI_HAL_FLASH_CYCLES_COUNT 10000
  13. /* Free flash space borders, exported by linker */
  14. extern const void __free_flash_start__;
  15. size_t furi_hal_flash_get_base() {
  16. return FLASH_BASE;
  17. }
  18. size_t furi_hal_flash_get_read_block_size() {
  19. return FURI_HAL_FLASH_READ_BLOCK;
  20. }
  21. size_t furi_hal_flash_get_write_block_size() {
  22. return FURI_HAL_FLASH_WRITE_BLOCK;
  23. }
  24. size_t furi_hal_flash_get_page_size() {
  25. return FURI_HAL_FLASH_PAGE_SIZE;
  26. }
  27. size_t furi_hal_flash_get_cycles_count() {
  28. return FURI_HAL_FLASH_CYCLES_COUNT;
  29. }
  30. const void* furi_hal_flash_get_free_start_address() {
  31. return &__free_flash_start__;
  32. }
  33. const void* furi_hal_flash_get_free_end_address() {
  34. uint32_t sfr_reg_val = READ_REG(FLASH->SFR);
  35. uint32_t sfsa = (READ_BIT(sfr_reg_val, FLASH_SFR_SFSA) >> FLASH_SFR_SFSA_Pos);
  36. return (const void*)((sfsa * FLASH_PAGE_SIZE) + FLASH_BASE);
  37. }
  38. size_t furi_hal_flash_get_free_page_start_address() {
  39. size_t start = (size_t)furi_hal_flash_get_free_start_address();
  40. size_t page_start = start - start % FURI_HAL_FLASH_PAGE_SIZE;
  41. if(page_start != start) {
  42. page_start += FURI_HAL_FLASH_PAGE_SIZE;
  43. }
  44. return page_start;
  45. }
  46. size_t furi_hal_flash_get_free_page_count() {
  47. size_t end = (size_t)furi_hal_flash_get_free_end_address();
  48. size_t page_start = (size_t)furi_hal_flash_get_free_page_start_address();
  49. return (end - page_start) / FURI_HAL_FLASH_PAGE_SIZE;
  50. }
  51. static void furi_hal_flash_unlock() {
  52. /* verify Flash is locked */
  53. furi_check(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != 0U);
  54. /* Authorize the FLASH Registers access */
  55. WRITE_REG(FLASH->KEYR, FLASH_KEY1);
  56. WRITE_REG(FLASH->KEYR, FLASH_KEY2);
  57. /* verify Flash is unlock */
  58. furi_check(READ_BIT(FLASH->CR, FLASH_CR_LOCK) == 0U);
  59. }
  60. static void furi_hal_flash_lock(void) {
  61. /* verify Flash is unlocked */
  62. furi_check(READ_BIT(FLASH->CR, FLASH_CR_LOCK) == 0U);
  63. /* Set the LOCK Bit to lock the FLASH Registers access */
  64. /* @Note The lock and unlock procedure is done only using CR registers even from CPU2 */
  65. SET_BIT(FLASH->CR, FLASH_CR_LOCK);
  66. /* verify Flash is locked */
  67. furi_check(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != 0U);
  68. }
  69. static void furi_hal_flash_begin_with_core2(bool erase_flag) {
  70. // Take flash controller ownership
  71. while(HAL_HSEM_FastTake(CFG_HW_FLASH_SEMID) != HAL_OK) {
  72. taskYIELD();
  73. }
  74. // Unlock flash operation
  75. furi_hal_flash_unlock();
  76. // Erase activity notification
  77. if(erase_flag) SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_ON);
  78. while(true) {
  79. // Wait till flash controller become usable
  80. while(LL_FLASH_IsActiveFlag_OperationSuspended()) {
  81. taskYIELD();
  82. };
  83. // Just a little more love
  84. taskENTER_CRITICAL();
  85. // Actually we already have mutex for it, but specification is specification
  86. if(HAL_HSEM_IsSemTaken(CFG_HW_BLOCK_FLASH_REQ_BY_CPU1_SEMID)) {
  87. taskEXIT_CRITICAL();
  88. continue;
  89. }
  90. // Take sempahopre and prevent core2 from anyting funky
  91. if(!HAL_HSEM_IsSemTaken(CFG_HW_BLOCK_FLASH_REQ_BY_CPU2_SEMID)) {
  92. if(HAL_HSEM_FastTake(CFG_HW_BLOCK_FLASH_REQ_BY_CPU2_SEMID) != HAL_OK) {
  93. taskEXIT_CRITICAL();
  94. continue;
  95. }
  96. }
  97. break;
  98. }
  99. }
  100. static void furi_hal_flash_begin(bool erase_flag) {
  101. // Acquire dangerous ops mutex
  102. furi_hal_bt_lock_core2();
  103. // If Core2 is running use IPC locking
  104. if(furi_hal_bt_is_alive()) {
  105. furi_hal_flash_begin_with_core2(erase_flag);
  106. } else {
  107. furi_hal_flash_unlock();
  108. }
  109. }
  110. static void furi_hal_flash_end_with_core2(bool erase_flag) {
  111. // Funky ops are ok at this point
  112. HAL_HSEM_Release(CFG_HW_BLOCK_FLASH_REQ_BY_CPU2_SEMID, 0);
  113. // Task switching is ok
  114. taskEXIT_CRITICAL();
  115. // Doesn't make much sense, does it?
  116. while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY)) {
  117. taskYIELD();
  118. }
  119. // Erase activity over, core2 can continue
  120. if(erase_flag) SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_OFF);
  121. // Lock flash controller
  122. furi_hal_flash_lock();
  123. // Release flash controller ownership
  124. HAL_HSEM_Release(CFG_HW_FLASH_SEMID, 0);
  125. }
  126. static void furi_hal_flash_end(bool erase_flag) {
  127. // If Core2 is running use IPC locking
  128. if(furi_hal_bt_is_alive()) {
  129. furi_hal_flash_end_with_core2(erase_flag);
  130. } else {
  131. furi_hal_flash_lock();
  132. }
  133. // Release dangerous ops mutex
  134. furi_hal_bt_unlock_core2();
  135. }
  136. static void furi_hal_flush_cache(void) {
  137. /* Flush instruction cache */
  138. if(READ_BIT(FLASH->ACR, FLASH_ACR_ICEN) == FLASH_ACR_ICEN) {
  139. /* Disable instruction cache */
  140. __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
  141. /* Reset instruction cache */
  142. __HAL_FLASH_INSTRUCTION_CACHE_RESET();
  143. /* Enable instruction cache */
  144. __HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
  145. }
  146. /* Flush data cache */
  147. if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) == FLASH_ACR_DCEN) {
  148. /* Disable data cache */
  149. __HAL_FLASH_DATA_CACHE_DISABLE();
  150. /* Reset data cache */
  151. __HAL_FLASH_DATA_CACHE_RESET();
  152. /* Enable data cache */
  153. __HAL_FLASH_DATA_CACHE_ENABLE();
  154. }
  155. }
  156. HAL_StatusTypeDef furi_hal_flash_wait_last_operation(uint32_t timeout) {
  157. uint32_t error = 0;
  158. uint32_t countdown = 0;
  159. // Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  160. // Even if the FLASH operation fails, the BUSY flag will be reset and an error
  161. // flag will be set
  162. countdown = timeout;
  163. while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY)) {
  164. if(LL_SYSTICK_IsActiveCounterFlag()) {
  165. countdown--;
  166. }
  167. if(countdown == 0) {
  168. return HAL_TIMEOUT;
  169. }
  170. }
  171. /* Check FLASH operation error flags */
  172. error = FLASH->SR;
  173. /* Check FLASH End of Operation flag */
  174. if((error & FLASH_FLAG_EOP) != 0U) {
  175. /* Clear FLASH End of Operation pending bit */
  176. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  177. }
  178. /* Now update error variable to only error value */
  179. error &= FLASH_FLAG_SR_ERRORS;
  180. furi_check(error == 0);
  181. /* clear error flags */
  182. __HAL_FLASH_CLEAR_FLAG(error);
  183. /* Wait for control register to be written */
  184. countdown = timeout;
  185. while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_CFGBSY)) {
  186. if(LL_SYSTICK_IsActiveCounterFlag()) {
  187. countdown--;
  188. }
  189. if(countdown == 0) {
  190. return HAL_TIMEOUT;
  191. }
  192. }
  193. return HAL_OK;
  194. }
  195. bool furi_hal_flash_erase(uint8_t page) {
  196. furi_hal_flash_begin(true);
  197. // Ensure that controller state is valid
  198. furi_check(FLASH->SR == 0);
  199. /* Verify that next operation can be proceed */
  200. furi_check(furi_hal_flash_wait_last_operation(FLASH_TIMEOUT_VALUE) == HAL_OK);
  201. /* Select page and start operation */
  202. MODIFY_REG(
  203. FLASH->CR, FLASH_CR_PNB, ((page << FLASH_CR_PNB_Pos) | FLASH_CR_PER | FLASH_CR_STRT));
  204. /* Wait for last operation to be completed */
  205. furi_check(furi_hal_flash_wait_last_operation(FLASH_TIMEOUT_VALUE) == HAL_OK);
  206. /* If operation is completed or interrupted, disable the Page Erase Bit */
  207. CLEAR_BIT(FLASH->CR, (FLASH_CR_PER | FLASH_CR_PNB));
  208. /* Flush the caches to be sure of the data consistency */
  209. furi_hal_flush_cache();
  210. furi_hal_flash_end(true);
  211. return true;
  212. }
  213. bool furi_hal_flash_write_dword(size_t address, uint64_t data) {
  214. furi_hal_flash_begin(false);
  215. // Ensure that controller state is valid
  216. furi_check(FLASH->SR == 0);
  217. /* Check the parameters */
  218. furi_check(IS_ADDR_ALIGNED_64BITS(address));
  219. furi_check(IS_FLASH_PROGRAM_ADDRESS(address));
  220. /* Set PG bit */
  221. SET_BIT(FLASH->CR, FLASH_CR_PG);
  222. /* Program first word */
  223. *(uint32_t*)address = (uint32_t)data;
  224. // Barrier to ensure programming is performed in 2 steps, in right order
  225. // (independently of compiler optimization behavior)
  226. __ISB();
  227. /* Program second word */
  228. *(uint32_t*)(address + 4U) = (uint32_t)(data >> 32U);
  229. /* Wait for last operation to be completed */
  230. furi_check(furi_hal_flash_wait_last_operation(FLASH_TIMEOUT_VALUE) == HAL_OK);
  231. /* If the program operation is completed, disable the PG or FSTPG Bit */
  232. CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
  233. furi_hal_flash_end(false);
  234. return true;
  235. }