gd32e23x_fmc.c 26 KB

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