stm32f1xx_hal_flash.c 29 KB

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