at32f413_flash.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. /**
  2. **************************************************************************
  3. * @file at32f413_flash.c
  4. * @brief contains all the functions for the flash firmware library
  5. **************************************************************************
  6. * Copyright notice & Disclaimer
  7. *
  8. * The software Board Support Package (BSP) that is made available to
  9. * download from Artery official website is the copyrighted work of Artery.
  10. * Artery authorizes customers to use, copy, and distribute the BSP
  11. * software and its related documentation for the purpose of design and
  12. * development in conjunction with Artery microcontrollers. Use of the
  13. * software is governed by this copyright notice and the following disclaimer.
  14. *
  15. * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  16. * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  17. * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  18. * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  19. * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  21. *
  22. **************************************************************************
  23. */
  24. #include "at32f413_conf.h"
  25. /** @addtogroup AT32F413_periph_driver
  26. * @{
  27. */
  28. /** @defgroup FLASH
  29. * @brief FLASH driver modules
  30. * @{
  31. */
  32. #ifdef FLASH_MODULE_ENABLED
  33. /** @defgroup FLASH_private_functions
  34. * @{
  35. */
  36. /**
  37. * @brief check whether the specified flash flag is set or not.
  38. * @param flash_flag: specifies the flash flag to check.
  39. * this parameter can be one of flash flag status:
  40. * - FLASH_OBF_FLAG
  41. * - FLASH_ODF_FLAG
  42. * - FLASH_PRGMERR_FLAG
  43. * - FLASH_EPPERR_FLAG
  44. * - FLASH_SPIM_OBF_FLAG
  45. * - FLASH_SPIM_ODF_FLAG
  46. * - FLASH_SPIM_PRGMERR_FLAG
  47. * - FLASH_SPIM_EPPERR_FLAG
  48. * - FLASH_USDERR_FLAG
  49. * @retval the new state of flash_flag (SET or RESET).
  50. */
  51. flag_status flash_flag_get(uint32_t flash_flag)
  52. {
  53. flag_status status = RESET;
  54. uint32_t flag_position;
  55. flag_position = flash_flag & 0x70000000;
  56. flash_flag &= 0x8FFFFFFF;
  57. switch(flag_position)
  58. {
  59. case 0x00000000:
  60. if(FLASH->sts & flash_flag)
  61. status = SET;
  62. break;
  63. case 0x20000000:
  64. if(FLASH->sts3 & flash_flag)
  65. status = SET;
  66. break;
  67. case 0x40000000:
  68. if(FLASH->usd & flash_flag)
  69. status = SET;
  70. break;
  71. default:
  72. break;
  73. }
  74. /* return the new state of flash_flag (SET or RESET) */
  75. return status;
  76. }
  77. /**
  78. * @brief clear the flash flag.
  79. * @param flash_flag: specifies the flash flags to clear.
  80. * this parameter can be any combination of the following values:
  81. * - FLASH_ODF_FLAG
  82. * - FLASH_PRGMERR_FLAG
  83. * - FLASH_EPPERR_FLAG
  84. * - FLASH_SPIM_ODF_FLAG
  85. * - FLASH_SPIM_PRGMERR_FLAG
  86. * - FLASH_SPIM_EPPERR_FLAG
  87. * @retval none
  88. */
  89. void flash_flag_clear(uint32_t flash_flag)
  90. {
  91. uint32_t flag_position;
  92. flag_position = flash_flag & 0x70000000;
  93. flash_flag &= 0x8FFFFFFF;
  94. switch(flag_position)
  95. {
  96. case 0x00000000:
  97. FLASH->sts = flash_flag;
  98. break;
  99. case 0x20000000:
  100. FLASH->sts3 = flash_flag;
  101. break;
  102. default:
  103. break;
  104. }
  105. }
  106. /**
  107. * @brief return the flash operation status.
  108. * @param none
  109. * @retval status: the returned value can be: FLASH_OPERATE_BUSY,
  110. * FLASH_PROGRAM_ERROR, FLASH_EPP_ERROR or FLASH_OPERATE_DONE.
  111. */
  112. flash_status_type flash_operation_status_get(void)
  113. {
  114. flash_status_type flash_status = FLASH_OPERATE_DONE;
  115. if(FLASH->sts_bit.obf != RESET)
  116. {
  117. flash_status = FLASH_OPERATE_BUSY;
  118. }
  119. else if(FLASH->sts_bit.prgmerr != RESET)
  120. {
  121. flash_status = FLASH_PROGRAM_ERROR;
  122. }
  123. else if(FLASH->sts_bit.epperr != RESET)
  124. {
  125. flash_status = FLASH_EPP_ERROR;
  126. }
  127. else
  128. {
  129. flash_status = FLASH_OPERATE_DONE;
  130. }
  131. /* return the flash status */
  132. return flash_status;
  133. }
  134. /**
  135. * @brief return the flash spim operation status.
  136. * @param none
  137. * @retval status: the returned value can be: FLASH_OPERATE_BUSY,
  138. * FLASH_PROGRAM_ERROR, FLASH_EPP_ERROR or FLASH_OPERATE_DONE.
  139. */
  140. flash_status_type flash_spim_operation_status_get(void)
  141. {
  142. flash_status_type flash_status = FLASH_OPERATE_DONE;
  143. if(FLASH->sts3_bit.obf != RESET)
  144. {
  145. flash_status = FLASH_OPERATE_BUSY;
  146. }
  147. else if(FLASH->sts3_bit.prgmerr != RESET)
  148. {
  149. flash_status = FLASH_PROGRAM_ERROR;
  150. }
  151. else if(FLASH->sts3_bit.epperr != RESET)
  152. {
  153. flash_status = FLASH_EPP_ERROR;
  154. }
  155. else
  156. {
  157. flash_status = FLASH_OPERATE_DONE;
  158. }
  159. /* return the flash status */
  160. return flash_status;
  161. }
  162. /**
  163. * @brief wait for flash operation complete or timeout.
  164. * @param time_out: flash operation timeout
  165. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  166. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  167. */
  168. flash_status_type flash_operation_wait_for(uint32_t time_out)
  169. {
  170. flash_status_type status = FLASH_OPERATE_DONE;
  171. /* check for the flash status */
  172. status = flash_operation_status_get();
  173. while((status == FLASH_OPERATE_BUSY) && (time_out != 0x00))
  174. {
  175. status = flash_operation_status_get();
  176. time_out--;
  177. }
  178. if(time_out == 0x00)
  179. {
  180. status = FLASH_OPERATE_TIMEOUT;
  181. }
  182. /* return the status */
  183. return status;
  184. }
  185. /**
  186. * @brief wait for flash spim operation complete or timeout.
  187. * @param time_out: flash operation timeout
  188. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  189. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  190. */
  191. flash_status_type flash_spim_operation_wait_for(uint32_t time_out)
  192. {
  193. flash_status_type status = FLASH_OPERATE_DONE;
  194. /* check for the flash status */
  195. status = flash_spim_operation_status_get();
  196. while((status == FLASH_OPERATE_BUSY) && (time_out != 0x00))
  197. {
  198. status = flash_spim_operation_status_get();
  199. time_out--;
  200. }
  201. if(time_out == 0x00)
  202. {
  203. status = FLASH_OPERATE_TIMEOUT;
  204. }
  205. /* return the operation status */
  206. return status;
  207. }
  208. /**
  209. * @brief unlock the flash controller.
  210. * @param none
  211. * @retval none
  212. */
  213. void flash_unlock(void)
  214. {
  215. FLASH->unlock = FLASH_UNLOCK_KEY1;
  216. FLASH->unlock = FLASH_UNLOCK_KEY2;
  217. }
  218. /**
  219. * @brief unlock the flash spim controller.
  220. * @param none
  221. * @retval none
  222. */
  223. void flash_spim_unlock(void)
  224. {
  225. FLASH->unlock3 = FLASH_UNLOCK_KEY1;
  226. FLASH->unlock3 = FLASH_UNLOCK_KEY2;
  227. }
  228. /**
  229. * @brief lock the flash controller.
  230. * @param none
  231. * @retval none
  232. */
  233. void flash_lock(void)
  234. {
  235. FLASH->ctrl_bit.oplk = TRUE;
  236. }
  237. /**
  238. * @brief lock the flash spim controller.
  239. * @param none
  240. * @retval none
  241. */
  242. void flash_spim_lock(void)
  243. {
  244. FLASH->ctrl3_bit.oplk = TRUE;
  245. }
  246. /**
  247. * @brief erase a specified flash sector.
  248. * @param sector_address: the sector address to be erased.
  249. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  250. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  251. */
  252. flash_status_type flash_sector_erase(uint32_t sector_address)
  253. {
  254. flash_status_type status = FLASH_OPERATE_DONE;
  255. /* spim : external flash */
  256. if(sector_address >= FLASH_SPIM_START_ADDR)
  257. {
  258. FLASH->ctrl3_bit.secers = TRUE;
  259. FLASH->addr3 = sector_address;
  260. FLASH->ctrl3_bit.erstr = TRUE;
  261. /* wait for operation to be completed */
  262. status = flash_spim_operation_wait_for(SPIM_ERASE_TIMEOUT);
  263. /* disable the secers bit */
  264. FLASH->ctrl3_bit.secers = FALSE;
  265. }
  266. else
  267. {
  268. FLASH->ctrl_bit.secers = TRUE;
  269. FLASH->addr = sector_address;
  270. FLASH->ctrl_bit.erstr = TRUE;
  271. /* wait for operation to be completed */
  272. status = flash_operation_wait_for(ERASE_TIMEOUT);
  273. /* disable the secers bit */
  274. FLASH->ctrl_bit.secers = FALSE;
  275. }
  276. /* return the erase status */
  277. return status;
  278. }
  279. /**
  280. * @brief erase flash all internal sectors.
  281. * @param none
  282. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  283. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  284. */
  285. flash_status_type flash_internal_all_erase(void)
  286. {
  287. flash_status_type status = FLASH_OPERATE_DONE;
  288. FLASH->ctrl_bit.bankers = TRUE;
  289. FLASH->ctrl_bit.erstr = TRUE;
  290. /* wait for operation to be completed */
  291. status = flash_operation_wait_for(ERASE_TIMEOUT);
  292. /* disable the bankers bit */
  293. FLASH->ctrl_bit.bankers = FALSE;
  294. /* return the erase status */
  295. return status;
  296. }
  297. /**
  298. * @brief erase flash spim sectors.
  299. * @param none
  300. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  301. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  302. */
  303. flash_status_type flash_spim_all_erase(void)
  304. {
  305. flash_status_type status = FLASH_OPERATE_DONE;
  306. FLASH->ctrl3_bit.chpers = TRUE;
  307. FLASH->ctrl3_bit.erstr = TRUE;
  308. /* wait for operation to be completed */
  309. status = flash_spim_operation_wait_for(SPIM_ERASE_TIMEOUT);
  310. /* disable the chpers bit */
  311. FLASH->ctrl3_bit.chpers = FALSE;
  312. /* return the erase status */
  313. return status;
  314. }
  315. /**
  316. * @brief erase the flash user system data.
  317. * @note this functions erases all user system data except the fap byte.
  318. * @param none
  319. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  320. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  321. */
  322. flash_status_type flash_user_system_data_erase(void)
  323. {
  324. flash_status_type status = FLASH_OPERATE_DONE;
  325. uint16_t fap_val = FAP_RELIEVE_KEY;
  326. /* get the flash access protection status */
  327. if(flash_fap_status_get() != RESET)
  328. {
  329. fap_val = 0x0000;
  330. }
  331. /* unlock the user system data */
  332. FLASH->usd_unlock = FLASH_UNLOCK_KEY1;
  333. FLASH->usd_unlock = FLASH_UNLOCK_KEY2;
  334. while(FLASH->ctrl_bit.usdulks == RESET);
  335. /* erase the user system data */
  336. FLASH->ctrl_bit.usders = TRUE;
  337. FLASH->ctrl_bit.erstr = TRUE;
  338. /* wait for operation to be completed */
  339. status = flash_operation_wait_for(ERASE_TIMEOUT);
  340. /* disable the usders bit */
  341. FLASH->ctrl_bit.usders = FALSE;
  342. if((status == FLASH_OPERATE_DONE) && (fap_val == FAP_RELIEVE_KEY))
  343. {
  344. /* enable the user system data programming operation */
  345. FLASH->ctrl_bit.usdprgm = TRUE;
  346. /* restore the last flash access protection value */
  347. USD->fap = (uint16_t)fap_val;
  348. /* wait for operation to be completed */
  349. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  350. /*disable the usdprgm bit */
  351. FLASH->ctrl_bit.usdprgm = FALSE;
  352. }
  353. /* return the erase status */
  354. return status;
  355. }
  356. /**
  357. * @brief program a word at a specified address.
  358. * @param address: specifies the address to be programmed, word alignment is recommended.
  359. * @param data: specifies the data to be programmed.
  360. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  361. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  362. */
  363. flash_status_type flash_word_program(uint32_t address, uint32_t data)
  364. {
  365. flash_status_type status = FLASH_OPERATE_DONE;
  366. /* spim : external flash */
  367. if(address >= FLASH_SPIM_START_ADDR)
  368. {
  369. FLASH->ctrl3_bit.fprgm = TRUE;
  370. *(__IO uint32_t*)address = data;
  371. /* wait for operation to be completed */
  372. status = flash_spim_operation_wait_for(SPIM_PROGRAMMING_TIMEOUT);
  373. /* disable the fprgm bit */
  374. FLASH->ctrl3_bit.fprgm = FALSE;
  375. }
  376. else
  377. {
  378. FLASH->ctrl_bit.fprgm = TRUE;
  379. *(__IO uint32_t*)address = data;
  380. /* wait for operation to be completed */
  381. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  382. /* disable the fprgm bit */
  383. FLASH->ctrl_bit.fprgm = FALSE;
  384. }
  385. /* return the program status */
  386. return status;
  387. }
  388. /**
  389. * @brief program a halfword at a specified address.
  390. * @param address: specifies the address to be programmed, halfword alignment is recommended.
  391. * @param data: specifies the data to be programmed.
  392. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  393. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  394. */
  395. flash_status_type flash_halfword_program(uint32_t address, uint16_t data)
  396. {
  397. flash_status_type status = FLASH_OPERATE_DONE;
  398. /* spim : external flash */
  399. if(address >= FLASH_SPIM_START_ADDR)
  400. {
  401. FLASH->ctrl3_bit.fprgm = TRUE;
  402. *(__IO uint16_t*)address = data;
  403. /* wait for operation to be completed */
  404. status = flash_spim_operation_wait_for(SPIM_PROGRAMMING_TIMEOUT);
  405. /* disable the fprgm bit */
  406. FLASH->ctrl3_bit.fprgm = FALSE;
  407. }
  408. else
  409. {
  410. FLASH->ctrl_bit.fprgm = TRUE;
  411. *(__IO uint16_t*)address = data;
  412. /* wait for operation to be completed */
  413. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  414. /* disable the fprgm bit */
  415. FLASH->ctrl_bit.fprgm = FALSE;
  416. }
  417. /* return the program status */
  418. return status;
  419. }
  420. /**
  421. * @brief program a byte at a specified address.
  422. * @note this function cannot be used to program spim.
  423. * @param address: specifies the address to be programmed.
  424. * @param data: specifies the data to be programmed.
  425. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  426. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  427. */
  428. flash_status_type flash_byte_program(uint32_t address, uint8_t data)
  429. {
  430. flash_status_type status = FLASH_OPERATE_DONE;
  431. FLASH->ctrl_bit.fprgm = TRUE;
  432. *(__IO uint8_t*)address = data;
  433. /* wait for operation to be completed */
  434. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  435. /* disable the fprgm bit */
  436. FLASH->ctrl_bit.fprgm = FALSE;
  437. /* return the program status */
  438. return status;
  439. }
  440. /**
  441. * @brief program a halfword at a specified user system data address.
  442. * @param address: specifies the address to be programmed.
  443. * @param data: specifies the data to be programmed.
  444. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  445. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  446. */
  447. flash_status_type flash_user_system_data_program(uint32_t address, uint8_t data)
  448. {
  449. flash_status_type status = FLASH_OPERATE_DONE;
  450. if(address == USD_BASE)
  451. {
  452. if(data != 0xA5)
  453. return FLASH_OPERATE_DONE;
  454. }
  455. /* unlock the user system data */
  456. FLASH->usd_unlock = FLASH_UNLOCK_KEY1;
  457. FLASH->usd_unlock = FLASH_UNLOCK_KEY2;
  458. while(FLASH->ctrl_bit.usdulks==RESET);
  459. /* enable the user system data programming operation */
  460. FLASH->ctrl_bit.usdprgm = TRUE;
  461. *(__IO uint16_t*)address = data;
  462. /* wait for operation to be completed */
  463. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  464. /* disable the usdprgm bit */
  465. FLASH->ctrl_bit.usdprgm = FALSE;
  466. /* return the user system data program status */
  467. return status;
  468. }
  469. /**
  470. * @brief config erase/program protection for the desired sectors.
  471. * @param sector_bits:
  472. * the pointer of the address of the sectors to be erase/program protected.
  473. * general every bit is used to protect the 4KB bytes, and the last one bit
  474. * is used to protect the rest.
  475. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  476. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  477. */
  478. flash_status_type flash_epp_set(uint32_t *sector_bits)
  479. {
  480. uint16_t epp_data[4] = {0xFFFF,0xFFFF,0xFFFF,0xFFFF};
  481. flash_status_type status = FLASH_OPERATE_DONE;
  482. sector_bits[0] = (uint32_t)(~sector_bits[0]);
  483. epp_data[0] = (uint16_t)((sector_bits[0] >> 0) & 0xFF);
  484. epp_data[1] = (uint16_t)((sector_bits[0] >> 8) & 0xFF);
  485. epp_data[2] = (uint16_t)((sector_bits[0] >> 16) & 0xFF);
  486. epp_data[3] = (uint16_t)((sector_bits[0] >> 24) & 0xFF);
  487. /* unlock the user system data */
  488. FLASH->usd_unlock = FLASH_UNLOCK_KEY1;
  489. FLASH->usd_unlock = FLASH_UNLOCK_KEY2;
  490. while(FLASH->ctrl_bit.usdulks==RESET);
  491. FLASH->ctrl_bit.usdprgm = TRUE;
  492. USD->epp0 = epp_data[0];
  493. /* wait for operation to be completed */
  494. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  495. if(status == FLASH_OPERATE_DONE)
  496. {
  497. USD->epp1 = epp_data[1];
  498. /* wait for operation to be completed */
  499. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  500. }
  501. if(status == FLASH_OPERATE_DONE)
  502. {
  503. USD->epp2 = epp_data[2];
  504. /* wait for operation to be completed */
  505. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  506. }
  507. if(status == FLASH_OPERATE_DONE)
  508. {
  509. USD->epp3 = epp_data[3];
  510. /* wait for operation to be completed */
  511. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  512. }
  513. /* disable the usdprgm bit */
  514. FLASH->ctrl_bit.usdprgm = FALSE;
  515. /* return the erase/program protection operation status */
  516. return status;
  517. }
  518. /**
  519. * @brief return the flash erase/program protection status.
  520. * @param sector_bits: pointer to get the epps register.
  521. * @retval none
  522. */
  523. void flash_epp_status_get(uint32_t *sector_bits)
  524. {
  525. /* return the flash erase/program protection register value */
  526. sector_bits[0] = (uint32_t)(FLASH->epps);
  527. }
  528. /**
  529. * @brief enable or disable the flash access protection.
  530. * @note if the user has already programmed the other user system data before calling
  531. * this function, must re-program them since this function erase all user system data.
  532. * @param new_state: new state of the flash access protection.
  533. * this parameter can be: TRUE or FALSE.
  534. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  535. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  536. */
  537. flash_status_type flash_fap_enable(confirm_state new_state)
  538. {
  539. flash_status_type status = FLASH_OPERATE_DONE;
  540. /* unlock the user system data */
  541. FLASH->usd_unlock = FLASH_UNLOCK_KEY1;
  542. FLASH->usd_unlock = FLASH_UNLOCK_KEY2;
  543. while(FLASH->ctrl_bit.usdulks==RESET);
  544. FLASH->ctrl_bit.usders = TRUE;
  545. FLASH->ctrl_bit.erstr = TRUE;
  546. /* wait for operation to be completed */
  547. status = flash_operation_wait_for(ERASE_TIMEOUT);
  548. /* disable the usders bit */
  549. FLASH->ctrl_bit.usders = FALSE;
  550. if(status == FLASH_OPERATE_DONE)
  551. {
  552. if(new_state == FALSE)
  553. {
  554. /* enable the user system data programming operation */
  555. FLASH->ctrl_bit.usdprgm = TRUE;
  556. USD->fap = FAP_RELIEVE_KEY;
  557. /* Wait for operation to be completed */
  558. status = flash_operation_wait_for(ERASE_TIMEOUT);
  559. /* disable the usdprgm bit */
  560. FLASH->ctrl_bit.usdprgm = FALSE;
  561. }
  562. }
  563. /* return the flash access protection operation status */
  564. return status;
  565. }
  566. /**
  567. * @brief check the flash access protection status.
  568. * @param none
  569. * @retval flash access protection status(SET or RESET)
  570. */
  571. flag_status flash_fap_status_get(void)
  572. {
  573. return (flag_status)FLASH->usd_bit.fap;
  574. }
  575. /**
  576. * @brief program the flash system setting byte in usd: wdt_ato_en / depslp_rst / stdby_rst.
  577. * @param usd_ssb: the system setting byte
  578. * @note this parameter usd_ssb must contain a combination of all the following 3 types of data
  579. * type 1: wdt_ato_en, select the wdt auto start
  580. * this data can be one of the following values:
  581. * - USD_WDT_ATO_DISABLE: disable wdt auto start
  582. * - USD_WDT_ATO_ENABLE: enable wdt auto start
  583. * type 2: depslp_rst, reset event when entering deepsleep mode.
  584. * this data can be one of the following values:
  585. * - USD_DEPSLP_NO_RST: no reset generated when entering in deepsleep
  586. * - USD_DEPSLP_RST: reset generated when entering in deepsleep
  587. * type 3: stdby_rst, reset event when entering standby mode.
  588. * this data can be one of the following values:
  589. * - USD_STDBY_NO_RST: no reset generated when entering in standby
  590. * - USD_STDBY_RST: reset generated when entering in standby
  591. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  592. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  593. */
  594. flash_status_type flash_ssb_set(uint8_t usd_ssb)
  595. {
  596. flash_status_type status = FLASH_OPERATE_DONE;
  597. /* unlock the user system data */
  598. FLASH->usd_unlock = FLASH_UNLOCK_KEY1;
  599. FLASH->usd_unlock = FLASH_UNLOCK_KEY2;
  600. while(FLASH->ctrl_bit.usdulks==RESET);
  601. /* enable the user system data programming operation */
  602. FLASH->ctrl_bit.usdprgm = TRUE;
  603. USD->ssb = usd_ssb;
  604. /* wait for operation to be completed */
  605. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  606. /* disable the usdprgm bit */
  607. FLASH->ctrl_bit.usdprgm = FALSE;
  608. /* return the user system data program status */
  609. return status;
  610. }
  611. /**
  612. * @brief return the flash system setting byte status.
  613. * @param none
  614. * @retval values from flash_usd register: wdt_ato_en(bit0), depslp_rst(bit1) and stdby_rst(bit2).
  615. */
  616. uint8_t flash_ssb_status_get(void)
  617. {
  618. /* return the system setting byte status */
  619. return (uint8_t)(FLASH->usd >> 2);
  620. }
  621. /**
  622. * @brief enable or disable the specified flash interrupts.
  623. * @param flash_int: specifies the flash interrupt sources to be enabled or disabled.
  624. * this parameter can be any combination of the following values:
  625. * - FLASH_ERR_INT
  626. * - FLASH_ODF_INT
  627. * - FLASH_SPIM_ERR_INT
  628. * - FLASH_SPIM_ODF_INT
  629. * @param new_state: new state of the specified flash interrupts.
  630. * this parameter can be: TRUE or FALSE.
  631. * @retval none
  632. */
  633. void flash_interrupt_enable(uint32_t flash_int, confirm_state new_state)
  634. {
  635. if(flash_int & FLASH_ERR_INT)
  636. FLASH->ctrl_bit.errie = new_state;
  637. if(flash_int & FLASH_ODF_INT)
  638. FLASH->ctrl_bit.odfie = new_state;
  639. if(flash_int & FLASH_SPIM_ERR_INT)
  640. FLASH->ctrl3_bit.errie = new_state;
  641. if(flash_int & FLASH_SPIM_ODF_INT)
  642. FLASH->ctrl3_bit.odfie = new_state;
  643. }
  644. /**
  645. * @brief select spim supports extended spi flash chip model.
  646. * @param mode: the extended spi flash model
  647. * @retval none
  648. */
  649. void flash_spim_model_select(flash_spim_model_type mode)
  650. {
  651. FLASH->select = mode;
  652. }
  653. /**
  654. * @brief set the range of encryption in spim flash.
  655. * when the address is larger than this value, the writing data will be
  656. * directly written to spim without encryption.
  657. * @param decode_address: the end address of encrypted data in spim
  658. * @retval none
  659. */
  660. void flash_spim_encryption_range_set(uint32_t decode_address)
  661. {
  662. FLASH->da = decode_address;
  663. }
  664. /**
  665. * @brief enable security library function.
  666. * @param pwd: slib password
  667. * start_sector: security library start sector
  668. * data_start_sector: security library d-bus area start sector
  669. * end_sector: security library end sector
  670. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  671. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  672. */
  673. flash_status_type flash_slib_enable(uint32_t pwd, uint16_t start_sector, uint16_t data_start_sector, uint16_t end_sector)
  674. {
  675. uint32_t slib_range;
  676. flash_status_type status = FLASH_OPERATE_DONE;
  677. /*check range param limits*/
  678. if((start_sector>=data_start_sector) || ((data_start_sector > end_sector) && \
  679. (data_start_sector != 0x7FF)) || (start_sector > end_sector))
  680. return FLASH_PROGRAM_ERROR;
  681. /* unlock slib cfg register */
  682. FLASH->slib_unlock = SLIB_UNLOCK_KEY;
  683. while(FLASH->slib_misc_sts_bit.slib_ulkf==RESET);
  684. slib_range = ((uint32_t)(data_start_sector << 11) & FLASH_SLIB_DATA_START_SECTOR) | \
  685. ((uint32_t)(end_sector << 22) & FLASH_SLIB_END_SECTOR) | \
  686. (start_sector & FLASH_SLIB_START_SECTOR);
  687. /* configure slib, set pwd and range */
  688. FLASH->slib_set_pwd = pwd;
  689. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  690. if(status == FLASH_OPERATE_DONE)
  691. {
  692. FLASH->slib_set_range = slib_range;
  693. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  694. }
  695. return status;
  696. }
  697. /**
  698. * @brief disable slib when slib enabled.
  699. * @param pwd: slib password
  700. * @retval success or error
  701. */
  702. error_status flash_slib_disable(uint32_t pwd)
  703. {
  704. flash_status_type status = FLASH_OPERATE_DONE;
  705. /* write password to disable slib */
  706. FLASH->slib_pwd_clr = pwd;
  707. status = flash_operation_wait_for(ERASE_TIMEOUT);
  708. if(status == FLASH_OPERATE_DONE)
  709. {
  710. if(FLASH->slib_misc_sts_bit.slib_pwd_ok)
  711. return SUCCESS;
  712. else
  713. return ERROR;
  714. }
  715. return ERROR;
  716. }
  717. /**
  718. * @brief get remaining count of slib(range: 256~0).
  719. * @param none
  720. * @retval uint32_t
  721. */
  722. uint32_t flash_slib_remaining_count_get(void)
  723. {
  724. return (uint32_t)FLASH->slib_misc_sts_bit.slib_rcnt;
  725. }
  726. /**
  727. * @brief get the slib state.
  728. * @param none
  729. * @retval SET or RESET
  730. */
  731. flag_status flash_slib_state_get(void)
  732. {
  733. if(FLASH->slib_sts0_bit.slib_enf)
  734. return SET;
  735. else
  736. return RESET;
  737. }
  738. /**
  739. * @brief get the start sector of slib.
  740. * @param none
  741. * @retval uint16_t
  742. */
  743. uint16_t flash_slib_start_sector_get(void)
  744. {
  745. return (uint16_t)FLASH->slib_sts1_bit.slib_ss;
  746. }
  747. /**
  748. * @brief get the data start sector of slib.
  749. * @param none
  750. * @retval uint16_t
  751. */
  752. uint16_t flash_slib_datastart_sector_get(void)
  753. {
  754. return (uint16_t)FLASH->slib_sts1_bit.slib_dat_ss;
  755. }
  756. /**
  757. * @brief get the end sector of slib.
  758. * @param none
  759. * @retval uint16_t
  760. */
  761. uint16_t flash_slib_end_sector_get(void)
  762. {
  763. return (uint16_t)FLASH->slib_sts1_bit.slib_es;
  764. }
  765. /**
  766. * @brief flash crc calibration in main block.
  767. * @param start_sector: crc calibration start sector number
  768. * sector_cnt: crc calibration sector count
  769. * @retval uint32: crc calibration result
  770. */
  771. uint32_t flash_crc_calibrate(uint32_t start_sector, uint32_t sector_cnt)
  772. {
  773. FLASH->crc_ctrl_bit.crc_ss = start_sector;
  774. FLASH->crc_ctrl_bit.crc_sn = sector_cnt;
  775. FLASH->crc_ctrl_bit.crc_strt = TRUE;
  776. flash_operation_wait_for(OPERATION_TIMEOUT);
  777. return FLASH->crc_chkr;
  778. }
  779. /**
  780. * @}
  781. */
  782. #endif
  783. /**
  784. * @}
  785. */
  786. /**
  787. * @}
  788. */