stm32f1xx_hal_flash.c 30 KB

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