gd32f30x_fmc.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /*!
  2. \file gd32f30x_fmc.c
  3. \brief FMC driver
  4. \version 2023-12-30, V2.2.0, firmware for GD32F30x
  5. */
  6. /*
  7. Copyright (c) 2020, 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 "gd32f30x_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
  34. \arg WS_WSCNT_1: FMC 1 wait
  35. \arg WS_WSCNT_2: FMC 2 wait
  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. if(FMC_BANK0_SIZE < FMC_SIZE){
  61. /* write the FMC unlock key */
  62. if(RESET != (FMC_CTL1 & FMC_CTL1_LK)){
  63. FMC_KEY1 = UNLOCK_KEY0;
  64. FMC_KEY1 = UNLOCK_KEY1;
  65. }
  66. }
  67. }
  68. /*!
  69. \brief unlock the FMC bank0 operation
  70. this function can be used for all GD32F30x devices.
  71. for GD32F30x with flash more than 512KB, this function unlocks bank0.
  72. for GD32F30x with flash no more than 512KB and it is equivalent to fmc_unlock function.
  73. \param[in] none
  74. \param[out] none
  75. \retval none
  76. */
  77. void fmc_bank0_unlock(void)
  78. {
  79. if((RESET != (FMC_CTL0 & FMC_CTL0_LK))){
  80. /* write the FMC unlock key */
  81. FMC_KEY0 = UNLOCK_KEY0;
  82. FMC_KEY0 = UNLOCK_KEY1;
  83. }
  84. }
  85. /*!
  86. \brief unlock the FMC bank1 operation
  87. this function can be used for GD32F30x with flash more than 512KB.
  88. \param[in] none
  89. \param[out] none
  90. \retval none
  91. */
  92. void fmc_bank1_unlock(void)
  93. {
  94. if((RESET != (FMC_CTL1 & FMC_CTL1_LK))){
  95. /* write the FMC unlock key */
  96. FMC_KEY1 = UNLOCK_KEY0;
  97. FMC_KEY1 = UNLOCK_KEY1;
  98. }
  99. }
  100. /*!
  101. \brief lock the main FMC operation
  102. \param[in] none
  103. \param[out] none
  104. \retval none
  105. */
  106. void fmc_lock(void)
  107. {
  108. /* set the LK bit */
  109. FMC_CTL0 |= FMC_CTL0_LK;
  110. if(FMC_BANK0_SIZE < FMC_SIZE){
  111. /* set the LK bit */
  112. FMC_CTL1 |= FMC_CTL1_LK;
  113. }
  114. }
  115. /*!
  116. \brief lock the FMC bank0 operation
  117. this function can be used for all GD32F30X devices.
  118. for GD32F30x with flash more than 512KB, this function locks bank0.
  119. for GD32F30x with flash no more than 512KB and it is equivalent to fmc_lock function.
  120. \param[in] none
  121. \param[out] none
  122. \retval none
  123. */
  124. void fmc_bank0_lock(void)
  125. {
  126. /* set the LK bit*/
  127. FMC_CTL0 |= FMC_CTL0_LK;
  128. }
  129. /*!
  130. \brief lock the FMC bank1 operation
  131. this function can be used for GD32F30x with flash more than 512KB.
  132. \param[in] none
  133. \param[out] none
  134. \retval none
  135. */
  136. void fmc_bank1_lock(void)
  137. {
  138. /* set the LK bit*/
  139. FMC_CTL1 |= FMC_CTL1_LK;
  140. }
  141. /*!
  142. \brief erase page
  143. \param[in] page_address: the page address to be erased.
  144. \param[out] none
  145. \retval state of FMC, refer to fmc_state_enum
  146. */
  147. fmc_state_enum fmc_page_erase(uint32_t page_address)
  148. {
  149. fmc_state_enum fmc_state;
  150. if(FMC_BANK0_SIZE < FMC_SIZE){
  151. if(FMC_BANK0_END_ADDRESS > page_address){
  152. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  153. /* if the last operation is completed, start page erase */
  154. if(FMC_READY == fmc_state){
  155. FMC_CTL0 |= FMC_CTL0_PER;
  156. FMC_ADDR0 = page_address;
  157. FMC_CTL0 |= FMC_CTL0_START;
  158. __NOP();
  159. __NOP();
  160. /* wait for the FMC ready */
  161. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  162. /* reset the PER bit */
  163. FMC_CTL0 &= ~FMC_CTL0_PER;
  164. }
  165. }else{
  166. /* wait for the FMC ready */
  167. fmc_state = fmc_bank1_ready_wait(FMC_TIMEOUT_COUNT);
  168. /* if the last operation is completed, start page erase */
  169. if(FMC_READY == fmc_state){
  170. FMC_CTL1 |= FMC_CTL1_PER;
  171. FMC_ADDR1 = page_address;
  172. if(FMC_OBSTAT & FMC_OBSTAT_SPC){
  173. FMC_ADDR0 = page_address;
  174. }
  175. FMC_CTL1 |= FMC_CTL1_START;
  176. __NOP();
  177. __NOP();
  178. /* wait for the FMC ready */
  179. fmc_state = fmc_bank1_ready_wait(FMC_TIMEOUT_COUNT);
  180. /* reset the PER bit */
  181. FMC_CTL1 &= ~FMC_CTL1_PER;
  182. }
  183. }
  184. }else{
  185. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  186. /* if the last operation is completed, start page erase */
  187. if(FMC_READY == fmc_state){
  188. FMC_CTL0 |= FMC_CTL0_PER;
  189. FMC_ADDR0 = page_address;
  190. FMC_CTL0 |= FMC_CTL0_START;
  191. __NOP();
  192. __NOP();
  193. /* wait for the FMC ready */
  194. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  195. /* reset the PER bit */
  196. FMC_CTL0 &= ~FMC_CTL0_PER;
  197. }
  198. }
  199. /* return the FMC state */
  200. return fmc_state;
  201. }
  202. /*!
  203. \brief erase whole chip
  204. \param[in] none
  205. \param[out] none
  206. \retval state of FMC, refer to fmc_state_enum
  207. */
  208. fmc_state_enum fmc_mass_erase(void)
  209. {
  210. fmc_state_enum fmc_state;
  211. if(FMC_BANK0_SIZE < FMC_SIZE){
  212. /* wait for the FMC ready */
  213. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  214. if(FMC_READY == fmc_state){
  215. /* start whole chip erase */
  216. FMC_CTL0 |= FMC_CTL0_MER;
  217. FMC_CTL0 |= FMC_CTL0_START;
  218. /* wait for the FMC ready */
  219. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  220. if(FMC_READY != fmc_state){
  221. return fmc_state;
  222. }
  223. /* reset the MER bit */
  224. FMC_CTL0 &= ~FMC_CTL0_MER;
  225. }
  226. fmc_state = fmc_bank1_ready_wait(FMC_TIMEOUT_COUNT);
  227. if(FMC_READY == fmc_state){
  228. /* start whole chip erase */
  229. FMC_CTL1 |= FMC_CTL1_MER;
  230. FMC_CTL1 |= FMC_CTL1_START;
  231. /* wait for the FMC ready */
  232. fmc_state = fmc_bank1_ready_wait(FMC_TIMEOUT_COUNT);
  233. /* reset the MER bit */
  234. FMC_CTL1 &= ~FMC_CTL1_MER;
  235. }
  236. }else{
  237. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  238. if(FMC_READY == fmc_state){
  239. /* start whole chip erase */
  240. FMC_CTL0 |= FMC_CTL0_MER;
  241. FMC_CTL0 |= FMC_CTL0_START;
  242. /* wait for the FMC ready */
  243. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  244. /* reset the MER bit */
  245. FMC_CTL0 &= ~FMC_CTL0_MER;
  246. }
  247. }
  248. /* return the FMC state */
  249. return fmc_state;
  250. }
  251. /*!
  252. \brief erase bank0
  253. \param[in] none
  254. \param[out] none
  255. \retval state of FMC, refer to fmc_state_enum
  256. */
  257. fmc_state_enum fmc_bank0_erase(void)
  258. {
  259. fmc_state_enum fmc_state = FMC_READY;
  260. /* wait for the FMC ready */
  261. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  262. if(FMC_READY == fmc_state){
  263. /* start FMC bank0 erase */
  264. FMC_CTL0 |= FMC_CTL0_MER;
  265. FMC_CTL0 |= FMC_CTL0_START;
  266. /* wait for the FMC ready */
  267. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  268. /* reset the MER bit */
  269. FMC_CTL0 &= ~FMC_CTL0_MER;
  270. }
  271. /* return the fmc state */
  272. return fmc_state;
  273. }
  274. /*!
  275. \brief erase bank1
  276. \param[in] none
  277. \param[out] none
  278. \retval state of FMC, refer to fmc_state_enum
  279. */
  280. fmc_state_enum fmc_bank1_erase(void)
  281. {
  282. fmc_state_enum fmc_state = FMC_READY;
  283. /* wait for the FMC ready */
  284. fmc_state = fmc_bank1_ready_wait(FMC_TIMEOUT_COUNT);
  285. if(FMC_READY == fmc_state){
  286. /* start FMC bank1 erase */
  287. FMC_CTL1 |= FMC_CTL1_MER;
  288. FMC_CTL1 |= FMC_CTL1_START;
  289. /* wait for the FMC ready */
  290. fmc_state = fmc_bank1_ready_wait(FMC_TIMEOUT_COUNT);
  291. /* reset the MER bit */
  292. FMC_CTL1 &= ~FMC_CTL1_MER;
  293. }
  294. /* return the fmc state */
  295. return fmc_state;
  296. }
  297. /*!
  298. \brief program a word at the corresponding address
  299. \param[in] address: address to program
  300. \param[in] data: word to program
  301. \param[out] none
  302. \retval state of FMC, refer to fmc_state_enum
  303. */
  304. fmc_state_enum fmc_word_program(uint32_t address, uint32_t data)
  305. {
  306. fmc_state_enum fmc_state = FMC_READY;
  307. if(FMC_BANK0_SIZE < FMC_SIZE){
  308. if(FMC_BANK0_END_ADDRESS > address){
  309. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  310. if(FMC_READY == fmc_state){
  311. /* set the PG bit to start program */
  312. FMC_CTL0 |= FMC_CTL0_PG;
  313. REG32(address) = data;
  314. /* wait for the FMC ready */
  315. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  316. /* reset the PG bit */
  317. FMC_CTL0 &= ~FMC_CTL0_PG;
  318. }
  319. }else{
  320. fmc_state = fmc_bank1_ready_wait(FMC_TIMEOUT_COUNT);
  321. if(FMC_READY == fmc_state){
  322. /* set the PG bit to start program */
  323. FMC_CTL1 |= FMC_CTL1_PG;
  324. REG32(address) = data;
  325. /* wait for the FMC ready */
  326. fmc_state = fmc_bank1_ready_wait(FMC_TIMEOUT_COUNT);
  327. /* reset the PG bit */
  328. FMC_CTL1 &= ~FMC_CTL1_PG;
  329. }
  330. }
  331. }else{
  332. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  333. if(FMC_READY == fmc_state){
  334. /* set the PG bit to start program */
  335. FMC_CTL0 |= FMC_CTL0_PG;
  336. REG32(address) = data;
  337. /* wait for the FMC ready */
  338. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  339. /* reset the PG bit */
  340. FMC_CTL0 &= ~FMC_CTL0_PG;
  341. }
  342. }
  343. /* return the FMC state */
  344. return fmc_state;
  345. }
  346. /*!
  347. \brief program a half word at the corresponding address
  348. \param[in] address: address to program
  349. \param[in] data: halfword to program
  350. \param[out] none
  351. \retval state of FMC, refer to fmc_state_enum
  352. */
  353. fmc_state_enum fmc_halfword_program(uint32_t address, uint16_t data)
  354. {
  355. fmc_state_enum fmc_state = FMC_READY;
  356. if(FMC_BANK0_SIZE < FMC_SIZE){
  357. if(FMC_BANK0_END_ADDRESS > address){
  358. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  359. if(FMC_READY == fmc_state){
  360. /* set the PG bit to start program */
  361. FMC_CTL0 |= FMC_CTL0_PG;
  362. REG16(address) = data;
  363. /* wait for the FMC ready */
  364. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  365. /* reset the PG bit */
  366. FMC_CTL0 &= ~FMC_CTL0_PG;
  367. }
  368. }else{
  369. fmc_state = fmc_bank1_ready_wait(FMC_TIMEOUT_COUNT);
  370. if(FMC_READY == fmc_state){
  371. /* set the PG bit to start program */
  372. FMC_CTL1 |= FMC_CTL1_PG;
  373. REG16(address) = data;
  374. /* wait for the FMC ready */
  375. fmc_state = fmc_bank1_ready_wait(FMC_TIMEOUT_COUNT);
  376. /* reset the PG bit */
  377. FMC_CTL1 &= ~FMC_CTL1_PG;
  378. }
  379. }
  380. }else{
  381. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  382. if(FMC_READY == fmc_state){
  383. /* set the PG bit to start program */
  384. FMC_CTL0 |= FMC_CTL0_PG;
  385. REG16(address) = data;
  386. /* wait for the FMC ready */
  387. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  388. /* reset the PG bit */
  389. FMC_CTL0 &= ~FMC_CTL0_PG;
  390. }
  391. }
  392. /* return the FMC state */
  393. return fmc_state;
  394. }
  395. /*!
  396. \brief program a word at the corresponding address without erasing
  397. \param[in] address: address to program
  398. \param[in] data: word to program
  399. \param[out] none
  400. \retval fmc_state
  401. */
  402. fmc_state_enum fmc_word_reprogram(uint32_t address, uint32_t data)
  403. {
  404. fmc_state_enum fmc_state = FMC_READY;
  405. if(FMC_BANK0_SIZE < FMC_SIZE){
  406. if(FMC_BANK0_END_ADDRESS > address){
  407. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  408. FMC_WSEN |= FMC_WSEN_BPEN;
  409. if(FMC_READY == fmc_state){
  410. /* set the PG bit to start program */
  411. FMC_CTL0 |= FMC_CTL0_PG;
  412. REG32(address) = data;
  413. /* wait for the FMC ready */
  414. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  415. /* reset the PG bit */
  416. FMC_CTL0 &= ~FMC_CTL0_PG;
  417. }
  418. }else{
  419. fmc_state = fmc_bank1_ready_wait(FMC_TIMEOUT_COUNT);
  420. FMC_WSEN |= FMC_WSEN_BPEN;
  421. if(FMC_READY == fmc_state){
  422. /* set the PG bit to start program */
  423. FMC_CTL1 |= FMC_CTL1_PG;
  424. REG32(address) = data;
  425. /* wait for the FMC ready */
  426. fmc_state = fmc_bank1_ready_wait(FMC_TIMEOUT_COUNT);
  427. /* reset the PG bit */
  428. FMC_CTL1 &= ~FMC_CTL1_PG;
  429. }
  430. }
  431. }else{
  432. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  433. FMC_WSEN |= FMC_WSEN_BPEN;
  434. if(FMC_READY == fmc_state){
  435. /* set the PG bit to start program */
  436. FMC_CTL0 |= FMC_CTL0_PG;
  437. REG32(address) = data;
  438. /* wait for the FMC ready */
  439. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  440. /* reset the PG bit */
  441. FMC_CTL0 &= ~FMC_CTL0_PG;
  442. }
  443. }
  444. /* return the FMC state */
  445. return fmc_state;
  446. }
  447. /*!
  448. \brief unlock the option byte operation
  449. \param[in] none
  450. \param[out] none
  451. \retval none
  452. */
  453. void ob_unlock(void)
  454. {
  455. if(RESET == (FMC_CTL0 & FMC_CTL0_OBWEN)){
  456. /* write the FMC key */
  457. FMC_OBKEY = UNLOCK_KEY0;
  458. FMC_OBKEY = UNLOCK_KEY1;
  459. }
  460. }
  461. /*!
  462. \brief lock the option byte operation
  463. \param[in] none
  464. \param[out] none
  465. \retval none
  466. */
  467. void ob_lock(void)
  468. {
  469. /* reset the OBWEN bit */
  470. FMC_CTL0 &= ~FMC_CTL0_OBWEN;
  471. }
  472. /*!
  473. \brief erase the FMC option byte
  474. unlock the FMC_CTL0 and option byte before calling this function
  475. \param[in] none
  476. \param[out] none
  477. \retval state of FMC, refer to fmc_state_enum
  478. */
  479. fmc_state_enum ob_erase(void)
  480. {
  481. uint16_t temp_spc = FMC_NSPC;
  482. fmc_state_enum fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  483. /* check the option byte security protection value */
  484. if(RESET != ob_spc_get()){
  485. temp_spc = FMC_USPC;
  486. }
  487. if(FMC_READY == fmc_state){
  488. /* start erase the option byte */
  489. FMC_CTL0 |= FMC_CTL0_OBER;
  490. FMC_CTL0 |= FMC_CTL0_START;
  491. /* wait for the FMC ready */
  492. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  493. if(FMC_READY == fmc_state){
  494. /* reset the OBER bit */
  495. FMC_CTL0 &= ~FMC_CTL0_OBER;
  496. /* set the OBPG bit */
  497. FMC_CTL0 |= FMC_CTL0_OBPG;
  498. /* no security protection */
  499. OB_SPC = (uint16_t)temp_spc;
  500. /* wait for the FMC ready */
  501. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  502. if(FMC_TOERR != fmc_state){
  503. /* reset the OBPG bit */
  504. FMC_CTL0 &= ~FMC_CTL0_OBPG;
  505. }
  506. }else{
  507. if(FMC_TOERR != fmc_state){
  508. /* reset the OBPG bit */
  509. FMC_CTL0 &= ~FMC_CTL0_OBPG;
  510. }
  511. }
  512. }
  513. /* return the FMC state */
  514. return fmc_state;
  515. }
  516. /*!
  517. \brief enable write protection
  518. \param[in] ob_wp: specify sector to be write protected
  519. \arg OB_WPx(x=0..31): write protect specify sector
  520. \arg OB_WP_ALL: write protect all sector
  521. \param[out] none
  522. \retval state of FMC, refer to fmc_state_enum
  523. */
  524. fmc_state_enum ob_write_protection_enable(uint32_t ob_wp)
  525. {
  526. uint16_t temp_wp0, temp_wp1, temp_wp2, temp_wp3;
  527. fmc_state_enum fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  528. ob_wp = (uint32_t)(~ob_wp);
  529. temp_wp0 = (uint16_t)(ob_wp & OB_WP0_WP0);
  530. temp_wp1 = (uint16_t)((ob_wp & OB_WP1_WP1) >> 8U);
  531. temp_wp2 = (uint16_t)((ob_wp & OB_WP2_WP2) >> 16U);
  532. temp_wp3 = (uint16_t)((ob_wp & OB_WP3_WP3) >> 24U);
  533. if(FMC_READY == fmc_state){
  534. /* set the OBPG bit*/
  535. FMC_CTL0 |= FMC_CTL0_OBPG;
  536. if(0xFFU != temp_wp0){
  537. OB_WP0 = temp_wp0;
  538. /* wait for the FMC ready */
  539. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  540. }
  541. if((FMC_READY == fmc_state) && (0xFFU != temp_wp1)){
  542. OB_WP1 = temp_wp1;
  543. /* wait for the FMC ready */
  544. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  545. }
  546. if((FMC_READY == fmc_state) && (0xFFU != temp_wp2)){
  547. OB_WP2 = temp_wp2;
  548. /* wait for the FMC ready */
  549. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  550. }
  551. if((FMC_READY == fmc_state) && (0xFFU != temp_wp3)){
  552. OB_WP3 = temp_wp3;
  553. /* wait for the FMC ready */
  554. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  555. }
  556. if(FMC_TOERR != fmc_state){
  557. /* reset the OBPG bit */
  558. FMC_CTL0 &= ~FMC_CTL0_OBPG;
  559. }
  560. }
  561. /* return the FMC state */
  562. return fmc_state;
  563. }
  564. /*!
  565. \brief configure security protection
  566. \param[in] ob_spc: specify security protection
  567. \arg FMC_NSPC: no security protection
  568. \arg FMC_USPC: under security protection
  569. \param[out] none
  570. \retval state of FMC, refer to fmc_state_enum
  571. */
  572. fmc_state_enum ob_security_protection_config(uint8_t ob_spc)
  573. {
  574. fmc_state_enum fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  575. if(FMC_READY == fmc_state){
  576. FMC_CTL0 |= FMC_CTL0_OBER;
  577. FMC_CTL0 |= FMC_CTL0_START;
  578. /* wait for the FMC ready */
  579. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  580. if(FMC_READY == fmc_state){
  581. /* reset the OBER bit */
  582. FMC_CTL0 &= ~FMC_CTL0_OBER;
  583. /* start the option byte program */
  584. FMC_CTL0 |= FMC_CTL0_OBPG;
  585. OB_SPC = (uint16_t)ob_spc;
  586. /* wait for the FMC ready */
  587. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  588. if(FMC_TOERR != fmc_state){
  589. /* reset the OBPG bit */
  590. FMC_CTL0 &= ~FMC_CTL0_OBPG;
  591. }
  592. }else{
  593. if(FMC_TOERR != fmc_state){
  594. /* reset the OBER bit */
  595. FMC_CTL0 &= ~FMC_CTL0_OBER;
  596. }
  597. }
  598. }
  599. /* return the FMC state */
  600. return fmc_state;
  601. }
  602. /*!
  603. \brief program the FMC user option byte
  604. \param[in] ob_fwdgt: option byte watchdog value
  605. \arg OB_FWDGT_SW: software free watchdog
  606. \arg OB_FWDGT_HW: hardware free watchdog
  607. \param[in] ob_deepsleep: option byte deepsleep reset value
  608. \arg OB_DEEPSLEEP_NRST: no reset when entering deepsleep mode
  609. \arg OB_DEEPSLEEP_RST: generate a reset instead of entering deepsleep mode
  610. \param[in] ob_stdby:option byte standby reset value
  611. \arg OB_STDBY_NRST: no reset when entering standby mode
  612. \arg OB_STDBY_RST: generate a reset instead of entering standby mode
  613. \param[in] ob_boot: specifies the option byte boot bank value
  614. \arg OB_BOOT_B0: boot from bank0
  615. \arg OB_BOOT_B1: boot from bank1 or bank0 if bank1 is void
  616. \param[out] none
  617. \retval state of FMC, refer to fmc_state_enum
  618. */
  619. fmc_state_enum ob_user_write(uint8_t ob_fwdgt, uint8_t ob_deepsleep, uint8_t ob_stdby, uint8_t ob_boot)
  620. {
  621. fmc_state_enum fmc_state = FMC_READY;
  622. uint8_t temp;
  623. /* wait for the FMC ready */
  624. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  625. if(FMC_READY == fmc_state){
  626. /* set the OBPG bit*/
  627. FMC_CTL0 |= FMC_CTL0_OBPG;
  628. temp = ((uint8_t)((uint8_t)((uint8_t)(ob_boot | ob_fwdgt) | ob_deepsleep) | ob_stdby) | OB_USER_MASK);
  629. OB_USER = (uint16_t)temp;
  630. /* wait for the FMC ready */
  631. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  632. if(FMC_TOERR != fmc_state){
  633. /* reset the OBPG bit */
  634. FMC_CTL0 &= ~FMC_CTL0_OBPG;
  635. }
  636. }
  637. /* return the FMC state */
  638. return fmc_state;
  639. }
  640. /*!
  641. \brief program option bytes data
  642. \param[in] address: the option bytes address to be programmed
  643. \param[in] data: the byte to be programmed
  644. \param[out] none
  645. \retval state of FMC, refer to fmc_state_enum
  646. */
  647. fmc_state_enum ob_data_program(uint32_t address, uint8_t data)
  648. {
  649. fmc_state_enum fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  650. if(FMC_READY == fmc_state){
  651. /* set the OBPG bit */
  652. FMC_CTL0 |= FMC_CTL0_OBPG;
  653. REG16(address) = data;
  654. /* wait for the FMC ready */
  655. fmc_state = fmc_bank0_ready_wait(FMC_TIMEOUT_COUNT);
  656. if(FMC_TOERR != fmc_state){
  657. /* reset the OBPG bit */
  658. FMC_CTL0 &= ~FMC_CTL0_OBPG;
  659. }
  660. }
  661. /* return the FMC state */
  662. return fmc_state;
  663. }
  664. /*!
  665. \brief get the FMC user option byte
  666. \param[in] none
  667. \param[out] none
  668. \retval the FMC user option byte values
  669. */
  670. uint8_t ob_user_get(void)
  671. {
  672. /* return the FMC user option byte value */
  673. return (uint8_t)(FMC_OBSTAT >> 2U);
  674. }
  675. /*!
  676. \brief get OB_DATA in register FMC_OBSTAT
  677. \param[in] none
  678. \param[out] none
  679. \retval ob_data
  680. */
  681. uint16_t ob_data_get(void)
  682. {
  683. return (uint16_t)(FMC_OBSTAT >> 10U);
  684. }
  685. /*!
  686. \brief get the FMC option byte write protection
  687. \param[in] none
  688. \param[out] none
  689. \retval the FMC write protection option byte value
  690. */
  691. uint32_t ob_write_protection_get(void)
  692. {
  693. /* return the FMC write protection option byte value */
  694. return FMC_WP;
  695. }
  696. /*!
  697. \brief get the FMC option byte security protection
  698. \param[in] none
  699. \param[out] none
  700. \retval FlagStatus: SET or RESET
  701. */
  702. FlagStatus ob_spc_get(void)
  703. {
  704. FlagStatus spc_state = RESET;
  705. if(RESET != (FMC_OBSTAT & FMC_OBSTAT_SPC)){
  706. spc_state = SET;
  707. }else{
  708. spc_state = RESET;
  709. }
  710. return spc_state;
  711. }
  712. /*!
  713. \brief enable FMC interrupt
  714. \param[in] interrupt: the FMC interrupt source
  715. \arg FMC_INT_BANK0_END: FMC bank0 end of program interrupt
  716. \arg FMC_INT_BANK0_ERR: FMC bank0 error interrupt
  717. \arg FMC_INT_BANK1_END: FMC bank1 end of program interrupt
  718. \arg FMC_INT_BANK1_ERR: FMC bank1 error interrupt
  719. \param[out] none
  720. \retval none
  721. */
  722. void fmc_interrupt_enable(uint32_t interrupt)
  723. {
  724. FMC_REG_VAL(interrupt) |= BIT(FMC_BIT_POS(interrupt));
  725. }
  726. /*!
  727. \brief disable FMC interrupt
  728. \param[in] interrupt: the FMC interrupt source
  729. \arg FMC_INT_BANK0_END: FMC bank0 end of program interrupt
  730. \arg FMC_INT_BANK0_ERR: FMC bank0 error interrupt
  731. \arg FMC_INT_BANK1_END: FMC bank1 end of program interrupt
  732. \arg FMC_INT_BANK1_ERR: FMC bank1 error interrupt
  733. \param[out] none
  734. \retval none
  735. */
  736. void fmc_interrupt_disable(uint32_t interrupt)
  737. {
  738. FMC_REG_VAL(interrupt) &= ~BIT(FMC_BIT_POS(interrupt));
  739. }
  740. /*!
  741. \brief check flag is set or not
  742. \param[in] flag: check FMC flag
  743. only one parameter can be selected which is shown as below:
  744. \arg FMC_FLAG_BANK0_BUSY: FMC bank0 busy flag bit
  745. \arg FMC_FLAG_BANK0_PGERR: FMC bank0 operation error flag bit
  746. \arg FMC_FLAG_BANK0_WPERR: FMC bank0 erase/program protection error flag bit
  747. \arg FMC_FLAG_BANK0_END: FMC bank0 end of operation flag bit
  748. \arg FMC_FLAG_OBERR: FMC option bytes read error flag bit
  749. \arg FMC_FLAG_BANK1_BUSY: FMC bank1 busy flag bit
  750. \arg FMC_FLAG_BANK1_PGERR: FMC bank1 operation error flag bit
  751. \arg FMC_FLAG_BANK1_WPERR: FMC bank1 erase/program protection error flag bit
  752. \arg FMC_FLAG_BANK1_END: FMC bank1 end of operation flag bit
  753. \param[out] none
  754. \retval FlagStatus: SET or RESET
  755. */
  756. FlagStatus fmc_flag_get(uint32_t flag)
  757. {
  758. if(RESET != (FMC_REG_VAL(flag) & BIT(FMC_BIT_POS(flag)))){
  759. return SET;
  760. }else{
  761. return RESET;
  762. }
  763. }
  764. /*!
  765. \brief clear the FMC flag
  766. \param[in] flag: clear FMC flag
  767. only one parameter can be selected which is shown as below:
  768. \arg FMC_FLAG_BANK0_PGERR: FMC bank0 operation error flag bit
  769. \arg FMC_FLAG_BANK0_WPERR: FMC bank0 erase/program protection error flag bit
  770. \arg FMC_FLAG_BANK0_END: FMC bank0 end of operation flag bit
  771. \arg FMC_FLAG_BANK1_PGERR: FMC bank1 operation error flag bit
  772. \arg FMC_FLAG_BANK1_WPERR: FMC bank1 erase/program protection error flag bit
  773. \arg FMC_FLAG_BANK1_END: FMC bank1 end of operation flag bit
  774. \param[out] none
  775. \retval none
  776. */
  777. void fmc_flag_clear(uint32_t flag)
  778. {
  779. FMC_REG_VAL(flag) |= BIT(FMC_BIT_POS(flag));
  780. }
  781. /*!
  782. \brief get FMC interrupt flag state
  783. \param[in] flag: FMC interrupt flags, refer to fmc_interrupt_flag_enum
  784. only one parameter can be selected which is shown as below:
  785. \arg FMC_INT_FLAG_BANK0_PGERR: FMC bank0 operation error interrupt flag bit
  786. \arg FMC_INT_FLAG_BANK0_WPERR: FMC bank0 erase/program protection error interrupt flag bit
  787. \arg FMC_INT_FLAG_BANK0_END: FMC bank0 end of operation interrupt flag bit
  788. \arg FMC_INT_FLAG_BANK1_PGERR: FMC bank1 operation error interrupt flag bit
  789. \arg FMC_INT_FLAG_BANK1_WPERR: FMC bank1 erase/program protection error interrupt flag bit
  790. \arg FMC_INT_FLAG_BANK1_END: FMC bank1 end of operation interrupt flag bit
  791. \param[out] none
  792. \retval FlagStatus: SET or RESET
  793. */
  794. FlagStatus fmc_interrupt_flag_get(fmc_interrupt_flag_enum flag)
  795. {
  796. FlagStatus ret1 = RESET;
  797. FlagStatus ret2 = RESET;
  798. if(FMC_STAT0_REG_OFFSET == FMC_REG_OFFSET_GET(flag)){
  799. /* get the staus of interrupt flag */
  800. ret1 = (FlagStatus)(FMC_REG_VALS(flag) & BIT(FMC_BIT_POS0(flag)));
  801. /* get the staus of interrupt enale bit */
  802. ret2 = (FlagStatus)(FMC_CTL0 & BIT(FMC_BIT_POS1(flag)));
  803. }else{
  804. /* get the staus of interrupt flag */
  805. ret1 = (FlagStatus)(FMC_REG_VALS(flag) & BIT(FMC_BIT_POS0(flag)));
  806. /* get the staus of interrupt enale bit */
  807. ret2 = (FlagStatus)(FMC_CTL1 & BIT(FMC_BIT_POS1(flag)));
  808. }
  809. if(ret1 && ret2){
  810. return SET;
  811. }else{
  812. return RESET;
  813. }
  814. }
  815. /*!
  816. \brief clear FMC interrupt flag state
  817. \param[in] flag: FMC interrupt flags, refer to can_interrupt_flag_enum
  818. only one parameter can be selected which is shown as below:
  819. \arg FMC_INT_FLAG_BANK0_PGERR: FMC bank0 operation error interrupt flag bit
  820. \arg FMC_INT_FLAG_BANK0_WPERR: FMC bank0 erase/program protection error interrupt flag bit
  821. \arg FMC_INT_FLAG_BANK0_END: FMC bank0 end of operation interrupt flag bit
  822. \arg FMC_INT_FLAG_BANK1_PGERR: FMC bank1 operation error interrupt flag bit
  823. \arg FMC_INT_FLAG_BANK1_WPERR: FMC bank1 erase/program protection error interrupt flag bit
  824. \arg FMC_INT_FLAG_BANK1_END: FMC bank1 end of operation interrupt flag bit
  825. \param[out] none
  826. \retval none
  827. */
  828. void fmc_interrupt_flag_clear(fmc_interrupt_flag_enum flag)
  829. {
  830. FMC_REG_VALS(flag) |= BIT(FMC_BIT_POS0(flag));
  831. }
  832. /*!
  833. \brief get the FMC bank0 state
  834. \param[in] none
  835. \param[out] none
  836. \retval state of FMC, refer to fmc_state_enum
  837. */
  838. fmc_state_enum fmc_bank0_state_get(void)
  839. {
  840. fmc_state_enum fmc_state = FMC_READY;
  841. if((uint32_t)0x00U != (FMC_STAT0 & FMC_STAT0_BUSY)){
  842. fmc_state = FMC_BUSY;
  843. }else{
  844. if((uint32_t)0x00U != (FMC_STAT0 & FMC_STAT0_WPERR)){
  845. fmc_state = FMC_WPERR;
  846. }else{
  847. if((uint32_t)0x00U != (FMC_STAT0 & (FMC_STAT0_PGERR))){
  848. fmc_state = FMC_PGERR;
  849. }
  850. }
  851. }
  852. /* return the FMC state */
  853. return fmc_state;
  854. }
  855. /*!
  856. \brief get the FMC bank1 state
  857. \param[in] none
  858. \param[out] none
  859. \retval state of FMC, refer to fmc_state_enum
  860. */
  861. fmc_state_enum fmc_bank1_state_get(void)
  862. {
  863. fmc_state_enum fmc_state = FMC_READY;
  864. if((uint32_t)0x00U != (FMC_STAT1 & FMC_STAT1_BUSY)){
  865. fmc_state = FMC_BUSY;
  866. }else{
  867. if((uint32_t)0x00U != (FMC_STAT1 & FMC_STAT1_WPERR)){
  868. fmc_state = FMC_WPERR;
  869. }else{
  870. if((uint32_t)0x00U != (FMC_STAT1 & FMC_STAT1_PGERR)){
  871. fmc_state = FMC_PGERR;
  872. }
  873. }
  874. }
  875. /* return the FMC state */
  876. return fmc_state;
  877. }
  878. /*!
  879. \brief check whether FMC bank0 is ready or not
  880. \param[in] timeout: count of loop
  881. \param[out] none
  882. \retval state of FMC, refer to fmc_state_enum
  883. */
  884. fmc_state_enum fmc_bank0_ready_wait(uint32_t timeout)
  885. {
  886. fmc_state_enum fmc_state = FMC_BUSY;
  887. /* wait for FMC ready */
  888. do{
  889. /* get FMC state */
  890. fmc_state = fmc_bank0_state_get();
  891. timeout--;
  892. }while((FMC_BUSY == fmc_state) && (0x00U != timeout));
  893. if(FMC_BUSY == fmc_state){
  894. fmc_state = FMC_TOERR;
  895. }
  896. /* return the FMC state */
  897. return fmc_state;
  898. }
  899. /*!
  900. \brief check whether FMC bank1 is ready or not
  901. \param[in] timeout: count of loop
  902. \param[out] none
  903. \retval state of FMC, refer to fmc_state_enum
  904. */
  905. fmc_state_enum fmc_bank1_ready_wait(uint32_t timeout)
  906. {
  907. fmc_state_enum fmc_state = FMC_BUSY;
  908. /* wait for FMC ready */
  909. do{
  910. /* get FMC state */
  911. fmc_state = fmc_bank1_state_get();
  912. timeout--;
  913. }while((FMC_BUSY == fmc_state) && (0x00U != timeout));
  914. if(FMC_BUSY == fmc_state){
  915. fmc_state = FMC_TOERR;
  916. }
  917. /* return the FMC state */
  918. return fmc_state;
  919. }