furi_hal_flash.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. osThreadYield();
  102. continue;
  103. }
  104. // Take sempahopre and prevent core2 from anything funky
  105. if(LL_HSEM_1StepLock(HSEM, CFG_HW_BLOCK_FLASH_REQ_BY_CPU2_SEMID) != 0) {
  106. taskEXIT_CRITICAL();
  107. osThreadYield();
  108. continue;
  109. }
  110. break;
  111. }
  112. }
  113. static void furi_hal_flash_begin(bool erase_flag) {
  114. // Acquire dangerous ops mutex
  115. furi_hal_bt_lock_core2();
  116. // If Core2 is running use IPC locking
  117. if(furi_hal_bt_is_alive()) {
  118. furi_hal_flash_begin_with_core2(erase_flag);
  119. } else {
  120. furi_hal_flash_unlock();
  121. }
  122. }
  123. static void furi_hal_flash_end_with_core2(bool erase_flag) {
  124. // Funky ops are ok at this point
  125. LL_HSEM_ReleaseLock(HSEM, CFG_HW_BLOCK_FLASH_REQ_BY_CPU2_SEMID, 0);
  126. // Task switching is ok
  127. taskEXIT_CRITICAL();
  128. // Doesn't make much sense, does it?
  129. while(READ_BIT(FLASH->SR, FLASH_SR_BSY)) {
  130. osThreadYield();
  131. }
  132. // Erase activity over, core2 can continue
  133. if(erase_flag) SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_OFF);
  134. // Lock flash controller
  135. furi_hal_flash_lock();
  136. // Release flash controller ownership
  137. LL_HSEM_ReleaseLock(HSEM, CFG_HW_FLASH_SEMID, 0);
  138. }
  139. static void furi_hal_flash_end(bool erase_flag) {
  140. // If Core2 is running use IPC locking
  141. if(furi_hal_bt_is_alive()) {
  142. furi_hal_flash_end_with_core2(erase_flag);
  143. } else {
  144. furi_hal_flash_lock();
  145. }
  146. // Release dangerous ops mutex
  147. furi_hal_bt_unlock_core2();
  148. }
  149. static void furi_hal_flush_cache(void) {
  150. /* Flush instruction cache */
  151. if(READ_BIT(FLASH->ACR, FLASH_ACR_ICEN) == FLASH_ACR_ICEN) {
  152. /* Disable instruction cache */
  153. LL_FLASH_DisableInstCache();
  154. /* Reset instruction cache */
  155. LL_FLASH_EnableInstCacheReset();
  156. LL_FLASH_DisableInstCacheReset();
  157. /* Enable instruction cache */
  158. LL_FLASH_EnableInstCache();
  159. }
  160. /* Flush data cache */
  161. if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) == FLASH_ACR_DCEN) {
  162. /* Disable data cache */
  163. LL_FLASH_DisableDataCache();
  164. /* Reset data cache */
  165. LL_FLASH_EnableDataCacheReset();
  166. LL_FLASH_DisableDataCacheReset();
  167. /* Enable data cache */
  168. LL_FLASH_EnableDataCache();
  169. }
  170. }
  171. bool furi_hal_flash_wait_last_operation(uint32_t timeout) {
  172. uint32_t error = 0;
  173. uint32_t countdown = 0;
  174. // Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  175. // Even if the FLASH operation fails, the BUSY flag will be reset and an error
  176. // flag will be set
  177. countdown = timeout;
  178. while(READ_BIT(FLASH->SR, FLASH_SR_BSY)) {
  179. if(LL_SYSTICK_IsActiveCounterFlag()) {
  180. countdown--;
  181. }
  182. if(countdown == 0) {
  183. return false;
  184. }
  185. }
  186. /* Check FLASH operation error flags */
  187. error = FLASH->SR;
  188. /* Check FLASH End of Operation flag */
  189. if((error & FLASH_SR_EOP) != 0U) {
  190. /* Clear FLASH End of Operation pending bit */
  191. CLEAR_BIT(FLASH->SR, FLASH_SR_EOP);
  192. }
  193. /* Now update error variable to only error value */
  194. error &= FURI_HAL_FLASH_SR_ERRORS;
  195. furi_check(error == 0);
  196. /* clear error flags */
  197. CLEAR_BIT(FLASH->SR, error);
  198. /* Wait for control register to be written */
  199. countdown = timeout;
  200. while(READ_BIT(FLASH->SR, FLASH_SR_CFGBSY)) {
  201. if(LL_SYSTICK_IsActiveCounterFlag()) {
  202. countdown--;
  203. }
  204. if(countdown == 0) {
  205. return false;
  206. }
  207. }
  208. return true;
  209. }
  210. bool furi_hal_flash_erase(uint8_t page) {
  211. furi_hal_flash_begin(true);
  212. // Ensure that controller state is valid
  213. furi_check(FLASH->SR == 0);
  214. /* Verify that next operation can be proceed */
  215. furi_check(furi_hal_flash_wait_last_operation(FURI_HAL_FLASH_TIMEOUT));
  216. /* Select page and start operation */
  217. MODIFY_REG(
  218. FLASH->CR, FLASH_CR_PNB, ((page << FLASH_CR_PNB_Pos) | FLASH_CR_PER | FLASH_CR_STRT));
  219. /* Wait for last operation to be completed */
  220. furi_check(furi_hal_flash_wait_last_operation(FURI_HAL_FLASH_TIMEOUT));
  221. /* If operation is completed or interrupted, disable the Page Erase Bit */
  222. CLEAR_BIT(FLASH->CR, (FLASH_CR_PER | FLASH_CR_PNB));
  223. /* Flush the caches to be sure of the data consistency */
  224. furi_hal_flush_cache();
  225. furi_hal_flash_end(true);
  226. return true;
  227. }
  228. bool furi_hal_flash_write_dword(size_t address, uint64_t data) {
  229. furi_hal_flash_begin(false);
  230. // Ensure that controller state is valid
  231. furi_check(FLASH->SR == 0);
  232. /* Check the parameters */
  233. furi_check(IS_ADDR_ALIGNED_64BITS(address));
  234. furi_check(IS_FLASH_PROGRAM_ADDRESS(address));
  235. /* Set PG bit */
  236. SET_BIT(FLASH->CR, FLASH_CR_PG);
  237. /* Program first word */
  238. *(uint32_t*)address = (uint32_t)data;
  239. // Barrier to ensure programming is performed in 2 steps, in right order
  240. // (independently of compiler optimization behavior)
  241. __ISB();
  242. /* Program second word */
  243. *(uint32_t*)(address + 4U) = (uint32_t)(data >> 32U);
  244. /* Wait for last operation to be completed */
  245. furi_check(furi_hal_flash_wait_last_operation(FURI_HAL_FLASH_TIMEOUT));
  246. /* If the program operation is completed, disable the PG or FSTPG Bit */
  247. CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
  248. furi_hal_flash_end(false);
  249. return true;
  250. }