gd32f10x_fmc.c 30 KB

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