Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1874 lines
65 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_flash_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended FLASH HAL module driver.
  6. *
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the internal FLASH memory:
  9. * + FLASH Interface configuration
  10. * + FLASH Memory Erasing
  11. * + DATA EEPROM Programming/Erasing
  12. * + Option Bytes Programming
  13. * + Interrupts management
  14. *
  15. @verbatim
  16. ==============================================================================
  17. ##### Flash peripheral Extended features #####
  18. ==============================================================================
  19. [..] Comparing to other products, the FLASH interface for STM32L1xx
  20. devices contains the following additional features
  21. (+) Erase functions
  22. (+) DATA_EEPROM memory management
  23. (+) BOOT option bit configuration
  24. (+) PCROP protection for all sectors
  25. ##### How to use this driver #####
  26. ==============================================================================
  27. [..] This driver provides functions to configure and program the FLASH memory
  28. of all STM32L1xx. It includes:
  29. (+) Full DATA_EEPROM erase and program management
  30. (+) Boot activation
  31. (+) PCROP protection configuration and control for all pages
  32. @endverbatim
  33. ******************************************************************************
  34. * @attention
  35. *
  36. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  37. * All rights reserved.</center></h2>
  38. *
  39. * This software component is licensed by ST under BSD 3-Clause license,
  40. * the "License"; You may not use this file except in compliance with the
  41. * License. You may obtain a copy of the License at:
  42. * opensource.org/licenses/BSD-3-Clause
  43. *
  44. ******************************************************************************
  45. */
  46. /* Includes ------------------------------------------------------------------*/
  47. #include "stm32l1xx_hal.h"
  48. /** @addtogroup STM32L1xx_HAL_Driver
  49. * @{
  50. */
  51. #ifdef HAL_FLASH_MODULE_ENABLED
  52. /** @addtogroup FLASH
  53. * @{
  54. */
  55. /** @addtogroup FLASH_Private_Variables
  56. * @{
  57. */
  58. /* Variables used for Erase pages under interruption*/
  59. extern FLASH_ProcessTypeDef pFlash;
  60. /**
  61. * @}
  62. */
  63. /**
  64. * @}
  65. */
  66. /** @defgroup FLASHEx FLASHEx
  67. * @brief FLASH HAL Extension module driver
  68. * @{
  69. */
  70. /* Private typedef -----------------------------------------------------------*/
  71. /* Private define ------------------------------------------------------------*/
  72. /** @defgroup FLASHEx_Private_Constants FLASHEx Private Constants
  73. * @{
  74. */
  75. /**
  76. * @}
  77. */
  78. /* Private macro -------------------------------------------------------------*/
  79. /** @defgroup FLASHEx_Private_Macros FLASHEx Private Macros
  80. * @{
  81. */
  82. /**
  83. * @}
  84. */
  85. /* Private variables ---------------------------------------------------------*/
  86. /* Private function prototypes -----------------------------------------------*/
  87. /** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions
  88. * @{
  89. */
  90. void FLASH_PageErase(uint32_t PageAddress);
  91. static HAL_StatusTypeDef FLASH_OB_WRPConfig(FLASH_OBProgramInitTypeDef *pOBInit, FunctionalState NewState);
  92. static void FLASH_OB_WRPConfigWRP1OrPCROP1(uint32_t WRP1OrPCROP1, FunctionalState NewState);
  93. #if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC) \
  94. || defined(STM32L151xCA) || defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xCA) \
  95. || defined(STM32L152xD) || defined(STM32L152xDX) || defined(STM32L162xCA) || defined(STM32L162xD) \
  96. || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE)
  97. static void FLASH_OB_WRPConfigWRP2OrPCROP2(uint32_t WRP2OrPCROP2, FunctionalState NewState);
  98. #endif /* STM32L100xC || STM32L151xC || STM32L152xC || (...) || STM32L151xE || STM32L152xE || STM32L162xE */
  99. #if defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xD) || defined(STM32L152xDX) \
  100. || defined(STM32L162xD) || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) \
  101. || defined(STM32L162xE)
  102. static void FLASH_OB_WRPConfigWRP3(uint32_t WRP3, FunctionalState NewState);
  103. #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
  104. #if defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE) || defined(STM32L151xDX) \
  105. || defined(STM32L152xDX) || defined(STM32L162xDX)
  106. static void FLASH_OB_WRPConfigWRP4(uint32_t WRP4, FunctionalState NewState);
  107. #endif /* STM32L151xE || STM32L152xE || STM32L151xDX || ... */
  108. #if defined(FLASH_OBR_SPRMOD)
  109. static HAL_StatusTypeDef FLASH_OB_PCROPConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit, FunctionalState NewState);
  110. #endif /* FLASH_OBR_SPRMOD */
  111. #if defined(FLASH_OBR_nRST_BFB2)
  112. static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t OB_BOOT);
  113. #endif /* FLASH_OBR_nRST_BFB2 */
  114. static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint8_t OB_RDP);
  115. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
  116. static HAL_StatusTypeDef FLASH_OB_BORConfig(uint8_t OB_BOR);
  117. static uint8_t FLASH_OB_GetRDP(void);
  118. static uint8_t FLASH_OB_GetUser(void);
  119. static uint8_t FLASH_OB_GetBOR(void);
  120. static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramByte(uint32_t Address, uint8_t Data);
  121. static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramHalfWord(uint32_t Address, uint16_t Data);
  122. static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramWord(uint32_t Address, uint32_t Data);
  123. static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramWord(uint32_t Address, uint32_t Data);
  124. static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramHalfWord(uint32_t Address, uint16_t Data);
  125. static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramByte(uint32_t Address, uint8_t Data);
  126. /**
  127. * @}
  128. */
  129. /* Exported functions ---------------------------------------------------------*/
  130. /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions
  131. * @{
  132. */
  133. /** @defgroup FLASHEx_Exported_Functions_Group1 FLASHEx Memory Erasing functions
  134. * @brief FLASH Memory Erasing functions
  135. *
  136. @verbatim
  137. ==============================================================================
  138. ##### FLASH Erasing Programming functions #####
  139. ==============================================================================
  140. [..] The FLASH Memory Erasing functions, includes the following functions:
  141. (+) @ref HAL_FLASHEx_Erase: return only when erase has been done
  142. (+) @ref HAL_FLASHEx_Erase_IT: end of erase is done when @ref HAL_FLASH_EndOfOperationCallback
  143. is called with parameter 0xFFFFFFFF
  144. [..] Any operation of erase should follow these steps:
  145. (#) Call the @ref HAL_FLASH_Unlock() function to enable the flash control register and
  146. program memory access.
  147. (#) Call the desired function to erase page.
  148. (#) Call the @ref HAL_FLASH_Lock() to disable the flash program memory access
  149. (recommended to protect the FLASH memory against possible unwanted operation).
  150. @endverbatim
  151. * @{
  152. */
  153. /**
  154. * @brief Erase the specified FLASH memory Pages
  155. * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
  156. * must be called before.
  157. * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
  158. * (recommended to protect the FLASH memory against possible unwanted operation)
  159. * @note For STM32L151xDX/STM32L152xDX/STM32L162xDX, as memory is not continuous between
  160. * 2 banks, user should perform pages erase by bank only.
  161. * @param[in] pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  162. * contains the configuration information for the erasing.
  163. *
  164. * @param[out] PageError pointer to variable that
  165. * contains the configuration information on faulty page in case of error
  166. * (0xFFFFFFFF means that all the pages have been correctly erased)
  167. *
  168. * @retval HAL_StatusTypeDef HAL Status
  169. */
  170. HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError)
  171. {
  172. HAL_StatusTypeDef status = HAL_ERROR;
  173. uint32_t address = 0U;
  174. /* Process Locked */
  175. __HAL_LOCK(&pFlash);
  176. /* Wait for last operation to be completed */
  177. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  178. if (status == HAL_OK)
  179. {
  180. /*Initialization of PageError variable*/
  181. *PageError = 0xFFFFFFFFU;
  182. /* Check the parameters */
  183. assert_param(IS_NBPAGES(pEraseInit->NbPages));
  184. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  185. assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
  186. assert_param(IS_FLASH_PROGRAM_ADDRESS((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1U)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1U));
  187. #if defined(STM32L151xDX) || defined(STM32L152xDX) || defined(STM32L162xDX)
  188. /* Check on which bank belongs the 1st address to erase */
  189. if (pEraseInit->PageAddress < FLASH_BANK2_BASE)
  190. {
  191. /* BANK1 */
  192. /* Check that last page to erase still belongs to BANK1 */
  193. if (((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1U)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1U) > FLASH_BANK1_END)
  194. {
  195. /* Last page does not belong to BANK1, erase procedure cannot be performed because memory is not
  196. continuous */
  197. /* Process Unlocked */
  198. __HAL_UNLOCK(&pFlash);
  199. return HAL_ERROR;
  200. }
  201. }
  202. else
  203. {
  204. /* BANK2 */
  205. /* Check that last page to erase still belongs to BANK2 */
  206. if (((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1U)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1U) > FLASH_BANK2_END)
  207. {
  208. /* Last page does not belong to BANK2, erase procedure cannot be performed because memory is not
  209. continuous */
  210. /* Process Unlocked */
  211. __HAL_UNLOCK(&pFlash);
  212. return HAL_ERROR;
  213. }
  214. }
  215. #endif /* STM32L151xDX || STM32L152xDX || STM32L162xDX */
  216. /* Erase page by page to be done*/
  217. for(address = pEraseInit->PageAddress;
  218. address < ((pEraseInit->NbPages * FLASH_PAGE_SIZE) + pEraseInit->PageAddress);
  219. address += FLASH_PAGE_SIZE)
  220. {
  221. FLASH_PageErase(address);
  222. /* Wait for last operation to be completed */
  223. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  224. /* If the erase operation is completed, disable the ERASE Bit */
  225. CLEAR_BIT(FLASH->PECR, FLASH_PECR_PROG);
  226. CLEAR_BIT(FLASH->PECR, FLASH_PECR_ERASE);
  227. if (status != HAL_OK)
  228. {
  229. /* In case of error, stop erase procedure and return the faulty address */
  230. *PageError = address;
  231. break;
  232. }
  233. }
  234. }
  235. /* Process Unlocked */
  236. __HAL_UNLOCK(&pFlash);
  237. return status;
  238. }
  239. /**
  240. * @brief Perform a page erase of the specified FLASH memory pages with interrupt enabled
  241. * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function
  242. * must be called before.
  243. * Call the @ref HAL_FLASH_Lock() to disable the flash memory access
  244. * (recommended to protect the FLASH memory against possible unwanted operation)
  245. * End of erase is done when @ref HAL_FLASH_EndOfOperationCallback is called with parameter
  246. * 0xFFFFFFFF
  247. * @note For STM32L151xDX/STM32L152xDX/STM32L162xDX, as memory is not continuous between
  248. * 2 banks, user should perform pages erase by bank only.
  249. * @param pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  250. * contains the configuration information for the erasing.
  251. *
  252. * @retval HAL_StatusTypeDef HAL Status
  253. */
  254. HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
  255. {
  256. HAL_StatusTypeDef status = HAL_ERROR;
  257. /* If procedure already ongoing, reject the next one */
  258. if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
  259. {
  260. return HAL_ERROR;
  261. }
  262. /* Check the parameters */
  263. assert_param(IS_NBPAGES(pEraseInit->NbPages));
  264. assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  265. assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress));
  266. assert_param(IS_FLASH_PROGRAM_ADDRESS((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1U)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1U));
  267. /* Process Locked */
  268. __HAL_LOCK(&pFlash);
  269. #if defined(STM32L151xDX) || defined(STM32L152xDX) || defined(STM32L162xDX)
  270. /* Check on which bank belongs the 1st address to erase */
  271. if (pEraseInit->PageAddress < FLASH_BANK2_BASE)
  272. {
  273. /* BANK1 */
  274. /* Check that last page to erase still belongs to BANK1 */
  275. if (((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1U)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1U) > FLASH_BANK1_END)
  276. {
  277. /* Last page does not belong to BANK1, erase procedure cannot be performed because memory is not
  278. continuous */
  279. /* Process Unlocked */
  280. __HAL_UNLOCK(&pFlash);
  281. return HAL_ERROR;
  282. }
  283. }
  284. else
  285. {
  286. /* BANK2 */
  287. /* Check that last page to erase still belongs to BANK2 */
  288. if (((pEraseInit->PageAddress & ~(FLASH_PAGE_SIZE - 1U)) + pEraseInit->NbPages * FLASH_PAGE_SIZE - 1U) > FLASH_BANK2_END)
  289. {
  290. /* Last page does not belong to BANK2, erase procedure cannot be performed because memory is not
  291. continuous */
  292. /* Process Unlocked */
  293. __HAL_UNLOCK(&pFlash);
  294. return HAL_ERROR;
  295. }
  296. }
  297. #endif /* STM32L151xDX || STM32L152xDX || STM32L162xDX */
  298. /* Wait for last operation to be completed */
  299. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  300. if (status == HAL_OK)
  301. {
  302. /* Enable End of FLASH Operation and Error source interrupts */
  303. __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
  304. pFlash.ProcedureOnGoing = FLASH_PROC_PAGEERASE;
  305. pFlash.NbPagesToErase = pEraseInit->NbPages;
  306. pFlash.Page = pEraseInit->PageAddress;
  307. /*Erase 1st page and wait for IT*/
  308. FLASH_PageErase(pEraseInit->PageAddress);
  309. }
  310. else
  311. {
  312. /* Process Unlocked */
  313. __HAL_UNLOCK(&pFlash);
  314. }
  315. return status;
  316. }
  317. /**
  318. * @}
  319. */
  320. /** @defgroup FLASHEx_Exported_Functions_Group2 Option Bytes Programming functions
  321. * @brief Option Bytes Programming functions
  322. *
  323. @verbatim
  324. ==============================================================================
  325. ##### Option Bytes Programming functions #####
  326. ==============================================================================
  327. [..] Any operation of erase or program should follow these steps:
  328. (#) Call the @ref HAL_FLASH_OB_Unlock() function to enable the Flash option control
  329. register access.
  330. (#) Call following function to program the desired option bytes.
  331. (++) @ref HAL_FLASHEx_OBProgram:
  332. - To Enable/Disable the desired sector write protection.
  333. - To set the desired read Protection Level.
  334. - To configure the user option Bytes: IWDG, STOP and the Standby.
  335. - To Set the BOR level.
  336. (#) Once all needed option bytes to be programmed are correctly written, call the
  337. @ref HAL_FLASH_OB_Launch(void) function to launch the Option Bytes programming process.
  338. (#) Call the @ref HAL_FLASH_OB_Lock() to disable the Flash option control register access (recommended
  339. to protect the option Bytes against possible unwanted operations).
  340. [..] Proprietary code Read Out Protection (PcROP):
  341. (#) The PcROP sector is selected by using the same option bytes as the Write
  342. protection (nWRPi bits). As a result, these 2 options are exclusive each other.
  343. (#) In order to activate the PcROP (change the function of the nWRPi option bits),
  344. the SPRMOD option bit must be activated.
  345. (#) The active value of nWRPi bits is inverted when PCROP mode is active, this
  346. means: if SPRMOD = 1 and nWRPi = 1 (default value), then the user sector "i"
  347. is read/write protected.
  348. (#) To activate PCROP mode for Flash sector(s), you need to call the following function:
  349. (++) @ref HAL_FLASHEx_AdvOBProgram in selecting sectors to be read/write protected
  350. (++) @ref HAL_FLASHEx_OB_SelectPCROP to enable the read/write protection
  351. (#) PcROP is available only in STM32L151xBA, STM32L152xBA, STM32L151xC, STM32L152xC & STM32L162xC devices.
  352. @endverbatim
  353. * @{
  354. */
  355. /**
  356. * @brief Program option bytes
  357. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  358. * contains the configuration information for the programming.
  359. *
  360. * @retval HAL_StatusTypeDef HAL Status
  361. */
  362. HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit)
  363. {
  364. HAL_StatusTypeDef status = HAL_ERROR;
  365. /* Process Locked */
  366. __HAL_LOCK(&pFlash);
  367. /* Check the parameters */
  368. assert_param(IS_OPTIONBYTE(pOBInit->OptionType));
  369. /*Write protection configuration*/
  370. if((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP)
  371. {
  372. assert_param(IS_WRPSTATE(pOBInit->WRPState));
  373. if (pOBInit->WRPState == OB_WRPSTATE_ENABLE)
  374. {
  375. /* Enable of Write protection on the selected Sector*/
  376. status = FLASH_OB_WRPConfig(pOBInit, ENABLE);
  377. }
  378. else
  379. {
  380. /* Disable of Write protection on the selected Sector*/
  381. status = FLASH_OB_WRPConfig(pOBInit, DISABLE);
  382. }
  383. if (status != HAL_OK)
  384. {
  385. /* Process Unlocked */
  386. __HAL_UNLOCK(&pFlash);
  387. return status;
  388. }
  389. }
  390. /* Read protection configuration*/
  391. if((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP)
  392. {
  393. status = FLASH_OB_RDPConfig(pOBInit->RDPLevel);
  394. if (status != HAL_OK)
  395. {
  396. /* Process Unlocked */
  397. __HAL_UNLOCK(&pFlash);
  398. return status;
  399. }
  400. }
  401. /* USER configuration*/
  402. if((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER)
  403. {
  404. status = FLASH_OB_UserConfig(pOBInit->USERConfig & OB_IWDG_SW,
  405. pOBInit->USERConfig & OB_STOP_NORST,
  406. pOBInit->USERConfig & OB_STDBY_NORST);
  407. if (status != HAL_OK)
  408. {
  409. /* Process Unlocked */
  410. __HAL_UNLOCK(&pFlash);
  411. return status;
  412. }
  413. }
  414. /* BOR Level configuration*/
  415. if((pOBInit->OptionType & OPTIONBYTE_BOR) == OPTIONBYTE_BOR)
  416. {
  417. status = FLASH_OB_BORConfig(pOBInit->BORLevel);
  418. if (status != HAL_OK)
  419. {
  420. /* Process Unlocked */
  421. __HAL_UNLOCK(&pFlash);
  422. return status;
  423. }
  424. }
  425. /* Process Unlocked */
  426. __HAL_UNLOCK(&pFlash);
  427. return status;
  428. }
  429. /**
  430. * @brief Get the Option byte configuration
  431. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  432. * contains the configuration information for the programming.
  433. *
  434. * @retval None
  435. */
  436. void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
  437. {
  438. pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_BOR;
  439. /*Get WRP1*/
  440. pOBInit->WRPSector0To31 = (uint32_t)(FLASH->WRPR1);
  441. #if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC) \
  442. || defined(STM32L151xCA) || defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xCA) \
  443. || defined(STM32L152xD) || defined(STM32L152xDX) || defined(STM32L162xCA) || defined(STM32L162xD) \
  444. || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE)
  445. /*Get WRP2*/
  446. pOBInit->WRPSector32To63 = (uint32_t)(FLASH->WRPR2);
  447. #endif /* STM32L100xC || STM32L151xC || STM32L152xC || (...) || STM32L151xE || STM32L152xE || STM32L162xE */
  448. #if defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xD) || defined(STM32L152xDX) \
  449. || defined(STM32L162xD) || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) \
  450. || defined(STM32L162xE)
  451. /*Get WRP3*/
  452. pOBInit->WRPSector64To95 = (uint32_t)(FLASH->WRPR3);
  453. #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
  454. #if defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE) || defined(STM32L151xDX) \
  455. || defined(STM32L152xDX) || defined(STM32L162xDX)
  456. /*Get WRP4*/
  457. pOBInit->WRPSector96To127 = (uint32_t)(FLASH->WRPR4);
  458. #endif /* STM32L151xE || STM32L152xE || STM32L162xE || STM32L151xDX || ... */
  459. /*Get RDP Level*/
  460. pOBInit->RDPLevel = FLASH_OB_GetRDP();
  461. /*Get USER*/
  462. pOBInit->USERConfig = FLASH_OB_GetUser();
  463. /*Get BOR Level*/
  464. pOBInit->BORLevel = FLASH_OB_GetBOR();
  465. }
  466. #if defined(FLASH_OBR_SPRMOD) || defined(FLASH_OBR_nRST_BFB2)
  467. /**
  468. * @brief Program option bytes
  469. * @note This function can be used only for Cat2 & Cat3 devices for PCROP and Cat4 & Cat5 for BFB2.
  470. * @param pAdvOBInit pointer to an FLASH_AdvOBProgramInitTypeDef structure that
  471. * contains the configuration information for the programming.
  472. *
  473. * @retval HAL_StatusTypeDef HAL Status
  474. */
  475. HAL_StatusTypeDef HAL_FLASHEx_AdvOBProgram (FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
  476. {
  477. HAL_StatusTypeDef status = HAL_ERROR;
  478. /* Check the parameters */
  479. assert_param(IS_OBEX(pAdvOBInit->OptionType));
  480. #if defined(FLASH_OBR_SPRMOD)
  481. /* Program PCROP option byte*/
  482. if ((pAdvOBInit->OptionType & OPTIONBYTE_PCROP) == OPTIONBYTE_PCROP)
  483. {
  484. /* Check the parameters */
  485. assert_param(IS_PCROPSTATE(pAdvOBInit->PCROPState));
  486. if (pAdvOBInit->PCROPState == OB_PCROP_STATE_ENABLE)
  487. {
  488. /*Enable of Write protection on the selected Sector*/
  489. status = FLASH_OB_PCROPConfig(pAdvOBInit, ENABLE);
  490. if (status != HAL_OK)
  491. {
  492. return status;
  493. }
  494. }
  495. else
  496. {
  497. /* Disable of Write protection on the selected Sector*/
  498. status = FLASH_OB_PCROPConfig(pAdvOBInit, DISABLE);
  499. if (status != HAL_OK)
  500. {
  501. return status;
  502. }
  503. }
  504. }
  505. #endif /* FLASH_OBR_SPRMOD */
  506. #if defined(FLASH_OBR_nRST_BFB2)
  507. /* Program BOOT config option byte */
  508. if ((pAdvOBInit->OptionType & OPTIONBYTE_BOOTCONFIG) == OPTIONBYTE_BOOTCONFIG)
  509. {
  510. status = FLASH_OB_BootConfig(pAdvOBInit->BootConfig);
  511. }
  512. #endif /* FLASH_OBR_nRST_BFB2 */
  513. return status;
  514. }
  515. /**
  516. * @brief Get the OBEX byte configuration
  517. * @note This function can be used only for Cat2 & Cat3 devices for PCROP and Cat4 & Cat5 for BFB2.
  518. * @param pAdvOBInit pointer to an FLASH_AdvOBProgramInitTypeDef structure that
  519. * contains the configuration information for the programming.
  520. *
  521. * @retval None
  522. */
  523. void HAL_FLASHEx_AdvOBGetConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit)
  524. {
  525. pAdvOBInit->OptionType = 0U;
  526. #if defined(FLASH_OBR_SPRMOD)
  527. pAdvOBInit->OptionType |= OPTIONBYTE_PCROP;
  528. /*Get PCROP state */
  529. pAdvOBInit->PCROPState = (FLASH->OBR & FLASH_OBR_SPRMOD) >> POSITION_VAL(FLASH_OBR_SPRMOD);
  530. /*Get PCROP protected sector from 0 to 31 */
  531. pAdvOBInit->PCROPSector0To31 = FLASH->WRPR1;
  532. #if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC)
  533. /*Get PCROP protected sector from 32 to 63 */
  534. pAdvOBInit->PCROPSector32To63 = FLASH->WRPR2;
  535. #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC */
  536. #endif /* FLASH_OBR_SPRMOD */
  537. #if defined(FLASH_OBR_nRST_BFB2)
  538. pAdvOBInit->OptionType |= OPTIONBYTE_BOOTCONFIG;
  539. /* Get Boot config OB */
  540. pAdvOBInit->BootConfig = (FLASH->OBR & FLASH_OBR_nRST_BFB2) >> 16U;
  541. #endif /* FLASH_OBR_nRST_BFB2 */
  542. }
  543. #endif /* FLASH_OBR_SPRMOD || FLASH_OBR_nRST_BFB2 */
  544. #if defined(FLASH_OBR_SPRMOD)
  545. /**
  546. * @brief Select the Protection Mode (SPRMOD).
  547. * @note This function can be used only for STM32L151xBA, STM32L152xBA, STM32L151xC, STM32L152xC & STM32L162xC devices
  548. * @note Once SPRMOD bit is active, unprotection of a protected sector is not possible
  549. * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
  550. * @retval HAL status
  551. */
  552. HAL_StatusTypeDef HAL_FLASHEx_OB_SelectPCROP(void)
  553. {
  554. HAL_StatusTypeDef status = HAL_OK;
  555. uint16_t tmp1 = 0U;
  556. uint32_t tmp2 = 0U;
  557. uint8_t optiontmp = 0U;
  558. uint16_t optiontmp2 = 0U;
  559. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  560. /* Mask RDP Byte */
  561. optiontmp = (uint8_t)(*(__IO uint8_t *)(OB_BASE));
  562. /* Update Option Byte */
  563. optiontmp2 = (uint16_t)(OB_PCROP_SELECTED | optiontmp);
  564. /* calculate the option byte to write */
  565. tmp1 = (uint16_t)(~(optiontmp2 ));
  566. tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16U)) | ((uint32_t)optiontmp2));
  567. if(status == HAL_OK)
  568. {
  569. /* Clean the error context */
  570. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  571. /* program PCRop */
  572. OB->RDP = tmp2;
  573. /* Wait for last operation to be completed */
  574. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  575. }
  576. /* Return the Read protection operation Status */
  577. return status;
  578. }
  579. /**
  580. * @brief Deselect the Protection Mode (SPRMOD).
  581. * @note This function can be used only for STM32L151xBA, STM32L152xBA, STM32L151xC, STM32L152xC & STM32L162xC devices
  582. * @note Once SPRMOD bit is active, unprotection of a protected sector is not possible
  583. * @note Read a protected sector will set RDERR Flag and write a protected sector will set WRPERR Flag
  584. * @retval HAL status
  585. */
  586. HAL_StatusTypeDef HAL_FLASHEx_OB_DeSelectPCROP(void)
  587. {
  588. HAL_StatusTypeDef status = HAL_OK;
  589. uint16_t tmp1 = 0U;
  590. uint32_t tmp2 = 0U;
  591. uint8_t optiontmp = 0U;
  592. uint16_t optiontmp2 = 0U;
  593. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  594. /* Mask RDP Byte */
  595. optiontmp = (uint8_t)(*(__IO uint8_t *)(OB_BASE));
  596. /* Update Option Byte */
  597. optiontmp2 = (uint16_t)(OB_PCROP_DESELECTED | optiontmp);
  598. /* calculate the option byte to write */
  599. tmp1 = (uint16_t)(~(optiontmp2 ));
  600. tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16U)) | ((uint32_t)optiontmp2));
  601. if(status == HAL_OK)
  602. {
  603. /* Clean the error context */
  604. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  605. /* program PCRop */
  606. OB->RDP = tmp2;
  607. /* Wait for last operation to be completed */
  608. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  609. }
  610. /* Return the Read protection operation Status */
  611. return status;
  612. }
  613. #endif /* FLASH_OBR_SPRMOD */
  614. /**
  615. * @}
  616. */
  617. /** @defgroup FLASHEx_Exported_Functions_Group3 DATA EEPROM Programming functions
  618. * @brief DATA EEPROM Programming functions
  619. *
  620. @verbatim
  621. ===============================================================================
  622. ##### DATA EEPROM Programming functions #####
  623. ===============================================================================
  624. [..] Any operation of erase or program should follow these steps:
  625. (#) Call the @ref HAL_FLASHEx_DATAEEPROM_Unlock() function to enable the data EEPROM access
  626. and Flash program erase control register access.
  627. (#) Call the desired function to erase or program data.
  628. (#) Call the @ref HAL_FLASHEx_DATAEEPROM_Lock() to disable the data EEPROM access
  629. and Flash program erase control register access(recommended
  630. to protect the DATA_EEPROM against possible unwanted operation).
  631. @endverbatim
  632. * @{
  633. */
  634. /**
  635. * @brief Unlocks the data memory and FLASH_PECR register access.
  636. * @retval HAL_StatusTypeDef HAL Status
  637. */
  638. HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Unlock(void)
  639. {
  640. if((FLASH->PECR & FLASH_PECR_PELOCK) != RESET)
  641. {
  642. /* Unlocking the Data memory and FLASH_PECR register access*/
  643. FLASH->PEKEYR = FLASH_PEKEY1;
  644. FLASH->PEKEYR = FLASH_PEKEY2;
  645. }
  646. else
  647. {
  648. return HAL_ERROR;
  649. }
  650. return HAL_OK;
  651. }
  652. /**
  653. * @brief Locks the Data memory and FLASH_PECR register access.
  654. * @retval HAL_StatusTypeDef HAL Status
  655. */
  656. HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Lock(void)
  657. {
  658. /* Set the PELOCK Bit to lock the data memory and FLASH_PECR register access */
  659. SET_BIT(FLASH->PECR, FLASH_PECR_PELOCK);
  660. return HAL_OK;
  661. }
  662. /**
  663. * @brief Erase a word in data memory.
  664. * @param Address specifies the address to be erased.
  665. * @param TypeErase Indicate the way to erase at a specified address.
  666. * This parameter can be a value of @ref FLASH_Type_Program
  667. * @note To correctly run this function, the @ref HAL_FLASHEx_DATAEEPROM_Unlock() function
  668. * must be called before.
  669. * Call the @ref HAL_FLASHEx_DATAEEPROM_Lock() to the data EEPROM access
  670. * and Flash program erase control register access(recommended to protect
  671. * the DATA_EEPROM against possible unwanted operation).
  672. * @retval HAL_StatusTypeDef HAL Status
  673. */
  674. HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Erase(uint32_t TypeErase, uint32_t Address)
  675. {
  676. HAL_StatusTypeDef status = HAL_OK;
  677. /* Check the parameters */
  678. assert_param(IS_TYPEERASEDATA(TypeErase));
  679. assert_param(IS_FLASH_DATA_ADDRESS(Address));
  680. /* Wait for last operation to be completed */
  681. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  682. if(status == HAL_OK)
  683. {
  684. /* Clean the error context */
  685. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  686. if(TypeErase == FLASH_TYPEERASEDATA_WORD)
  687. {
  688. /* Write 00000000h to valid address in the data memory */
  689. *(__IO uint32_t *) Address = 0x00000000U;
  690. }
  691. if(TypeErase == FLASH_TYPEERASEDATA_HALFWORD)
  692. {
  693. /* Write 0000h to valid address in the data memory */
  694. *(__IO uint16_t *) Address = (uint16_t)0x0000;
  695. }
  696. if(TypeErase == FLASH_TYPEERASEDATA_BYTE)
  697. {
  698. /* Write 00h to valid address in the data memory */
  699. *(__IO uint8_t *) Address = (uint8_t)0x00;
  700. }
  701. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  702. }
  703. /* Return the erase status */
  704. return status;
  705. }
  706. /**
  707. * @brief Program word at a specified address
  708. * @note To correctly run this function, the @ref HAL_FLASHEx_DATAEEPROM_Unlock() function
  709. * must be called before.
  710. * Call the @ref HAL_FLASHEx_DATAEEPROM_Unlock() to he data EEPROM access
  711. * and Flash program erase control register access(recommended to protect
  712. * the DATA_EEPROM against possible unwanted operation).
  713. * @note The function @ref HAL_FLASHEx_DATAEEPROM_EnableFixedTimeProgram() can be called before
  714. * this function to configure the Fixed Time Programming.
  715. * @param TypeProgram Indicate the way to program at a specified address.
  716. * This parameter can be a value of @ref FLASHEx_Type_Program_Data
  717. * @param Address specifie the address to be programmed.
  718. * @param Data specifie the data to be programmed
  719. *
  720. * @retval HAL_StatusTypeDef HAL Status
  721. */
  722. HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data)
  723. {
  724. HAL_StatusTypeDef status = HAL_ERROR;
  725. /* Process Locked */
  726. __HAL_LOCK(&pFlash);
  727. /* Check the parameters */
  728. assert_param(IS_TYPEPROGRAMDATA(TypeProgram));
  729. /* Wait for last operation to be completed */
  730. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  731. if(status == HAL_OK)
  732. {
  733. /* Clean the error context */
  734. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  735. if(TypeProgram == FLASH_TYPEPROGRAMDATA_WORD)
  736. {
  737. /* Program word (32-bit) at a specified address.*/
  738. status = FLASH_DATAEEPROM_ProgramWord(Address, (uint32_t) Data);
  739. }
  740. else if(TypeProgram == FLASH_TYPEPROGRAMDATA_HALFWORD)
  741. {
  742. /* Program halfword (16-bit) at a specified address.*/
  743. status = FLASH_DATAEEPROM_ProgramHalfWord(Address, (uint16_t) Data);
  744. }
  745. else if(TypeProgram == FLASH_TYPEPROGRAMDATA_BYTE)
  746. {
  747. /* Program byte (8-bit) at a specified address.*/
  748. status = FLASH_DATAEEPROM_ProgramByte(Address, (uint8_t) Data);
  749. }
  750. else if(TypeProgram == FLASH_TYPEPROGRAMDATA_FASTBYTE)
  751. {
  752. /*Program word (8-bit) at a specified address.*/
  753. status = FLASH_DATAEEPROM_FastProgramByte(Address, (uint8_t) Data);
  754. }
  755. else if(TypeProgram == FLASH_TYPEPROGRAMDATA_FASTHALFWORD)
  756. {
  757. /* Program halfword (16-bit) at a specified address.*/
  758. status = FLASH_DATAEEPROM_FastProgramHalfWord(Address, (uint16_t) Data);
  759. }
  760. else if(TypeProgram == FLASH_TYPEPROGRAMDATA_FASTWORD)
  761. {
  762. /* Program word (32-bit) at a specified address.*/
  763. status = FLASH_DATAEEPROM_FastProgramWord(Address, (uint32_t) Data);
  764. }
  765. else
  766. {
  767. status = HAL_ERROR;
  768. }
  769. }
  770. /* Process Unlocked */
  771. __HAL_UNLOCK(&pFlash);
  772. return status;
  773. }
  774. /**
  775. * @brief Enable DATA EEPROM fixed Time programming (2*Tprog).
  776. * @retval None
  777. */
  778. void HAL_FLASHEx_DATAEEPROM_EnableFixedTimeProgram(void)
  779. {
  780. SET_BIT(FLASH->PECR, FLASH_PECR_FTDW);
  781. }
  782. /**
  783. * @brief Disables DATA EEPROM fixed Time programming (2*Tprog).
  784. * @retval None
  785. */
  786. void HAL_FLASHEx_DATAEEPROM_DisableFixedTimeProgram(void)
  787. {
  788. CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
  789. }
  790. /**
  791. * @}
  792. */
  793. /**
  794. * @}
  795. */
  796. /** @addtogroup FLASHEx_Private_Functions
  797. * @{
  798. */
  799. /*
  800. ==============================================================================
  801. OPTIONS BYTES
  802. ==============================================================================
  803. */
  804. /**
  805. * @brief Enables or disables the read out protection.
  806. * @note To correctly run this function, the @ref HAL_FLASH_OB_Unlock() function
  807. * must be called before.
  808. * @param OB_RDP specifies the read protection level.
  809. * This parameter can be:
  810. * @arg @ref OB_RDP_LEVEL_0 No protection
  811. * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
  812. * @arg @ref OB_RDP_LEVEL_2 Chip protection
  813. *
  814. * !!!Warning!!! When enabling OB_RDP_LEVEL_2 it's no more possible to go back to level 1 or 0
  815. *
  816. * @retval HAL status
  817. */
  818. static HAL_StatusTypeDef FLASH_OB_RDPConfig(uint8_t OB_RDP)
  819. {
  820. HAL_StatusTypeDef status = HAL_OK;
  821. uint32_t tmp1 = 0U, tmp2 = 0U, tmp3 = 0U;
  822. /* Check the parameters */
  823. assert_param(IS_OB_RDP(OB_RDP));
  824. tmp1 = (uint32_t)(OB->RDP & FLASH_OBR_RDPRT);
  825. /* According to errata sheet, DocID022054 Rev 5, par2.1.5
  826. Before setting Level0 in the RDP register, check that the current level is not equal to Level0.
  827. If the current level is not equal to Level0, Level0 can be activated.
  828. If the current level is Level0 then the RDP register must not be written again with Level0. */
  829. if ((tmp1 == OB_RDP_LEVEL_0) && (OB_RDP == OB_RDP_LEVEL_0))
  830. {
  831. /*current level is Level0 then the RDP register must not be written again with Level0. */
  832. status = HAL_ERROR;
  833. }
  834. else
  835. {
  836. #if defined(FLASH_OBR_SPRMOD)
  837. /* Mask SPRMOD bit */
  838. tmp3 = (uint32_t)(OB->RDP & FLASH_OBR_SPRMOD);
  839. #endif
  840. /* calculate the option byte to write */
  841. tmp1 = (~((uint32_t)(OB_RDP | tmp3)));
  842. tmp2 = (uint32_t)(((uint32_t)((uint32_t)(tmp1) << 16U)) | ((uint32_t)(OB_RDP | tmp3)));
  843. /* Wait for last operation to be completed */
  844. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  845. if(status == HAL_OK)
  846. {
  847. /* Clean the error context */
  848. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  849. /* program read protection level */
  850. OB->RDP = tmp2;
  851. /* Wait for last operation to be completed */
  852. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  853. }
  854. }
  855. /* Return the Read protection operation Status */
  856. return status;
  857. }
  858. /**
  859. * @brief Programs the FLASH brownout reset threshold level Option Byte.
  860. * @param OB_BOR Selects the brownout reset threshold level.
  861. * This parameter can be one of the following values:
  862. * @arg @ref OB_BOR_OFF BOR is disabled at power down, the reset is asserted when the VDD
  863. * power supply reaches the PDR(Power Down Reset) threshold (1.5V)
  864. * @arg @ref OB_BOR_LEVEL1 BOR Reset threshold levels for 1.7V - 1.8V VDD power supply
  865. * @arg @ref OB_BOR_LEVEL2 BOR Reset threshold levels for 1.9V - 2.0V VDD power supply
  866. * @arg @ref OB_BOR_LEVEL3 BOR Reset threshold levels for 2.3V - 2.4V VDD power supply
  867. * @arg @ref OB_BOR_LEVEL4 BOR Reset threshold levels for 2.55V - 2.65V VDD power supply
  868. * @arg @ref OB_BOR_LEVEL5 BOR Reset threshold levels for 2.8V - 2.9V VDD power supply
  869. * @retval HAL status
  870. */
  871. static HAL_StatusTypeDef FLASH_OB_BORConfig(uint8_t OB_BOR)
  872. {
  873. HAL_StatusTypeDef status = HAL_OK;
  874. uint32_t tmp = 0U, tmp1 = 0U;
  875. /* Check the parameters */
  876. assert_param(IS_OB_BOR_LEVEL(OB_BOR));
  877. /* Get the User Option byte register */
  878. tmp1 = OB->USER & ((~FLASH_OBR_BOR_LEV) >> 16U);
  879. /* Calculate the option byte to write - [0xFFU | nUSER | 0x00U | USER]*/
  880. tmp = (uint32_t)~((OB_BOR | tmp1)) << 16U;
  881. tmp |= (OB_BOR | tmp1);
  882. /* Wait for last operation to be completed */
  883. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  884. if(status == HAL_OK)
  885. {
  886. /* Clean the error context */
  887. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  888. /* Write the BOR Option Byte */
  889. OB->USER = tmp;
  890. /* Wait for last operation to be completed */
  891. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  892. }
  893. /* Return the Option Byte BOR Programming Status */
  894. return status;
  895. }
  896. /**
  897. * @brief Returns the FLASH User Option Bytes values.
  898. * @retval The FLASH User Option Bytes.
  899. */
  900. static uint8_t FLASH_OB_GetUser(void)
  901. {
  902. /* Return the User Option Byte */
  903. return (uint8_t)((FLASH->OBR & (FLASH_OBR_IWDG_SW | FLASH_OBR_nRST_STOP | FLASH_OBR_nRST_STDBY)) >> 16U);
  904. }
  905. /**
  906. * @brief Returns the FLASH Read Protection level.
  907. * @retval FLASH RDP level
  908. * This parameter can be one of the following values:
  909. * @arg @ref OB_RDP_LEVEL_0 No protection
  910. * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory
  911. * @arg @ref OB_RDP_LEVEL_2 Full chip protection
  912. */
  913. static uint8_t FLASH_OB_GetRDP(void)
  914. {
  915. uint8_t rdp_level = (uint8_t)(FLASH->OBR & FLASH_OBR_RDPRT);
  916. if ((rdp_level != OB_RDP_LEVEL_0) && (rdp_level != OB_RDP_LEVEL_2))
  917. {
  918. return (OB_RDP_LEVEL_1);
  919. }
  920. else
  921. {
  922. return (rdp_level);
  923. }
  924. }
  925. /**
  926. * @brief Returns the FLASH BOR level.
  927. * @retval The BOR level Option Bytes.
  928. */
  929. static uint8_t FLASH_OB_GetBOR(void)
  930. {
  931. /* Return the BOR level */
  932. return (uint8_t)((FLASH->OBR & (uint32_t)FLASH_OBR_BOR_LEV) >> 16U);
  933. }
  934. /**
  935. * @brief Write protects the desired pages of the first 64KB of the Flash.
  936. * @param pOBInit pointer to an FLASH_OBInitStruct structure that
  937. * contains WRP parameters.
  938. * @param NewState new state of the specified FLASH Pages Wtite protection.
  939. * This parameter can be: ENABLE or DISABLE.
  940. * @retval HAL_StatusTypeDef
  941. */
  942. static HAL_StatusTypeDef FLASH_OB_WRPConfig(FLASH_OBProgramInitTypeDef *pOBInit, FunctionalState NewState)
  943. {
  944. HAL_StatusTypeDef status = HAL_OK;
  945. /* Wait for last operation to be completed */
  946. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  947. if(status == HAL_OK)
  948. {
  949. /* Clean the error context */
  950. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  951. /* WRP for sector between 0 to 31 */
  952. if (pOBInit->WRPSector0To31 != 0U)
  953. {
  954. FLASH_OB_WRPConfigWRP1OrPCROP1(pOBInit->WRPSector0To31, NewState);
  955. }
  956. #if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC) \
  957. || defined(STM32L151xCA) || defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xCA) \
  958. || defined(STM32L152xD) || defined(STM32L152xDX) || defined(STM32L162xCA) || defined(STM32L162xD) \
  959. || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE)
  960. /* Pages for Cat3, Cat4 & Cat5 devices*/
  961. /* WRP for sector between 32 to 63 */
  962. if (pOBInit->WRPSector32To63 != 0U)
  963. {
  964. FLASH_OB_WRPConfigWRP2OrPCROP2(pOBInit->WRPSector32To63, NewState);
  965. }
  966. #endif /* STM32L100xC || STM32L151xC || STM32L152xC || (...) || STM32L151xE || STM32L152xE || STM32L162xE */
  967. #if defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xD) || defined(STM32L152xDX) \
  968. || defined(STM32L162xD) || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) \
  969. || defined(STM32L162xE)
  970. /* Pages for devices with FLASH >= 256KB*/
  971. /* WRP for sector between 64 to 95 */
  972. if (pOBInit->WRPSector64To95 != 0U)
  973. {
  974. FLASH_OB_WRPConfigWRP3(pOBInit->WRPSector64To95, NewState);
  975. }
  976. #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
  977. #if defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE) || defined(STM32L151xDX) \
  978. || defined(STM32L152xDX) || defined(STM32L162xDX)
  979. /* Pages for Cat5 devices*/
  980. /* WRP for sector between 96 to 127 */
  981. if (pOBInit->WRPSector96To127 != 0U)
  982. {
  983. FLASH_OB_WRPConfigWRP4(pOBInit->WRPSector96To127, NewState);
  984. }
  985. #endif /* STM32L151xE || STM32L152xE || STM32L162xE || STM32L151xDX || ... */
  986. /* Wait for last operation to be completed */
  987. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  988. }
  989. /* Return the write protection operation Status */
  990. return status;
  991. }
  992. #if defined(STM32L151xBA) || defined(STM32L152xBA) || defined(STM32L151xC) || defined(STM32L152xC) \
  993. || defined(STM32L162xC)
  994. /**
  995. * @brief Enables the read/write protection (PCROP) of the desired
  996. * sectors.
  997. * @note This function can be used only for Cat2 & Cat3 devices
  998. * @param pAdvOBInit pointer to an FLASH_AdvOBProgramInitTypeDef structure that
  999. * contains PCROP parameters.
  1000. * @param NewState new state of the specified FLASH Pages read/Write protection.
  1001. * This parameter can be: ENABLE or DISABLE.
  1002. * @retval HAL status
  1003. */
  1004. static HAL_StatusTypeDef FLASH_OB_PCROPConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit, FunctionalState NewState)
  1005. {
  1006. HAL_StatusTypeDef status = HAL_OK;
  1007. FunctionalState pcropstate = DISABLE;
  1008. /* Wait for last operation to be completed */
  1009. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1010. /* Invert state to use same function of WRP */
  1011. if (NewState == DISABLE)
  1012. {
  1013. pcropstate = ENABLE;
  1014. }
  1015. if(status == HAL_OK)
  1016. {
  1017. /* Clean the error context */
  1018. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  1019. /* Pages for Cat2 devices*/
  1020. /* PCROP for sector between 0 to 31 */
  1021. if (pAdvOBInit->PCROPSector0To31 != 0U)
  1022. {
  1023. FLASH_OB_WRPConfigWRP1OrPCROP1(pAdvOBInit->PCROPSector0To31, pcropstate);
  1024. }
  1025. #if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC)
  1026. /* Pages for Cat3 devices*/
  1027. /* WRP for sector between 32 to 63 */
  1028. if (pAdvOBInit->PCROPSector32To63 != 0U)
  1029. {
  1030. FLASH_OB_WRPConfigWRP2OrPCROP2(pAdvOBInit->PCROPSector32To63, pcropstate);
  1031. }
  1032. #endif /* STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC */
  1033. /* Wait for last operation to be completed */
  1034. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1035. }
  1036. /* Return the write protection operation Status */
  1037. return status;
  1038. }
  1039. #endif /* STM32L151xBA || STM32L152xBA || STM32L151xC || STM32L152xC || STM32L162xC */
  1040. /**
  1041. * @brief Write protects the desired pages of the first 128KB of the Flash.
  1042. * @param WRP1OrPCROP1 specifies the address of the pages to be write protected.
  1043. * This parameter can be a combination of @ref FLASHEx_Option_Bytes_Write_Protection1
  1044. * @param NewState new state of the specified FLASH Pages Write protection.
  1045. * This parameter can be: ENABLE or DISABLE.
  1046. * @retval None
  1047. */
  1048. static void FLASH_OB_WRPConfigWRP1OrPCROP1(uint32_t WRP1OrPCROP1, FunctionalState NewState)
  1049. {
  1050. uint32_t wrp01data = 0U, wrp23data = 0U;
  1051. uint32_t tmp1 = 0U, tmp2 = 0U;
  1052. /* Check the parameters */
  1053. assert_param(IS_OB_WRP(WRP1OrPCROP1));
  1054. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1055. if (NewState != DISABLE)
  1056. {
  1057. wrp01data = (uint16_t)(((WRP1OrPCROP1 & WRP_MASK_LOW) | OB->WRP01));
  1058. wrp23data = (uint16_t)((((WRP1OrPCROP1 & WRP_MASK_HIGH)>>16U | OB->WRP23)));
  1059. tmp1 = (uint32_t)(~(wrp01data) << 16U)|(wrp01data);
  1060. OB->WRP01 = tmp1;
  1061. tmp2 = (uint32_t)(~(wrp23data) << 16U)|(wrp23data);
  1062. OB->WRP23 = tmp2;
  1063. }
  1064. else
  1065. {
  1066. wrp01data = (uint16_t)(~WRP1OrPCROP1 & (WRP_MASK_LOW & OB->WRP01));
  1067. wrp23data = (uint16_t)((((~WRP1OrPCROP1 & WRP_MASK_HIGH)>>16U & OB->WRP23)));
  1068. tmp1 = (uint32_t)((~wrp01data) << 16U)|(wrp01data);
  1069. OB->WRP01 = tmp1;
  1070. tmp2 = (uint32_t)((~wrp23data) << 16U)|(wrp23data);
  1071. OB->WRP23 = tmp2;
  1072. }
  1073. }
  1074. #if defined(STM32L100xC) || defined(STM32L151xC) || defined(STM32L152xC) || defined(STM32L162xC) \
  1075. || defined(STM32L151xCA) || defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xCA) \
  1076. || defined(STM32L152xD) || defined(STM32L152xDX) || defined(STM32L162xCA) || defined(STM32L162xD) \
  1077. || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE)
  1078. /**
  1079. * @brief Enable Write protects the desired pages of the second 128KB of the Flash.
  1080. * @note This function can be used only for Cat3, Cat4 & Cat5 devices.
  1081. * @param WRP2OrPCROP2 specifies the address of the pages to be write protected.
  1082. * This parameter can be a combination of @ref FLASHEx_Option_Bytes_Write_Protection2
  1083. * @param NewState new state of the specified FLASH Pages Wtite protection.
  1084. * This parameter can be: ENABLE or DISABLE.
  1085. * @retval None
  1086. */
  1087. static void FLASH_OB_WRPConfigWRP2OrPCROP2(uint32_t WRP2OrPCROP2, FunctionalState NewState)
  1088. {
  1089. uint32_t wrp45data = 0U, wrp67data = 0U;
  1090. uint32_t tmp1 = 0U, tmp2 = 0U;
  1091. /* Check the parameters */
  1092. assert_param(IS_OB_WRP(WRP2OrPCROP2));
  1093. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1094. if (NewState != DISABLE)
  1095. {
  1096. wrp45data = (uint16_t)(((WRP2OrPCROP2 & WRP_MASK_LOW) | OB->WRP45));
  1097. wrp67data = (uint16_t)((((WRP2OrPCROP2 & WRP_MASK_HIGH)>>16U | OB->WRP67)));
  1098. tmp1 = (uint32_t)(~(wrp45data) << 16U)|(wrp45data);
  1099. OB->WRP45 = tmp1;
  1100. tmp2 = (uint32_t)(~(wrp67data) << 16U)|(wrp67data);
  1101. OB->WRP67 = tmp2;
  1102. }
  1103. else
  1104. {
  1105. wrp45data = (uint16_t)(~WRP2OrPCROP2 & (WRP_MASK_LOW & OB->WRP45));
  1106. wrp67data = (uint16_t)((((~WRP2OrPCROP2 & WRP_MASK_HIGH)>>16U & OB->WRP67)));
  1107. tmp1 = (uint32_t)((~wrp45data) << 16U)|(wrp45data);
  1108. OB->WRP45 = tmp1;
  1109. tmp2 = (uint32_t)((~wrp67data) << 16U)|(wrp67data);
  1110. OB->WRP67 = tmp2;
  1111. }
  1112. }
  1113. #endif /* STM32L100xC || STM32L151xC || STM32L152xC || (...) || STM32L151xE || STM32L152xE || STM32L162xE */
  1114. #if defined(STM32L151xD) || defined(STM32L151xDX) || defined(STM32L152xD) || defined(STM32L152xDX) \
  1115. || defined(STM32L162xD) || defined(STM32L162xDX) || defined(STM32L151xE) || defined(STM32L152xE) \
  1116. || defined(STM32L162xE)
  1117. /**
  1118. * @brief Enable Write protects the desired pages of the third 128KB of the Flash.
  1119. * @note This function can be used only for STM32L151xD, STM32L152xD, STM32L162xD & Cat5 devices.
  1120. * @param WRP3 specifies the address of the pages to be write protected.
  1121. * This parameter can be a combination of @ref FLASHEx_Option_Bytes_Write_Protection3
  1122. * @param NewState new state of the specified FLASH Pages Wtite protection.
  1123. * This parameter can be: ENABLE or DISABLE.
  1124. * @retval None
  1125. */
  1126. static void FLASH_OB_WRPConfigWRP3(uint32_t WRP3, FunctionalState NewState)
  1127. {
  1128. uint32_t wrp89data = 0U, wrp1011data = 0U;
  1129. uint32_t tmp1 = 0U, tmp2 = 0U;
  1130. /* Check the parameters */
  1131. assert_param(IS_OB_WRP(WRP3));
  1132. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1133. if (NewState != DISABLE)
  1134. {
  1135. wrp89data = (uint16_t)(((WRP3 & WRP_MASK_LOW) | OB->WRP89));
  1136. wrp1011data = (uint16_t)((((WRP3 & WRP_MASK_HIGH)>>16U | OB->WRP1011)));
  1137. tmp1 = (uint32_t)(~(wrp89data) << 16U)|(wrp89data);
  1138. OB->WRP89 = tmp1;
  1139. tmp2 = (uint32_t)(~(wrp1011data) << 16U)|(wrp1011data);
  1140. OB->WRP1011 = tmp2;
  1141. }
  1142. else
  1143. {
  1144. wrp89data = (uint16_t)(~WRP3 & (WRP_MASK_LOW & OB->WRP89));
  1145. wrp1011data = (uint16_t)((((~WRP3 & WRP_MASK_HIGH)>>16U & OB->WRP1011)));
  1146. tmp1 = (uint32_t)((~wrp89data) << 16U)|(wrp89data);
  1147. OB->WRP89 = tmp1;
  1148. tmp2 = (uint32_t)((~wrp1011data) << 16U)|(wrp1011data);
  1149. OB->WRP1011 = tmp2;
  1150. }
  1151. }
  1152. #endif /* STM32L151xD || STM32L152xD || STM32L162xD || STM32L151xE || STM32L152xE || STM32L162xE */
  1153. #if defined(STM32L151xE) || defined(STM32L152xE) || defined(STM32L162xE) || defined(STM32L151xDX) \
  1154. || defined(STM32L152xDX) || defined(STM32L162xDX)
  1155. /**
  1156. * @brief Enable Write protects the desired pages of the Fourth 128KB of the Flash.
  1157. * @note This function can be used only for Cat5 & STM32L1xxDX devices.
  1158. * @param WRP4 specifies the address of the pages to be write protected.
  1159. * This parameter can be a combination of @ref FLASHEx_Option_Bytes_Write_Protection4
  1160. * @param NewState new state of the specified FLASH Pages Wtite protection.
  1161. * This parameter can be: ENABLE or DISABLE.
  1162. * @retval None
  1163. */
  1164. static void FLASH_OB_WRPConfigWRP4(uint32_t WRP4, FunctionalState NewState)
  1165. {
  1166. uint32_t wrp1213data = 0U, wrp1415data = 0U;
  1167. uint32_t tmp1 = 0U, tmp2 = 0U;
  1168. /* Check the parameters */
  1169. assert_param(IS_OB_WRP(WRP4));
  1170. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1171. if (NewState != DISABLE)
  1172. {
  1173. wrp1213data = (uint16_t)(((WRP4 & WRP_MASK_LOW) | OB->WRP1213));
  1174. wrp1415data = (uint16_t)((((WRP4 & WRP_MASK_HIGH)>>16U | OB->WRP1415)));
  1175. tmp1 = (uint32_t)(~(wrp1213data) << 16U)|(wrp1213data);
  1176. OB->WRP1213 = tmp1;
  1177. tmp2 = (uint32_t)(~(wrp1415data) << 16U)|(wrp1415data);
  1178. OB->WRP1415 = tmp2;
  1179. }
  1180. else
  1181. {
  1182. wrp1213data = (uint16_t)(~WRP4 & (WRP_MASK_LOW & OB->WRP1213));
  1183. wrp1415data = (uint16_t)((((~WRP4 & WRP_MASK_HIGH)>>16U & OB->WRP1415)));
  1184. tmp1 = (uint32_t)((~wrp1213data) << 16U)|(wrp1213data);
  1185. OB->WRP1213 = tmp1;
  1186. tmp2 = (uint32_t)((~wrp1415data) << 16U)|(wrp1415data);
  1187. OB->WRP1415 = tmp2;
  1188. }
  1189. }
  1190. #endif /* STM32L151xE || STM32L152xE || STM32L162xE || STM32L151xDX || ... */
  1191. /**
  1192. * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
  1193. * @param OB_IWDG Selects the WDG mode.
  1194. * This parameter can be one of the following values:
  1195. * @arg @ref OB_IWDG_SW Software WDG selected
  1196. * @arg @ref OB_IWDG_HW Hardware WDG selected
  1197. * @param OB_STOP Reset event when entering STOP mode.
  1198. * This parameter can be one of the following values:
  1199. * @arg @ref OB_STOP_NORST No reset generated when entering in STOP
  1200. * @arg @ref OB_STOP_RST Reset generated when entering in STOP
  1201. * @param OB_STDBY Reset event when entering Standby mode.
  1202. * This parameter can be one of the following values:
  1203. * @arg @ref OB_STDBY_NORST No reset generated when entering in STANDBY
  1204. * @arg @ref OB_STDBY_RST Reset generated when entering in STANDBY
  1205. * @retval HAL status
  1206. */
  1207. static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
  1208. {
  1209. HAL_StatusTypeDef status = HAL_OK;
  1210. uint32_t tmp = 0U, tmp1 = 0U;
  1211. /* Check the parameters */
  1212. assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
  1213. assert_param(IS_OB_STOP_SOURCE(OB_STOP));
  1214. assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
  1215. /* Get the User Option byte register */
  1216. tmp1 = OB->USER & ((~(FLASH_OBR_IWDG_SW | FLASH_OBR_nRST_STOP | FLASH_OBR_nRST_STDBY)) >> 16U);
  1217. /* Calculate the user option byte to write */
  1218. tmp = (uint32_t)(((uint32_t)~((uint32_t)((uint32_t)(OB_IWDG) | (uint32_t)(OB_STOP) | (uint32_t)(OB_STDBY) | tmp1))) << 16U);
  1219. tmp |= ((uint32_t)(OB_IWDG) | ((uint32_t)OB_STOP) | (uint32_t)(OB_STDBY) | tmp1);
  1220. /* Wait for last operation to be completed */
  1221. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1222. if(status == HAL_OK)
  1223. {
  1224. /* Clean the error context */
  1225. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  1226. /* Write the User Option Byte */
  1227. OB->USER = tmp;
  1228. /* Wait for last operation to be completed */
  1229. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1230. }
  1231. /* Return the Option Byte program Status */
  1232. return status;
  1233. }
  1234. #if defined(FLASH_OBR_nRST_BFB2)
  1235. /**
  1236. * @brief Configures to boot from Bank1 or Bank2.
  1237. * @param OB_BOOT select the FLASH Bank to boot from.
  1238. * This parameter can be one of the following values:
  1239. * @arg @ref OB_BOOT_BANK2 At startup, if boot pins are set in boot from user Flash
  1240. * position and this parameter is selected the device will boot from Bank2 or Bank1,
  1241. * depending on the activation of the bank. The active banks are checked in
  1242. * the following order: Bank2, followed by Bank1.
  1243. * The active bank is recognized by the value programmed at the base address
  1244. * of the respective bank (corresponding to the initial stack pointer value
  1245. * in the interrupt vector table).
  1246. * @arg @ref OB_BOOT_BANK1 At startup, if boot pins are set in boot from user Flash
  1247. * position and this parameter is selected the device will boot from Bank1(Default).
  1248. * For more information, please refer to AN2606 from www.st.com.
  1249. * @retval HAL status
  1250. */
  1251. static HAL_StatusTypeDef FLASH_OB_BootConfig(uint8_t OB_BOOT)
  1252. {
  1253. HAL_StatusTypeDef status = HAL_OK;
  1254. uint32_t tmp = 0U, tmp1 = 0U;
  1255. /* Check the parameters */
  1256. assert_param(IS_OB_BOOT_BANK(OB_BOOT));
  1257. /* Get the User Option byte register and BOR Level*/
  1258. tmp1 = OB->USER & ((~FLASH_OBR_nRST_BFB2) >> 16U);
  1259. /* Calculate the option byte to write */
  1260. tmp = (uint32_t)~(OB_BOOT | tmp1) << 16U;
  1261. tmp |= (OB_BOOT | tmp1);
  1262. /* Wait for last operation to be completed */
  1263. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1264. if(status == HAL_OK)
  1265. {
  1266. /* Clean the error context */
  1267. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  1268. /* Write the BOOT Option Byte */
  1269. OB->USER = tmp;
  1270. /* Wait for last operation to be completed */
  1271. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1272. }
  1273. /* Return the Option Byte program Status */
  1274. return status;
  1275. }
  1276. #endif /* FLASH_OBR_nRST_BFB2 */
  1277. /*
  1278. ==============================================================================
  1279. DATA
  1280. ==============================================================================
  1281. */
  1282. /**
  1283. * @brief Write a Byte at a specified address in data memory.
  1284. * @param Address specifies the address to be written.
  1285. * @param Data specifies the data to be written.
  1286. * @note This function assumes that the is data word is already erased.
  1287. * @retval HAL status
  1288. */
  1289. static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramByte(uint32_t Address, uint8_t Data)
  1290. {
  1291. HAL_StatusTypeDef status = HAL_OK;
  1292. #if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
  1293. uint32_t tmp = 0U, tmpaddr = 0U;
  1294. #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
  1295. /* Check the parameters */
  1296. assert_param(IS_FLASH_DATA_ADDRESS(Address));
  1297. /* Wait for last operation to be completed */
  1298. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1299. if(status == HAL_OK)
  1300. {
  1301. /* Clear the FTDW bit */
  1302. CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
  1303. #if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
  1304. /* Possible only on Cat1 devices */
  1305. if(Data != (uint8_t)0x00U)
  1306. {
  1307. /* If the previous operation is completed, proceed to write the new Data */
  1308. *(__IO uint8_t *)Address = Data;
  1309. /* Wait for last operation to be completed */
  1310. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1311. }
  1312. else
  1313. {
  1314. tmpaddr = Address & 0xFFFFFFFCU;
  1315. tmp = * (__IO uint32_t *) tmpaddr;
  1316. tmpaddr = 0xFFU << ((uint32_t) (0x8U * (Address & 0x3U)));
  1317. tmp &= ~tmpaddr;
  1318. status = HAL_FLASHEx_DATAEEPROM_Erase(FLASH_TYPEERASEDATA_WORD, Address & 0xFFFFFFFCU);
  1319. /* Process Unlocked */
  1320. __HAL_UNLOCK(&pFlash);
  1321. status = HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFCU), tmp);
  1322. /* Process Locked */
  1323. __HAL_LOCK(&pFlash);
  1324. }
  1325. #else /*!Cat1*/
  1326. /* If the previous operation is completed, proceed to write the new Data */
  1327. *(__IO uint8_t *)Address = Data;
  1328. /* Wait for last operation to be completed */
  1329. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1330. #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
  1331. }
  1332. /* Return the Write Status */
  1333. return status;
  1334. }
  1335. /**
  1336. * @brief Writes a half word at a specified address in data memory.
  1337. * @param Address specifies the address to be written.
  1338. * @param Data specifies the data to be written.
  1339. * @note This function assumes that the is data word is already erased.
  1340. * @retval HAL status
  1341. */
  1342. static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramHalfWord(uint32_t Address, uint16_t Data)
  1343. {
  1344. HAL_StatusTypeDef status = HAL_OK;
  1345. #if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
  1346. uint32_t tmp = 0U, tmpaddr = 0U;
  1347. #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
  1348. /* Check the parameters */
  1349. assert_param(IS_FLASH_DATA_ADDRESS(Address));
  1350. /* Wait for last operation to be completed */
  1351. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1352. if(status == HAL_OK)
  1353. {
  1354. /* Clear the FTDW bit */
  1355. CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
  1356. #if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
  1357. /* Possible only on Cat1 devices */
  1358. if(Data != (uint16_t)0x0000U)
  1359. {
  1360. /* If the previous operation is completed, proceed to write the new data */
  1361. *(__IO uint16_t *)Address = Data;
  1362. /* Wait for last operation to be completed */
  1363. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1364. }
  1365. else
  1366. {
  1367. /* Process Unlocked */
  1368. __HAL_UNLOCK(&pFlash);
  1369. if((Address & 0x3U) != 0x3U)
  1370. {
  1371. tmpaddr = Address & 0xFFFFFFFCU;
  1372. tmp = * (__IO uint32_t *) tmpaddr;
  1373. tmpaddr = 0xFFFFU << ((uint32_t) (0x8U * (Address & 0x3U)));
  1374. tmp &= ~tmpaddr;
  1375. status = HAL_FLASHEx_DATAEEPROM_Erase(FLASH_TYPEERASEDATA_WORD, Address & 0xFFFFFFFCU);
  1376. status = HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFCU), tmp);
  1377. }
  1378. else
  1379. {
  1380. HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTBYTE, Address, 0x00U);
  1381. HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTBYTE, Address + 1U, 0x00U);
  1382. }
  1383. /* Process Locked */
  1384. __HAL_LOCK(&pFlash);
  1385. }
  1386. #else /* !Cat1 */
  1387. /* If the previous operation is completed, proceed to write the new data */
  1388. *(__IO uint16_t *)Address = Data;
  1389. /* Wait for last operation to be completed */
  1390. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1391. #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
  1392. }
  1393. /* Return the Write Status */
  1394. return status;
  1395. }
  1396. /**
  1397. * @brief Programs a word at a specified address in data memory.
  1398. * @param Address specifies the address to be written.
  1399. * @param Data specifies the data to be written.
  1400. * @note This function assumes that the is data word is already erased.
  1401. * @retval HAL status
  1402. */
  1403. static HAL_StatusTypeDef FLASH_DATAEEPROM_FastProgramWord(uint32_t Address, uint32_t Data)
  1404. {
  1405. HAL_StatusTypeDef status = HAL_OK;
  1406. /* Check the parameters */
  1407. assert_param(IS_FLASH_DATA_ADDRESS(Address));
  1408. /* Wait for last operation to be completed */
  1409. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1410. if(status == HAL_OK)
  1411. {
  1412. /* Clear the FTDW bit */
  1413. CLEAR_BIT(FLASH->PECR, FLASH_PECR_FTDW);
  1414. /* If the previous operation is completed, proceed to program the new data */
  1415. *(__IO uint32_t *)Address = Data;
  1416. /* Wait for last operation to be completed */
  1417. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1418. }
  1419. /* Return the Write Status */
  1420. return status;
  1421. }
  1422. /**
  1423. * @brief Write a Byte at a specified address in data memory without erase.
  1424. * @param Address specifies the address to be written.
  1425. * @param Data specifies the data to be written.
  1426. * @retval HAL status
  1427. */
  1428. static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramByte(uint32_t Address, uint8_t Data)
  1429. {
  1430. HAL_StatusTypeDef status = HAL_OK;
  1431. #if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
  1432. uint32_t tmp = 0U, tmpaddr = 0U;
  1433. #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
  1434. /* Check the parameters */
  1435. assert_param(IS_FLASH_DATA_ADDRESS(Address));
  1436. /* Wait for last operation to be completed */
  1437. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1438. if(status == HAL_OK)
  1439. {
  1440. #if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
  1441. if(Data != (uint8_t) 0x00U)
  1442. {
  1443. *(__IO uint8_t *)Address = Data;
  1444. /* Wait for last operation to be completed */
  1445. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1446. }
  1447. else
  1448. {
  1449. tmpaddr = Address & 0xFFFFFFFCU;
  1450. tmp = * (__IO uint32_t *) tmpaddr;
  1451. tmpaddr = 0xFFU << ((uint32_t) (0x8U * (Address & 0x3U)));
  1452. tmp &= ~tmpaddr;
  1453. status = HAL_FLASHEx_DATAEEPROM_Erase(FLASH_TYPEERASEDATA_WORD, Address & 0xFFFFFFFCU);
  1454. /* Process Unlocked */
  1455. __HAL_UNLOCK(&pFlash);
  1456. status = HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFCU), tmp);
  1457. /* Process Locked */
  1458. __HAL_LOCK(&pFlash);
  1459. }
  1460. #else /* Not Cat1*/
  1461. *(__IO uint8_t *)Address = Data;
  1462. /* Wait for last operation to be completed */
  1463. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1464. #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
  1465. }
  1466. /* Return the Write Status */
  1467. return status;
  1468. }
  1469. /**
  1470. * @brief Writes a half word at a specified address in data memory without erase.
  1471. * @param Address specifies the address to be written.
  1472. * @param Data specifies the data to be written.
  1473. * @retval HAL status
  1474. */
  1475. static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramHalfWord(uint32_t Address, uint16_t Data)
  1476. {
  1477. HAL_StatusTypeDef status = HAL_OK;
  1478. #if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
  1479. uint32_t tmp = 0U, tmpaddr = 0U;
  1480. #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
  1481. /* Check the parameters */
  1482. assert_param(IS_FLASH_DATA_ADDRESS(Address));
  1483. /* Wait for last operation to be completed */
  1484. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1485. if(status == HAL_OK)
  1486. {
  1487. #if defined(STM32L100xB) || defined(STM32L151xB) || defined(STM32L152xB)
  1488. if(Data != (uint16_t)0x0000U)
  1489. {
  1490. *(__IO uint16_t *)Address = Data;
  1491. /* Wait for last operation to be completed */
  1492. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1493. }
  1494. else
  1495. {
  1496. /* Process Unlocked */
  1497. __HAL_UNLOCK(&pFlash);
  1498. if((Address & 0x3U) != 0x3U)
  1499. {
  1500. tmpaddr = Address & 0xFFFFFFFCU;
  1501. tmp = * (__IO uint32_t *) tmpaddr;
  1502. tmpaddr = 0xFFFFU << ((uint32_t) (0x8U * (Address & 0x3U)));
  1503. tmp &= ~tmpaddr;
  1504. status = HAL_FLASHEx_DATAEEPROM_Erase(FLASH_TYPEERASEDATA_WORD, Address & 0xFFFFFFFCU);
  1505. status = HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTWORD, (Address & 0xFFFFFFFCU), tmp);
  1506. }
  1507. else
  1508. {
  1509. HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTBYTE, Address, 0x00U);
  1510. HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_FASTBYTE, Address + 1U, 0x00U);
  1511. }
  1512. /* Process Locked */
  1513. __HAL_LOCK(&pFlash);
  1514. }
  1515. #else /* Not Cat1*/
  1516. *(__IO uint16_t *)Address = Data;
  1517. /* Wait for last operation to be completed */
  1518. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1519. #endif /* STM32L100xB || STM32L151xB || STM32L152xB */
  1520. }
  1521. /* Return the Write Status */
  1522. return status;
  1523. }
  1524. /**
  1525. * @brief Programs a word at a specified address in data memory without erase.
  1526. * @param Address specifies the address to be written.
  1527. * @param Data specifies the data to be written.
  1528. * @retval HAL status
  1529. */
  1530. static HAL_StatusTypeDef FLASH_DATAEEPROM_ProgramWord(uint32_t Address, uint32_t Data)
  1531. {
  1532. HAL_StatusTypeDef status = HAL_OK;
  1533. /* Check the parameters */
  1534. assert_param(IS_FLASH_DATA_ADDRESS(Address));
  1535. /* Wait for last operation to be completed */
  1536. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1537. if(status == HAL_OK)
  1538. {
  1539. *(__IO uint32_t *)Address = Data;
  1540. /* Wait for last operation to be completed */
  1541. status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
  1542. }
  1543. /* Return the Write Status */
  1544. return status;
  1545. }
  1546. /**
  1547. * @}
  1548. */
  1549. /**
  1550. * @}
  1551. */
  1552. /** @addtogroup FLASH
  1553. * @{
  1554. */
  1555. /** @addtogroup FLASH_Private_Functions
  1556. * @{
  1557. */
  1558. /**
  1559. * @brief Erases a specified page in program memory.
  1560. * @param PageAddress The page address in program memory to be erased.
  1561. * @note A Page is erased in the Program memory only if the address to load
  1562. * is the start address of a page (multiple of @ref FLASH_PAGE_SIZE bytes).
  1563. * @retval None
  1564. */
  1565. void FLASH_PageErase(uint32_t PageAddress)
  1566. {
  1567. /* Clean the error context */
  1568. pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
  1569. /* Set the ERASE bit */
  1570. SET_BIT(FLASH->PECR, FLASH_PECR_ERASE);
  1571. /* Set PROG bit */
  1572. SET_BIT(FLASH->PECR, FLASH_PECR_PROG);
  1573. /* Write 00000000h to the first word of the program page to erase */
  1574. *(__IO uint32_t *)(uint32_t)(PageAddress & ~(FLASH_PAGE_SIZE - 1)) = 0x00000000;
  1575. }
  1576. /**
  1577. * @}
  1578. */
  1579. /**
  1580. * @}
  1581. */
  1582. #endif /* HAL_FLASH_MODULE_ENABLED */
  1583. /**
  1584. * @}
  1585. */
  1586. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/