gd32e23x_fmc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*!
  2. \file gd32e23x_fmc.c
  3. \brief FMC driver
  4. \version 2019-02-19, V1.0.0, firmware for GD32E23x
  5. \version 2020-12-12, V1.1.0, firmware for GD32E23x
  6. */
  7. /*
  8. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  9. Redistribution and use in source and binary forms, with or without modification,
  10. are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright notice, this
  12. list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holder nor the names of its contributors
  17. may be used to endorse or promote products derived from this software without
  18. specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. OF SUCH DAMAGE.
  29. */
  30. #include "gd32e23x_fmc.h"
  31. /* FMC register bit offset */
  32. #define OB_HIGH_WP_OFFSET ((uint32_t)8U)
  33. #define FMC_OBSTAT_USER_OFFSET ((uint32_t)8U)
  34. #define FMC_OBSTAT_DATA_OFFSET ((uint32_t)16U)
  35. /*!
  36. \brief unlock the main FMC operation
  37. it is better to used in pairs with fmc_lock
  38. \param[in] none
  39. \param[out] none
  40. \retval none
  41. */
  42. void fmc_unlock(void)
  43. {
  44. if((RESET != (FMC_CTL & FMC_CTL_LK))){
  45. /* write the FMC key */
  46. FMC_KEY = UNLOCK_KEY0;
  47. FMC_KEY = UNLOCK_KEY1;
  48. }
  49. }
  50. /*!
  51. \brief lock the main FMC operation
  52. it is better to used in pairs with fmc_unlock after an operation
  53. \param[in] none
  54. \param[out] none
  55. \retval none
  56. */
  57. void fmc_lock(void)
  58. {
  59. /* set the LK bit*/
  60. FMC_CTL |= FMC_CTL_LK;
  61. }
  62. /*!
  63. \brief set the wait state counter value
  64. \param[in] wscnt: wait state counter value
  65. \arg WS_WSCNT_0: 0 wait state added
  66. \arg WS_WSCNT_1: 1 wait state added
  67. \arg WS_WSCNT_2: 2 wait state added
  68. \param[out] none
  69. \retval none
  70. */
  71. void fmc_wscnt_set(uint8_t wscnt)
  72. {
  73. uint32_t reg;
  74. reg = FMC_WS;
  75. /* set the wait state counter value */
  76. reg &= ~FMC_WS_WSCNT;
  77. FMC_WS = (reg | wscnt);
  78. }
  79. /*!
  80. \brief pre-fetch enable
  81. \param[in] none
  82. \param[out] none
  83. \retval none
  84. */
  85. void fmc_prefetch_enable(void)
  86. {
  87. FMC_WS |= FMC_WS_PFEN;
  88. }
  89. /*!
  90. \brief pre-fetch disable
  91. \param[in] none
  92. \param[out] none
  93. \retval none
  94. */
  95. void fmc_prefetch_disable(void)
  96. {
  97. FMC_WS &= ~FMC_WS_PFEN;
  98. }
  99. /*!
  100. \brief erase page
  101. \param[in] page_address: target page start address
  102. \param[out] none
  103. \retval fmc_state: state of FMC
  104. \arg FMC_READY: the operation has been completed
  105. \arg FMC_BUSY: the operation is in progress
  106. \arg FMC_PGERR: program error
  107. \arg FMC_PGAERR: program alignment error
  108. \arg FMC_WPERR: erase/program protection error
  109. \arg FMC_TOERR: timeout error
  110. \arg FMC_OB_HSPC: option byte security protection code high
  111. */
  112. fmc_state_enum fmc_page_erase(uint32_t page_address)
  113. {
  114. fmc_state_enum fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  115. if(FMC_READY == fmc_state){
  116. /* start page erase */
  117. FMC_CTL |= FMC_CTL_PER;
  118. FMC_ADDR = page_address;
  119. FMC_CTL |= FMC_CTL_START;
  120. /* wait for the FMC ready */
  121. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  122. /* reset the PER bit */
  123. FMC_CTL &= ~FMC_CTL_PER;
  124. }
  125. /* return the FMC state */
  126. return fmc_state;
  127. }
  128. /*!
  129. \brief erase whole chip
  130. \param[in] none
  131. \param[out] none
  132. \retval fmc_state: state of FMC
  133. \arg FMC_READY: the operation has been completed
  134. \arg FMC_BUSY: the operation is in progress
  135. \arg FMC_PGERR: program error
  136. \arg FMC_PGAERR: program alignment error
  137. \arg FMC_WPERR: erase/program protection error
  138. \arg FMC_TOERR: timeout error
  139. \arg FMC_OB_HSPC: option byte security protection code high
  140. */
  141. fmc_state_enum fmc_mass_erase(void)
  142. {
  143. fmc_state_enum fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  144. if(FMC_READY == fmc_state){
  145. /* start chip erase */
  146. FMC_CTL |= FMC_CTL_MER;
  147. FMC_CTL |= FMC_CTL_START;
  148. /* wait for the FMC ready */
  149. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  150. /* reset the MER bit */
  151. FMC_CTL &= ~FMC_CTL_MER;
  152. }
  153. /* return the fmc state */
  154. return fmc_state;
  155. }
  156. /*!
  157. \brief program a double word at the corresponding address in main flash, this
  158. function also applies to OTP(address 0x1FFF_7000~0x1FFF_73FF) programming
  159. \param[in] address: address to program
  160. \param[in] data: double word to program
  161. \param[out] none
  162. \retval fmc_state: state of FMC
  163. \arg FMC_READY: the operation has been completed
  164. \arg FMC_BUSY: the operation is in progress
  165. \arg FMC_PGERR: program error
  166. \arg FMC_PGAERR: program alignment error
  167. \arg FMC_WPERR: erase/program protection error
  168. \arg FMC_TOERR: timeout error
  169. \arg FMC_OB_HSPC: option byte security protection code high
  170. */
  171. fmc_state_enum fmc_doubleword_program(uint32_t address, uint64_t data)
  172. {
  173. uint32_t data0, data1;
  174. fmc_state_enum fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  175. data0 = (uint32_t)(data & 0xFFFFFFFFU);
  176. data1 = (uint32_t)((data>>32U) & 0xFFFFFFFFU);
  177. /* configure program width */
  178. FMC_WS |= FMC_WS_PGW;
  179. if(FMC_READY == fmc_state){
  180. /* set the PG bit to start program */
  181. FMC_CTL |= FMC_CTL_PG;
  182. REG32(address) = data0;
  183. REG32(address+4U) = data1;
  184. /* wait for the FMC ready */
  185. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  186. /* reset the PG bit */
  187. FMC_CTL &= ~FMC_CTL_PG;
  188. }
  189. FMC_WS &= ~(FMC_WS_PGW);
  190. /* return the FMC state */
  191. return fmc_state;
  192. }
  193. /*!
  194. \brief program a word at the corresponding address in main flash, this function
  195. also applies to OTP(address 0x1FFF_7000~0x1FFF_73FF) programming
  196. \param[in] address: address to program
  197. \param[in] data: word to program
  198. \param[out] none
  199. \retval fmc_state: state of FMC
  200. \arg FMC_READY: the operation has been completed
  201. \arg FMC_BUSY: the operation is in progress
  202. \arg FMC_PGERR: program error
  203. \arg FMC_PGAERR: program alignment error
  204. \arg FMC_WPERR: erase/program protection error
  205. \arg FMC_TOERR: timeout error
  206. \arg FMC_OB_HSPC: option byte security protection code high
  207. */
  208. fmc_state_enum fmc_word_program(uint32_t address, uint32_t data)
  209. {
  210. fmc_state_enum fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  211. if(FMC_READY == fmc_state){
  212. /* set the PG bit to start program */
  213. FMC_CTL |= FMC_CTL_PG;
  214. REG32(address) = data;
  215. /* wait for the FMC ready */
  216. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  217. /* reset the PG bit */
  218. FMC_CTL &= ~FMC_CTL_PG;
  219. }
  220. /* return the FMC state */
  221. return fmc_state;
  222. }
  223. /*!
  224. \brief unlock the option byte operation
  225. it is better to used in pairs with ob_lock
  226. \param[in] none
  227. \param[out] none
  228. \retval none
  229. */
  230. void ob_unlock(void)
  231. {
  232. if(RESET == (FMC_CTL & FMC_CTL_OBWEN)){
  233. /* write the FMC key */
  234. FMC_OBKEY = UNLOCK_KEY0;
  235. FMC_OBKEY = UNLOCK_KEY1;
  236. }
  237. /* wait until OBWEN bit is set by hardware */
  238. while(RESET == (FMC_CTL & FMC_CTL_OBWEN)){
  239. }
  240. }
  241. /*!
  242. \brief lock the option byte operation
  243. it is better to used in pairs with ob_unlock after an operation
  244. \param[in] none
  245. \param[out] none
  246. \retval none
  247. */
  248. void ob_lock(void)
  249. {
  250. /* reset the OBWE bit */
  251. FMC_CTL &= ~FMC_CTL_OBWEN;
  252. }
  253. /*!
  254. \brief reload the option byte and generate a system reset
  255. \param[in] none
  256. \param[out] none
  257. \retval none
  258. */
  259. void ob_reset(void)
  260. {
  261. /* set the OBRLD bit */
  262. FMC_CTL |= FMC_CTL_OBRLD;
  263. }
  264. /*!
  265. \brief get option byte value
  266. \param[in] addr: address of option byte
  267. \arg OB_SPC_USER_ADDRESS: address of option byte security protection and user
  268. \arg OB_DATA_ADDRESS: address of option byte data
  269. \arg OB_WP_ADDRESS: address of option byte write protection
  270. \param[out] option byte value
  271. */
  272. uint32_t option_byte_value_get(uint32_t addr)
  273. {
  274. return *(volatile uint32_t *)(addr);
  275. }
  276. /*!
  277. \brief erase the option byte
  278. programmer must ensure FMC & option byte are both unlocked before calling this function
  279. \param[in] none
  280. \param[out] none
  281. \retval fmc_state: state of FMC
  282. \arg FMC_READY: the operation has been completed
  283. \arg FMC_BUSY: the operation is in progress
  284. \arg FMC_PGERR: program error
  285. \arg FMC_PGAERR: program alignment error
  286. \arg FMC_WPERR: erase/program protection error
  287. \arg FMC_TOERR: timeout error
  288. \arg FMC_OB_HSPC: option byte security protection code high
  289. */
  290. fmc_state_enum ob_erase(void)
  291. {
  292. uint16_t fmc_spc;
  293. uint32_t val;
  294. uint32_t fmc_plevel = ob_obstat_plevel_get();
  295. fmc_state_enum fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  296. /* get the original option byte security protection code */
  297. if(OB_OBSTAT_PLEVEL_NO == fmc_plevel){
  298. fmc_spc = FMC_NSPC;
  299. }else if(OB_OBSTAT_PLEVEL_LOW == fmc_plevel){
  300. fmc_spc = FMC_LSPC;
  301. }else{
  302. fmc_spc = FMC_HSPC;
  303. fmc_state = FMC_OB_HSPC;
  304. }
  305. val = HIGH_16BITS_MASK | (uint32_t)fmc_spc;
  306. if(FMC_READY == fmc_state){
  307. /* start erase the option byte */
  308. FMC_CTL |= FMC_CTL_OBER;
  309. FMC_CTL |= FMC_CTL_START;
  310. /* wait for the FMC ready */
  311. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  312. if(FMC_READY == fmc_state){
  313. /* reset the OBER bit */
  314. FMC_CTL &= ~FMC_CTL_OBER;
  315. /* set the OBPG bit */
  316. FMC_CTL |= FMC_CTL_OBPG;
  317. /* restore the last get option byte security protection code */
  318. OB_SPC_USER = val;
  319. /* wait for the FMC ready */
  320. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  321. if(FMC_TOERR != fmc_state){
  322. /* reset the OBPG bit */
  323. FMC_CTL &= ~FMC_CTL_OBPG;
  324. }
  325. }else{
  326. if(FMC_TOERR != fmc_state){
  327. /* reset the OBER bit */
  328. FMC_CTL &= ~FMC_CTL_OBER;
  329. }
  330. }
  331. }
  332. /* return the FMC state */
  333. return fmc_state;
  334. }
  335. /*!
  336. \brief enable option byte write protection (OB_WP)
  337. \param[in] ob_wp: write protection configuration data. Notice that set the
  338. bit to 1 if you want to protect the corresponding pages.
  339. \param[out] none
  340. \retval fmc_state: state of FMC
  341. \arg FMC_READY: the operation has been completed
  342. \arg FMC_BUSY: the operation is in progress
  343. \arg FMC_PGERR: program error
  344. \arg FMC_PGAERR: program alignment error
  345. \arg FMC_WPERR: erase/program protection error
  346. \arg FMC_TOERR: timeout error
  347. \arg FMC_OB_HSPC: option byte security protection code high
  348. */
  349. fmc_state_enum ob_write_protection_enable(uint16_t ob_wp)
  350. {
  351. uint32_t ob_wrp_val = 0U;
  352. fmc_state_enum fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  353. ob_wp = (uint16_t)(~ob_wp);
  354. ob_wrp_val |= (uint32_t)ob_wp&0x00FFU;
  355. ob_wrp_val |= (((uint32_t)ob_wp&0xFF00U)>>8U) << 16U;
  356. if(FMC_READY == fmc_state){
  357. /* set the OBPG bit*/
  358. FMC_CTL |= FMC_CTL_OBPG;
  359. if(0xFFFFFFFFU != ob_wrp_val){
  360. OB_WP = ob_wrp_val;
  361. /* wait for the FMC ready */
  362. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  363. }
  364. if(FMC_TOERR != fmc_state){
  365. /* reset the OBPG bit */
  366. FMC_CTL &= ~FMC_CTL_OBPG;
  367. }
  368. }
  369. /* return the FMC state */
  370. return fmc_state;
  371. }
  372. /*!
  373. \brief configure security protection
  374. \param[in] ob_spc: specify security protection code
  375. \arg FMC_NSPC: no security protection
  376. \arg FMC_LSPC: low security protection
  377. \arg FMC_HSPC: high security protection
  378. \param[out] none
  379. \retval fmc_state: state of FMC
  380. \arg FMC_READY: the operation has been completed
  381. \arg FMC_BUSY: the operation is in progress
  382. \arg FMC_PGERR: program error
  383. \arg FMC_PGAERR: program alignment error
  384. \arg FMC_WPERR: erase/program protection error
  385. \arg FMC_TOERR: timeout error
  386. \arg FMC_OB_HSPC: option byte security protection code high
  387. */
  388. fmc_state_enum ob_security_protection_config(uint16_t ob_spc)
  389. {
  390. uint32_t val;
  391. fmc_state_enum fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  392. val = option_byte_value_get(OB_SPC_USER_ADDRESS);
  393. /* the OB_SPC byte cannot be reprogrammed if protection level is high */
  394. if(OB_OBSTAT_PLEVEL_HIGH == ob_obstat_plevel_get()){
  395. fmc_state = FMC_OB_HSPC;
  396. }
  397. val &= ~LOW_16BITS_MASK;
  398. val |= (uint32_t)ob_spc;
  399. if(FMC_READY == fmc_state){
  400. /* start erase the option byte */
  401. FMC_CTL |= FMC_CTL_OBER;
  402. FMC_CTL |= FMC_CTL_START;
  403. /* wait for the FMC ready */
  404. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  405. if(FMC_READY == fmc_state){
  406. /* reset the OBER bit */
  407. FMC_CTL &= ~FMC_CTL_OBER;
  408. /* enable the option bytes programming */
  409. FMC_CTL |= FMC_CTL_OBPG;
  410. OB_SPC_USER = val;
  411. /* wait for the FMC ready */
  412. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  413. if(FMC_TOERR != fmc_state){
  414. /* reset the OBPG bit */
  415. FMC_CTL &= ~FMC_CTL_OBPG;
  416. }
  417. }else{
  418. if(FMC_TOERR != fmc_state){
  419. /* reset the OBER bit */
  420. FMC_CTL &= ~FMC_CTL_OBER;
  421. }
  422. }
  423. }
  424. /* return the FMC state */
  425. return fmc_state;
  426. }
  427. /*!
  428. \brief program the FMC user option byte
  429. this function can only clear the corresponding bits to be 0 rather than 1.
  430. the function ob_erase is used to set all the bits to be 1.
  431. \param[in] ob_user: user option byte
  432. one or more parameters (bitwise AND) can be selected which are shown as below:
  433. \arg OB_FWDGT_HW: hardware free watchdog timer
  434. \arg OB_DEEPSLEEP_RST: no reset when entering deepsleep mode
  435. \arg OB_STDBY_RST: no reset when entering standby mode
  436. \arg OB_BOOT1_SET_1: BOOT1 bit is 1
  437. \arg OB_VDDA_DISABLE: disable VDDA monitor
  438. \arg OB_SRAM_PARITY_ENABLE: enable sram parity check
  439. \param[out] none
  440. \retval fmc_state: state of FMC
  441. \arg FMC_READY: the operation has been completed
  442. \arg FMC_BUSY: the operation is in progress
  443. \arg FMC_PGERR: program error
  444. \arg FMC_PGAERR: program alignment error
  445. \arg FMC_WPERR: erase/program protection error
  446. \arg FMC_TOERR: timeout error
  447. \arg FMC_OB_HSPC: option byte security protection code high
  448. */
  449. fmc_state_enum ob_user_write(uint8_t ob_user)
  450. {
  451. uint32_t val_spc_user;
  452. /* check whether FMC is ready or not */
  453. fmc_state_enum fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  454. val_spc_user = option_byte_value_get(OB_SPC_USER_ADDRESS);
  455. val_spc_user &= ~HIGH_16BITS_MASK;
  456. val_spc_user |= ((uint32_t)ob_user<<16U);
  457. if(FMC_READY == fmc_state){
  458. /* start erase the option byte */
  459. FMC_CTL |= FMC_CTL_OBER;
  460. FMC_CTL |= FMC_CTL_START;
  461. /* wait for the FMC ready */
  462. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  463. if(FMC_READY == fmc_state){
  464. /* reset the OBER bit */
  465. FMC_CTL &= ~FMC_CTL_OBER;
  466. /* enable the option bytes programming */
  467. FMC_CTL |= FMC_CTL_OBPG;
  468. OB_SPC_USER = val_spc_user;
  469. /* wait for the FMC ready */
  470. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  471. if(FMC_TOERR != fmc_state){
  472. /* reset the OBPG bit */
  473. FMC_CTL &= ~FMC_CTL_OBPG;
  474. }
  475. }else{
  476. if(FMC_TOERR != fmc_state){
  477. /* reset the OBER bit */
  478. FMC_CTL &= ~FMC_CTL_OBER;
  479. }
  480. }
  481. }
  482. /* return the FMC state */
  483. return fmc_state;
  484. }
  485. /*!
  486. \brief program the FMC data option byte
  487. \param[in] data: the data to be programmed, OB_DATA[0:15]
  488. \param[out] none
  489. \retval fmc_state: state of FMC
  490. \arg FMC_READY: the operation has been completed
  491. \arg FMC_BUSY: the operation is in progress
  492. \arg FMC_PGERR: program error
  493. \arg FMC_PGAERR: program alignment error
  494. \arg FMC_WPERR: erase/program protection error
  495. \arg FMC_TOERR: timeout error
  496. \arg FMC_OB_HSPC: option byte security protection code high
  497. */
  498. fmc_state_enum ob_data_program(uint16_t data)
  499. {
  500. fmc_state_enum fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  501. uint32_t val = 0U;
  502. val |= (uint32_t)data&0x00FFU;
  503. val |= (((uint32_t)data&0xFF00U)>>8U) << 16U;
  504. if(FMC_READY == fmc_state){
  505. /* set the OBPG bit */
  506. FMC_CTL |= FMC_CTL_OBPG;
  507. OB_DATA = val;
  508. /* wait for the FMC ready */
  509. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  510. if(FMC_TOERR != fmc_state){
  511. /* reset the OBPG bit */
  512. FMC_CTL &= ~FMC_CTL_OBPG;
  513. }
  514. }
  515. /* return the FMC state */
  516. return fmc_state;
  517. }
  518. /*!
  519. \brief get OB_USER in register FMC_OBSTAT
  520. \param[in] none
  521. \param[out] none
  522. \retval ob_user
  523. */
  524. uint8_t ob_user_get(void)
  525. {
  526. return (uint8_t)(FMC_OBSTAT >> FMC_OBSTAT_USER_OFFSET);
  527. }
  528. /*!
  529. \brief get OB_DATA in register FMC_OBSTAT
  530. \param[in] none
  531. \param[out] none
  532. \retval ob_data
  533. */
  534. uint16_t ob_data_get(void)
  535. {
  536. return (uint16_t)(FMC_OBSTAT >> FMC_OBSTAT_DATA_OFFSET);
  537. }
  538. /*!
  539. \brief get the FMC option byte write protection (OB_WP) in register FMC_WP
  540. \param[in] none
  541. \param[out] none
  542. \retval OB_WP
  543. */
  544. uint16_t ob_write_protection_get(void)
  545. {
  546. return (uint16_t)(FMC_WP);
  547. }
  548. /*!
  549. \brief get the value of FMC option byte security protection level (PLEVEL) in FMC_OBSTAT register
  550. \param[in] none
  551. \param[out] none
  552. \retval the value of PLEVEL
  553. \arg OB_OBSTAT_PLEVEL_NO: no security protection
  554. \arg OB_OBSTAT_PLEVEL_LOW: low security protection
  555. \arg OB_OBSTAT_PLEVEL_HIGH: high security protection
  556. */
  557. uint32_t ob_obstat_plevel_get(void)
  558. {
  559. return (FMC_OBSTAT & (FMC_OBSTAT_PLEVEL_BIT0 | FMC_OBSTAT_PLEVEL_BIT1));
  560. }
  561. /* FMC interrupts and flags management functions */
  562. /*!
  563. \brief enable FMC interrupt
  564. \param[in] interrupt: the FMC interrupt source
  565. \arg FMC_INTEN_END: FMC end of operation interrupt
  566. \arg FMC_INTEN_ERR: FMC error interrupt
  567. \param[out] none
  568. \retval none
  569. */
  570. void fmc_interrupt_enable(uint32_t interrupt)
  571. {
  572. FMC_CTL |= interrupt;
  573. }
  574. /*!
  575. \brief disable FMC interrupt
  576. \param[in] interrupt: the FMC interrupt source
  577. \arg FMC_INTEN_END: FMC end of operation interrupt
  578. \arg FMC_INTEN_ERR: FMC error interrupt
  579. \param[out] none
  580. \retval none
  581. */
  582. void fmc_interrupt_disable(uint32_t interrupt)
  583. {
  584. FMC_CTL &= ~(uint32_t)interrupt;
  585. }
  586. /*!
  587. \brief get flag set or reset
  588. \param[in] flag: check FMC flag
  589. only one parameter can be selected which is shown as below:
  590. \arg FMC_FLAG_BUSY: FMC busy flag
  591. \arg FMC_FLAG_PGERR: FMC programming error flag
  592. \arg FMC_FLAG_PGAERR: FMC program alignment error flag
  593. \arg FMC_FLAG_WPERR: FMC write protection error flag
  594. \arg FMC_FLAG_END: FMC end of programming flag
  595. \param[out] none
  596. \retval FlagStatus: SET or RESET
  597. */
  598. FlagStatus fmc_flag_get(uint32_t flag)
  599. {
  600. FlagStatus status = RESET;
  601. if(FMC_STAT & flag){
  602. status = SET;
  603. }
  604. /* return the state of corresponding FMC flag */
  605. return status;
  606. }
  607. /*!
  608. \brief clear the FMC pending flag by writing 1
  609. \param[in] flag: clear FMC flag
  610. one or more parameters can be selected which is shown as below:
  611. \arg FMC_FLAG_PGERR: FMC programming error flag
  612. \arg FMC_FLAG_PGAERR: FMC program alignment error flag
  613. \arg FMC_FLAG_WPERR: FMC write protection error flag
  614. \arg FMC_FLAG_END: FMC end of programming flag
  615. \param[out] none
  616. \retval none
  617. */
  618. void fmc_flag_clear(uint32_t flag)
  619. {
  620. /* clear the flags */
  621. FMC_STAT = flag;
  622. }
  623. /*!
  624. \brief get intrrupt flag set or reset
  625. \param[in] flag: check FMC flag
  626. only one parameter can be selected which is shown as below:
  627. \arg FMC_INT_FLAG_PGERR: FMC programming error flag
  628. \arg FMC_INT_FLAG_PGAERR: FMC program alignment error flag
  629. \arg FMC_INT_FLAG_WPERR: FMC write protection error flag
  630. \arg FMC_INT_FLAG_END: FMC end of programming flag
  631. \param[out] none
  632. \retval FlagStatus: SET or RESET
  633. */
  634. FlagStatus fmc_interrupt_flag_get(uint32_t int_flag)
  635. {
  636. uint32_t intenable = 0U, flagstatus = 0U;
  637. if(FMC_INT_FLAG_END == int_flag){
  638. /* get the interrupt enable bit status */
  639. intenable = FMC_CTL & FMC_INTEN_END;
  640. /* get the corresponding flag bit status */
  641. flagstatus = FMC_STAT & int_flag;
  642. if(intenable && flagstatus){
  643. return SET;
  644. }else{
  645. return RESET;
  646. }
  647. }else{
  648. /* get the interrupt enable bit status */
  649. intenable = FMC_CTL & FMC_INTEN_ERR;
  650. /* get the corresponding flag bit status */
  651. flagstatus = FMC_STAT & int_flag;
  652. if(intenable && flagstatus){
  653. return SET;
  654. }else{
  655. return RESET;
  656. }
  657. }
  658. }
  659. /*!
  660. \brief clear the FMC interrupt pending flag by writing 1
  661. \param[in] flag: clear FMC flag
  662. one or more parameters can be selected which is shown as below:
  663. \arg FMC_INT_FLAG_PGERR: FMC programming error flag
  664. \arg FMC_INT_FLAG_PGAERR: FMC program alignment error flag
  665. \arg FMC_INT_FLAG_WPERR: FMC write protection error flag
  666. \arg FMC_INT_FLAG_END: FMC end of programming flag
  667. \param[out] none
  668. \retval none
  669. */
  670. void fmc_interrupt_flag_clear(uint32_t int_flag)
  671. {
  672. /* clear the flags */
  673. FMC_STAT = int_flag;
  674. }
  675. /*!
  676. \brief get the FMC state
  677. \param[in] none
  678. \param[out] none
  679. \retval fmc_state
  680. */
  681. fmc_state_enum fmc_state_get(void)
  682. {
  683. fmc_state_enum fmc_state = FMC_READY;
  684. if((uint32_t)0x00U != (FMC_STAT & FMC_STAT_BUSY)){
  685. fmc_state = FMC_BUSY;
  686. }else{
  687. if((uint32_t)0x00U != (FMC_STAT & FMC_STAT_WPERR)){
  688. fmc_state = FMC_WPERR;
  689. }else{
  690. if((uint32_t)0x00U != (FMC_STAT & FMC_STAT_PGERR)){
  691. fmc_state = FMC_PGERR;
  692. }
  693. }
  694. }
  695. /* return the FMC state */
  696. return fmc_state;
  697. }
  698. /*!
  699. \brief check whether FMC is ready or not
  700. \param[in] timeout: timeout count
  701. \param[out] none
  702. \retval fmc_state
  703. */
  704. fmc_state_enum fmc_ready_wait(uint32_t timeout)
  705. {
  706. fmc_state_enum fmc_state = FMC_BUSY;
  707. /* wait for FMC ready */
  708. do{
  709. /* get FMC state */
  710. fmc_state = fmc_state_get();
  711. timeout--;
  712. }while((FMC_BUSY == fmc_state) && (0U != timeout));
  713. if(FMC_BUSY == fmc_state){
  714. fmc_state = FMC_TOERR;
  715. }
  716. /* return the FMC state */
  717. return fmc_state;
  718. }