stm32l1xx_hal_flash.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_flash.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 5-September-2014
  7. * @brief FLASH HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the internal FLASH memory:
  10. * + FLASH Interface configuration
  11. * + FLASH Memory Programming
  12. * + Interrupts and flags management
  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 prefetch.
  22. [..] The FLASH main features are:
  23. (+) Flash memory read operations
  24. (+) Flash memory program/erase operations
  25. (+) Read / write protections
  26. (+) Prefetch on I-Code
  27. (+) Option Bytes programming
  28. ##### How to use this driver #####
  29. ==============================================================================
  30. [..] This driver provides functions to configure and program the Flash
  31. memory of all STM32L1xx devices.
  32. (#) FLASH Memory Programming functions: this group includes all
  33. needed functions to erase and program the main memory:
  34. (++) Lock and Unlock the Flash interface.
  35. (++) Erase function: Erase Page.
  36. (++) Program functions: Fast Word and Half Page(should be
  37. executed from internal SRAM).
  38. (#) DATA EEPROM Programming functions: this group includes all
  39. needed functions to erase and program the DATA EEPROM memory:
  40. (++) Lock and Unlock the DATA EEPROM interface.
  41. (++) Erase function: Erase Byte, erase HalfWord, erase Word, erase
  42. Double Word (should be executed from internal SRAM).
  43. (++) Program functions: Fast Program Byte, Fast Program Half-Word,
  44. FastProgramWord, Program Byte, Program Half-Word,
  45. Program Word and Program Double-Word (should be executed
  46. from internal SRAM).
  47. (#) FLASH Option Bytes Programming functions: this group includes
  48. all needed functions to:
  49. (++) Lock and Unlock the Flash Option bytes.
  50. (++) Set/Reset the write protection.
  51. (++) Set the Read protection Level.
  52. (++) Set the BOR level.
  53. (++) Program the user option Bytes.
  54. (++) Launch the Option Bytes loader.
  55. (++) Get the Write protection.
  56. (++) Get the read protection status.
  57. (++) Get the BOR level.
  58. (++) Get the user option bytes.
  59. (#) Interrupts and flags management functions :
  60. (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
  61. (++) Wait for last FLASH operation according to its status
  62. (++) Get error flag status by calling HAL_GetErrorCode()
  63. (#) FLASH Interface configuration functions: this group includes
  64. the management of following features:
  65. (++) Enable/Disable the RUN PowerDown mode.
  66. (++) Enable/Disable the SLEEP PowerDown mode.
  67. (#) FLASH Peripheral State methods: this group includes
  68. the management of following features:
  69. (++) Wait for the FLASH operation
  70. (++) Get the specific FLASH error flag
  71. [..] In addition to these function, this driver includes a set of macros allowing
  72. to handle the following operations:
  73. (+) Set/Get the latency
  74. (+) Enable/Disable the prefetch buffer
  75. (+) Enable/Disable the 64 bit Read Access.
  76. (+) Enable/Disable the Flash power-down
  77. (+) Enable/Disable the FLASH interrupts
  78. (+) Monitor the FLASH flags status
  79. ===============================================================================
  80. ##### Programming operation functions #####
  81. ===============================================================================
  82. [..]
  83. This subsection provides a set of functions allowing to manage the FLASH
  84. program operations.
  85. [..] The FLASH Memory Programming functions, includes the following functions:
  86. (+) HAL_FLASH_Unlock(void);
  87. (+) HAL_FLASH_Lock(void);
  88. (+) HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
  89. (+) HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
  90. [..] Any operation of erase or program should follow these steps:
  91. (#) Call the HAL_FLASH_Unlock() function to enable the flash control register and
  92. program memory access.
  93. (#) Call the desired function to erase page or program data.
  94. (#) Call the HAL_FLASH_Lock() to disable the flash program memory access
  95. (recommended to protect the FLASH memory against possible unwanted operation).
  96. ==============================================================================
  97. ##### Option Bytes Programming functions #####
  98. ==============================================================================
  99. [..] The FLASH_Option Bytes Programming_functions, includes the following functions:
  100. (+) HAL_FLASH_OB_Unlock(void);
  101. (+) HAL_FLASH_OB_Lock(void);
  102. (+) HAL_FLASH_OB_Launch(void);
  103. (+) HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit);
  104. (+) HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit);
  105. [..] Any operation of erase or program should follow these steps:
  106. (#) Call the HAL_FLASH_OB_Unlock() function to enable the Flash option control
  107. register access.
  108. (#) Call the following functions to program the desired option bytes.
  109. (++) HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit);
  110. (#) Once all needed option bytes to be programmed are correctly written, call the
  111. HAL_FLASH_OB_Launch(void) function to launch the Option Bytes programming process.
  112. (#) Call the HAL_FLASH_OB_Lock() to disable the Flash option control register access (recommended
  113. to protect the option Bytes against possible unwanted operations).
  114. * @endverbatim
  115. *
  116. ******************************************************************************
  117. * @attention
  118. *
  119. * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  120. *
  121. * Redistribution and use in source and binary forms, with or without modification,
  122. * are permitted provided that the following conditions are met:
  123. * 1. Redistributions of source code must retain the above copyright notice,
  124. * this list of conditions and the following disclaimer.
  125. * 2. Redistributions in binary form must reproduce the above copyright notice,
  126. * this list of conditions and the following disclaimer in the documentation
  127. * and/or other materials provided with the distribution.
  128. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  129. * may be used to endorse or promote products derived from this software
  130. * without specific prior written permission.
  131. *
  132. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  133. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  134. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  135. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  136. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  137. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  138. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  139. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  140. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  141. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  142. *
  143. ******************************************************************************
  144. */
  145. /* Includes ------------------------------------------------------------------*/
  146. #include "stm32l1xx_hal.h"
  147. /** @addtogroup STM32L1xx_HAL_Driver
  148. * @{
  149. */
  150. #ifdef HAL_FLASH_MODULE_ENABLED
  151. /** @defgroup FLASH FLASH
  152. * @brief FLASH driver modules
  153. * @{
  154. */
  155. /* Private typedef -----------------------------------------------------------*/
  156. /* Private define ------------------------------------------------------------*/
  157. /* Private macro -------------------------------------------------------------*/
  158. /* Private variables ---------------------------------------------------------*/
  159. /** @defgroup FLASH_Internal_Variables FLASH Internal Variables
  160. * @{
  161. */
  162. /**
  163. * @brief Variable used for Program/Erase sectors under interruption
  164. */
  165. FLASH_ProcessTypeDef ProcFlash;
  166. /**
  167. * @}
  168. */
  169. /* Private function prototypes -----------------------------------------------*/
  170. /* Private functions ---------------------------------------------------------*/
  171. /** @defgroup FLASH_Exported_Functions FLASH Exported functions
  172. * @{
  173. */
  174. /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
  175. * @brief Programming operation functions
  176. *
  177. @verbatim
  178. @endverbatim
  179. * @{
  180. */
  181. /**
  182. * @brief Program word at a specified address
  183. * @note To correctly run this function, the HAL_FLASH_Unlock() function
  184. * must be called before.
  185. * Call the HAL_FLASH_Lock() to disable the flash memory access
  186. * (recommended to protect the FLASH memory against possible unwanted operation).
  187. * @param TypeProgram: Indicate the way to program at a specified address.
  188. * This parameter can be a value of @ref FLASH_Type_Program
  189. * @param Address: specifies the address to be programmed.
  190. * @param Data: specifies the data to be programmed
  191. *
  192. * @retval HAL_StatusTypeDef HAL Status
  193. */
  194. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  195. {
  196. HAL_StatusTypeDef status = HAL_ERROR;
  197. /* Process Locked */
  198. __HAL_LOCK(&ProcFlash);
  199. /* Check the parameters */
  200. assert_param(IS_TYPEPROGRAMFLASH(TypeProgram));
  201. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  202. /* Wait for last operation to be completed */
  203. status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
  204. if(status == HAL_OK)
  205. {
  206. /*Program word (32-bit) at a specified address.*/
  207. *(__IO uint32_t *)Address = Data;
  208. /* Wait for last operation to be completed */
  209. status = FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE);
  210. }
  211. /* Process Unlocked */
  212. __HAL_UNLOCK(&ProcFlash);
  213. return status;
  214. }
  215. /**
  216. * @brief Program word at a specified address with interrupt enabled.
  217. * @param TypeProgram: Indicate the way to program at a specified address.
  218. * This parameter can be a value of @ref FLASH_Type_Program
  219. * @param Address: specifies the address to be programmed.
  220. * @param Data: specifies the data to be programmed
  221. *
  222. * @retval HAL_StatusTypeDef HAL Status
  223. */
  224. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
  225. {
  226. HAL_StatusTypeDef status = HAL_OK;
  227. /* Process Locked */
  228. __HAL_LOCK(&ProcFlash);
  229. /* Check the parameters */
  230. assert_param(IS_TYPEPROGRAMFLASH(TypeProgram));
  231. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  232. /* Enable End of FLASH Operation interrupt */
  233. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
  234. /* Enable Error source interrupt */
  235. __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
  236. /* Clear pending flags (if any) */
  237. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_MASK);
  238. ProcFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
  239. ProcFlash.Address = Address;
  240. if(TypeProgram == TYPEPROGRAM_WORD)
  241. {
  242. /*Program word (32-bit) at a specified address.*/
  243. *(__IO uint32_t *)Address = Data;
  244. }
  245. /* Process Unlocked */
  246. __HAL_UNLOCK(&ProcFlash);
  247. return status;
  248. }
  249. /**
  250. * @brief FLASH end of operation interrupt callback
  251. * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
  252. * - Pages Erase: Sector which has been erased
  253. * (if 0xFFFFFFFF, it means that all the selected sectors have been erased)
  254. * - Program: Address which was selected for data program
  255. * @retval none
  256. */
  257. __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
  258. {
  259. /* NOTE : This function Should not be modified, when the callback is needed,
  260. the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
  261. */
  262. }
  263. /**
  264. * @brief FLASH operation error interrupt callback
  265. * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
  266. * - Pagess Erase: Sector number which returned an error
  267. * - Program: Address which was selected for data program
  268. * @retval none
  269. */
  270. __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
  271. {
  272. /* NOTE : This function Should not be modified, when the callback is needed,
  273. the HAL_FLASH_OperationErrorCallback could be implemented in the user file
  274. */
  275. }
  276. /**
  277. * @}
  278. */
  279. /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
  280. * @brief management functions
  281. *
  282. @verbatim
  283. ===============================================================================
  284. ##### Peripheral Control functions #####
  285. ===============================================================================
  286. [..]
  287. This subsection provides a set of functions allowing to control the FLASH
  288. memory operations.
  289. @endverbatim
  290. * @{
  291. */
  292. /**
  293. * @brief Unlock the FLASH control register access
  294. * @retval HAL_StatusTypeDef HAL Status
  295. */
  296. HAL_StatusTypeDef HAL_FLASH_Unlock(void)
  297. {
  298. if((FLASH->PECR & FLASH_PECR_PRGLOCK) != RESET)
  299. {
  300. /* Unlocking FLASH_PECR register access*/
  301. if((FLASH->PECR & FLASH_PECR_PELOCK) != RESET)
  302. {
  303. FLASH->PEKEYR = FLASH_PEKEY1;
  304. FLASH->PEKEYR = FLASH_PEKEY2;
  305. }
  306. /* Unlocking the program memory access */
  307. FLASH->PRGKEYR = FLASH_PRGKEY1;
  308. FLASH->PRGKEYR = FLASH_PRGKEY2;
  309. }
  310. else
  311. {
  312. return HAL_ERROR;
  313. }
  314. return HAL_OK;
  315. }
  316. /**
  317. * @brief Locks the FLASH control register access
  318. * @retval HAL_StatusTypeDef HAL Status
  319. */
  320. HAL_StatusTypeDef HAL_FLASH_Lock(void)
  321. {
  322. /* Set the PRGLOCK Bit to lock the program memory access */
  323. SET_BIT(FLASH->PECR, FLASH_PECR_PRGLOCK);
  324. return HAL_OK;
  325. }
  326. /**
  327. * @brief Unlock the FLASH Option Control Registers access.
  328. * @retval HAL_StatusTypeDef HAL Status
  329. */
  330. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
  331. {
  332. if((FLASH->PECR & FLASH_PECR_OPTLOCK) != RESET)
  333. {
  334. /* Unlocking FLASH_PECR register access*/
  335. if((FLASH->PECR & FLASH_PECR_PELOCK) != RESET)
  336. {
  337. /* Unlocking FLASH_PECR register access*/
  338. FLASH->PEKEYR = FLASH_PEKEY1;
  339. FLASH->PEKEYR = FLASH_PEKEY2;
  340. }
  341. /* Unlocking the option bytes block access */
  342. FLASH->OPTKEYR = FLASH_OPTKEY1;
  343. FLASH->OPTKEYR = FLASH_OPTKEY2;
  344. }
  345. else
  346. {
  347. return HAL_ERROR;
  348. }
  349. return HAL_OK;
  350. }
  351. /**
  352. * @brief Lock the FLASH Option Control Registers access.
  353. * @retval HAL_StatusTypeDef HAL Status
  354. */
  355. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
  356. {
  357. /* Set the OPTLOCK Bit to lock the option bytes block access */
  358. SET_BIT(FLASH->PECR, FLASH_PECR_OPTLOCK);
  359. return HAL_OK;
  360. }
  361. /**
  362. * @brief Launch the option byte loading.
  363. * @retval HAL_StatusTypeDef HAL Status
  364. */
  365. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
  366. {
  367. /* Set the OBL_Launch bit to lauch the option byte loading */
  368. SET_BIT(FLASH->PECR, FLASH_PECR_OBL_LAUNCH);
  369. /* Wait for last operation to be completed */
  370. return(FLASH_WaitForLastOperation((uint32_t)HAL_FLASH_TIMEOUT_VALUE));
  371. }
  372. /**
  373. * @}
  374. */
  375. /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
  376. * @brief Peripheral Errors functions
  377. *
  378. @verbatim
  379. ===============================================================================
  380. ##### Peripheral Errors functions #####
  381. ===============================================================================
  382. [..]
  383. This subsection permit to get in run-time Errors of the FLASH peripheral.
  384. @endverbatim
  385. * @{
  386. */
  387. /**
  388. * @brief Get the specific FLASH error flag.
  389. * @retval FLASH_ErrorCode: The returned value can be:
  390. * @arg FLASH_ERROR_WRP: FLASH Write protected error flag
  391. * @arg FLASH_ERROR_PGA: FLASH Programming Alignment error flag
  392. * @arg FLASH_ERROR_SIZE: FLASH Size error flag
  393. * @arg FLASH_ERROR_OPTV: Option validity error flag
  394. * @arg FLASH_ERROR_OPTVUSR: Option UserValidity Error flag (available only Cat.3, Cat.4 and Cat.5 devices)
  395. * @arg FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP) (available only Cat.2 and Cat.3 devices)
  396. */
  397. FLASH_ErrorTypeDef HAL_FLASH_GetError(void)
  398. {
  399. return ProcFlash.ErrorCode;
  400. }
  401. /**
  402. * @}
  403. */
  404. /**
  405. * @}
  406. */
  407. #endif /* HAL_FLASH_MODULE_ENABLED */
  408. /**
  409. * @}
  410. */
  411. /**
  412. * @}
  413. */
  414. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/