gd32vf103_fmc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*!
  2. \file gd32vf103_fmc.c
  3. \brief FMC driver
  4. \version 2019-6-5, V1.0.0, firmware for GD32VF103
  5. */
  6. /*
  7. Copyright (c) 2019, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #include "gd32vf103_fmc.h"
  30. /*!
  31. \brief set the wait state counter value
  32. \param[in] wscnt£ºwait state counter value
  33. \arg WS_WSCNT_0: FMC 0 wait state
  34. \arg WS_WSCNT_1: FMC 1 wait state
  35. \arg WS_WSCNT_2: FMC 2 wait state
  36. \param[out] none
  37. \retval none
  38. */
  39. void fmc_wscnt_set(uint32_t wscnt)
  40. {
  41. uint32_t reg;
  42. reg = FMC_WS;
  43. /* set the wait state counter value */
  44. reg &= ~FMC_WS_WSCNT;
  45. FMC_WS = (reg | wscnt);
  46. }
  47. /*!
  48. \brief unlock the main FMC operation
  49. \param[in] none
  50. \param[out] none
  51. \retval none
  52. */
  53. void fmc_unlock(void)
  54. {
  55. if((RESET != (FMC_CTL0 & FMC_CTL0_LK))){
  56. /* write the FMC unlock key */
  57. FMC_KEY0 = UNLOCK_KEY0;
  58. FMC_KEY0 = UNLOCK_KEY1;
  59. }
  60. }
  61. /*!
  62. \brief lock the main FMC operation
  63. \param[in] none
  64. \param[out] none
  65. \retval none
  66. */
  67. void fmc_lock(void)
  68. {
  69. /* set the LK bit */
  70. FMC_CTL0 |= FMC_CTL0_LK;
  71. }
  72. /*!
  73. \brief FMC erase page
  74. \param[in] page_address: the page address to be erased.
  75. \param[out] none
  76. \retval state of FMC, refer to fmc_state_enum
  77. */
  78. fmc_state_enum fmc_page_erase(uint32_t page_address)
  79. {
  80. fmc_state_enum fmc_state;
  81. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  82. /* if the last operation is completed, start page erase */
  83. if (FMC_READY == fmc_state) {
  84. FMC_CTL0 |= FMC_CTL0_PER;
  85. FMC_ADDR0 = page_address;
  86. FMC_CTL0 |= FMC_CTL0_START;
  87. /* wait for the FMC ready */
  88. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  89. /* reset the PER bit */
  90. FMC_CTL0 &= ~FMC_CTL0_PER;
  91. }
  92. /* return the FMC state */
  93. return fmc_state;
  94. }
  95. /*!
  96. \brief FMC erase whole chip
  97. \param[in] none
  98. \param[out] none
  99. \retval state of FMC, refer to fmc_state_enum
  100. */
  101. fmc_state_enum fmc_mass_erase(void)
  102. {
  103. fmc_state_enum fmc_state;
  104. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  105. if(FMC_READY == fmc_state){
  106. /* start whole chip erase */
  107. FMC_CTL0 |= FMC_CTL0_MER;
  108. FMC_CTL0 |= FMC_CTL0_START;
  109. /* wait for the FMC ready */
  110. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  111. /* reset the MER bit */
  112. FMC_CTL0 &= ~FMC_CTL0_MER;
  113. }
  114. /* return the FMC state */
  115. return fmc_state;
  116. }
  117. /*!
  118. \brief FMC program a word at the corresponding address
  119. \param[in] address: address to program
  120. \param[in] data: word to program
  121. \param[out] none
  122. \retval state of FMC, refer to fmc_state_enum
  123. */
  124. fmc_state_enum fmc_word_program(uint32_t address, uint32_t data)
  125. {
  126. fmc_state_enum fmc_state = FMC_READY;
  127. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  128. if(FMC_READY == fmc_state){
  129. /* set the PG bit to start program */
  130. FMC_CTL0 |= FMC_CTL0_PG;
  131. REG32(address) = data;
  132. /* wait for the FMC ready */
  133. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  134. /* reset the PG bit */
  135. FMC_CTL0 &= ~FMC_CTL0_PG;
  136. }
  137. /* return the FMC state */
  138. return fmc_state;
  139. }
  140. /*
  141. \brief FMC program a half word at the corresponding address
  142. \param[in] address: address to program
  143. \param[in] data: halfword to program
  144. \param[out] none
  145. \retval state of FMC, refer to fmc_state_enum
  146. */
  147. fmc_state_enum fmc_halfword_program(uint32_t address, uint16_t data)
  148. {
  149. fmc_state_enum fmc_state = FMC_READY;
  150. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  151. if(FMC_READY == fmc_state){
  152. /* set the PG bit to start program */
  153. FMC_CTL0 |= FMC_CTL0_PG;
  154. REG16(address) = data;
  155. /* wait for the FMC ready */
  156. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  157. /* reset the PG bit */
  158. FMC_CTL0 &= ~FMC_CTL0_PG;
  159. }
  160. /* return the FMC state */
  161. return fmc_state;
  162. }
  163. /*!
  164. \brief unlock the option byte operation
  165. \param[in] none
  166. \param[out] none
  167. \retval none
  168. */
  169. void ob_unlock(void)
  170. {
  171. if(RESET == (FMC_CTL0 & FMC_CTL0_OBWEN)){
  172. /* write the FMC key */
  173. FMC_OBKEY = UNLOCK_KEY0;
  174. FMC_OBKEY = UNLOCK_KEY1;
  175. }
  176. /* wait until OBWEN bit is set by hardware */
  177. while (RESET == (FMC_CTL0 & FMC_CTL0_OBWEN)) {
  178. }
  179. }
  180. /*!
  181. \brief lock the option byte operation
  182. \param[in] none
  183. \param[out] none
  184. \retval none
  185. */
  186. void ob_lock(void)
  187. {
  188. /* reset the OBWEN bit */
  189. FMC_CTL0 &= ~FMC_CTL0_OBWEN;
  190. }
  191. /*!
  192. \brief erase the FMC option byte
  193. unlock the FMC_CTL0 and option byte before calling this function
  194. \param[in] none
  195. \param[out] none
  196. \retval state of FMC, refer to fmc_state_enum
  197. */
  198. fmc_state_enum ob_erase(void)
  199. {
  200. uint16_t temp_spc = FMC_NSPC;
  201. fmc_state_enum fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  202. /* check the option byte security protection value */
  203. if(RESET != ob_spc_get()){
  204. temp_spc = FMC_USPC;
  205. }
  206. if(FMC_READY == fmc_state){
  207. /* start erase the option byte */
  208. FMC_CTL0 |= FMC_CTL0_OBER;
  209. FMC_CTL0 |= FMC_CTL0_START;
  210. /* wait for the FMC ready */
  211. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  212. if(FMC_READY == fmc_state){
  213. /* reset the OBER bit */
  214. FMC_CTL0 &= ~FMC_CTL0_OBER;
  215. /* set the OBPG bit */
  216. FMC_CTL0 |= FMC_CTL0_OBPG;
  217. /* no security protection */
  218. OB_SPC = (uint16_t) temp_spc;
  219. /* wait for the FMC ready */
  220. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  221. if (FMC_TOERR != fmc_state) {
  222. /* reset the OBPG bit */
  223. FMC_CTL0 &= ~FMC_CTL0_OBPG;
  224. }
  225. }else{
  226. if(FMC_TOERR != fmc_state){
  227. /* reset the OBPG bit */
  228. FMC_CTL0 &= ~FMC_CTL0_OBPG;
  229. }
  230. }
  231. }
  232. /* return the FMC state */
  233. return fmc_state;
  234. }
  235. /*!
  236. \brief enable write protection
  237. \param[in] ob_wp: specify sector to be write protected, set the bit to 1 if
  238. you want to protect the corresponding pages. meanwhile, sector
  239. macro could used to set specific sector write protected.
  240. one or more parameters can be selected which are shown as below:
  241. \arg OB_WPx(x = 0..31): write protect specify sector
  242. \arg OB_WP_ALL: write protect all sector
  243. \param[out] none
  244. \retval state of FMC, refer to fmc_state_enum
  245. */
  246. fmc_state_enum ob_write_protection_enable(uint32_t ob_wp)
  247. {
  248. uint16_t temp_wp0, temp_wp1, temp_wp2, temp_wp3;
  249. fmc_state_enum fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  250. ob_wp = (uint32_t) (~ob_wp);
  251. temp_wp0 = (uint16_t) (ob_wp & OB_WP0_WP0);
  252. temp_wp1 = (uint16_t) ((ob_wp & OB_WP1_WP1) >> 8U);
  253. temp_wp2 = (uint16_t) ((ob_wp & OB_WP2_WP2) >> 16U);
  254. temp_wp3 = (uint16_t) ((ob_wp & OB_WP3_WP3) >> 24U);
  255. if(FMC_READY == fmc_state){
  256. /* set the OBPG bit*/
  257. FMC_CTL0 |= FMC_CTL0_OBPG;
  258. if(0xFFU != temp_wp0){
  259. OB_WP0 = temp_wp0;
  260. /* wait for the FMC ready */
  261. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  262. }
  263. if((FMC_READY == fmc_state) && (0xFFU != temp_wp1)){
  264. OB_WP1 = temp_wp1;
  265. /* wait for the FMC ready */
  266. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  267. }
  268. if((FMC_READY == fmc_state) && (0xFFU != temp_wp2)){
  269. OB_WP2 = temp_wp2;
  270. /* wait for the FMC ready */
  271. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  272. }
  273. if((FMC_READY == fmc_state) && (0xFFU != temp_wp3)){
  274. OB_WP3 = temp_wp3;
  275. /* wait for the FMC ready */
  276. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  277. }
  278. if(FMC_TOERR != fmc_state){
  279. /* reset the OBPG bit */
  280. FMC_CTL0 &= ~FMC_CTL0_OBPG;
  281. }
  282. }
  283. /* return the FMC state */
  284. return fmc_state;
  285. }
  286. /*!
  287. \brief configure security protection
  288. \param[in] ob_spc: specify security protection
  289. only one parameter can be selected which is shown as below:
  290. \arg FMC_NSPC: no security protection
  291. \arg FMC_USPC: under security protection
  292. \param[out] none
  293. \retval state of FMC, refer to fmc_state_enum
  294. */
  295. fmc_state_enum ob_security_protection_config(uint8_t ob_spc)
  296. {
  297. fmc_state_enum fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  298. if(FMC_READY == fmc_state){
  299. FMC_CTL0 |= FMC_CTL0_OBER;
  300. FMC_CTL0 |= FMC_CTL0_START;
  301. /* wait for the FMC ready */
  302. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  303. if(FMC_READY == fmc_state){
  304. /* reset the OBER bit */
  305. FMC_CTL0 &= ~FMC_CTL0_OBER;
  306. /* start the option byte program */
  307. FMC_CTL0 |= FMC_CTL0_OBPG;
  308. OB_SPC = (uint16_t) ob_spc;
  309. /* wait for the FMC ready */
  310. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  311. if (FMC_TOERR != fmc_state) {
  312. /* reset the OBPG bit */
  313. FMC_CTL0 &= ~FMC_CTL0_OBPG;
  314. }
  315. }else{
  316. if (FMC_TOERR != fmc_state) {
  317. /* reset the OBER bit */
  318. FMC_CTL0 &= ~FMC_CTL0_OBER;
  319. }
  320. }
  321. }
  322. /* return the FMC state */
  323. return fmc_state;
  324. }
  325. /*!
  326. \brief program the FMC user option byte
  327. \param[in] ob_fwdgt: option byte watchdog value
  328. \arg OB_FWDGT_SW: software free watchdog
  329. \arg OB_FWDGT_HW: hardware free watchdog
  330. \param[in] ob_deepsleep: option byte deepsleep reset value
  331. \arg OB_DEEPSLEEP_NRST: no reset when entering deepsleep mode
  332. \arg OB_DEEPSLEEP_RST: generate a reset instead of entering deepsleep mode
  333. \param[in] ob_stdby:option byte standby reset value
  334. \arg OB_STDBY_NRST: no reset when entering standby mode
  335. \arg OB_STDBY_RST: generate a reset instead of entering standby mode
  336. \param[in] ob_boot: specifies the option byte boot bank value
  337. \arg OB_BOOT_B0: boot from bank0
  338. \param[out] none
  339. \retval state of FMC, refer to fmc_state_enum
  340. */
  341. fmc_state_enum ob_user_write(uint8_t ob_fwdgt, uint8_t ob_deepsleep, uint8_t ob_stdby, uint8_t ob_boot)
  342. {
  343. fmc_state_enum fmc_state = FMC_READY;
  344. uint8_t temp;
  345. /* wait for the FMC ready */
  346. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  347. if(FMC_READY == fmc_state){
  348. /* set the OBPG bit*/
  349. FMC_CTL0 |= FMC_CTL0_OBPG;
  350. temp = ((uint8_t)((uint8_t)((uint8_t)(ob_boot | ob_fwdgt) | ob_deepsleep) | ob_stdby) | OB_USER_MASK);
  351. OB_USER = (uint16_t) temp;
  352. /* wait for the FMC ready */
  353. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  354. if(FMC_TOERR != fmc_state){
  355. /* reset the OBPG bit */
  356. FMC_CTL0 &= ~FMC_CTL0_OBPG;
  357. }
  358. }
  359. /* return the FMC state */
  360. return fmc_state;
  361. }
  362. /*!
  363. \brief program option bytes data
  364. \param[in] address: the option bytes address to be programmed
  365. \param[in] data: the byte to be programmed
  366. \param[out] none
  367. \retval state of FMC, refer to fmc_state_enum
  368. */
  369. fmc_state_enum ob_data_program(uint32_t address, uint8_t data) {
  370. fmc_state_enum fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  371. if(FMC_READY == fmc_state){
  372. /* set the OBPG bit */
  373. FMC_CTL0 |= FMC_CTL0_OBPG;
  374. REG16(address) = data;
  375. /* wait for the FMC ready */
  376. fmc_state = fmc_ready_wait(FMC_TIMEOUT_COUNT);
  377. if(FMC_TOERR != fmc_state){
  378. /* reset the OBPG bit */
  379. FMC_CTL0 &= ~FMC_CTL0_OBPG;
  380. }
  381. }
  382. /* return the FMC state */
  383. return fmc_state;
  384. }
  385. /*!
  386. \brief get the FMC user option byte
  387. \param[in] none
  388. \param[out] none
  389. \retval the FMC user option byte values
  390. */
  391. uint8_t ob_user_get(void)
  392. {
  393. /* return the FMC user option byte value */
  394. return (uint8_t) (FMC_OBSTAT >> 2U);
  395. }
  396. /*!
  397. \brief get OB_DATA in register FMC_OBSTAT
  398. \param[in] none
  399. \param[out] none
  400. \retval ob_data
  401. */
  402. uint16_t ob_data_get(void)
  403. {
  404. return (uint16_t) (FMC_OBSTAT >> 10U);
  405. }
  406. /*!
  407. \brief get the FMC option byte write protection
  408. \param[in] none
  409. \param[out] none
  410. \retval the FMC write protection option byte value
  411. */
  412. uint32_t ob_write_protection_get(void)
  413. {
  414. /* return the FMC write protection option byte value */
  415. return FMC_WP;
  416. }
  417. /*!
  418. \brief get the FMC option byte security protection
  419. \param[in] none
  420. \param[out] none
  421. \retval FlagStatus: SET or RESET
  422. */
  423. FlagStatus ob_spc_get(void)
  424. {
  425. FlagStatus spc_state = RESET;
  426. if(RESET != (FMC_OBSTAT & FMC_OBSTAT_SPC)){
  427. spc_state = SET;
  428. }else{
  429. spc_state = RESET;
  430. }
  431. return spc_state;
  432. }
  433. /*!
  434. \brief enable FMC interrupt
  435. \param[in] interrupt: the FMC interrupt source
  436. only one parameter can be selected which is shown as below:
  437. \arg FMC_INT_END: enable FMC end of program interrupt
  438. \arg FMC_INT_ERR: enable FMC error interrupt
  439. \param[out] none
  440. \retval none
  441. */
  442. void fmc_interrupt_enable(uint32_t interrupt)
  443. {
  444. FMC_REG_VAL(interrupt) |= BIT(FMC_BIT_POS(interrupt));
  445. }
  446. /*!
  447. \brief disable FMC interrupt
  448. \param[in] interrupt: the FMC interrupt source
  449. only one parameter can be selected which is shown as below:
  450. \arg FMC_INT_END: enable FMC end of program interrupt
  451. \arg FMC_INT_ERR: enable FMC error interrupt
  452. \param[out] none
  453. \retval none
  454. */
  455. void fmc_interrupt_disable(uint32_t interrupt)
  456. {
  457. FMC_REG_VAL(interrupt) &= ~BIT(FMC_BIT_POS(interrupt));
  458. }
  459. /*!
  460. \brief check flag is set or not
  461. \param[in] flag: check FMC flag
  462. only one parameter can be selected which is shown as below:
  463. \arg FMC_FLAG_BUSY: FMC busy flag bit
  464. \arg FMC_FLAG_PGERR: FMC operation error flag bit
  465. \arg FMC_FLAG_WPERR: FMC erase/program protection error flag bit
  466. \arg FMC_FLAG_END: FMC end of operation flag bit
  467. \arg FMC_FLAG_OBERR: FMC option bytes read error flag bit
  468. \param[out] none
  469. \retval FlagStatus: SET or RESET
  470. */
  471. FlagStatus fmc_flag_get(uint32_t flag)
  472. {
  473. if(RESET != (FMC_REG_VAL(flag) & BIT(FMC_BIT_POS(flag)))){
  474. return SET;
  475. } else {
  476. return RESET;
  477. }
  478. }
  479. /*!
  480. \brief clear the FMC flag
  481. \param[in] flag: clear FMC flag
  482. only one parameter can be selected which is shown as below:
  483. \arg FMC_FLAG_PGERR: FMC operation error flag bit
  484. \arg FMC_FLAG_WPERR: FMC erase/program protection error flag bit
  485. \arg FMC_FLAG_END: FMC end of operation flag bit
  486. \param[out] none
  487. \retval none
  488. */
  489. void fmc_flag_clear(uint32_t flag)
  490. {
  491. FMC_REG_VAL(flag) = (!FMC_REG_VAL(flag)) | BIT(FMC_BIT_POS(flag));
  492. }
  493. /*!
  494. \brief get FMC interrupt flag state
  495. \param[in] flag: FMC interrupt flags, refer to fmc_interrupt_flag_enum
  496. only one parameter can be selected which is shown as below:
  497. \arg FMC_INT_FLAG_PGERR: FMC operation error interrupt flag bit
  498. \arg FMC_INT_FLAG_WPERR: FMC erase/program protection error interrupt flag bit
  499. \arg FMC_INT_FLAG_END: FMC end of operation interrupt flag bit
  500. \param[out] none
  501. \retval FlagStatus: SET or RESET
  502. */
  503. FlagStatus fmc_interrupt_flag_get(fmc_interrupt_flag_enum flag)
  504. {
  505. FlagStatus ret1 = RESET;
  506. FlagStatus ret2 = RESET;
  507. if(FMC_STAT0_REG_OFFSET == FMC_REG_OFFSET_GET(flag)){
  508. /* get the staus of interrupt flag */
  509. ret1 = (FlagStatus) (FMC_REG_VALS(flag) & BIT(FMC_BIT_POS0(flag)));
  510. /* get the staus of interrupt enale bit */
  511. ret2 = (FlagStatus) (FMC_CTL0 & BIT(FMC_BIT_POS1(flag)));
  512. }
  513. if(ret1 && ret2){
  514. return SET;
  515. }else{
  516. return RESET;
  517. }
  518. }
  519. /*!
  520. \brief clear FMC interrupt flag state
  521. \param[in] flag: FMC interrupt flags, refer to can_interrupt_flag_enum
  522. only one parameter can be selected which is shown as below:
  523. \arg FMC_INT_FLAG_PGERR: FMC operation error interrupt flag bit
  524. \arg FMC_INT_FLAG_WPERR: FMC erase/program protection error interrupt flag bit
  525. \arg FMC_INT_FLAG_END: FMC end of operation interrupt flag bit
  526. \param[out] none
  527. \retval none
  528. */
  529. void fmc_interrupt_flag_clear(fmc_interrupt_flag_enum flag)
  530. {
  531. FMC_REG_VALS(flag) |= BIT(FMC_BIT_POS0(flag));
  532. }
  533. /*!
  534. \brief get the FMC state
  535. \param[in] none
  536. \param[out] none
  537. \retval state of FMC, refer to fmc_state_enum
  538. */
  539. fmc_state_enum fmc_state_get(void)
  540. {
  541. fmc_state_enum fmc_state = FMC_READY;
  542. if((uint32_t) 0x00U != (FMC_STAT0 & FMC_STAT0_BUSY)){
  543. fmc_state = FMC_BUSY;
  544. }else{
  545. if((uint32_t) 0x00U != (FMC_STAT0 & FMC_STAT0_WPERR)){
  546. fmc_state = FMC_WPERR;
  547. }else{
  548. if((uint32_t) 0x00U != (FMC_STAT0 & (FMC_STAT0_PGERR))){
  549. fmc_state = FMC_PGERR;
  550. }
  551. }
  552. }
  553. /* return the FMC state */
  554. return fmc_state;
  555. }
  556. /*!
  557. \brief check whether FMC is ready or not
  558. \param[in] timeout: count of loop
  559. \param[out] none
  560. \retval state of FMC, refer to fmc_state_enum
  561. */
  562. fmc_state_enum fmc_ready_wait(uint32_t timeout)
  563. {
  564. fmc_state_enum fmc_state = FMC_BUSY;
  565. /* wait for FMC ready */
  566. do{
  567. /* get FMC state */
  568. fmc_state = fmc_state_get();
  569. timeout--;
  570. }while((FMC_BUSY == fmc_state) && (0x00U != timeout));
  571. if(FMC_BUSY == fmc_state){
  572. fmc_state = FMC_TOERR;
  573. }
  574. /* return the FMC state */
  575. return fmc_state;
  576. }