furi_hal_flash.c 9.4 KB

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