ft32f0xx_flash.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  1. /**
  2. ******************************************************************************
  3. * @file ft32f0xx_flash.c
  4. * @author FMD AE
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the FLASH peripheral:
  7. * - FLASH Interface configuration
  8. * - FLASH Memory Programming
  9. * - Option Bytes Programming
  10. * - Interrupts and flags management
  11. * @version V1.0.0
  12. * @data 2021-07-01
  13. ******************************************************************************
  14. */
  15. /* Includes ------------------------------------------------------------------*/
  16. #include "ft32f0xx_flash.h"
  17. /**
  18. * @brief Sets the code latency value.
  19. * @param FLASH_Latency: specifies the FLASH Latency value.
  20. * This parameter can be one of the following values:
  21. * @arg FLASH_Latency_0: FLASH Zero Latency cycle
  22. * @arg FLASH_Latency_1: FLASH One Latency cycle
  23. * @retval None
  24. */
  25. void FLASH_SetLatency(uint32_t FLASH_Latency)
  26. {
  27. uint32_t tmpreg = 0;
  28. /* Check the parameters */
  29. assert_param(IS_FLASH_LATENCY(FLASH_Latency));
  30. /* Read the ACR register */
  31. tmpreg = FLASH->ACR;
  32. /* Sets the Latency value */
  33. tmpreg &= (uint32_t) (~((uint32_t)FLASH_ACR_LATENCY));
  34. tmpreg |= FLASH_Latency;
  35. /* Write the ACR register */
  36. FLASH->ACR = tmpreg;
  37. }
  38. /**
  39. * @brief Enables or disables the Prefetch Buffer.
  40. * @param NewState: new state of the FLASH prefetch buffer.
  41. * This parameter can be: ENABLE or DISABLE.
  42. * @retval None
  43. */
  44. void FLASH_PrefetchBufferCmd(FunctionalState NewState)
  45. {
  46. /* Check the parameters */
  47. assert_param(IS_FUNCTIONAL_STATE(NewState));
  48. if(NewState != DISABLE)
  49. {
  50. FLASH->ACR |= FLASH_ACR_PRFTBE;
  51. }
  52. else
  53. {
  54. FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_PRFTBE));
  55. }
  56. }
  57. /**
  58. * @brief Checks whether the FLASH Prefetch Buffer status is set or not.
  59. * @param None
  60. * @retval FLASH Prefetch Buffer Status (SET or RESET).
  61. */
  62. FlagStatus FLASH_GetPrefetchBufferStatus(void)
  63. {
  64. FlagStatus bitstatus = RESET;
  65. if ((FLASH->ACR & FLASH_ACR_PRFTBS) != (uint32_t)RESET)
  66. {
  67. bitstatus = SET;
  68. }
  69. else
  70. {
  71. bitstatus = RESET;
  72. }
  73. /* Return the new state of FLASH Prefetch Buffer Status (SET or RESET) */
  74. return bitstatus;
  75. }
  76. /**
  77. * @brief Unlocks the FLASH control register and program memory access.
  78. * @param None
  79. * @retval None
  80. */
  81. void FLASH_Unlock(void)
  82. {
  83. if((FLASH->CR & FLASH_CR_LOCK) != RESET)
  84. {
  85. /* Unlocking the program memory access */
  86. FLASH->KEYR = FLASH_FKEY1;
  87. FLASH->KEYR = FLASH_FKEY2;
  88. }
  89. }
  90. /**
  91. * @brief Locks the Program memory access.
  92. * @param None
  93. * @retval None
  94. */
  95. void FLASH_Lock(void)
  96. {
  97. /* Set the LOCK Bit to lock the FLASH control register and program memory access */
  98. FLASH->CR |= FLASH_CR_LOCK;
  99. }
  100. /**
  101. * @brief Erases a specified page in program memory.
  102. * @note To correctly run this function, the FLASH_Unlock() function must be called before.
  103. * @note Call the FLASH_Lock() to disable the flash memory access (recommended
  104. * to protect the FLASH memory against possible unwanted operation)
  105. * @param Page_Address: The page address in program memory to be erased.
  106. * @note A Page is erased in the Program memory only if the address to load
  107. * is the start address of a page (multiple of 512 bytes,in FT32F072XB is 1024 bytes).
  108. * @retval FLASH Status: The returned value can be:
  109. * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  110. */
  111. FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
  112. {
  113. FLASH_Status status = FLASH_COMPLETE;
  114. /* Check the parameters */
  115. assert_param(IS_FLASH_PROGRAM_ADDRESS(Page_Address));
  116. FLASH_PrefetchBufferCmd(DISABLE);
  117. __ASM("ISB");
  118. /* Wait for last operation to be completed */
  119. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  120. if(status == FLASH_COMPLETE)
  121. {
  122. /* If the previous operation is completed, proceed to erase the page */
  123. FLASH->CR |= FLASH_CR_PER;
  124. FLASH->AR = Page_Address;
  125. FLASH->CR |= FLASH_CR_STRT;
  126. /* Wait for last operation to be completed */
  127. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  128. /* Disable the PER Bit */
  129. FLASH->CR &= ~FLASH_CR_PER;
  130. }
  131. FLASH_PrefetchBufferCmd(ENABLE);
  132. /* Return the Erase Status */
  133. return status;
  134. }
  135. /**
  136. * @brief Erases all FLASH pages.
  137. * @note To correctly run this function, the FLASH_Unlock() function must be called before.
  138. * @note Call the FLASH_Lock() to disable the flash memory access (recommended
  139. * to protect the FLASH memory against possible unwanted operation)
  140. * @param None
  141. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  142. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  143. */
  144. FLASH_Status FLASH_EraseAllPages(void)
  145. {
  146. FLASH_Status status = FLASH_COMPLETE;
  147. FLASH_PrefetchBufferCmd(DISABLE);
  148. __ASM("ISB");
  149. /* Wait for last operation to be completed */
  150. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  151. if(status == FLASH_COMPLETE)
  152. {
  153. /* if the previous operation is completed, proceed to erase all pages */
  154. FLASH->CR |= FLASH_CR_MER;
  155. FLASH->CR |= FLASH_CR_STRT;
  156. /* Wait for last operation to be completed */
  157. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  158. /* Disable the MER Bit */
  159. FLASH->CR &= ~FLASH_CR_MER;
  160. }
  161. FLASH_PrefetchBufferCmd(ENABLE);
  162. /* Return the Erase Status */
  163. return status;
  164. }
  165. #if defined(FT32F072xB)
  166. /**
  167. * @brief Programs a word at a specified address.
  168. * @note To correctly run this function, the FLASH_Unlock() function must be called before.
  169. * @note Call the FLASH_Lock() to disable the flash memory access (recommended
  170. * to protect the FLASH memory against possible unwanted operation)
  171. * @param Address: specifies the address to be programmed.
  172. * @param Data: specifies the data to be programmed.
  173. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  174. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  175. */
  176. FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
  177. {
  178. FLASH_Status status = FLASH_COMPLETE;
  179. __IO uint32_t tmp = 0;
  180. /* Check the parameters */
  181. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  182. FLASH_PrefetchBufferCmd(DISABLE);
  183. __ASM("ISB");
  184. /* Wait for last operation to be completed */
  185. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  186. if(status == FLASH_COMPLETE)
  187. {
  188. /* If the previous operation is completed, proceed to program the new first
  189. half word */
  190. FLASH->CR |= FLASH_CR_PG;
  191. *(__IO uint16_t*)Address = (uint16_t)Data;
  192. /* Wait for last operation to be completed */
  193. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  194. if(status == FLASH_COMPLETE)
  195. {
  196. /* If the previous operation is completed, proceed to program the new second
  197. half word */
  198. tmp = Address + 2;
  199. *(__IO uint16_t*) tmp = Data >> 16;
  200. /* Wait for last operation to be completed */
  201. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  202. /* Disable the PG Bit */
  203. FLASH->CR &= ~FLASH_CR_PG;
  204. }
  205. else
  206. {
  207. /* Disable the PG Bit */
  208. FLASH->CR &= ~FLASH_CR_PG;
  209. }
  210. }
  211. FLASH_PrefetchBufferCmd(ENABLE);
  212. /* Return the Program Status */
  213. return status;
  214. }
  215. /**
  216. * @brief Programs a half word at a specified address.
  217. * @note To correctly run this function, the FLASH_Unlock() function must be called before.
  218. * @note Call the FLASH_Lock() to disable the flash memory access (recommended
  219. * to protect the FLASH memory against possible unwanted operation)
  220. * @param Address: specifies the address to be programmed.
  221. * @param Data: specifies the data to be programmed.
  222. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  223. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  224. */
  225. FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
  226. {
  227. FLASH_Status status = FLASH_COMPLETE;
  228. /* Check the parameters */
  229. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  230. FLASH_PrefetchBufferCmd(DISABLE);
  231. __ASM("ISB");
  232. /* Wait for last operation to be completed */
  233. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  234. if(status == FLASH_COMPLETE)
  235. {
  236. /* If the previous operation is completed, proceed to program the new data */
  237. FLASH->CR |= FLASH_CR_PG;
  238. *(__IO uint16_t*)Address = Data;
  239. /* Wait for last operation to be completed */
  240. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  241. /* Disable the PG Bit */
  242. FLASH->CR &= ~FLASH_CR_PG;
  243. }
  244. FLASH_PrefetchBufferCmd(ENABLE);
  245. /* Return the Program Status */
  246. return status;
  247. }
  248. #else
  249. /**
  250. * @brief Programs a word at a specified address.
  251. * @note To correctly run this function, the FLASH_Unlock() function must be called before.
  252. * @note Call the FLASH_Lock() to disable the flash memory access (recommended
  253. * to protect the FLASH memory against possible unwanted operation)
  254. * @param Address: specifies the address to be programmed.
  255. * @param Data: specifies the data to be programmed.
  256. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  257. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  258. */
  259. FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
  260. {
  261. FLASH_Status status = FLASH_COMPLETE;
  262. __IO uint32_t tmp = 0;
  263. /* Check the parameters */
  264. assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
  265. FLASH_PrefetchBufferCmd(DISABLE);
  266. __ASM("ISB");
  267. /* Wait for last operation to be completed */
  268. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  269. if(status == FLASH_COMPLETE)
  270. {
  271. /* If the previous operation is completed, proceed to program the new first word */
  272. FLASH->CR |= FLASH_CR_PG;
  273. *(__IO uint32_t*)Address = Data;
  274. /* Wait for last operation to be completed */
  275. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  276. /* Disable the PG Bit */
  277. FLASH->CR &= ~FLASH_CR_PG;
  278. }
  279. FLASH_PrefetchBufferCmd(ENABLE);
  280. /* Return the Program Status */
  281. return status;
  282. }
  283. #endif
  284. /**
  285. * @}
  286. */
  287. /**
  288. * @brief Unlocks the option bytes block access.
  289. * @param None
  290. * @retval None
  291. */
  292. void FLASH_OB_Unlock(void)
  293. {
  294. if((FLASH->CR & FLASH_CR_OPTWRE) == RESET)
  295. {
  296. /* Unlocking the option bytes block access */
  297. FLASH->OPTKEYR = FLASH_OPTKEY1;
  298. FLASH->OPTKEYR = FLASH_OPTKEY2;
  299. }
  300. }
  301. /**
  302. * @brief Locks the option bytes block access.
  303. * @param None
  304. * @retval None
  305. */
  306. void FLASH_OB_Lock(void)
  307. {
  308. /* Set the OPTWREN Bit to lock the option bytes block access */
  309. FLASH->CR &= ~FLASH_CR_OPTWRE;
  310. }
  311. /**
  312. * @brief Launch the option byte loading.
  313. * @param None
  314. * @retval None
  315. */
  316. void FLASH_OB_Launch(void)
  317. {
  318. /* Set the OBL_Launch bit to launch the option byte loading */
  319. FLASH->CR |= FLASH_CR_OBL_LAUNCH;
  320. }
  321. #if defined(FT32F072xB)
  322. /**
  323. * @brief Erases the FLASH option bytes.
  324. * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
  325. * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
  326. * bytes (recommended to protect the FLASH memory against possible unwanted operation)
  327. * @note This functions erases all option bytes except the Read protection (RDP).
  328. * @param None
  329. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  330. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  331. */
  332. FLASH_Status FLASH_OB_Erase(void)
  333. {
  334. uint16_t rdptmp = OB_RDP_Level_0;
  335. FLASH_Status status = FLASH_COMPLETE;
  336. /* Get the actual read protection Option Byte value */
  337. if(FLASH_OB_GetRDP() != RESET)
  338. {
  339. rdptmp = 0x0;
  340. }
  341. /* Wait for last operation to be completed */
  342. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  343. if(status == FLASH_COMPLETE)
  344. {
  345. /* If the previous operation is completed, proceed to erase the option bytes */
  346. FLASH->CR |= FLASH_CR_OPTER;
  347. FLASH->CR |= FLASH_CR_STRT;
  348. /* Wait for last operation to be completed */
  349. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  350. if(status == FLASH_COMPLETE)
  351. {
  352. /* If the erase operation is completed, disable the OPTER Bit */
  353. FLASH->CR &= ~FLASH_CR_OPTER;
  354. /* Enable the Option Bytes Programming operation */
  355. FLASH->CR |= FLASH_CR_OPTPG;
  356. /* Restore the last read protection Option Byte value */
  357. OB->RDP = (uint16_t)rdptmp;
  358. /* Wait for last operation to be completed */
  359. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  360. if(status != FLASH_TIMEOUT)
  361. {
  362. /* if the program operation is completed, disable the OPTPG Bit */
  363. FLASH->CR &= ~FLASH_CR_OPTPG;
  364. }
  365. }
  366. else
  367. {
  368. if (status != FLASH_TIMEOUT)
  369. {
  370. FLASH->CR &= ~FLASH_CR_OPTER;
  371. }
  372. }
  373. }
  374. /* Return the erase status */
  375. return status;
  376. }
  377. /**
  378. * @brief Write protects the desired pages
  379. * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
  380. * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
  381. * bytes (recommended to protect the FLASH memory against possible unwanted operation)
  382. * @param OB_WRP: specifies the address of the pages to be write protected.
  383. * This parameter can be:
  384. * @arg OB_WRP_Pages0to7..OB_WRP_Pages120to127
  385. * @arg OB_WRP_AllPages
  386. * @retval FLASH Status: The returned value can be:
  387. * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  388. */
  389. FLASH_Status FLASH_OB_EnableWRP(uint32_t OB_WRP)
  390. {
  391. uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
  392. FLASH_Status status = FLASH_COMPLETE;
  393. /* Check the parameters */
  394. assert_param(IS_OB_WRP(OB_WRP));
  395. OB_WRP = (uint32_t)(~OB_WRP);
  396. WRP0_Data = (uint16_t)(OB_WRP & OB_WRP0_WRP0);
  397. WRP1_Data = (uint16_t)((OB_WRP >> 8) & OB_WRP0_WRP0);
  398. WRP2_Data = (uint16_t)((OB_WRP >> 16) & OB_WRP0_WRP0) ;
  399. WRP3_Data = (uint16_t)((OB_WRP >> 24) & OB_WRP0_WRP0) ;
  400. /* Wait for last operation to be completed */
  401. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  402. if(status == FLASH_COMPLETE)
  403. {
  404. FLASH->CR |= FLASH_CR_OPTPG;
  405. if(WRP0_Data != 0xFF)
  406. {
  407. OB->WRP0 = WRP0_Data;
  408. /* Wait for last operation to be completed */
  409. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  410. }
  411. if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))
  412. {
  413. OB->WRP1 = WRP1_Data;
  414. /* Wait for last operation to be completed */
  415. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  416. }
  417. if((status == FLASH_COMPLETE) && (WRP2_Data != 0xFF))
  418. {
  419. OB->WRP2 = WRP2_Data;
  420. /* Wait for last operation to be completed */
  421. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  422. }
  423. if((status == FLASH_COMPLETE) && (WRP3_Data != 0xFF))
  424. {
  425. OB->WRP3 = WRP3_Data;
  426. /* Wait for last operation to be completed */
  427. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  428. }
  429. if(status != FLASH_TIMEOUT)
  430. {
  431. /* if the program operation is completed, disable the OPTPG Bit */
  432. FLASH->CR &= ~FLASH_CR_OPTPG;
  433. }
  434. }
  435. /* Return the write protection operation Status */
  436. return status;
  437. }
  438. /**
  439. * @brief Enables or disables the read out protection.
  440. * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
  441. * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
  442. * bytes (recommended to protect the FLASH memory against possible unwanted operation)
  443. * @param FLASH_ReadProtection_Level: specifies the read protection level.
  444. * This parameter can be:
  445. * @arg OB_RDP_Level_0: No protection
  446. * @arg OB_RDP_Level_1: Read protection of the memory
  447. * @arg
  448. * @note When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
  449. * @retval FLASH Status: The returned value can be:
  450. * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  451. */
  452. FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP)
  453. {
  454. FLASH_Status status = FLASH_COMPLETE;
  455. /* Check the parameters */
  456. assert_param(IS_OB_RDP(OB_RDP));
  457. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  458. if(status == FLASH_COMPLETE)
  459. {
  460. FLASH->CR |= FLASH_CR_OPTER;
  461. FLASH->CR |= FLASH_CR_STRT;
  462. /* Wait for last operation to be completed */
  463. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  464. if(status == FLASH_COMPLETE)
  465. {
  466. /* If the erase operation is completed, disable the OPTER Bit */
  467. FLASH->CR &= ~FLASH_CR_OPTER;
  468. /* Enable the Option Bytes Programming operation */
  469. FLASH->CR |= FLASH_CR_OPTPG;
  470. OB->RDP = OB_RDP;
  471. /* Wait for last operation to be completed */
  472. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  473. if(status != FLASH_TIMEOUT)
  474. {
  475. /* if the program operation is completed, disable the OPTPG Bit */
  476. FLASH->CR &= ~FLASH_CR_OPTPG;
  477. }
  478. }
  479. else
  480. {
  481. if(status != FLASH_TIMEOUT)
  482. {
  483. /* Disable the OPTER Bit */
  484. FLASH->CR &= ~FLASH_CR_OPTER;
  485. }
  486. }
  487. }
  488. /* Return the protection operation Status */
  489. return status;
  490. }
  491. /**
  492. * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
  493. * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
  494. * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
  495. * bytes (recommended to protect the FLASH memory against possible unwanted operation)
  496. * @param OB_IWDG: Selects the WDG mode
  497. * This parameter can be one of the following values:
  498. * @arg OB_IWDG_SW: Software WDG selected
  499. * @arg OB_IWDG_HW: Hardware WDG selected
  500. * @param OB_STOP: Reset event when entering STOP mode.
  501. * This parameter can be one of the following values:
  502. * @arg OB_STOP_NoRST: No reset generated when entering in STOP
  503. * @arg OB_STOP_RST: Reset generated when entering in STOP
  504. * @param OB_STDBY: Reset event when entering Standby mode.
  505. * This parameter can be one of the following values:
  506. * @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
  507. * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
  508. * @retval FLASH Status: The returned value can be:
  509. * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  510. */
  511. FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
  512. {
  513. FLASH_Status status = FLASH_COMPLETE;
  514. /* Check the parameters */
  515. assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
  516. assert_param(IS_OB_STOP_SOURCE(OB_STOP));
  517. assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
  518. /* Wait for last operation to be completed */
  519. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  520. if(status == FLASH_COMPLETE)
  521. {
  522. /* Enable the Option Bytes Programming operation */
  523. FLASH->CR |= FLASH_CR_OPTPG;
  524. OB->USER = (uint16_t)((uint16_t)(OB_IWDG | OB_STOP) | (uint16_t)(OB_STDBY | 0xF8));
  525. /* Wait for last operation to be completed */
  526. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  527. if(status != FLASH_TIMEOUT)
  528. {
  529. /* If the program operation is completed, disable the OPTPG Bit */
  530. FLASH->CR &= ~FLASH_CR_OPTPG;
  531. }
  532. }
  533. /* Return the Option Byte program Status */
  534. return status;
  535. }
  536. /**
  537. * @brief Sets or resets the BOOT1 option bit.
  538. * @param OB_BOOT1: Set or Reset the BOOT1 option bit.
  539. * This parameter can be one of the following values:
  540. * @arg OB_BOOT1_RESET: BOOT1 option bit reset
  541. * @arg OB_BOOT1_SET: BOOT1 option bit set
  542. * @retval None
  543. */
  544. FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1)
  545. {
  546. FLASH_Status status = FLASH_COMPLETE;
  547. /* Check the parameters */
  548. assert_param(IS_OB_BOOT1(OB_BOOT1));
  549. /* Wait for last operation to be completed */
  550. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  551. if(status == FLASH_COMPLETE)
  552. {
  553. /* Enable the Option Bytes Programming operation */
  554. FLASH->CR |= FLASH_CR_OPTPG;
  555. OB->USER = OB_BOOT1 | 0xEF;
  556. /* Wait for last operation to be completed */
  557. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  558. if(status != FLASH_TIMEOUT)
  559. {
  560. /* If the program operation is completed, disable the OPTPG Bit */
  561. FLASH->CR &= ~FLASH_CR_OPTPG;
  562. }
  563. }
  564. /* Return the Option Byte program Status */
  565. return status;
  566. }
  567. /**
  568. * @brief Sets or resets the analogue monitoring on VDDA Power source.
  569. * @param OB_VDDA_ANALOG: Selects the analog monitoring on VDDA Power source.
  570. * This parameter can be one of the following values:
  571. * @arg OB_VDDA_ANALOG_ON: Analog monitoring on VDDA Power source ON
  572. * @arg OB_VDDA_ANALOG_OFF: Analog monitoring on VDDA Power source OFF
  573. * @retval None
  574. */
  575. FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG)
  576. {
  577. FLASH_Status status = FLASH_COMPLETE;
  578. /* Check the parameters */
  579. assert_param(IS_OB_VDDA_ANALOG(OB_VDDA_ANALOG));
  580. /* Wait for last operation to be completed */
  581. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  582. if(status == FLASH_COMPLETE)
  583. {
  584. /* Enable the Option Bytes Programming operation */
  585. FLASH->CR |= FLASH_CR_OPTPG;
  586. OB->USER = OB_VDDA_ANALOG | 0xDF;
  587. /* Wait for last operation to be completed */
  588. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  589. if(status != FLASH_TIMEOUT)
  590. {
  591. /* if the program operation is completed, disable the OPTPG Bit */
  592. FLASH->CR &= ~FLASH_CR_OPTPG;
  593. }
  594. }
  595. /* Return the Option Byte program Status */
  596. return status;
  597. }
  598. /**
  599. * @brief Sets or resets the SRAM parity.
  600. * @param OB_SRAM_Parity: Set or Reset the SRAM parity enable bit.
  601. * This parameter can be one of the following values:
  602. * @arg OB_SRAM_PARITY_SET: Set SRAM parity.
  603. * @arg OB_SRAM_PARITY_RESET: Reset SRAM parity.
  604. * @retval None
  605. */
  606. FLASH_Status FLASH_OB_SRAMParityConfig(uint8_t OB_SRAM_Parity)
  607. {
  608. FLASH_Status status = FLASH_COMPLETE;
  609. /* Check the parameters */
  610. assert_param(IS_OB_SRAM_PARITY(OB_SRAM_Parity));
  611. /* Wait for last operation to be completed */
  612. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  613. if(status == FLASH_COMPLETE)
  614. {
  615. /* Enable the Option Bytes Programming operation */
  616. FLASH->CR |= FLASH_CR_OPTPG;
  617. OB->USER = OB_SRAM_Parity | 0xBF;
  618. /* Wait for last operation to be completed */
  619. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  620. if(status != FLASH_TIMEOUT)
  621. {
  622. /* if the program operation is completed, disable the OPTPG Bit */
  623. FLASH->CR &= ~FLASH_CR_OPTPG;
  624. }
  625. }
  626. /* Return the Option Byte program Status */
  627. return status;
  628. }
  629. /**
  630. * @brief Programs the FLASH User Option Byte: IWDG_SW, RST_STOP, RST_STDBY,
  631. * BOOT1 and VDDA ANALOG monitoring.
  632. * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
  633. * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
  634. * bytes (recommended to protect the FLASH memory against possible unwanted operation)
  635. * @param OB_USER: Selects all user option bytes
  636. * This parameter is a combination of the following values:
  637. * @arg OB_IWDG_SW / OB_IWDG_HW: Software / Hardware WDG selected
  638. * @arg OB_STOP_NoRST / OB_STOP_RST: No reset / Reset generated when entering in STOP
  639. * @arg OB_STDBY_NoRST / OB_STDBY_RST: No reset / Reset generated when entering in STANDBY
  640. * @arg OB_BOOT1_RESET / OB_BOOT1_SET: BOOT1 Reset / Set
  641. * @arg OB_VDDA_ANALOG_ON / OB_VDDA_ANALOG_OFF: Analog monitoring on VDDA Power source ON / OFF
  642. * @arg OB_SRAM_PARITY_SET / OB_SRAM_PARITY_RESET: SRAM Parity SET / RESET
  643. * @arg OB_BOOT0_RESET / OB_BOOT0_SET: BOOT0 Reset / Set
  644. * @arg OB_BOOT0_SW / OB_BOOT0_SW: BOOT0 pin disabled / BOOT0 pin bonded with GPIO
  645. * @retval FLASH Status: The returned value can be:
  646. * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  647. */
  648. FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER)
  649. {
  650. FLASH_Status status = FLASH_COMPLETE;
  651. /* Wait for last operation to be completed */
  652. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  653. if(status == FLASH_COMPLETE)
  654. {
  655. /* If the erase operation is completed, disable the OPTER Bit */
  656. FLASH->CR &= ~FLASH_CR_OPTER;
  657. /* Enable the Option Bytes Programming operation */
  658. FLASH->CR |= FLASH_CR_OPTPG;
  659. OB->USER = OB_USER;
  660. /* Wait for last operation to be completed */
  661. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  662. if(status != FLASH_TIMEOUT)
  663. {
  664. /* If the program operation is completed, disable the OPTPG Bit */
  665. FLASH->CR &= ~FLASH_CR_OPTPG;
  666. }
  667. }
  668. /* Return the Option Byte program Status */
  669. return status;
  670. }
  671. /**
  672. * @brief Programs a half word at a specified Option Byte Data address.
  673. * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
  674. * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
  675. * bytes (recommended to protect the FLASH memory against possible unwanted operation)
  676. * @param Address: specifies the address to be programmed.
  677. * This parameter can be 0x1FFFF804.
  678. * @param Data: specifies the data to be programmed.
  679. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  680. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  681. */
  682. FLASH_Status FLASH_OB_ProgramData(uint32_t Address, uint8_t Data)
  683. {
  684. FLASH_Status status = FLASH_COMPLETE;
  685. /* Check the parameters */
  686. assert_param(IS_OB_DATA_ADDRESS(Address));
  687. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  688. if(status == FLASH_COMPLETE)
  689. {
  690. /* Enables the Option Bytes Programming operation */
  691. FLASH->CR |= FLASH_CR_OPTPG;
  692. *(__IO uint16_t*)Address = Data;
  693. /* Wait for last operation to be completed */
  694. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  695. if(status != FLASH_TIMEOUT)
  696. {
  697. /* If the program operation is completed, disable the OPTPG Bit */
  698. FLASH->CR &= ~FLASH_CR_OPTPG;
  699. }
  700. }
  701. /* Return the Option Byte Data Program Status */
  702. return status;
  703. }
  704. #else
  705. /**
  706. * @brief Erases the FLASH option bytes.
  707. * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
  708. * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
  709. * bytes (recommended to protect the FLASH memory against possible unwanted operation)
  710. * @note This functions erases all option bytes except the Read protection (RDP).
  711. * @param None
  712. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  713. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  714. */
  715. FLASH_Status FLASH_OB_Erase(void)
  716. {
  717. uint32_t rdptmp = 0;
  718. FLASH_Status status = FLASH_COMPLETE;
  719. /* Get the actual read protection Option Byte value */
  720. if(FLASH_OB_GetRDP() != RESET)
  721. {
  722. rdptmp = 0x0000 | 0xff00; //��������1
  723. }
  724. else
  725. {
  726. rdptmp = OB_RDP_Level_0 | 0x5500; //��������0
  727. }
  728. /*Get iwdg value */
  729. // if ((uint8_t)(FLASH->OBR & (FLASH_OBR_IWDG_SW)) != RESET)
  730. // {
  731. // rdptmp |= 0x0f0000 | 0xf0000000;//HW iwdg
  732. // }
  733. // else
  734. {
  735. rdptmp |= 0x0e0000 | 0xf1000000;//sw iwdg
  736. }
  737. /* Wait for last operation to be completed */
  738. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  739. if(status == FLASH_COMPLETE)
  740. {
  741. /* If the previous operation is completed, proceed to erase the option bytes */
  742. FLASH->CR |= FLASH_CR_OPTER;
  743. FLASH->CR |= FLASH_CR_STRT;
  744. /* Wait for last operation to be completed */
  745. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  746. if(status == FLASH_COMPLETE)
  747. {
  748. /* If the erase operation is completed, disable the OPTER Bit */
  749. FLASH->CR &= ~FLASH_CR_OPTER;
  750. /* Enable the Option Bytes Programming operation */
  751. FLASH->CR |= FLASH_CR_OPTPG;
  752. /* Restore the last read protection Option Byte value */
  753. OB->USER_RDP = rdptmp;
  754. /* Wait for last operation to be completed */
  755. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  756. if(status != FLASH_TIMEOUT)
  757. {
  758. /* if the program operation is completed, disable the OPTPG Bit */
  759. FLASH->CR &= ~FLASH_CR_OPTPG;
  760. }
  761. }
  762. else
  763. {
  764. if (status != FLASH_TIMEOUT)
  765. {
  766. /* Disable the OPTPG Bit */
  767. FLASH->CR &= ~FLASH_CR_OPTPG;
  768. }
  769. }
  770. }
  771. /* Return the erase status */
  772. return status;
  773. }
  774. /**
  775. * @brief Write protects the desired pages
  776. * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
  777. * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
  778. * bytes (recommended to protect the FLASH memory against possible unwanted operation)
  779. * @param OB_WRP: specifies the address of the pages to be write protected.
  780. * This parameter can be:
  781. * @arg OB_WRP_Pages0to3..OB_WRP_Pages60to63
  782. * @arg OB_WRP_AllPages
  783. * @retval FLASH Status: The returned value can be:
  784. * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  785. */
  786. FLASH_Status FLASH_OB_EnableWRP(uint32_t OB_WRP)
  787. {
  788. uint8_t WRP0_Data = 0xFF,WRP1_Data = 0xFF,WRP2_Data = 0xFF,WRP3_Data = 0xFF;
  789. uint8_t nWRP0_Data = 0,nWRP1_Data = 0,nWRP2_Data = 0,nWRP3_Data = 0;
  790. FLASH_Status status = FLASH_COMPLETE;
  791. /* Check the parameters */
  792. assert_param(IS_OB_WRP(OB_WRP));
  793. OB_WRP = (uint32_t)(~OB_WRP);
  794. WRP0_Data = (uint8_t)(OB_WRP & OB_WRP0_WRP0);
  795. nWRP0_Data = ~WRP0_Data;
  796. WRP1_Data = (uint8_t)((OB_WRP >> 8) & OB_WRP0_WRP0);
  797. nWRP1_Data = ~WRP1_Data;
  798. WRP2_Data = (uint8_t)((OB_WRP >> 16) & OB_WRP0_WRP0);
  799. nWRP2_Data = ~WRP2_Data;
  800. WRP3_Data = (uint8_t)((OB_WRP >> 24) & OB_WRP0_WRP0);
  801. nWRP3_Data = ~WRP3_Data;
  802. /* Wait for last operation to be completed */
  803. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  804. if(status == FLASH_COMPLETE)
  805. {
  806. FLASH->CR |= FLASH_CR_OPTPG;
  807. if((WRP0_Data != 0xFF) ||(WRP1_Data != 0xFF))
  808. {
  809. OB->WRP1_WRP0 = (WRP0_Data) | (nWRP0_Data<<8) | (WRP1_Data<<16) | (nWRP1_Data<<24);
  810. /* Wait for last operation to be completed */
  811. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  812. }
  813. if((status == FLASH_COMPLETE) && (WRP2_Data != 0xFF))
  814. {
  815. OB->WRP3_WRP2 = (WRP2_Data) | (nWRP2_Data<<8) | (WRP3_Data<<16) | (nWRP3_Data<<24);
  816. /* Wait for last operation to be completed */
  817. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  818. }
  819. if(status != FLASH_TIMEOUT)
  820. {
  821. /* if the program operation is completed, disable the OPTPG Bit */
  822. FLASH->CR &= ~FLASH_CR_OPTPG;
  823. }
  824. }
  825. /* Return the write protection operation Status */
  826. return status;
  827. }
  828. /**
  829. * @brief Enables or disables the read out protection.
  830. * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
  831. * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
  832. * bytes (recommended to protect the FLASH memory against possible unwanted operation)
  833. * @param FLASH_ReadProtection_Level: specifies the read protection level.
  834. * This parameter can be:
  835. * @arg OB_RDP_Level_0: No protection
  836. * @arg OB_RDP_Level_1: Read protection of the memory
  837. * @arg
  838. * @note When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
  839. * @retval FLASH Status: The returned value can be:
  840. * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  841. */
  842. FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP)
  843. {
  844. FLASH_Status status = FLASH_COMPLETE;
  845. uint32_t ob_user_rdp = 0;
  846. uint16_t ob_rdp_nrdp = 0;
  847. /* Check the parameters */
  848. assert_param(IS_OB_RDP(OB_RDP));
  849. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  850. ob_user_rdp = OB->USER_RDP & 0xffff0000;
  851. ob_rdp_nrdp = OB_RDP;
  852. OB_RDP = ~OB_RDP;
  853. ob_rdp_nrdp |= OB_RDP<<8;
  854. ob_user_rdp |= ob_rdp_nrdp;
  855. if(status == FLASH_COMPLETE)
  856. {
  857. FLASH->CR |= FLASH_CR_OPTER;
  858. FLASH->CR |= FLASH_CR_STRT;
  859. /* Wait for last operation to be completed */
  860. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  861. if(status == FLASH_COMPLETE)
  862. {
  863. /* If the erase operation is completed, disable the OPTER Bit */
  864. FLASH->CR &= ~FLASH_CR_OPTER;
  865. /* Enable the Option Bytes Programming operation */
  866. FLASH->CR |= FLASH_CR_OPTPG;
  867. OB->USER_RDP = ob_user_rdp;
  868. /* Wait for last operation to be completed */
  869. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  870. if(status != FLASH_TIMEOUT)
  871. {
  872. /* if the program operation is completed, disable the OPTPG Bit */
  873. FLASH->CR &= ~FLASH_CR_OPTPG;
  874. }
  875. }
  876. else
  877. {
  878. if(status != FLASH_TIMEOUT)
  879. {
  880. /* Disable the OPTER Bit */
  881. FLASH->CR &= ~FLASH_CR_OPTER;
  882. }
  883. }
  884. }
  885. /* Return the protection operation Status */
  886. return status;
  887. }
  888. /**
  889. * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
  890. * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
  891. * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
  892. * bytes (recommended to protect the FLASH memory against possible unwanted operation)
  893. * @param OB_IWDG: Selects the WDG mode
  894. * This parameter can be one of the following values:
  895. * @arg OB_IWDG_SW: Software WDG selected
  896. * @arg OB_IWDG_HW: Hardware WDG selected
  897. * @param OB_STOP: Reset event when entering STOP mode.
  898. * This parameter can be one of the following values:
  899. * @arg OB_STOP_NoRST: No reset generated when entering in STOP
  900. * @arg OB_STOP_RST: Reset generated when entering in STOP
  901. * @param OB_STDBY: Reset event when entering Standby mode.
  902. * This parameter can be one of the following values:
  903. * @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
  904. * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
  905. * @retval FLASH Status: The returned value can be:
  906. * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  907. */
  908. FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
  909. {
  910. FLASH_Status status = FLASH_COMPLETE;
  911. uint32_t ob_user_rdp = 0;
  912. uint8_t ob_user = 0,ob_nuser = 0;
  913. /* Check the parameters */
  914. assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
  915. assert_param(IS_OB_STOP_SOURCE(OB_STOP));
  916. assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
  917. /* Wait for last operation to be completed */
  918. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  919. ob_user = OB->USER_RDP >>16 & 0xF8;//Clear
  920. ob_user |= OB_IWDG | OB_STOP | OB_STDBY;
  921. ob_nuser = ~ob_user;
  922. ob_user_rdp = (OB->USER_RDP &0x0000ffff) | ob_user<<16 | ob_nuser<<24;
  923. if(status == FLASH_COMPLETE)
  924. {
  925. FLASH->CR |= FLASH_CR_OPTER;
  926. FLASH->CR |= FLASH_CR_STRT;
  927. /* Wait for last operation to be completed */
  928. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  929. if(status == FLASH_COMPLETE)
  930. {
  931. /* If the erase operation is completed, disable the OPTER Bit */
  932. FLASH->CR &= ~FLASH_CR_OPTER;
  933. /* Enable the Option Bytes Programming operation */
  934. FLASH->CR |= FLASH_CR_OPTPG;
  935. OB->USER_RDP = ob_user_rdp;
  936. /* Wait for last operation to be completed */
  937. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  938. if(status != FLASH_TIMEOUT)
  939. {
  940. /* If the program operation is completed, disable the OPTPG Bit */
  941. FLASH->CR &= ~FLASH_CR_OPTPG;
  942. }
  943. }
  944. }
  945. /* Return the Option Byte program Status */
  946. return status;
  947. }
  948. /**
  949. * @brief Sets or resets the BOOT1 option bit.
  950. * @param OB_BOOT1: Set or Reset the BOOT1 option bit.
  951. * This parameter can be one of the following values:
  952. * @arg OB_BOOT1_RESET: BOOT1 option bit reset
  953. * @arg OB_BOOT1_SET: BOOT1 option bit set
  954. * @retval None
  955. */
  956. FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1)
  957. {
  958. FLASH_Status status = FLASH_COMPLETE;
  959. uint32_t ob_user_rdp = 0;
  960. uint8_t ob_user =0,ob_nuser = 0;
  961. /* Check the parameters */
  962. assert_param(IS_OB_BOOT1(OB_BOOT1));
  963. /* Wait for last operation to be completed */
  964. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  965. ob_user = OB->USER_RDP >>16 & 0xef;//Clear
  966. ob_user |= OB_BOOT1;
  967. ob_nuser = ~ob_user;
  968. ob_user_rdp = (OB->USER_RDP &0x0000ffff) | ob_user<<16 | ob_nuser<<24;
  969. if(status == FLASH_COMPLETE)
  970. {
  971. FLASH->CR |= FLASH_CR_OPTER;
  972. FLASH->CR |= FLASH_CR_STRT;
  973. /* Wait for last operation to be completed */
  974. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  975. if(status == FLASH_COMPLETE)
  976. {
  977. /* If the erase operation is completed, disable the OPTER Bit */
  978. FLASH->CR &= ~FLASH_CR_OPTER;
  979. /* Enable the Option Bytes Programming operation */
  980. FLASH->CR |= FLASH_CR_OPTPG;
  981. OB->USER_RDP = ob_user_rdp;
  982. /* Wait for last operation to be completed */
  983. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  984. if(status != FLASH_TIMEOUT)
  985. {
  986. /* If the program operation is completed, disable the OPTPG Bit */
  987. FLASH->CR &= ~FLASH_CR_OPTPG;
  988. }
  989. }
  990. }
  991. /* Return the Option Byte program Status */
  992. return status;
  993. }
  994. /**
  995. * @brief Sets or resets the analogue monitoring on VDDA Power source.
  996. * @param OB_VDDA_ANALOG: Selects the analog monitoring on VDDA Power source.
  997. * This parameter can be one of the following values:
  998. * @arg OB_VDDA_ANALOG_ON: Analog monitoring on VDDA Power source ON
  999. * @arg OB_VDDA_ANALOG_OFF: Analog monitoring on VDDA Power source OFF
  1000. * @retval None
  1001. */
  1002. FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG)
  1003. {
  1004. FLASH_Status status = FLASH_COMPLETE;
  1005. uint32_t ob_user_rdp = 0;
  1006. uint8_t ob_user = 0,ob_nuser = 0;
  1007. /* Check the parameters */
  1008. assert_param(IS_OB_VDDA_ANALOG(OB_VDDA_ANALOG));
  1009. /* Wait for last operation to be completed */
  1010. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  1011. ob_user = OB->USER_RDP >>16 & 0xdf;//Clear
  1012. ob_user |= OB_VDDA_ANALOG;
  1013. ob_nuser = ~ob_user;
  1014. ob_user_rdp = (OB->USER_RDP &0x0000ffff) | ob_user<<16 | ob_nuser<<24;
  1015. if(status == FLASH_COMPLETE)
  1016. {
  1017. FLASH->CR |= FLASH_CR_OPTER;
  1018. FLASH->CR |= FLASH_CR_STRT;
  1019. /* Wait for last operation to be completed */
  1020. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  1021. if(status == FLASH_COMPLETE)
  1022. {
  1023. /* If the erase operation is completed, disable the OPTER Bit */
  1024. FLASH->CR &= ~FLASH_CR_OPTER;
  1025. /* Enable the Option Bytes Programming operation */
  1026. FLASH->CR |= FLASH_CR_OPTPG;
  1027. OB->USER_RDP = ob_user_rdp;
  1028. /* Wait for last operation to be completed */
  1029. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  1030. if(status != FLASH_TIMEOUT)
  1031. {
  1032. /* if the program operation is completed, disable the OPTPG Bit */
  1033. FLASH->CR &= ~FLASH_CR_OPTPG;
  1034. }
  1035. }
  1036. }
  1037. /* Return the Option Byte program Status */
  1038. return status;
  1039. }
  1040. /**
  1041. * @brief Sets or resets the SRAM parity.
  1042. * @param OB_SRAM_Parity: Set or Reset the SRAM parity enable bit.
  1043. * This parameter can be one of the following values:
  1044. * @arg OB_SRAM_PARITY_SET: Set SRAM parity.
  1045. * @arg OB_SRAM_PARITY_RESET: Reset SRAM parity.
  1046. * @retval None
  1047. */
  1048. FLASH_Status FLASH_OB_SRAMParityConfig(uint8_t OB_SRAM_Parity)
  1049. {
  1050. FLASH_Status status = FLASH_COMPLETE;
  1051. uint32_t ob_user_rdp = 0;
  1052. uint8_t ob_user = 0,ob_nuser = 0;
  1053. /* Check the parameters */
  1054. assert_param(IS_OB_SRAM_PARITY(OB_SRAM_Parity));
  1055. /* Wait for last operation to be completed */
  1056. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  1057. ob_user = OB->USER_RDP >>16 & 0xbf;//Clear
  1058. ob_user |= OB_SRAM_Parity;
  1059. ob_nuser = ~ob_user;
  1060. ob_user_rdp = (OB->USER_RDP &0x0000ffff) | ob_user<<16 | ob_nuser<<24;
  1061. if(status == FLASH_COMPLETE)
  1062. {
  1063. FLASH->CR |= FLASH_CR_OPTER;
  1064. FLASH->CR |= FLASH_CR_STRT;
  1065. /* Wait for last operation to be completed */
  1066. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  1067. if(status == FLASH_COMPLETE)
  1068. {
  1069. /* If the erase operation is completed, disable the OPTER Bit */
  1070. FLASH->CR &= ~FLASH_CR_OPTER;
  1071. /* Enable the Option Bytes Programming operation */
  1072. FLASH->CR |= FLASH_CR_OPTPG;
  1073. OB->USER_RDP = ob_user_rdp;
  1074. /* Wait for last operation to be completed */
  1075. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  1076. if(status != FLASH_TIMEOUT)
  1077. {
  1078. /* if the program operation is completed, disable the OPTPG Bit */
  1079. FLASH->CR &= ~FLASH_CR_OPTPG;
  1080. }
  1081. }
  1082. }
  1083. /* Return the Option Byte program Status */
  1084. return status;
  1085. }
  1086. /**
  1087. * @brief Programs the FLASH User Option Byte: IWDG_SW, RST_STOP, RST_STDBY,
  1088. * BOOT1 and VDDA ANALOG monitoring.
  1089. * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
  1090. * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
  1091. * bytes (recommended to protect the FLASH memory against possible unwanted operation)
  1092. * @param OB_USER: Selects all user option bytes
  1093. * This parameter is a combination of the following values:
  1094. * @arg OB_IWDG_SW / OB_IWDG_HW: Software / Hardware WDG selected
  1095. * @arg OB_STOP_NoRST / OB_STOP_RST: No reset / Reset generated when entering in STOP
  1096. * @arg OB_STDBY_NoRST / OB_STDBY_RST: No reset / Reset generated when entering in STANDBY
  1097. * @arg OB_BOOT1_RESET / OB_BOOT1_SET: BOOT1 Reset / Set
  1098. * @arg OB_VDDA_ANALOG_ON / OB_VDDA_ANALOG_OFF: Analog monitoring on VDDA Power source ON / OFF
  1099. * @arg OB_SRAM_PARITY_SET / OB_SRAM_PARITY_RESET: SRAM Parity SET / RESET
  1100. * @arg OB_BOOT0_RESET / OB_BOOT0_SET: BOOT0 Reset / Set
  1101. * @arg OB_BOOT0_SW / OB_BOOT0_SW: BOOT0 pin disabled / BOOT0 pin bonded with GPIO
  1102. * @retval FLASH Status: The returned value can be:
  1103. * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  1104. */
  1105. FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER)
  1106. {
  1107. FLASH_Status status = FLASH_COMPLETE;
  1108. uint32_t ob_user_rdp = 0;
  1109. uint8_t ob_user = 0,ob_nuser = 0;
  1110. /* Wait for last operation to be completed */
  1111. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  1112. ob_user = OB->USER_RDP >>16 & 0x00;//Clear
  1113. ob_user |= OB_USER;
  1114. ob_nuser = ~ob_user;
  1115. ob_user_rdp = (OB->USER_RDP &0x0000ffff) | ob_user<<16 | ob_nuser<<24;
  1116. if(status == FLASH_COMPLETE)
  1117. {
  1118. FLASH->CR |= FLASH_CR_OPTER;
  1119. FLASH->CR |= FLASH_CR_STRT;
  1120. /* Wait for last operation to be completed */
  1121. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  1122. if(status == FLASH_COMPLETE)
  1123. {
  1124. /* If the erase operation is completed, disable the OPTER Bit */
  1125. FLASH->CR &= ~FLASH_CR_OPTER;
  1126. /* Enable the Option Bytes Programming operation */
  1127. FLASH->CR |= FLASH_CR_OPTPG;
  1128. OB->USER_RDP = ob_user_rdp;
  1129. /* Wait for last operation to be completed */
  1130. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  1131. if(status != FLASH_TIMEOUT)
  1132. {
  1133. /* If the program operation is completed, disable the OPTPG Bit */
  1134. FLASH->CR &= ~FLASH_CR_OPTPG;
  1135. }
  1136. }
  1137. }
  1138. /* Return the Option Byte program Status */
  1139. return status;
  1140. }
  1141. /**
  1142. * @brief Programs a half word at a specified Option Byte Data address.
  1143. * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
  1144. * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
  1145. * bytes (recommended to protect the FLASH memory against possible unwanted operation)
  1146. * @param Address: specifies the address to be programmed.
  1147. * This parameter can be 0x1FFFF804.
  1148. * @param Data: specifies the data to be programmed.
  1149. * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
  1150. * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  1151. */
  1152. FLASH_Status FLASH_OB_ProgramData(uint32_t Address, uint32_t Data)
  1153. {
  1154. FLASH_Status status = FLASH_COMPLETE;
  1155. /* Check the parameters */
  1156. assert_param(IS_OB_DATA_ADDRESS(Address));
  1157. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  1158. if(status == FLASH_COMPLETE)
  1159. {
  1160. /* Enables the Option Bytes Programming operation */
  1161. FLASH->CR |= FLASH_CR_OPTPG;
  1162. *(__IO uint32_t*)Address = Data;
  1163. /* Wait for last operation to be completed */
  1164. status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
  1165. if(status != FLASH_TIMEOUT)
  1166. {
  1167. /* If the program operation is completed, disable the OPTPG Bit */
  1168. FLASH->CR &= ~FLASH_CR_OPTPG;
  1169. }
  1170. }
  1171. /* Return the Option Byte Data Program Status */
  1172. return status;
  1173. }
  1174. #endif
  1175. /**
  1176. * @brief Returns the FLASH User Option Bytes values.
  1177. * @param None
  1178. * @retval The FLASH User Option Bytes .
  1179. */
  1180. uint8_t FLASH_OB_GetUser(void)
  1181. {
  1182. /* Return the User Option Byte */
  1183. return (uint8_t)(FLASH->OBR >> 8);
  1184. }
  1185. /**
  1186. * @brief Returns the FLASH Write Protection Option Bytes value.
  1187. * @param None
  1188. * @retval The FLASH Write Protection Option Bytes value
  1189. */
  1190. uint32_t FLASH_OB_GetWRP(void)
  1191. {
  1192. /* Return the FLASH write protection Register value */
  1193. return (uint32_t)(FLASH->WRPR);
  1194. }
  1195. /**
  1196. * @brief Checks whether the FLASH Read out Protection Status is set or not.
  1197. * @param None
  1198. * @retval FLASH ReadOut Protection Status(SET or RESET)
  1199. */
  1200. FlagStatus FLASH_OB_GetRDP(void)
  1201. {
  1202. FlagStatus readstatus = RESET;
  1203. if ((uint8_t)(FLASH->OBR & (FLASH_OBR_RDPRT1 | FLASH_OBR_RDPRT2)) != RESET)
  1204. {
  1205. readstatus = SET;
  1206. }
  1207. else
  1208. {
  1209. readstatus = RESET;
  1210. }
  1211. return readstatus;
  1212. }
  1213. /**
  1214. * @}
  1215. */
  1216. /**
  1217. * @brief Enables or disables the specified FLASH interrupts.
  1218. * @param FLASH_IT: specifies the FLASH interrupt sources to be enabled or
  1219. * disabled.
  1220. * This parameter can be any combination of the following values:
  1221. * @arg FLASH_IT_EOP: FLASH end of programming Interrupt
  1222. * @arg FLASH_IT_ERR: FLASH Error Interrupt
  1223. * @retval None
  1224. */
  1225. void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState)
  1226. {
  1227. /* Check the parameters */
  1228. assert_param(IS_FLASH_IT(FLASH_IT));
  1229. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1230. if(NewState != DISABLE)
  1231. {
  1232. /* Enable the interrupt sources */
  1233. FLASH->CR |= FLASH_IT;
  1234. }
  1235. else
  1236. {
  1237. /* Disable the interrupt sources */
  1238. FLASH->CR &= ~(uint32_t)FLASH_IT;
  1239. }
  1240. }
  1241. /**
  1242. * @brief Checks whether the specified FLASH flag is set or not.
  1243. * @param FLASH_FLAG: specifies the FLASH flag to check.
  1244. * This parameter can be one of the following values:
  1245. * @arg FLASH_FLAG_BSY: FLASH write/erase operations in progress flag
  1246. * @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag
  1247. * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
  1248. * @arg FLASH_FLAG_EOP: FLASH End of Programming flag
  1249. * @retval The new state of FLASH_FLAG (SET or RESET).
  1250. */
  1251. FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)
  1252. {
  1253. FlagStatus bitstatus = RESET;
  1254. /* Check the parameters */
  1255. assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG));
  1256. if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
  1257. {
  1258. bitstatus = SET;
  1259. }
  1260. else
  1261. {
  1262. bitstatus = RESET;
  1263. }
  1264. /* Return the new state of FLASH_FLAG (SET or RESET) */
  1265. return bitstatus;
  1266. }
  1267. /**
  1268. * @brief Clears the FLASH's pending flags.
  1269. * @param FLASH_FLAG: specifies the FLASH flags to clear.
  1270. * This parameter can be any combination of the following values:
  1271. * @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag
  1272. * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
  1273. * @arg FLASH_FLAG_EOP: FLASH End of Programming flag
  1274. * @retval None
  1275. */
  1276. void FLASH_ClearFlag(uint32_t FLASH_FLAG)
  1277. {
  1278. /* Check the parameters */
  1279. assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG));
  1280. /* Clear the flags */
  1281. FLASH->SR = FLASH_FLAG;
  1282. }
  1283. /**
  1284. * @brief Returns the FLASH Status.
  1285. * @param None
  1286. * @retval FLASH Status: The returned value can be:
  1287. * FLASH_BUSY, FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP or FLASH_COMPLETE.
  1288. */
  1289. FLASH_Status FLASH_GetStatus(void)
  1290. {
  1291. FLASH_Status FLASHstatus = FLASH_COMPLETE;
  1292. if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
  1293. {
  1294. FLASHstatus = FLASH_BUSY;
  1295. }
  1296. else
  1297. {
  1298. if((FLASH->SR & (uint32_t)FLASH_FLAG_WRPERR)!= (uint32_t)0x00)
  1299. {
  1300. FLASHstatus = FLASH_ERROR_WRP;
  1301. }
  1302. else
  1303. {
  1304. if((FLASH->SR & (uint32_t)(FLASH_SR_PGERR)) != (uint32_t)0x00)
  1305. {
  1306. FLASHstatus = FLASH_ERROR_PROGRAM;
  1307. }
  1308. else
  1309. {
  1310. FLASHstatus = FLASH_COMPLETE;
  1311. }
  1312. }
  1313. }
  1314. /* Return the FLASH Status */
  1315. return FLASHstatus;
  1316. }
  1317. /**
  1318. * @brief Waits for a FLASH operation to complete or a TIMEOUT to occur.
  1319. * @param Timeout: FLASH programming Timeout
  1320. * @retval FLASH Status: The returned value can be: FLASH_BUSY,
  1321. * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
  1322. */
  1323. FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
  1324. {
  1325. FLASH_Status status = FLASH_COMPLETE;
  1326. /* Check for the FLASH Status */
  1327. status = FLASH_GetStatus();
  1328. /* Wait for a FLASH operation to complete or a TIMEOUT to occur */
  1329. while((status == FLASH_BUSY) && (Timeout != 0x00))
  1330. {
  1331. status = FLASH_GetStatus();
  1332. Timeout--;
  1333. }
  1334. if(Timeout == 0x00 )
  1335. {
  1336. status = FLASH_TIMEOUT;
  1337. }
  1338. /* Return the operation status */
  1339. return status;
  1340. }
  1341. /**
  1342. * @}
  1343. */
  1344. /**
  1345. * @}
  1346. */
  1347. /**
  1348. * @}
  1349. */
  1350. /**
  1351. * @}
  1352. */
  1353. /************************ (C) COPYRIGHT FMD *****END OF FILE****/