stm32l1xx_hal_flash_ex.c 65 KB

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