stm32l1xx_hal_flash_ex.c 69 KB

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