gd32f30x_fmc.c 28 KB

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