stm32f1xx_hal_flash.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_flash.c
  4. * @author MCD Application Team
  5. * @brief FLASH HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the internal FLASH memory:
  8. * + Program operations functions
  9. * + Memory Control functions
  10. * + Peripheral State functions
  11. *
  12. @verbatim
  13. ==============================================================================
  14. ##### FLASH peripheral features #####
  15. ==============================================================================
  16. [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses
  17. to the Flash memory. It implements the erase and program Flash memory operations
  18. and the read and write protection mechanisms.
  19. [..] The Flash memory interface accelerates code execution with a system of instruction
  20. prefetch.
  21. [..] The FLASH main features are:
  22. (+) Flash memory read operations
  23. (+) Flash memory program/erase operations
  24. (+) Read / write protections
  25. (+) Prefetch on I-Code
  26. (+) Option Bytes programming
  27. ##### How to use this driver #####
  28. ==============================================================================
  29. [..]
  30. This driver provides functions and macros to configure and program the FLASH
  31. memory of all STM32F1xx devices.
  32. (#) FLASH Memory I/O Programming functions: this group includes all needed
  33. functions to erase and program the main memory:
  34. (++) Lock and Unlock the FLASH interface
  35. (++) Erase function: Erase page, erase all pages
  36. (++) Program functions: half word, word and doubleword
  37. (#) FLASH Option Bytes Programming functions: this group includes all needed
  38. functions to manage the Option Bytes:
  39. (++) Lock and Unlock the Option Bytes
  40. (++) Set/Reset the write protection
  41. (++) Set the Read protection Level
  42. (++) Program the user Option Bytes
  43. (++) Launch the Option Bytes loader
  44. (++) Erase Option Bytes
  45. (++) Program the data Option Bytes
  46. (++) Get the Write protection.
  47. (++) Get the user option bytes.
  48. (#) Interrupts and flags management functions : this group
  49. includes all needed functions to:
  50. (++) Handle FLASH interrupts
  51. (++) Wait for last FLASH operation according to its status
  52. (++) Get error flag status
  53. [..] In addition to these function, this driver includes a set of macros allowing
  54. to handle the following operations:
  55. (+) Set/Get the latency
  56. (+) Enable/Disable the prefetch buffer
  57. (+) Enable/Disable the half cycle access
  58. (+) Enable/Disable the FLASH interrupts
  59. (+) Monitor the FLASH flags status
  60. @endverbatim
  61. ******************************************************************************
  62. * @attention
  63. *
  64. * Copyright (c) 2016 STMicroelectronics.
  65. * All rights reserved.
  66. *
  67. * This software is licensed under terms that can be found in the LICENSE file in
  68. * the root directory of this software component.
  69. * If no LICENSE file comes with this software, it is provided AS-IS.
  70. ******************************************************************************
  71. */
  72. /* Includes ------------------------------------------------------------------*/
  73. #include "stm32f1xx_hal.h"
  74. /** @addtogroup STM32F1xx_HAL_Driver
  75. * @{
  76. */
  77. #ifdef HAL_FLASH_MODULE_ENABLED
  78. /** @defgroup FLASH FLASH
  79. * @brief FLASH HAL module driver
  80. * @{
  81. */
  82. /* Private typedef -----------------------------------------------------------*/
  83. /* Private define ------------------------------------------------------------*/
  84. /** @defgroup FLASH_Private_Constants FLASH Private Constants
  85. * @{
  86. */
  87. /**
  88. * @}
  89. */
  90. /* Private macro ---------------------------- ---------------------------------*/
  91. /** @defgroup FLASH_Private_Macros FLASH Private Macros
  92. * @{
  93. */
  94. /**
  95. * @}
  96. */
  97. /* Private variables ---------------------------------------------------------*/
  98. /** @defgroup FLASH_Private_Variables FLASH Private Variables
  99. * @{
  100. */
  101. /* Variables used for Erase pages under interruption*/
  102. FLASH_ProcessTypeDef pFlash;
  103. /**
  104. * @}
  105. */
  106. /* Private function prototypes -----------------------------------------------*/
  107. /** @defgroup FLASH_Private_Functions FLASH Private Functions
  108. * @{
  109. */
  110. static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data);
  111. static void FLASH_SetErrorCode(void);
  112. extern void FLASH_PageErase(uint32_t PageAddress);
  113. /**
  114. * @}
  115. */
  116. /* Exported functions ---------------------------------------------------------*/
  117. /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
  118. * @{
  119. */
  120. /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
  121. * @brief Programming operation functions
  122. *
  123. @verbatim
  124. @endverbatim
  125. * @{
  126. */
  127. /**
  128. * @brief Program halfword, word or double word at a specified address
  129. * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
  130. * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
  131. *
  132. * @note If an erase and a program operations are requested simultaneously,
  133. * the erase operation is performed before the program one.
  134. *
  135. * @note FLASH should be previously erased before new programmation (only exception to this
  136. * is when 0x0000 is programmed)
  137. *
  138. * @param TypeProgram: Indicate the way to program at a specified address.
  139. * This parameter can be a value of @ref FLASH_Type_Program
  140. * @param Address: Specifies the address to be programmed.
  141. * @param Data: Specifies the data to be programmed
  142. *
  143. * @retval HAL_StatusTypeDef HAL Status
  144. */
  145. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  146. {
  147. HAL_StatusTypeDef status = HAL_ERROR;
  148. uint8_t index = 0;
  149. uint8_t nbiterations = 0;
  150. /* Process Locked */
  151. __HAL_LOCK(&pFlash);
  152. /* Check the parameters */
  153. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  154. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  155. #if defined(FLASH_BANK2_END)
  156. if(Address <= FLASH_BANK1_END)
  157. {
  158. #endif /* FLASH_BANK2_END */
  159. /* Wait for last operation to be completed */
  160. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  161. #if defined(FLASH_BANK2_END)
  162. }
  163. else
  164. {
  165. /* Wait for last operation to be completed */
  166. status = FLASH_WaitForLastOperationBank2(FLASH_TIMEOUT_VALUE);
  167. }
  168. #endif /* FLASH_BANK2_END */
  169. if(status == HAL_OK)
  170. {
  171. if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
  172. {
  173. /* Program halfword (16-bit) at a specified address. */
  174. nbiterations = 1U;
  175. }
  176. else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
  177. {
  178. /* Program word (32-bit = 2*16-bit) at a specified address. */
  179. nbiterations = 2U;
  180. }
  181. else
  182. {
  183. /* Program double word (64-bit = 4*16-bit) at a specified address. */
  184. nbiterations = 4U;
  185. }
  186. for (index = 0U; index < nbiterations; index++)
  187. {
  188. FLASH_Program_HalfWord((Address + (2U*index)), (uint16_t)(Data >> (16U*index)));
  189. #if defined(FLASH_BANK2_END)
  190. if(Address <= FLASH_BANK1_END)
  191. {
  192. #endif /* FLASH_BANK2_END */
  193. /* Wait for last operation to be completed */
  194. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  195. /* If the program operation is completed, disable the PG Bit */
  196. CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
  197. #if defined(FLASH_BANK2_END)
  198. }
  199. else
  200. {
  201. /* Wait for last operation to be completed */
  202. status = FLASH_WaitForLastOperationBank2(FLASH_TIMEOUT_VALUE);
  203. /* If the program operation is completed, disable the PG Bit */
  204. CLEAR_BIT(FLASH->CR2, FLASH_CR2_PG);
  205. }
  206. #endif /* FLASH_BANK2_END */
  207. /* In case of error, stop programation procedure */
  208. if (status != HAL_OK)
  209. {
  210. break;
  211. }
  212. }
  213. }
  214. /* Process Unlocked */
  215. __HAL_UNLOCK(&pFlash);
  216. return status;
  217. }
  218. /**
  219. * @brief Program halfword, word or double word at a specified address with interrupt enabled.
  220. * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
  221. * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
  222. *
  223. * @note If an erase and a program operations are requested simultaneously,
  224. * the erase operation is performed before the program one.
  225. *
  226. * @param TypeProgram: Indicate the way to program at a specified address.
  227. * This parameter can be a value of @ref FLASH_Type_Program
  228. * @param Address: Specifies the address to be programmed.
  229. * @param Data: Specifies the data to be programmed
  230. *
  231. * @retval HAL_StatusTypeDef HAL Status
  232. */
  233. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  234. {
  235. HAL_StatusTypeDef status = HAL_OK;
  236. /* Process Locked */
  237. __HAL_LOCK(&pFlash);
  238. /* Check the parameters */
  239. assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
  240. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  241. #if defined(FLASH_BANK2_END)
  242. /* If procedure already ongoing, reject the next one */
  243. if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
  244. {
  245. return HAL_ERROR;
  246. }
  247. if(Address <= FLASH_BANK1_END)
  248. {
  249. /* Enable End of FLASH Operation and Error source interrupts */
  250. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP_BANK1 | FLASH_IT_ERR_BANK1);
  251. }else
  252. {
  253. /* Enable End of FLASH Operation and Error source interrupts */
  254. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP_BANK2 | FLASH_IT_ERR_BANK2);
  255. }
  256. #else
  257. /* Enable End of FLASH Operation and Error source interrupts */
  258. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
  259. #endif /* FLASH_BANK2_END */
  260. pFlash.Address = Address;
  261. pFlash.Data = Data;
  262. if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
  263. {
  264. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMHALFWORD;
  265. /* Program halfword (16-bit) at a specified address. */
  266. pFlash.DataRemaining = 1U;
  267. }
  268. else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
  269. {
  270. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMWORD;
  271. /* Program word (32-bit : 2*16-bit) at a specified address. */
  272. pFlash.DataRemaining = 2U;
  273. }
  274. else
  275. {
  276. pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMDOUBLEWORD;
  277. /* Program double word (64-bit : 4*16-bit) at a specified address. */
  278. pFlash.DataRemaining = 4U;
  279. }
  280. /* Program halfword (16-bit) at a specified address. */
  281. FLASH_Program_HalfWord(Address, (uint16_t)Data);
  282. return status;
  283. }
  284. /**
  285. * @brief This function handles FLASH interrupt request.
  286. * @retval None
  287. */
  288. void HAL_FLASH_IRQHandler(void)
  289. {
  290. uint32_t addresstmp = 0U;
  291. /* Check FLASH operation error flags */
  292. #if defined(FLASH_BANK2_END)
  293. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK1) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK1) || \
  294. (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK2) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK2)))
  295. #else
  296. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
  297. #endif /* FLASH_BANK2_END */
  298. {
  299. /* Return the faulty address */
  300. addresstmp = pFlash.Address;
  301. /* Reset address */
  302. pFlash.Address = 0xFFFFFFFFU;
  303. /* Save the Error code */
  304. FLASH_SetErrorCode();
  305. /* FLASH error interrupt user callback */
  306. HAL_FLASH_OperationErrorCallback(addresstmp);
  307. /* Stop the procedure ongoing */
  308. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  309. }
  310. /* Check FLASH End of Operation flag */
  311. #if defined(FLASH_BANK2_END)
  312. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP_BANK1))
  313. {
  314. /* Clear FLASH End of Operation pending bit */
  315. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP_BANK1);
  316. #else
  317. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  318. {
  319. /* Clear FLASH End of Operation pending bit */
  320. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  321. #endif /* FLASH_BANK2_END */
  322. /* Process can continue only if no error detected */
  323. if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
  324. {
  325. if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
  326. {
  327. /* Nb of pages to erased can be decreased */
  328. pFlash.DataRemaining--;
  329. /* Check if there are still pages to erase */
  330. if(pFlash.DataRemaining != 0U)
  331. {
  332. addresstmp = pFlash.Address;
  333. /*Indicate user which sector has been erased */
  334. HAL_FLASH_EndOfOperationCallback(addresstmp);
  335. /*Increment sector number*/
  336. addresstmp = pFlash.Address + FLASH_PAGE_SIZE;
  337. pFlash.Address = addresstmp;
  338. /* If the erase operation is completed, disable the PER Bit */
  339. CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
  340. FLASH_PageErase(addresstmp);
  341. }
  342. else
  343. {
  344. /* No more pages to Erase, user callback can be called. */
  345. /* Reset Sector and stop Erase pages procedure */
  346. pFlash.Address = addresstmp = 0xFFFFFFFFU;
  347. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  348. /* FLASH EOP interrupt user callback */
  349. HAL_FLASH_EndOfOperationCallback(addresstmp);
  350. }
  351. }
  352. else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
  353. {
  354. /* Operation is completed, disable the MER Bit */
  355. CLEAR_BIT(FLASH->CR, FLASH_CR_MER);
  356. #if defined(FLASH_BANK2_END)
  357. /* Stop Mass Erase procedure if no pending mass erase on other bank */
  358. if (HAL_IS_BIT_CLR(FLASH->CR2, FLASH_CR2_MER))
  359. {
  360. #endif /* FLASH_BANK2_END */
  361. /* MassErase ended. Return the selected bank */
  362. /* FLASH EOP interrupt user callback */
  363. HAL_FLASH_EndOfOperationCallback(0U);
  364. /* Stop Mass Erase procedure*/
  365. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  366. }
  367. #if defined(FLASH_BANK2_END)
  368. }
  369. #endif /* FLASH_BANK2_END */
  370. else
  371. {
  372. /* Nb of 16-bit data to program can be decreased */
  373. pFlash.DataRemaining--;
  374. /* Check if there are still 16-bit data to program */
  375. if(pFlash.DataRemaining != 0U)
  376. {
  377. /* Increment address to 16-bit */
  378. pFlash.Address += 2U;
  379. addresstmp = pFlash.Address;
  380. /* Shift to have next 16-bit data */
  381. pFlash.Data = (pFlash.Data >> 16U);
  382. /* Operation is completed, disable the PG Bit */
  383. CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
  384. /*Program halfword (16-bit) at a specified address.*/
  385. FLASH_Program_HalfWord(addresstmp, (uint16_t)pFlash.Data);
  386. }
  387. else
  388. {
  389. /* Program ended. Return the selected address */
  390. /* FLASH EOP interrupt user callback */
  391. if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMHALFWORD)
  392. {
  393. HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  394. }
  395. else if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMWORD)
  396. {
  397. HAL_FLASH_EndOfOperationCallback(pFlash.Address - 2U);
  398. }
  399. else
  400. {
  401. HAL_FLASH_EndOfOperationCallback(pFlash.Address - 6U);
  402. }
  403. /* Reset Address and stop Program procedure */
  404. pFlash.Address = 0xFFFFFFFFU;
  405. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  406. }
  407. }
  408. }
  409. }
  410. #if defined(FLASH_BANK2_END)
  411. /* Check FLASH End of Operation flag */
  412. if(__HAL_FLASH_GET_FLAG( FLASH_FLAG_EOP_BANK2))
  413. {
  414. /* Clear FLASH End of Operation pending bit */
  415. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP_BANK2);
  416. /* Process can continue only if no error detected */
  417. if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
  418. {
  419. if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
  420. {
  421. /* Nb of pages to erased can be decreased */
  422. pFlash.DataRemaining--;
  423. /* Check if there are still pages to erase*/
  424. if(pFlash.DataRemaining != 0U)
  425. {
  426. /* Indicate user which page address has been erased*/
  427. HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  428. /* Increment page address to next page */
  429. pFlash.Address += FLASH_PAGE_SIZE;
  430. addresstmp = pFlash.Address;
  431. /* Operation is completed, disable the PER Bit */
  432. CLEAR_BIT(FLASH->CR2, FLASH_CR2_PER);
  433. FLASH_PageErase(addresstmp);
  434. }
  435. else
  436. {
  437. /*No more pages to Erase*/
  438. /*Reset Address and stop Erase pages procedure*/
  439. pFlash.Address = 0xFFFFFFFFU;
  440. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  441. /* FLASH EOP interrupt user callback */
  442. HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  443. }
  444. }
  445. else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
  446. {
  447. /* Operation is completed, disable the MER Bit */
  448. CLEAR_BIT(FLASH->CR2, FLASH_CR2_MER);
  449. if (HAL_IS_BIT_CLR(FLASH->CR, FLASH_CR_MER))
  450. {
  451. /* MassErase ended. Return the selected bank*/
  452. /* FLASH EOP interrupt user callback */
  453. HAL_FLASH_EndOfOperationCallback(0U);
  454. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  455. }
  456. }
  457. else
  458. {
  459. /* Nb of 16-bit data to program can be decreased */
  460. pFlash.DataRemaining--;
  461. /* Check if there are still 16-bit data to program */
  462. if(pFlash.DataRemaining != 0U)
  463. {
  464. /* Increment address to 16-bit */
  465. pFlash.Address += 2U;
  466. addresstmp = pFlash.Address;
  467. /* Shift to have next 16-bit data */
  468. pFlash.Data = (pFlash.Data >> 16U);
  469. /* Operation is completed, disable the PG Bit */
  470. CLEAR_BIT(FLASH->CR2, FLASH_CR2_PG);
  471. /*Program halfword (16-bit) at a specified address.*/
  472. FLASH_Program_HalfWord(addresstmp, (uint16_t)pFlash.Data);
  473. }
  474. else
  475. {
  476. /*Program ended. Return the selected address*/
  477. /* FLASH EOP interrupt user callback */
  478. if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMHALFWORD)
  479. {
  480. HAL_FLASH_EndOfOperationCallback(pFlash.Address);
  481. }
  482. else if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMWORD)
  483. {
  484. HAL_FLASH_EndOfOperationCallback(pFlash.Address-2U);
  485. }
  486. else
  487. {
  488. HAL_FLASH_EndOfOperationCallback(pFlash.Address-6U);
  489. }
  490. /* Reset Address and stop Program procedure*/
  491. pFlash.Address = 0xFFFFFFFFU;
  492. pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  493. }
  494. }
  495. }
  496. }
  497. #endif
  498. if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
  499. {
  500. #if defined(FLASH_BANK2_END)
  501. /* Operation is completed, disable the PG, PER and MER Bits for both bank */
  502. CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_PER | FLASH_CR_MER));
  503. CLEAR_BIT(FLASH->CR2, (FLASH_CR2_PG | FLASH_CR2_PER | FLASH_CR2_MER));
  504. /* Disable End of FLASH Operation and Error source interrupts for both banks */
  505. __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP_BANK1 | FLASH_IT_ERR_BANK1 | FLASH_IT_EOP_BANK2 | FLASH_IT_ERR_BANK2);
  506. #else
  507. /* Operation is completed, disable the PG, PER and MER Bits */
  508. CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_PER | FLASH_CR_MER));
  509. /* Disable End of FLASH Operation and Error source interrupts */
  510. __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
  511. #endif /* FLASH_BANK2_END */
  512. /* Process Unlocked */
  513. __HAL_UNLOCK(&pFlash);
  514. }
  515. }
  516. /**
  517. * @brief FLASH end of operation interrupt callback
  518. * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
  519. * - Mass Erase: No return value expected
  520. * - Pages Erase: Address of the page which has been erased
  521. * (if 0xFFFFFFFF, it means that all the selected pages have been erased)
  522. * - Program: Address which was selected for data program
  523. * @retval none
  524. */
  525. __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
  526. {
  527. /* Prevent unused argument(s) compilation warning */
  528. UNUSED(ReturnValue);
  529. /* NOTE : This function Should not be modified, when the callback is needed,
  530. the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
  531. */
  532. }
  533. /**
  534. * @brief FLASH operation error interrupt callback
  535. * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
  536. * - Mass Erase: No return value expected
  537. * - Pages Erase: Address of the page which returned an error
  538. * - Program: Address which was selected for data program
  539. * @retval none
  540. */
  541. __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
  542. {
  543. /* Prevent unused argument(s) compilation warning */
  544. UNUSED(ReturnValue);
  545. /* NOTE : This function Should not be modified, when the callback is needed,
  546. the HAL_FLASH_OperationErrorCallback could be implemented in the user file
  547. */
  548. }
  549. /**
  550. * @}
  551. */
  552. /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
  553. * @brief management functions
  554. *
  555. @verbatim
  556. ===============================================================================
  557. ##### Peripheral Control functions #####
  558. ===============================================================================
  559. [..]
  560. This subsection provides a set of functions allowing to control the FLASH
  561. memory operations.
  562. @endverbatim
  563. * @{
  564. */
  565. /**
  566. * @brief Unlock the FLASH control register access
  567. * @retval HAL Status
  568. */
  569. HAL_StatusTypeDef HAL_FLASH_Unlock(void)
  570. {
  571. HAL_StatusTypeDef status = HAL_OK;
  572. if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
  573. {
  574. /* Authorize the FLASH Registers access */
  575. WRITE_REG(FLASH->KEYR, FLASH_KEY1);
  576. WRITE_REG(FLASH->KEYR, FLASH_KEY2);
  577. /* Verify Flash is unlocked */
  578. if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
  579. {
  580. status = HAL_ERROR;
  581. }
  582. }
  583. #if defined(FLASH_BANK2_END)
  584. if(READ_BIT(FLASH->CR2, FLASH_CR2_LOCK) != RESET)
  585. {
  586. /* Authorize the FLASH BANK2 Registers access */
  587. WRITE_REG(FLASH->KEYR2, FLASH_KEY1);
  588. WRITE_REG(FLASH->KEYR2, FLASH_KEY2);
  589. /* Verify Flash BANK2 is unlocked */
  590. if(READ_BIT(FLASH->CR2, FLASH_CR2_LOCK) != RESET)
  591. {
  592. status = HAL_ERROR;
  593. }
  594. }
  595. #endif /* FLASH_BANK2_END */
  596. return status;
  597. }
  598. /**
  599. * @brief Locks the FLASH control register access
  600. * @retval HAL Status
  601. */
  602. HAL_StatusTypeDef HAL_FLASH_Lock(void)
  603. {
  604. /* Set the LOCK Bit to lock the FLASH Registers access */
  605. SET_BIT(FLASH->CR, FLASH_CR_LOCK);
  606. #if defined(FLASH_BANK2_END)
  607. /* Set the LOCK Bit to lock the FLASH BANK2 Registers access */
  608. SET_BIT(FLASH->CR2, FLASH_CR2_LOCK);
  609. #endif /* FLASH_BANK2_END */
  610. return HAL_OK;
  611. }
  612. /**
  613. * @brief Unlock the FLASH Option Control Registers access.
  614. * @retval HAL Status
  615. */
  616. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
  617. {
  618. if (HAL_IS_BIT_CLR(FLASH->CR, FLASH_CR_OPTWRE))
  619. {
  620. /* Authorizes the Option Byte register programming */
  621. WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);
  622. WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);
  623. }
  624. else
  625. {
  626. return HAL_ERROR;
  627. }
  628. return HAL_OK;
  629. }
  630. /**
  631. * @brief Lock the FLASH Option Control Registers access.
  632. * @retval HAL Status
  633. */
  634. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
  635. {
  636. /* Clear the OPTWRE Bit to lock the FLASH Option Byte Registers access */
  637. CLEAR_BIT(FLASH->CR, FLASH_CR_OPTWRE);
  638. return HAL_OK;
  639. }
  640. /**
  641. * @brief Launch the option byte loading.
  642. * @note This function will reset automatically the MCU.
  643. * @retval None
  644. */
  645. void HAL_FLASH_OB_Launch(void)
  646. {
  647. /* Initiates a system reset request to launch the option byte loading */
  648. HAL_NVIC_SystemReset();
  649. }
  650. /**
  651. * @}
  652. */
  653. /** @defgroup FLASH_Exported_Functions_Group3 Peripheral errors functions
  654. * @brief Peripheral errors functions
  655. *
  656. @verbatim
  657. ===============================================================================
  658. ##### Peripheral Errors functions #####
  659. ===============================================================================
  660. [..]
  661. This subsection permit to get in run-time errors of the FLASH peripheral.
  662. @endverbatim
  663. * @{
  664. */
  665. /**
  666. * @brief Get the specific FLASH error flag.
  667. * @retval FLASH_ErrorCode The returned value can be:
  668. * @ref FLASH_Error_Codes
  669. */
  670. uint32_t HAL_FLASH_GetError(void)
  671. {
  672. return pFlash.ErrorCode;
  673. }
  674. /**
  675. * @}
  676. */
  677. /**
  678. * @}
  679. */
  680. /** @addtogroup FLASH_Private_Functions
  681. * @{
  682. */
  683. /**
  684. * @brief Program a half-word (16-bit) at a specified address.
  685. * @param Address specify the address to be programmed.
  686. * @param Data specify the data to be programmed.
  687. * @retval None
  688. */
  689. static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
  690. {
  691. /* Clean the error context */
  692. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  693. #if defined(FLASH_BANK2_END)
  694. if(Address <= FLASH_BANK1_END)
  695. {
  696. #endif /* FLASH_BANK2_END */
  697. /* Proceed to program the new data */
  698. SET_BIT(FLASH->CR, FLASH_CR_PG);
  699. #if defined(FLASH_BANK2_END)
  700. }
  701. else
  702. {
  703. /* Proceed to program the new data */
  704. SET_BIT(FLASH->CR2, FLASH_CR2_PG);
  705. }
  706. #endif /* FLASH_BANK2_END */
  707. /* Write data in the address */
  708. *(__IO uint16_t*)Address = Data;
  709. }
  710. /**
  711. * @brief Wait for a FLASH operation to complete.
  712. * @param Timeout maximum flash operation timeout
  713. * @retval HAL Status
  714. */
  715. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
  716. {
  717. /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
  718. Even if the FLASH operation fails, the BUSY flag will be reset and an error
  719. flag will be set */
  720. uint32_t tickstart = HAL_GetTick();
  721. while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
  722. {
  723. if (Timeout != HAL_MAX_DELAY)
  724. {
  725. if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout))
  726. {
  727. return HAL_TIMEOUT;
  728. }
  729. }
  730. }
  731. /* Check FLASH End of Operation flag */
  732. if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
  733. {
  734. /* Clear FLASH End of Operation pending bit */
  735. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
  736. }
  737. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||
  738. __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) ||
  739. __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
  740. {
  741. /*Save the error code*/
  742. FLASH_SetErrorCode();
  743. return HAL_ERROR;
  744. }
  745. /* There is no error flag set */
  746. return HAL_OK;
  747. }
  748. #if defined(FLASH_BANK2_END)
  749. /**
  750. * @brief Wait for a FLASH BANK2 operation to complete.
  751. * @param Timeout maximum flash operation timeout
  752. * @retval HAL_StatusTypeDef HAL Status
  753. */
  754. HAL_StatusTypeDef FLASH_WaitForLastOperationBank2(uint32_t Timeout)
  755. {
  756. /* Wait for the FLASH BANK2 operation to complete by polling on BUSY flag to be reset.
  757. Even if the FLASH BANK2 operation fails, the BUSY flag will be reset and an error
  758. flag will be set */
  759. uint32_t tickstart = HAL_GetTick();
  760. while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY_BANK2))
  761. {
  762. if (Timeout != HAL_MAX_DELAY)
  763. {
  764. if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout))
  765. {
  766. return HAL_TIMEOUT;
  767. }
  768. }
  769. }
  770. /* Check FLASH End of Operation flag */
  771. if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP_BANK2))
  772. {
  773. /* Clear FLASH End of Operation pending bit */
  774. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP_BANK2);
  775. }
  776. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK2) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK2))
  777. {
  778. /*Save the error code*/
  779. FLASH_SetErrorCode();
  780. return HAL_ERROR;
  781. }
  782. /* If there is an error flag set */
  783. return HAL_OK;
  784. }
  785. #endif /* FLASH_BANK2_END */
  786. /**
  787. * @brief Set the specific FLASH error flag.
  788. * @retval None
  789. */
  790. static void FLASH_SetErrorCode(void)
  791. {
  792. uint32_t flags = 0U;
  793. #if defined(FLASH_BANK2_END)
  794. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK2))
  795. #else
  796. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
  797. #endif /* FLASH_BANK2_END */
  798. {
  799. pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
  800. #if defined(FLASH_BANK2_END)
  801. flags |= FLASH_FLAG_WRPERR | FLASH_FLAG_WRPERR_BANK2;
  802. #else
  803. flags |= FLASH_FLAG_WRPERR;
  804. #endif /* FLASH_BANK2_END */
  805. }
  806. #if defined(FLASH_BANK2_END)
  807. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK2))
  808. #else
  809. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
  810. #endif /* FLASH_BANK2_END */
  811. {
  812. pFlash.ErrorCode |= HAL_FLASH_ERROR_PROG;
  813. #if defined(FLASH_BANK2_END)
  814. flags |= FLASH_FLAG_PGERR | FLASH_FLAG_PGERR_BANK2;
  815. #else
  816. flags |= FLASH_FLAG_PGERR;
  817. #endif /* FLASH_BANK2_END */
  818. }
  819. if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
  820. {
  821. pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV;
  822. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
  823. }
  824. /* Clear FLASH error pending bits */
  825. __HAL_FLASH_CLEAR_FLAG(flags);
  826. }
  827. /**
  828. * @}
  829. */
  830. /**
  831. * @}
  832. */
  833. #endif /* HAL_FLASH_MODULE_ENABLED */
  834. /**
  835. * @}
  836. */