n32g45x_flash.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_flash.c
  29. * @author Nations
  30. * @version v1.0.3
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "n32g45x_flash.h"
  35. /** @addtogroup N32G45X_StdPeriph_Driver
  36. * @{
  37. */
  38. /** @addtogroup FLASH
  39. * @brief FLASH driver modules
  40. * @{
  41. */
  42. /** @addtogroup FLASH_Private_TypesDefinitions
  43. * @{
  44. */
  45. /**
  46. * @}
  47. */
  48. /** @addtogroup FLASH_Private_Defines
  49. * @{
  50. */
  51. /* Flash Access Control Register bits */
  52. #define AC_LATENCY_MSK ((uint32_t)0x000000F8)
  53. #define AC_PRFTBE_MSK ((uint32_t)0xFFFFFFEF)
  54. #define AC_ICAHEN_MSK ((uint32_t)0xFFFFFF7F)
  55. /* Flash Access Control Register bits */
  56. #define AC_PRFTBS_MSK ((uint32_t)0x00000020)
  57. #define AC_ICAHRST_MSK ((uint32_t)0x00000040)
  58. /* Flash Control Register bits */
  59. #define CTRL_Set_PG ((uint32_t)0x00000001)
  60. #define CTRL_Reset_PG ((uint32_t)0x00003FFE)
  61. #define CTRL_Set_PER ((uint32_t)0x00000002)
  62. #define CTRL_Reset_PER ((uint32_t)0x00003FFD)
  63. #define CTRL_Set_MER ((uint32_t)0x00000004)
  64. #define CTRL_Reset_MER ((uint32_t)0x00003FFB)
  65. #define CTRL_Set_OPTPG ((uint32_t)0x00000010)
  66. #define CTRL_Reset_OPTPG ((uint32_t)0x00003FEF)
  67. #define CTRL_Set_OPTER ((uint32_t)0x00000020)
  68. #define CTRL_Reset_OPTER ((uint32_t)0x00003FDF)
  69. #define CTRL_Set_START ((uint32_t)0x00000040)
  70. #define CTRL_Set_LOCK ((uint32_t)0x00000080)
  71. #define CTRL_Reset_SMPSEL ((uint32_t)0x00003EFF)
  72. #define CTRL_SMPSEL_SMP1 ((uint32_t)0x00000000)
  73. #define CTRL_SMPSEL_SMP2 ((uint32_t)0x00000100)
  74. /* FLASH Mask */
  75. #define RDPRTL1_MSK ((uint32_t)0x00000002)
  76. #define RDPRTL2_MSK ((uint32_t)0x80000000)
  77. #define OBR_USER_MSK ((uint32_t)0x0000001C)
  78. #define WRP0_MSK ((uint32_t)0x000000FF)
  79. #define WRP1_MSK ((uint32_t)0x0000FF00)
  80. #define WRP2_MSK ((uint32_t)0x00FF0000)
  81. #define WRP3_MSK ((uint32_t)0xFF000000)
  82. /* FLASH Keys */
  83. #define L1_RDP_Key ((uint32_t)0xFFFF00A5)
  84. #define RDP_USER_Key ((uint32_t)0xFFF800A5)
  85. #define L2_RDP_Key ((uint32_t)0xFFFF33CC)
  86. #define FLASH_KEY1 ((uint32_t)0x45670123)
  87. #define FLASH_KEY2 ((uint32_t)0xCDEF89AB)
  88. /* Delay definition */
  89. #define EraseTimeout ((uint32_t)0x000B0000)
  90. #define ProgramTimeout ((uint32_t)0x00002000)
  91. /**
  92. * @}
  93. */
  94. /** @addtogroup FLASH_Private_Macros
  95. * @{
  96. */
  97. /**
  98. * @}
  99. */
  100. /** @addtogroup FLASH_Private_Variables
  101. * @{
  102. */
  103. /**
  104. * @}
  105. */
  106. /** @addtogroup FLASH_Private_FunctionPrototypes
  107. * @{
  108. */
  109. /**
  110. * @}
  111. */
  112. /** @addtogroup FLASH_Private_Functions
  113. * @{
  114. */
  115. /**
  116. * @brief Sets the code latency value.
  117. * @note This function can be used for N32G45X devices.
  118. * @param FLASH_Latency specifies the FLASH Latency value.
  119. * This parameter can be one of the following values:
  120. * @arg FLASH_LATENCY_0 FLASH Zero Latency cycle
  121. * @arg FLASH_LATENCY_1 FLASH One Latency cycle
  122. * @arg FLASH_LATENCY_2 FLASH Two Latency cycles
  123. * @arg FLASH_LATENCY_3 FLASH Three Latency cycles
  124. * @arg FLASH_LATENCY_4 FLASH Four Latency cycles
  125. */
  126. void FLASH_SetLatency(uint32_t FLASH_Latency)
  127. {
  128. uint32_t tmpregister = 0;
  129. /* Check the parameters */
  130. assert_param(IS_FLASH_LATENCY(FLASH_Latency));
  131. /* Read the AC register */
  132. tmpregister = FLASH->AC;
  133. /* Sets the Latency value */
  134. tmpregister &= AC_LATENCY_MSK;
  135. tmpregister |= FLASH_Latency;
  136. /* Write the AC register */
  137. FLASH->AC = tmpregister;
  138. }
  139. /**
  140. * @brief Enables or disables the Prefetch Buffer.
  141. * @note This function can be used for N32G45X devices.
  142. * @param FLASH_PrefetchBuf specifies the Prefetch buffer status.
  143. * This parameter can be one of the following values:
  144. * @arg FLASH_PrefetchBuf_EN FLASH Prefetch Buffer Enable
  145. * @arg FLASH_PrefetchBuf_DIS FLASH Prefetch Buffer Disable
  146. */
  147. void FLASH_PrefetchBufSet(uint32_t FLASH_PrefetchBuf)
  148. {
  149. /* Check the parameters */
  150. assert_param(IS_FLASH_PREFETCHBUF_STATE(FLASH_PrefetchBuf));
  151. /* Enable or disable the Prefetch Buffer */
  152. FLASH->AC &= AC_PRFTBE_MSK;
  153. FLASH->AC |= FLASH_PrefetchBuf;
  154. }
  155. /**
  156. * @brief ICache Reset.
  157. * @note This function can be used for N32G45X devices.
  158. */
  159. void FLASH_iCacheRST(void)
  160. {
  161. /* ICache Reset */
  162. FLASH->AC |= FLASH_AC_ICAHRST;
  163. }
  164. /**
  165. * @brief Enables or disables the iCache.
  166. * @note This function can be used for N32G45X devices.
  167. * @param FLASH_iCache specifies the iCache status.
  168. * This parameter can be one of the following values:
  169. * @arg FLASH_iCache_EN FLASH iCache Enable
  170. * @arg FLASH_iCache_DIS FLASH iCache Disable
  171. */
  172. void FLASH_iCacheCmd(uint32_t FLASH_iCache)
  173. {
  174. /* Check the parameters */
  175. assert_param(IS_FLASH_ICACHE_STATE(FLASH_iCache));
  176. /* Enable or disable the iCache */
  177. FLASH->AC &= AC_ICAHEN_MSK;
  178. FLASH->AC |= FLASH_iCache;
  179. }
  180. /**
  181. * @brief Checks whether the FLASH SMPSEL is SMP1 or SMP2.
  182. * @note This function can be used for N32G45X devices.
  183. * @param FLASH_smpsel FLASH_SMP1 or FLASH_SMP2
  184. * @return FLASH_SMPSEL_SMP1 or FLASH_SMPSEL_SMP2.
  185. */
  186. void FLASH_SetSMPSELStatus(uint32_t FLASH_smpsel)
  187. {
  188. /* Check the parameters */
  189. assert_param(IS_FLASH_SMPSEL_STATE(FLASH_smpsel));
  190. /* SMP1 or SMP2 */
  191. FLASH->CTRL &= CTRL_Reset_SMPSEL;
  192. FLASH->CTRL |= FLASH_smpsel;
  193. }
  194. /**
  195. * @brief Unlocks the FLASH Program Erase Controller.
  196. * @note This function can be used for N32G45X devices.
  197. * - For N32G45X devices this function unlocks Bank1.
  198. * to FLASH_UnlockBank1 function..
  199. */
  200. void FLASH_Unlock(void)
  201. {
  202. /* Authorize the FPEC of Bank1 Access */
  203. FLASH->KEY = FLASH_KEY1;
  204. FLASH->KEY = FLASH_KEY2;
  205. }
  206. /**
  207. * @brief Locks the FLASH Program Erase Controller.
  208. * @note This function can be used for N32G45X devices.
  209. * - For N32G45X devices this function Locks Bank1.
  210. * to FLASH_LockBank1 function.
  211. */
  212. void FLASH_Lock(void)
  213. {
  214. /* Set the Lock Bit to lock the FPEC and the CTRL of Bank1 */
  215. FLASH->CTRL |= CTRL_Set_LOCK;
  216. }
  217. /**
  218. * @brief Erases a specified FLASH page.
  219. * @note This function can be used for N32G45X devices.
  220. * @param Page_Address The page address to be erased.
  221. * @return FLASH Status: The returned value can be: FLASH_BUSY,
  222. * FLASH_ERR_PG, FLASH_ERR_PV, FLASH_ERR_WRP, FLASH_COMPL,
  223. * FLASH_ERR_EV or FLASH_TIMEOUT.
  224. */
  225. FLASH_STS FLASH_EraseOnePage(uint32_t Page_Address)
  226. {
  227. FLASH_STS status = FLASH_COMPL;
  228. /* Check the parameters */
  229. assert_param(IS_FLASH_ADDRESS(Page_Address));
  230. /* Clears the FLASH's pending flags */
  231. FLASH_ClearFlag(FLASH_STS_CLRFLAG);
  232. /* Wait for last operation to be completed */
  233. status = FLASH_WaitForLastOpt(EraseTimeout);
  234. if (status == FLASH_COMPL)
  235. {
  236. /* if the previous operation is completed, proceed to erase the page */
  237. FLASH->CTRL |= CTRL_Set_PER;
  238. FLASH->ADD = Page_Address;
  239. FLASH->CTRL |= CTRL_Set_START;
  240. /* Wait for last operation to be completed */
  241. status = FLASH_WaitForLastOpt(EraseTimeout);
  242. /* Disable the PER Bit */
  243. FLASH->CTRL &= CTRL_Reset_PER;
  244. }
  245. /* Return the Erase Status */
  246. return status;
  247. }
  248. /**
  249. * @brief Erases all FLASH pages.
  250. * @note This function can be used for all N32G45X devices.
  251. * @return FLASH Status: The returned value can be: FLASH_BUSY,
  252. * FLASH_ERR_PG, FLASH_ERR_PV, FLASH_ERR_WRP, FLASH_COMPL,
  253. * FLASH_ERR_EV or FLASH_TIMEOUT.
  254. */
  255. FLASH_STS FLASH_MassErase(void)
  256. {
  257. FLASH_STS status = FLASH_COMPL;
  258. /* Clears the FLASH's pending flags */
  259. FLASH_ClearFlag(FLASH_STS_CLRFLAG);
  260. /* Wait for last operation to be completed */
  261. status = FLASH_WaitForLastOpt(EraseTimeout);
  262. if (status == FLASH_COMPL)
  263. {
  264. /* if the previous operation is completed, proceed to erase all pages */
  265. FLASH->CTRL |= CTRL_Set_MER;
  266. FLASH->CTRL |= CTRL_Set_START;
  267. /* Wait for last operation to be completed */
  268. status = FLASH_WaitForLastOpt(EraseTimeout);
  269. /* Disable the MER Bit */
  270. FLASH->CTRL &= CTRL_Reset_MER;
  271. }
  272. /* Return the Erase Status */
  273. return status;
  274. }
  275. /**
  276. * @brief Erases the FLASH option bytes.
  277. * @note This functions erases all option bytes except the Read protection (RDP).
  278. * @note This function can be used for N32G45X devices.
  279. * @return FLASH Status: The returned value can be: FLASH_BUSY,
  280. * FLASH_ERR_PG, FLASH_ERR_PV, FLASH_ERR_WRP, FLASH_COMPL,
  281. * FLASH_ERR_EV, FLASH_ERR_RDP2 or FLASH_TIMEOUT.
  282. */
  283. FLASH_STS FLASH_EraseOB(void)
  284. {
  285. uint32_t rdptmp = L1_RDP_Key;
  286. FLASH_STS status = FLASH_COMPL;
  287. /* Get the actual read protection L2 Option Byte value */
  288. if (FLASH_GetReadOutProtectionL2STS() != RESET)
  289. {
  290. status = FLASH_ERR_RDP2;
  291. return status;
  292. }
  293. /* Get the actual read protection Option Byte value */
  294. if (FLASH_GetReadOutProtectionSTS() != RESET)
  295. {
  296. rdptmp = (L1_RDP_Key & FLASH_USER_USER);
  297. }
  298. /* Clears the FLASH's pending flags */
  299. FLASH_ClearFlag(FLASH_STS_CLRFLAG);
  300. /* Wait for last operation to be completed */
  301. status = FLASH_WaitForLastOpt(EraseTimeout);
  302. if (status == FLASH_COMPL)
  303. {
  304. /* Authorize the small information block programming */
  305. FLASH->OPTKEY = FLASH_KEY1;
  306. FLASH->OPTKEY = FLASH_KEY2;
  307. /* if the previous operation is completed, proceed to erase the option bytes */
  308. FLASH->CTRL |= CTRL_Set_OPTER;
  309. FLASH->CTRL |= CTRL_Set_START;
  310. /* Wait for last operation to be completed */
  311. status = FLASH_WaitForLastOpt(EraseTimeout);
  312. if (status == FLASH_COMPL)
  313. {
  314. /* Clears the FLASH's pending flags */
  315. FLASH_ClearFlag(FLASH_STS_CLRFLAG);
  316. /* if the erase operation is completed, disable the OPTER Bit */
  317. FLASH->CTRL &= CTRL_Reset_OPTER;
  318. /* Enable the Option Bytes Programming operation */
  319. FLASH->CTRL |= CTRL_Set_OPTPG;
  320. /* Restore the last read protection Option Byte value */
  321. OB->USER_RDP = (uint32_t)rdptmp;
  322. /* Wait for last operation to be completed */
  323. status = FLASH_WaitForLastOpt(ProgramTimeout);
  324. if (status != FLASH_TIMEOUT)
  325. {
  326. /* if the program operation is completed, disable the OPTPG Bit */
  327. FLASH->CTRL &= CTRL_Reset_OPTPG;
  328. }
  329. }
  330. else
  331. {
  332. if (status != FLASH_TIMEOUT)
  333. {
  334. /* Disable the OPTER Bit */
  335. FLASH->CTRL &= CTRL_Reset_OPTER;
  336. }
  337. }
  338. }
  339. /* Return the erase status */
  340. return status;
  341. }
  342. /**
  343. * @brief Programs a word at a specified address.
  344. * @note This function can be used for N32G45X devices.
  345. * @param Address specifies the address to be programmed.
  346. * @param Data specifies the data to be programmed.
  347. * @return FLASH Status: The returned value can be: FLASH_BUSY,
  348. * FLASH_ERR_PG, FLASH_ERR_PV, FLASH_ERR_WRP, FLASH_COMPL,
  349. * FLASH_ERR_EV, FLASH_ERR_ADD or FLASH_TIMEOUT.
  350. */
  351. FLASH_STS FLASH_ProgramWord(uint32_t Address, uint32_t Data)
  352. {
  353. FLASH_STS status = FLASH_COMPL;
  354. /* Check the parameters */
  355. assert_param(IS_FLASH_ADDRESS(Address));
  356. if((Address & (uint32_t)0x3) != 0)
  357. {
  358. /* The programming address is not a multiple of 4 */
  359. status = FLASH_ERR_ADD;
  360. return status;
  361. }
  362. /* Clears the FLASH's pending flags */
  363. FLASH_ClearFlag(FLASH_STS_CLRFLAG);
  364. /* Wait for last operation to be completed */
  365. status = FLASH_WaitForLastOpt(ProgramTimeout);
  366. if (status == FLASH_COMPL)
  367. {
  368. /* if the previous operation is completed, proceed to program the new word */
  369. FLASH->CTRL |= CTRL_Set_PG;
  370. *(__IO uint32_t*)Address = (uint32_t)Data;
  371. /* Wait for last operation to be completed */
  372. status = FLASH_WaitForLastOpt(ProgramTimeout);
  373. /* Disable the PG Bit */
  374. FLASH->CTRL &= CTRL_Reset_PG;
  375. }
  376. /* Return the Program Status */
  377. return status;
  378. }
  379. /**
  380. * @brief Programs a half word at a specified Option Byte Data address.
  381. * @note This function can be used for N32G45X devices.
  382. * @param Address specifies the address to be programmed.
  383. * This parameter can be 0x1FFFF804.
  384. * @param Data specifies the data to be programmed(Data0 and Data1).
  385. * @return FLASH Status: The returned value can be: FLASH_BUSY,
  386. * FLASH_ERR_PG, FLASH_ERR_PV, FLASH_ERR_WRP, FLASH_COMPL,
  387. * FLASH_ERR_EV, FLASH_ERR_RDP2 or FLASH_TIMEOUT.
  388. */
  389. FLASH_STS FLASH_ProgramOBData(uint32_t Address, uint32_t Data)
  390. {
  391. FLASH_STS status = FLASH_COMPL;
  392. /* Check the parameters */
  393. assert_param(IS_OB_DATA_ADDRESS(Address));
  394. /* Clears the FLASH's pending flags */
  395. FLASH_ClearFlag(FLASH_STS_CLRFLAG);
  396. /* Wait for last operation to be completed */
  397. status = FLASH_WaitForLastOpt(ProgramTimeout);
  398. /* Get the actual read protection L2 Option Byte value */
  399. if (FLASH_GetReadOutProtectionL2STS() != RESET)
  400. {
  401. status = FLASH_ERR_RDP2;
  402. return status;
  403. }
  404. if (status == FLASH_COMPL)
  405. {
  406. /* Authorize the small information block programming */
  407. FLASH->OPTKEY = FLASH_KEY1;
  408. FLASH->OPTKEY = FLASH_KEY2;
  409. /* Enables the Option Bytes Programming operation */
  410. FLASH->CTRL |= CTRL_Set_OPTPG;
  411. *(__IO uint32_t*)Address = (uint32_t)Data;
  412. /* Wait for last operation to be completed */
  413. status = FLASH_WaitForLastOpt(ProgramTimeout);
  414. if (status != FLASH_TIMEOUT)
  415. {
  416. /* if the program operation is completed, disable the OPTPG Bit */
  417. FLASH->CTRL &= CTRL_Reset_OPTPG;
  418. }
  419. }
  420. /* Return the Option Byte Data Program Status */
  421. return status;
  422. }
  423. /**
  424. * @brief Write protects the desired pages
  425. * @note This function can be used for N32G45X devices.
  426. * @param FLASH_Pages specifies the address of the pages to be write protected.
  427. * This parameter can be:
  428. * @arg For @b N32G45X_devices: value between FLASH_WRP_Pages0to1 and
  429. * FLASH_WRP_Pages60to61 or FLASH_WRP_Pages62to255
  430. * @arg FLASH_WRP_AllPages
  431. * @return FLASH Status: The returned value can be: FLASH_BUSY,
  432. * FLASH_ERR_PG, FLASH_ERR_PV, FLASH_ERR_WRP, FLASH_COMPL,
  433. * FLASH_ERR_EV, FLASH_ERR_RDP2 or FLASH_TIMEOUT.
  434. */
  435. FLASH_STS FLASH_EnWriteProtection(uint32_t FLASH_Pages)
  436. {
  437. uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
  438. FLASH_STS status = FLASH_COMPL;
  439. /* Check the parameters */
  440. assert_param(IS_FLASH_WRP_PAGE(FLASH_Pages));
  441. /* Get the actual read protection L2 Option Byte value */
  442. if (FLASH_GetReadOutProtectionL2STS() != RESET)
  443. {
  444. status = FLASH_ERR_RDP2;
  445. return status;
  446. }
  447. FLASH_Pages = (uint32_t)(~FLASH_Pages);
  448. WRP0_Data = (uint16_t)(FLASH_Pages & WRP0_MSK);
  449. WRP1_Data = (uint16_t)((FLASH_Pages & WRP1_MSK) >> 8);
  450. WRP2_Data = (uint16_t)((FLASH_Pages & WRP2_MSK) >> 16);
  451. WRP3_Data = (uint16_t)((FLASH_Pages & WRP3_MSK) >> 24);
  452. /* Clears the FLASH's pending flags */
  453. FLASH_ClearFlag(FLASH_STS_CLRFLAG);
  454. /* Wait for last operation to be completed */
  455. status = FLASH_WaitForLastOpt(ProgramTimeout);
  456. if (status == FLASH_COMPL)
  457. {
  458. /* Authorizes the small information block programming */
  459. FLASH->OPTKEY = FLASH_KEY1;
  460. FLASH->OPTKEY = FLASH_KEY2;
  461. FLASH->CTRL |= CTRL_Set_OPTPG;
  462. if ((WRP0_Data != 0xFF) || (WRP1_Data != 0xFF))
  463. {
  464. OB->WRP1_WRP0 = (((uint32_t)WRP0_Data) | (((uint32_t)WRP1_Data) << 16));
  465. /* Wait for last operation to be completed */
  466. status = FLASH_WaitForLastOpt(ProgramTimeout);
  467. }
  468. if (((WRP2_Data != 0xFF) || (WRP3_Data != 0xFF)) && (status == FLASH_COMPL))
  469. {
  470. /* Clears the FLASH's pending flags */
  471. FLASH_ClearFlag(FLASH_STS_CLRFLAG);
  472. OB->WRP3_WRP2 = (((uint32_t)WRP2_Data) | (((uint32_t)WRP3_Data) << 16));
  473. /* Wait for last operation to be completed */
  474. status = FLASH_WaitForLastOpt(ProgramTimeout);
  475. }
  476. if (status != FLASH_TIMEOUT)
  477. {
  478. /* if the program operation is completed, disable the OPTPG Bit */
  479. FLASH->CTRL &= CTRL_Reset_OPTPG;
  480. }
  481. }
  482. /* Return the write protection operation Status */
  483. return status;
  484. }
  485. /**
  486. * @brief Enables or disables the read out protection.
  487. * @note If the user has already programmed the other option bytes before calling
  488. * this function, he must re-program them since this function erases all option bytes.
  489. * @note This function can be used for N32G45X devices.
  490. * @param Cmd new state of the ReadOut Protection.
  491. * This parameter can be: ENABLE or DISABLE.
  492. * @return FLASH Status: The returned value can be: FLASH_BUSY,
  493. * FLASH_ERR_PG, FLASH_ERR_PV, FLASH_ERR_WRP, FLASH_COMPL,
  494. * FLASH_ERR_EV, FLASH_ERR_RDP2 or FLASH_TIMEOUT.
  495. */
  496. FLASH_STS FLASH_ReadOutProtectionL1(FunctionalState Cmd)
  497. {
  498. uint32_t usertmp;
  499. FLASH_STS status = FLASH_COMPL;
  500. usertmp = ((OBR_USER_MSK & FLASH->OBR) << 0x0E);
  501. /* Check the parameters */
  502. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  503. /* Get the actual read protection L2 Option Byte value */
  504. if (FLASH_GetReadOutProtectionL2STS() != RESET)
  505. {
  506. status = FLASH_ERR_RDP2;
  507. return status;
  508. }
  509. /* Clears the FLASH's pending flags */
  510. FLASH_ClearFlag(FLASH_STS_CLRFLAG);
  511. /* Wait for last operation to be completed */
  512. status = FLASH_WaitForLastOpt(EraseTimeout);
  513. if (status == FLASH_COMPL)
  514. {
  515. /* Authorizes the small information block programming */
  516. FLASH->OPTKEY = FLASH_KEY1;
  517. FLASH->OPTKEY = FLASH_KEY2;
  518. FLASH->CTRL |= CTRL_Set_OPTER;
  519. FLASH->CTRL |= CTRL_Set_START;
  520. /* Wait for last operation to be completed */
  521. status = FLASH_WaitForLastOpt(EraseTimeout);
  522. if (status == FLASH_COMPL)
  523. {
  524. /* Clears the FLASH's pending flags */
  525. FLASH_ClearFlag(FLASH_STS_CLRFLAG);
  526. /* if the erase operation is completed, disable the OPTER Bit */
  527. FLASH->CTRL &= CTRL_Reset_OPTER;
  528. /* Enable the Option Bytes Programming operation */
  529. FLASH->CTRL |= CTRL_Set_OPTPG;
  530. if (Cmd != DISABLE)
  531. {
  532. OB->USER_RDP = (FLASH_USER_USER & usertmp);
  533. }
  534. else
  535. {
  536. OB->USER_RDP = ((L1_RDP_Key & FLASH_RDP_RDP1) | usertmp);
  537. }
  538. /* Wait for last operation to be completed */
  539. status = FLASH_WaitForLastOpt(EraseTimeout);
  540. if (status != FLASH_TIMEOUT)
  541. {
  542. /* if the program operation is completed, disable the OPTPG Bit */
  543. FLASH->CTRL &= CTRL_Reset_OPTPG;
  544. }
  545. }
  546. else
  547. {
  548. if (status != FLASH_TIMEOUT)
  549. {
  550. /* Disable the OPTER Bit */
  551. FLASH->CTRL &= CTRL_Reset_OPTER;
  552. }
  553. }
  554. }
  555. /* Return the protection operation Status */
  556. return status;
  557. }
  558. /**
  559. * @brief Enables or disables the read out protection L2.
  560. * @note If the user has already programmed the other option bytes before calling
  561. * this function, he must re-program them since this function erases all option bytes.
  562. * @note This function can be used for N32G45X devices.
  563. * @return FLASH Status: The returned value can be: FLASH_BUSY,
  564. * FLASH_ERR_PG, FLASH_ERR_PV, FLASH_ERR_WRP, FLASH_COMPL,
  565. * FLASH_ERR_EV, FLASH_ERR_RDP2 or FLASH_TIMEOUT.
  566. */
  567. FLASH_STS FLASH_ReadOutProtectionL2_ENABLE(void)
  568. {
  569. uint32_t usertmp;
  570. FLASH_STS status = FLASH_COMPL;
  571. usertmp = ((OBR_USER_MSK & FLASH->OBR) << 0x0E);
  572. /* Get the actual read protection L1 Option Byte value */
  573. if (FLASH_GetReadOutProtectionSTS() == RESET)
  574. {
  575. usertmp |= (L1_RDP_Key & FLASH_RDP_RDP1);
  576. }
  577. /* Get the actual read protection L2 Option Byte value */
  578. if (FLASH_GetReadOutProtectionL2STS() != RESET)
  579. {
  580. status = FLASH_ERR_RDP2;
  581. return status;
  582. }
  583. /* Clears the FLASH's pending flags */
  584. FLASH_ClearFlag(FLASH_STS_CLRFLAG);
  585. /* Wait for last operation to be completed */
  586. status = FLASH_WaitForLastOpt(EraseTimeout);
  587. if (status == FLASH_COMPL)
  588. {
  589. /* Authorizes the small information block programming */
  590. FLASH->OPTKEY = FLASH_KEY1;
  591. FLASH->OPTKEY = FLASH_KEY2;
  592. FLASH->CTRL |= CTRL_Set_OPTER;
  593. FLASH->CTRL |= CTRL_Set_START;
  594. /* Wait for last operation to be completed */
  595. status = FLASH_WaitForLastOpt(EraseTimeout);
  596. if (status == FLASH_COMPL)
  597. {
  598. /* Clears the FLASH's pending flags */
  599. FLASH_ClearFlag(FLASH_STS_CLRFLAG);
  600. /* if the erase operation is completed, disable the OPTER Bit */
  601. FLASH->CTRL &= CTRL_Reset_OPTER;
  602. /* Enable the Option Bytes Programming operation */
  603. FLASH->CTRL |= CTRL_Set_OPTPG;
  604. OB->USER_RDP = usertmp;
  605. /* Wait for last operation to be completed */
  606. status = FLASH_WaitForLastOpt(EraseTimeout);
  607. if (status == FLASH_COMPL)
  608. {
  609. /* Clears the FLASH's pending flags */
  610. FLASH_ClearFlag(FLASH_STS_CLRFLAG);
  611. /* Enables the read out protection L2 */
  612. OB->RDP2 = L2_RDP_Key;
  613. /* Wait for last operation to be completed */
  614. status = FLASH_WaitForLastOpt(EraseTimeout);
  615. }
  616. if (status != FLASH_TIMEOUT)
  617. {
  618. /* if the program operation is completed, disable the OPTPG Bit */
  619. FLASH->CTRL &= CTRL_Reset_OPTPG;
  620. }
  621. }
  622. else
  623. {
  624. if (status != FLASH_TIMEOUT)
  625. {
  626. /* Disable the OPTER Bit */
  627. FLASH->CTRL &= CTRL_Reset_OPTER;
  628. }
  629. }
  630. }
  631. /* Return the protection operation Status */
  632. return status;
  633. }
  634. /**
  635. * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
  636. * @note This function can be used for N32G45X devices.
  637. * @param OB_IWDG Selects the IWDG mode
  638. * This parameter can be one of the following values:
  639. * @arg OB_IWDG_SW Software IWDG selected
  640. * @arg OB_IWDG_HW Hardware IWDG selected
  641. * @param OB_STOP Reset event when entering STOP mode.
  642. * This parameter can be one of the following values:
  643. * @arg OB_STOP0_NORST No reset generated when entering in STOP
  644. * @arg OB_STOP0_RST Reset generated when entering in STOP
  645. * @param OB_STDBY Reset event when entering Standby mode.
  646. * This parameter can be one of the following values:
  647. * @arg OB_STDBY_NORST No reset generated when entering in STANDBY
  648. * @arg OB_STDBY_RST Reset generated when entering in STANDBY
  649. * @return FLASH Status: The returned value can be: FLASH_BUSY,
  650. * FLASH_ERR_PG, FLASH_ERR_PV, FLASH_ERR_WRP, FLASH_COMPL,
  651. * FLASH_ERR_EV, FLASH_ERR_RDP2 or FLASH_TIMEOUT.
  652. */
  653. FLASH_STS FLASH_ConfigUserOB(uint16_t OB_IWDG, uint16_t OB_STOP, uint16_t OB_STDBY)
  654. {
  655. uint32_t rdptmp = RDP_USER_Key;
  656. FLASH_STS status = FLASH_COMPL;
  657. /* Check the parameters */
  658. assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
  659. assert_param(IS_OB_STOP0_SOURCE(OB_STOP));
  660. assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
  661. /* Get the actual read protection L2 Option Byte value */
  662. if (FLASH_GetReadOutProtectionL2STS() != RESET)
  663. {
  664. status = FLASH_ERR_RDP2;
  665. return status;
  666. }
  667. /* Get the actual read protection Option Byte value */
  668. if (FLASH_GetReadOutProtectionSTS() != RESET)
  669. {
  670. rdptmp = 0xFFF80000;
  671. }
  672. /* Authorize the small information block programming */
  673. FLASH->OPTKEY = FLASH_KEY1;
  674. FLASH->OPTKEY = FLASH_KEY2;
  675. /* Clears the FLASH's pending flags */
  676. FLASH_ClearFlag(FLASH_STS_CLRFLAG);
  677. /* Wait for last operation to be completed */
  678. status = FLASH_WaitForLastOpt(EraseTimeout);
  679. if (status == FLASH_COMPL)
  680. {
  681. /* if the previous operation is completed, proceed to erase the option bytes */
  682. FLASH->CTRL |= CTRL_Set_OPTER;
  683. FLASH->CTRL |= CTRL_Set_START;
  684. /* Wait for last operation to be completed */
  685. status = FLASH_WaitForLastOpt(EraseTimeout);
  686. if (status == FLASH_COMPL)
  687. {
  688. /* Clears the FLASH's pending flags */
  689. FLASH_ClearFlag(FLASH_STS_CLRFLAG);
  690. /* if the erase operation is completed, disable the OPTER Bit */
  691. FLASH->CTRL &= CTRL_Reset_OPTER;
  692. /* Enable the Option Bytes Programming operation */
  693. FLASH->CTRL |= CTRL_Set_OPTPG;
  694. /* Restore the last read protection Option Byte value */
  695. OB->USER_RDP =
  696. (uint32_t)rdptmp
  697. | ((uint32_t)(OB_IWDG | (uint32_t)(OB_STOP | (uint32_t)(OB_STDBY | ((uint32_t)0xF8)))) << 16);
  698. /* Wait for last operation to be completed */
  699. status = FLASH_WaitForLastOpt(ProgramTimeout);
  700. if (status != FLASH_TIMEOUT)
  701. {
  702. /* if the program operation is completed, disable the OPTPG Bit */
  703. FLASH->CTRL &= CTRL_Reset_OPTPG;
  704. }
  705. }
  706. else
  707. {
  708. if (status != FLASH_TIMEOUT)
  709. {
  710. /* Disable the OPTER Bit */
  711. FLASH->CTRL &= CTRL_Reset_OPTER;
  712. }
  713. }
  714. }
  715. /* Return the Option Byte program Status */
  716. return status;
  717. }
  718. /**
  719. * @brief Returns the FLASH User Option Bytes values.
  720. * @note This function can be used for N32G45X devices.
  721. * @return The FLASH User Option Bytes values:IWDG_SW(Bit0), RST_STOP(Bit1)
  722. * and RST_STDBY(Bit2).
  723. */
  724. uint32_t FLASH_GetUserOB(void)
  725. {
  726. /* Return the User Option Byte */
  727. return (uint32_t)((FLASH->OBR << 27) >> 29);
  728. }
  729. /**
  730. * @brief Returns the FLASH Write Protection Option Bytes Register value.
  731. * @note This function can be used for N32G45X devices.
  732. * @return The FLASH Write Protection Option Bytes Register value
  733. */
  734. uint32_t FLASH_GetWriteProtectionOB(void)
  735. {
  736. /* Return the Flash write protection Register value */
  737. return (uint32_t)(FLASH->WRP);
  738. }
  739. /**
  740. * @brief Checks whether the FLASH Read Out Protection Status is set or not.
  741. * @note This function can be used for N32G45X devices.
  742. * @return FLASH ReadOut Protection Status(SET or RESET)
  743. */
  744. FlagStatus FLASH_GetReadOutProtectionSTS(void)
  745. {
  746. FlagStatus readoutstatus = RESET;
  747. if ((FLASH->OBR & RDPRTL1_MSK) != (uint32_t)RESET)
  748. {
  749. readoutstatus = SET;
  750. }
  751. else
  752. {
  753. readoutstatus = RESET;
  754. }
  755. return readoutstatus;
  756. }
  757. /**
  758. * @brief Checks whether the FLASH Read Out Protection L2 Status is set or not.
  759. * @note This function can be used for N32G45x devices.
  760. * @return FLASH ReadOut Protection L2 Status(SET or RESET)
  761. */
  762. FlagStatus FLASH_GetReadOutProtectionL2STS(void)
  763. {
  764. FlagStatus readoutstatus = RESET;
  765. if ((FLASH->OBR & RDPRTL2_MSK) != (uint32_t)RESET)
  766. {
  767. readoutstatus = SET;
  768. }
  769. else
  770. {
  771. readoutstatus = RESET;
  772. }
  773. return readoutstatus;
  774. }
  775. /**
  776. * @brief Checks whether the FLASH Prefetch Buffer status is set or not.
  777. * @note This function can be used for N32G45X devices.
  778. * @return FLASH Prefetch Buffer Status (SET or RESET).
  779. */
  780. FlagStatus FLASH_GetPrefetchBufSTS(void)
  781. {
  782. FlagStatus bitstatus = RESET;
  783. if ((FLASH->AC & AC_PRFTBS_MSK) != (uint32_t)RESET)
  784. {
  785. bitstatus = SET;
  786. }
  787. else
  788. {
  789. bitstatus = RESET;
  790. }
  791. /* Return the new state of FLASH Prefetch Buffer Status (SET or RESET) */
  792. return bitstatus;
  793. }
  794. /**
  795. * @brief Checks whether the FLASH SMPSEL is SMP1 or SMP2.
  796. * @note This function can be used for N32G45X devices.
  797. * @return FLASH SMPSEL (FLASH_SMP1 or FLASH_SMP2).
  798. */
  799. FLASH_SMPSEL FLASH_GetSMPSELStatus(void)
  800. {
  801. FLASH_SMPSEL bitstatus = FLASH_SMP1;
  802. if ((FLASH->CTRL & CTRL_SMPSEL_SMP2) != (uint32_t)FLASH_SMP1)
  803. {
  804. bitstatus = FLASH_SMP2;
  805. }
  806. else
  807. {
  808. bitstatus = FLASH_SMP1;
  809. }
  810. /* Return the new state of FLASH SMPSEL (FLASH_SMP1 or FLASH_SMP2) */
  811. return bitstatus;
  812. }
  813. /**
  814. * @brief Enables or disables the specified FLASH interrupts.
  815. * @note This function can be used for N32G45X devices.
  816. * @param FLASH_INT specifies the FLASH interrupt sources to be enabled or disabled.
  817. * This parameter can be any combination of the following values:
  818. * @arg FLASH_IT_ERROR FLASH Error Interrupt
  819. * @arg FLASH_INT_FERR EVERR PVERR Interrupt
  820. * @arg FLASH_INT_EOP FLASH end of operation Interrupt
  821. * @param Cmd new state of the specified Flash interrupts.
  822. * This parameter can be: ENABLE or DISABLE.
  823. */
  824. void FLASH_INTConfig(uint32_t FLASH_INT, FunctionalState Cmd)
  825. {
  826. /* Check the parameters */
  827. assert_param(IS_FLASH_INT(FLASH_INT));
  828. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  829. if (Cmd != DISABLE)
  830. {
  831. /* Enable the interrupt sources */
  832. FLASH->CTRL |= FLASH_INT;
  833. }
  834. else
  835. {
  836. /* Disable the interrupt sources */
  837. FLASH->CTRL &= ~(uint32_t)FLASH_INT;
  838. }
  839. }
  840. /**
  841. * @brief Checks whether the specified FLASH flag is set or not.
  842. * @note This function can be used for N32G45X devices.
  843. * @param FLASH_FLAG specifies the FLASH flag to check.
  844. * This parameter can be one of the following values:
  845. * @arg FLASH_FLAG_BUSY FLASH Busy flag
  846. * @arg FLASH_FLAG_PGERR FLASH Program error flag
  847. * @arg FLASH_FLAG_PVERR FLASH Program Verify ERROR flag
  848. * @arg FLASH_FLAG_WRPERR FLASH Write protected error flag
  849. * @arg FLASH_FLAG_EOP FLASH End of Operation flag
  850. * @arg FLASH_FLAG_EVERR FLASH Erase Verify ERROR flag
  851. * @arg FLASH_FLAG_OBERR FLASH Option Byte error flag
  852. * @return The new state of FLASH_FLAG (SET or RESET).
  853. */
  854. FlagStatus FLASH_GetFlagSTS(uint32_t FLASH_FLAG)
  855. {
  856. FlagStatus bitstatus = RESET;
  857. /* Check the parameters */
  858. assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG));
  859. if (FLASH_FLAG == FLASH_FLAG_OBERR)
  860. {
  861. if ((FLASH->OBR & FLASH_FLAG_OBERR) != (uint32_t)RESET)
  862. {
  863. bitstatus = SET;
  864. }
  865. else
  866. {
  867. bitstatus = RESET;
  868. }
  869. }
  870. else
  871. {
  872. if ((FLASH->STS & FLASH_FLAG) != (uint32_t)RESET)
  873. {
  874. bitstatus = SET;
  875. }
  876. else
  877. {
  878. bitstatus = RESET;
  879. }
  880. }
  881. /* Return the new state of FLASH_FLAG (SET or RESET) */
  882. return bitstatus;
  883. }
  884. /**
  885. * @brief Clears the FLASH's pending flags.
  886. * @note This function can be used for N32G45X devices.
  887. * @param FLASH_FLAG specifies the FLASH flags to clear.
  888. * This parameter can be any combination of the following values:
  889. * @arg FLASH_FLAG_PGERR FLASH Program error flag
  890. * @arg FLASH_FLAG_PVERR FLASH Program Verify ERROR flag
  891. * @arg FLASH_FLAG_WRPERR FLASH Write protected error flag
  892. * @arg FLASH_FLAG_EOP FLASH End of Operation flag
  893. * @arg FLASH_FLAG_EVERR FLASH Erase Verify ERROR flag
  894. */
  895. void FLASH_ClearFlag(uint32_t FLASH_FLAG)
  896. {
  897. /* Check the parameters */
  898. assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG));
  899. /* Clear the flags */
  900. FLASH->STS |= FLASH_FLAG;
  901. }
  902. /**
  903. * @brief Returns the FLASH Status.
  904. * @note This function can be used for N32G45X devices, it is equivalent
  905. * to FLASH_GetBank1Status function.
  906. * @return FLASH Status: The returned value can be: FLASH_BUSY,
  907. * FLASH_ERR_PG, FLASH_ERR_PV, FLASH_ERR_WRP, FLASH_COMPL,
  908. * FLASH_ERR_EV or FLASH_TIMEOUT.
  909. */
  910. FLASH_STS FLASH_GetSTS(void)
  911. {
  912. FLASH_STS flashstatus = FLASH_COMPL;
  913. if ((FLASH->STS & FLASH_FLAG_BUSY) == FLASH_FLAG_BUSY)
  914. {
  915. flashstatus = FLASH_BUSY;
  916. }
  917. else
  918. {
  919. if ((FLASH->STS & FLASH_FLAG_PGERR) != 0)
  920. {
  921. flashstatus = FLASH_ERR_PG;
  922. }
  923. else
  924. {
  925. if ((FLASH->STS & FLASH_FLAG_PVERR) != 0)
  926. {
  927. flashstatus = FLASH_ERR_PV;
  928. }
  929. else
  930. {
  931. if ((FLASH->STS & FLASH_FLAG_WRPERR) != 0)
  932. {
  933. flashstatus = FLASH_ERR_WRP;
  934. }
  935. else
  936. {
  937. if ((FLASH->STS & FLASH_FLAG_EVERR) != 0)
  938. {
  939. flashstatus = FLASH_ERR_EV;
  940. }
  941. else
  942. {
  943. flashstatus = FLASH_COMPL;
  944. }
  945. }
  946. }
  947. }
  948. }
  949. /* Return the Flash Status */
  950. return flashstatus;
  951. }
  952. /**
  953. * @brief Waits for a Flash operation to complete or a TIMEOUT to occur.
  954. * @note This function can be used for N32G45X devices,
  955. * it is equivalent to FLASH_WaitForLastBank1Operation..
  956. * @param Timeout FLASH programming Timeout
  957. * @return FLASH Status: The returned value can be: FLASH_BUSY,
  958. * FLASH_ERR_PG, FLASH_ERR_PV, FLASH_ERR_WRP, FLASH_COMPL,
  959. * FLASH_ERR_EV or FLASH_TIMEOUT.
  960. */
  961. FLASH_STS FLASH_WaitForLastOpt(uint32_t Timeout)
  962. {
  963. FLASH_STS status = FLASH_COMPL;
  964. /* Check for the Flash Status */
  965. status = FLASH_GetSTS();
  966. /* Wait for a Flash operation to complete or a TIMEOUT to occur */
  967. while ((status == FLASH_BUSY) && (Timeout != 0x00))
  968. {
  969. status = FLASH_GetSTS();
  970. Timeout--;
  971. }
  972. if (Timeout == 0x00)
  973. {
  974. status = FLASH_TIMEOUT;
  975. }
  976. /* Return the operation status */
  977. return status;
  978. }
  979. /**
  980. * @}
  981. */
  982. /**
  983. * @}
  984. */
  985. /**
  986. * @}
  987. */