at32f415_flash.c 26 KB

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