at32a423_flash.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /**
  2. **************************************************************************
  3. * @file at32a423_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 "at32a423_conf.h"
  25. /** @addtogroup AT32A423_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_USDERR_FLAG
  45. * @retval the new state of flash_flag (SET or RESET).
  46. */
  47. flag_status flash_flag_get(uint32_t flash_flag)
  48. {
  49. flag_status status = RESET;
  50. uint32_t flag_position;
  51. flag_position = flash_flag & 0x70000000;
  52. flash_flag &= 0x8FFFFFFF;
  53. switch(flag_position)
  54. {
  55. case 0x00000000:
  56. if(FLASH->sts & flash_flag)
  57. status = SET;
  58. break;
  59. case 0x40000000:
  60. if(FLASH->usd & flash_flag)
  61. status = SET;
  62. break;
  63. default:
  64. break;
  65. }
  66. /* return the new state of flash_flag (SET or RESET) */
  67. return status;
  68. }
  69. /**
  70. * @brief clear the flash flag.
  71. * @param flash_flag: specifies the flash flags to clear.
  72. * this parameter can be any combination of the following values:
  73. * - FLASH_ODF_FLAG
  74. * - FLASH_PRGMERR_FLAG
  75. * - FLASH_EPPERR_FLAG
  76. * @retval none
  77. */
  78. void flash_flag_clear(uint32_t flash_flag)
  79. {
  80. FLASH->sts = flash_flag;
  81. }
  82. /**
  83. * @brief return the flash operation status.
  84. * @param none
  85. * @retval status: the returned value can be: FLASH_OPERATE_BUSY,
  86. * FLASH_PROGRAM_ERROR, FLASH_EPP_ERROR or FLASH_OPERATE_DONE.
  87. */
  88. flash_status_type flash_operation_status_get(void)
  89. {
  90. flash_status_type flash_status = FLASH_OPERATE_DONE;
  91. if(FLASH->sts_bit.obf != RESET)
  92. {
  93. flash_status = FLASH_OPERATE_BUSY;
  94. }
  95. else if(FLASH->sts_bit.prgmerr != RESET)
  96. {
  97. flash_status = FLASH_PROGRAM_ERROR;
  98. }
  99. else if(FLASH->sts_bit.epperr != RESET)
  100. {
  101. flash_status = FLASH_EPP_ERROR;
  102. }
  103. else
  104. {
  105. flash_status = FLASH_OPERATE_DONE;
  106. }
  107. /* return the flash status */
  108. return flash_status;
  109. }
  110. /**
  111. * @brief wait for flash operation complete or timeout.
  112. * @param time_out: flash operation timeout
  113. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  114. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  115. */
  116. flash_status_type flash_operation_wait_for(uint32_t time_out)
  117. {
  118. flash_status_type status = FLASH_OPERATE_DONE;
  119. /* check for the flash status */
  120. status = flash_operation_status_get();
  121. while((status == FLASH_OPERATE_BUSY) && (time_out != 0x00))
  122. {
  123. status = flash_operation_status_get();
  124. time_out--;
  125. }
  126. if(time_out == 0x00)
  127. {
  128. status = FLASH_OPERATE_TIMEOUT;
  129. }
  130. /* return the status */
  131. return status;
  132. }
  133. /**
  134. * @brief unlock the flash controller.
  135. * @param none
  136. * @retval none
  137. */
  138. void flash_unlock(void)
  139. {
  140. FLASH->unlock = FLASH_UNLOCK_KEY1;
  141. FLASH->unlock = FLASH_UNLOCK_KEY2;
  142. }
  143. /**
  144. * @brief lock the flash controller.
  145. * @param none
  146. * @retval none
  147. */
  148. void flash_lock(void)
  149. {
  150. FLASH->ctrl_bit.oplk = TRUE;
  151. }
  152. /**
  153. * @brief erase a specified flash sector.
  154. * @param sector_address: the sector address to be erased.
  155. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  156. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  157. */
  158. flash_status_type flash_sector_erase(uint32_t sector_address)
  159. {
  160. flash_status_type status = FLASH_OPERATE_DONE;
  161. FLASH->ctrl_bit.secers = TRUE;
  162. FLASH->addr = sector_address;
  163. FLASH->ctrl_bit.erstr = TRUE;
  164. /* wait for operation to be completed */
  165. status = flash_operation_wait_for(ERASE_TIMEOUT);
  166. /* disable the secers bit */
  167. FLASH->ctrl_bit.secers = FALSE;
  168. /* return the erase status */
  169. return status;
  170. }
  171. /**
  172. * @brief erase flash all internal sectors.
  173. * @param none
  174. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  175. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  176. */
  177. flash_status_type flash_internal_all_erase(void)
  178. {
  179. flash_status_type status = FLASH_OPERATE_DONE;
  180. FLASH->ctrl_bit.bankers = TRUE;
  181. FLASH->ctrl_bit.erstr = TRUE;
  182. /* wait for operation to be completed */
  183. status = flash_operation_wait_for(ERASE_TIMEOUT);
  184. /* disable the bankers bit */
  185. FLASH->ctrl_bit.bankers = FALSE;
  186. /* return the erase status */
  187. return status;
  188. }
  189. /**
  190. * @brief erase the flash user system data.
  191. * @note this functions erases all user system data except the fap byte.
  192. * when fap high level enabled, can't use this function.
  193. * @param none
  194. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  195. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  196. */
  197. flash_status_type flash_user_system_data_erase(void)
  198. {
  199. flash_status_type status = FLASH_OPERATE_DONE;
  200. uint16_t fap_val = FAP_RELIEVE_KEY;
  201. /* get the flash access protection status */
  202. if(flash_fap_status_get() != RESET)
  203. {
  204. fap_val = 0x0000;
  205. }
  206. /* unlock the user system data */
  207. FLASH->usd_unlock = FLASH_UNLOCK_KEY1;
  208. FLASH->usd_unlock = FLASH_UNLOCK_KEY2;
  209. while(FLASH->ctrl_bit.usdulks==RESET);
  210. /* erase the user system data */
  211. FLASH->ctrl_bit.usders = TRUE;
  212. FLASH->ctrl_bit.erstr = TRUE;
  213. /* wait for operation to be completed */
  214. status = flash_operation_wait_for(ERASE_TIMEOUT);
  215. /* disable the usders bit */
  216. FLASH->ctrl_bit.usders = FALSE;
  217. if((status == FLASH_OPERATE_DONE) && (fap_val == FAP_RELIEVE_KEY))
  218. {
  219. /* enable the user system data programming operation */
  220. FLASH->ctrl_bit.usdprgm = TRUE;
  221. /* restore the last flash access protection value */
  222. USD->fap = (uint16_t)fap_val;
  223. /* wait for operation to be completed */
  224. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  225. /*disable the usdprgm bit */
  226. FLASH->ctrl_bit.usdprgm = FALSE;
  227. }
  228. /* return the erase status */
  229. return status;
  230. }
  231. /**
  232. * @brief program a word at a specified address.
  233. * @param address: specifies the address to be programmed, word alignment is recommended.
  234. * @param data: specifies the data to be programmed.
  235. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  236. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  237. */
  238. flash_status_type flash_word_program(uint32_t address, uint32_t data)
  239. {
  240. flash_status_type status = FLASH_OPERATE_DONE;
  241. FLASH->ctrl_bit.fprgm = TRUE;
  242. *(__IO uint32_t*)address = data;
  243. /* wait for operation to be completed */
  244. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  245. /* disable the fprgm bit */
  246. FLASH->ctrl_bit.fprgm = FALSE;
  247. /* return the program status */
  248. return status;
  249. }
  250. /**
  251. * @brief program a halfword at a specified address.
  252. * @param address: specifies the address to be programmed, halfword 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_halfword_program(uint32_t address, uint16_t data)
  258. {
  259. flash_status_type status = FLASH_OPERATE_DONE;
  260. FLASH->ctrl_bit.fprgm = TRUE;
  261. *(__IO uint16_t*)address = data;
  262. /* wait for operation to be completed */
  263. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  264. /* disable the fprgm bit */
  265. FLASH->ctrl_bit.fprgm = FALSE;
  266. /* return the program status */
  267. return status;
  268. }
  269. /**
  270. * @brief program a byte at a specified address.
  271. * @param address: specifies the address to be programmed.
  272. * @param data: specifies the data to be programmed.
  273. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  274. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  275. */
  276. flash_status_type flash_byte_program(uint32_t address, uint8_t data)
  277. {
  278. flash_status_type status = FLASH_OPERATE_DONE;
  279. FLASH->ctrl_bit.fprgm = TRUE;
  280. *(__IO uint8_t*)address = data;
  281. /* wait for operation to be completed */
  282. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  283. /* disable the fprgm bit */
  284. FLASH->ctrl_bit.fprgm = FALSE;
  285. /* return the program status */
  286. return status;
  287. }
  288. /**
  289. * @brief program a halfword at a specified user system data address.
  290. * @param address: specifies the address to be programmed.
  291. * @param data: specifies the data to be programmed.
  292. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  293. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  294. */
  295. flash_status_type flash_user_system_data_program(uint32_t address, uint8_t data)
  296. {
  297. flash_status_type status = FLASH_OPERATE_DONE;
  298. /* unlock the user system data */
  299. FLASH->usd_unlock = FLASH_UNLOCK_KEY1;
  300. FLASH->usd_unlock = FLASH_UNLOCK_KEY2;
  301. while(FLASH->ctrl_bit.usdulks==RESET);
  302. /* enable the user system data programming operation */
  303. FLASH->ctrl_bit.usdprgm = TRUE;
  304. *(__IO uint16_t*)address = data;
  305. /* wait for operation to be completed */
  306. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  307. /* disable the usdprgm bit */
  308. FLASH->ctrl_bit.usdprgm = FALSE;
  309. /* return the user system data program status */
  310. return status;
  311. }
  312. /**
  313. * @brief config erase/program protection for the desired sectors.
  314. * @param sector_bits:
  315. * the pointer of the address of the sectors to be erase/program protected.
  316. * the first 16bits general every bit is used to protect the 4KB bytes. the
  317. * bit 31 is used to protect the extension memory.
  318. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  319. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  320. */
  321. flash_status_type flash_epp_set(uint32_t *sector_bits)
  322. {
  323. uint16_t epp_data[4] = {0xFFFF,0xFFFF,0xFFFF,0xFFFF};
  324. flash_status_type status = FLASH_OPERATE_DONE;
  325. sector_bits[0] = (uint32_t)(~sector_bits[0]);
  326. epp_data[0] = (uint16_t)((sector_bits[0] >> 0) & 0xFF);
  327. epp_data[1] = (uint16_t)((sector_bits[0] >> 8) & 0xFF);
  328. epp_data[2] = (uint16_t)((sector_bits[0] >> 16) & 0xFF);
  329. epp_data[3] = (uint16_t)((sector_bits[0] >> 24) & 0xFF);
  330. /* unlock the user system data */
  331. FLASH->usd_unlock = FLASH_UNLOCK_KEY1;
  332. FLASH->usd_unlock = FLASH_UNLOCK_KEY2;
  333. while(FLASH->ctrl_bit.usdulks==RESET);
  334. FLASH->ctrl_bit.usdprgm = TRUE;
  335. USD->epp0 = epp_data[0];
  336. /* wait for operation to be completed */
  337. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  338. if(status == FLASH_OPERATE_DONE)
  339. {
  340. USD->epp1 = epp_data[1];
  341. /* wait for operation to be completed */
  342. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  343. }
  344. if(status == FLASH_OPERATE_DONE)
  345. {
  346. USD->epp2 = epp_data[2];
  347. /* wait for operation to be completed */
  348. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  349. }
  350. if(status == FLASH_OPERATE_DONE)
  351. {
  352. USD->epp3 = epp_data[3];
  353. /* wait for operation to be completed */
  354. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  355. }
  356. /* disable the usdprgm bit */
  357. FLASH->ctrl_bit.usdprgm = FALSE;
  358. /* return the erase/program protection operation status */
  359. return status;
  360. }
  361. /**
  362. * @brief return the flash erase/program protection status.
  363. * @param sector_bits: pointer to get the epps register.
  364. * @retval none
  365. */
  366. void flash_epp_status_get(uint32_t *sector_bits)
  367. {
  368. /* return the flash erase/program protection register value */
  369. sector_bits[0] = (uint32_t)(FLASH->epps);
  370. }
  371. /**
  372. * @brief enable or disable the flash access protection.
  373. * @note if the user has already programmed the other user system data before calling
  374. * this function, must re-program them since this function erase all user system data.
  375. * @param new_state: new state of the flash access protection.
  376. * this parameter can be: TRUE or FALSE.
  377. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  378. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  379. */
  380. flash_status_type flash_fap_enable(confirm_state new_state)
  381. {
  382. flash_status_type status = FLASH_OPERATE_DONE;
  383. /* unlock the user system data */
  384. FLASH->usd_unlock = FLASH_UNLOCK_KEY1;
  385. FLASH->usd_unlock = FLASH_UNLOCK_KEY2;
  386. while(FLASH->ctrl_bit.usdulks==RESET);
  387. FLASH->ctrl_bit.usders = TRUE;
  388. FLASH->ctrl_bit.erstr = TRUE;
  389. /* wait for operation to be completed */
  390. status = flash_operation_wait_for(ERASE_TIMEOUT);
  391. /* disable the usders bit */
  392. FLASH->ctrl_bit.usders = FALSE;
  393. if(status == FLASH_OPERATE_DONE)
  394. {
  395. if(new_state == FALSE)
  396. {
  397. /* enable the user system data programming operation */
  398. FLASH->ctrl_bit.usdprgm = TRUE;
  399. USD->fap = FAP_RELIEVE_KEY;
  400. /* Wait for operation to be completed */
  401. status = flash_operation_wait_for(ERASE_TIMEOUT);
  402. /* disable the usdprgm bit */
  403. FLASH->ctrl_bit.usdprgm = FALSE;
  404. }
  405. }
  406. /* return the flash access protection operation status */
  407. return status;
  408. }
  409. /**
  410. * @brief check the flash access protection status.
  411. * @param none
  412. * @retval flash access protection status(SET or RESET)
  413. */
  414. flag_status flash_fap_status_get(void)
  415. {
  416. return (flag_status)FLASH->usd_bit.fap;
  417. }
  418. /**
  419. * @brief enable or disable the flash access protection high level.
  420. * @note if the user has already programmed the other user system data before calling
  421. * this function, must re-program them since this function erase all user system data.
  422. * @note once the fap high level enabled, the swd debugger port will be permanently disable!!!
  423. * @param new_state: new state of the flash access protection high level.
  424. * this parameter can be: TRUE or FALSE.
  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_fap_high_level_enable(void)
  429. {
  430. flash_status_type status = FLASH_OPERATE_DONE;
  431. /* unlock the user system data */
  432. FLASH->usd_unlock = FLASH_UNLOCK_KEY1;
  433. FLASH->usd_unlock = FLASH_UNLOCK_KEY2;
  434. while(FLASH->ctrl_bit.usdulks==RESET);
  435. FLASH->ctrl_bit.usders = TRUE;
  436. FLASH->ctrl_bit.erstr = TRUE;
  437. /* wait for operation to be completed */
  438. status = flash_operation_wait_for(ERASE_TIMEOUT);
  439. /* disable the usders bit */
  440. FLASH->ctrl_bit.usders = FALSE;
  441. if(status == FLASH_OPERATE_DONE)
  442. {
  443. /* enable the user system data programming operation */
  444. FLASH->ctrl_bit.usdprgm = TRUE;
  445. USD->fap = FAP_HIGH_LEVEL_KEY;
  446. /* wait for operation to be completed */
  447. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  448. /* disable the usdprgm bit */
  449. FLASH->ctrl_bit.usdprgm = FALSE;
  450. }
  451. /* return the flash access protection operation status */
  452. return status;
  453. }
  454. /**
  455. * @brief check the flash access protection high level status.
  456. * @param none
  457. * @retval flash access protection high level status(SET or RESET)
  458. */
  459. flag_status flash_fap_high_level_status_get(void)
  460. {
  461. return (flag_status)FLASH->usd_bit.fap_hl;
  462. }
  463. /**
  464. * @brief program the flash system setting byte in usd: wdt_ato_en / depslp_rst / stdby_rst / boot1 / depslp_wdt / stdby_wdt.
  465. * @param usd_ssb: the system setting byte
  466. * @note this parameter usd_ssb must contain a combination of all the following 6 types of data
  467. * type 1: wdt_ato_en, select the wdt auto start
  468. * this data can be one of the following values:
  469. * - USD_WDT_ATO_DISABLE: disable wdt auto start
  470. * - USD_WDT_ATO_ENABLE: enable wdt auto start
  471. * type 2: depslp_rst, reset event when entering deepsleep mode.
  472. * this data can be one of the following values:
  473. * - USD_DEPSLP_NO_RST: no reset generated when entering in deepsleep
  474. * - USD_DEPSLP_RST: reset generated when entering in deepsleep
  475. * type 3: stdby_rst, reset event when entering standby mode.
  476. * this data can be one of the following values:
  477. * - USD_STDBY_NO_RST: no reset generated when entering in standby
  478. * - USD_STDBY_RST: reset generated when entering in standby
  479. * type 4: boot1, at startup, when boot0 pin is high level, selected the device boot from bootmem/sram.
  480. * this data can be one of the following values:
  481. * - USD_BOOT1_LOW: when boot0 is high level, boot from bootmem
  482. * - USD_BOOT1_HIGH: when boot0 is high level, boot from sram
  483. * type 5: depslp_wdt, wdt stop count when entering deepsleep mode.
  484. * this data can be one of the following values:
  485. * - USD_DEPSLP_WDT_CONTINUE: wdt continue count when entering in deepsleep
  486. * - USD_DEPSLP_WDT_STOP: wdt stop count when entering in deepsleep
  487. * type 6: stdby_wdt, wdt stop count when entering standby mode.
  488. * this data can be one of the following values:
  489. * - USD_STDBY_WDT_CONTINUE: wdt continue count when entering in standby
  490. * - USD_STDBY_WDT_STOP: wdt stop count when entering in standby
  491. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  492. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  493. */
  494. flash_status_type flash_ssb_set(uint8_t usd_ssb)
  495. {
  496. flash_status_type status = FLASH_OPERATE_DONE;
  497. /* unlock the user system data */
  498. FLASH->usd_unlock = FLASH_UNLOCK_KEY1;
  499. FLASH->usd_unlock = FLASH_UNLOCK_KEY2;
  500. while(FLASH->ctrl_bit.usdulks==RESET);
  501. /* enable the user system data programming operation */
  502. FLASH->ctrl_bit.usdprgm = TRUE;
  503. USD->ssb = usd_ssb;
  504. /* wait for operation to be completed */
  505. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  506. /* disable the usdprgm bit */
  507. FLASH->ctrl_bit.usdprgm = FALSE;
  508. /* return the user system data program status */
  509. return status;
  510. }
  511. /**
  512. * @brief return the flash system setting byte status.
  513. * @param none
  514. * @retval values from flash_usd register: wdt_ato_en(bit0), depslp_rst(bit1),
  515. * stdby_rst(bit2), boot1(bit4), depslp_wdt(bit5) and stdby_wdt(bit6).
  516. */
  517. uint8_t flash_ssb_status_get(void)
  518. {
  519. /* return the system setting byte status */
  520. return (uint8_t)(FLASH->usd >> 2);
  521. }
  522. /**
  523. * @brief enable or disable the specified flash interrupts.
  524. * @param flash_int: specifies the flash interrupt sources to be enabled or disabled.
  525. * this parameter can be any combination of the following values:
  526. * - FLASH_ERR_INT
  527. * - FLASH_ODF_INT
  528. * @param new_state: new state of the specified flash interrupts.
  529. * this parameter can be: TRUE or FALSE.
  530. * @retval none
  531. */
  532. void flash_interrupt_enable(uint32_t flash_int, confirm_state new_state)
  533. {
  534. if(flash_int & FLASH_ERR_INT)
  535. FLASH->ctrl_bit.errie = new_state;
  536. if(flash_int & FLASH_ODF_INT)
  537. FLASH->ctrl_bit.odfie = new_state;
  538. }
  539. /**
  540. * @brief enable main block security library function.
  541. * @param pwd: slib password
  542. * start_sector: security library start sector
  543. * inst_start_sector: security library i-bus area start sector
  544. * end_sector: security library end sector
  545. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  546. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  547. */
  548. flash_status_type flash_slib_enable(uint32_t pwd, uint16_t start_sector, uint16_t inst_start_sector, uint16_t end_sector)
  549. {
  550. uint32_t slib_range;
  551. flash_status_type status = FLASH_OPERATE_DONE;
  552. /*check range param limits*/
  553. if((start_sector>inst_start_sector) || ((inst_start_sector > end_sector) && \
  554. (inst_start_sector != 0x7FF)) || (start_sector > end_sector))
  555. return FLASH_PROGRAM_ERROR;
  556. /* unlock slib cfg register */
  557. FLASH->slib_unlock = SLIB_UNLOCK_KEY;
  558. while(FLASH->slib_misc_sts_bit.slib_ulkf==RESET);
  559. slib_range = ((uint32_t)(inst_start_sector << 11) & FLASH_SLIB_INST_START_SECTOR) | \
  560. ((uint32_t)(end_sector << 22) & FLASH_SLIB_END_SECTOR) | \
  561. (start_sector & FLASH_SLIB_START_SECTOR);
  562. /* configure slib, set pwd and range */
  563. FLASH->slib_set_pwd = pwd;
  564. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  565. if(status == FLASH_OPERATE_DONE)
  566. {
  567. FLASH->slib_set_range = slib_range;
  568. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  569. }
  570. return status;
  571. }
  572. /**
  573. * @brief disable main block slib when slib enabled.
  574. * @param pwd: slib password
  575. * @retval success or error
  576. */
  577. error_status flash_slib_disable(uint32_t pwd)
  578. {
  579. flash_status_type status = FLASH_OPERATE_DONE;
  580. /* write password to disable slib */
  581. FLASH->slib_pwd_clr = pwd;
  582. status = flash_operation_wait_for(ERASE_TIMEOUT);
  583. if(status == FLASH_OPERATE_DONE)
  584. {
  585. if(FLASH->slib_misc_sts_bit.slib_pwd_ok)
  586. return SUCCESS;
  587. else
  588. return ERROR;
  589. }
  590. return ERROR;
  591. }
  592. /**
  593. * @brief get the main block slib state.
  594. * @param none
  595. * @retval SET or RESET
  596. */
  597. flag_status flash_slib_state_get(void)
  598. {
  599. if(FLASH->slib_sts0_bit.slib_enf)
  600. return SET;
  601. else
  602. return RESET;
  603. }
  604. /**
  605. * @brief get the main block start sector of slib.
  606. * @param none
  607. * @retval uint16_t
  608. */
  609. uint16_t flash_slib_start_sector_get(void)
  610. {
  611. return (uint16_t)FLASH->slib_sts1_bit.slib_ss;
  612. }
  613. /**
  614. * @brief get the main block inst start sector of slib.
  615. * @param none
  616. * @retval uint16_t
  617. */
  618. uint16_t flash_slib_inststart_sector_get(void)
  619. {
  620. return (uint16_t)FLASH->slib_sts1_bit.slib_inst_ss;
  621. }
  622. /**
  623. * @brief get the main block end sector of slib.
  624. * @param none
  625. * @retval uint16_t
  626. */
  627. uint16_t flash_slib_end_sector_get(void)
  628. {
  629. return (uint16_t)FLASH->slib_sts1_bit.slib_es;
  630. }
  631. /**
  632. * @brief flash crc calibration.
  633. * @param start_addr: crc calibration start sector address
  634. * sector_cnt: crc calibration sector count
  635. * @retval uint32: crc calibration result
  636. */
  637. uint32_t flash_crc_calibrate(uint32_t start_addr, uint32_t sector_cnt)
  638. {
  639. FLASH->crc_addr = start_addr;
  640. FLASH->crc_ctrl = sector_cnt | 0x10000;
  641. flash_operation_wait_for(OPERATION_TIMEOUT);
  642. return FLASH->crc_chkr;
  643. }
  644. /**
  645. * @brief enable boot memory as extension mode.
  646. * @note this function is irreversible, can not disable!!!
  647. * @param none
  648. * @retval none.
  649. */
  650. void flash_boot_memory_extension_mode_enable(void)
  651. {
  652. if(FLASH->slib_sts0_bit.btm_ap_enf == RESET)
  653. {
  654. FLASH->slib_unlock = SLIB_UNLOCK_KEY;
  655. while(FLASH->slib_misc_sts_bit.slib_ulkf==RESET);
  656. FLASH->btm_mode_set = 0;
  657. flash_operation_wait_for(OPERATION_TIMEOUT);
  658. }
  659. }
  660. /**
  661. * @brief enable extension memory security library function.
  662. * @param pwd: slib password
  663. * inst_start_sector: extension memory security library i-bus area start sector, range 0~3 or 0xFF
  664. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  665. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  666. */
  667. flash_status_type flash_extension_memory_slib_enable(uint32_t pwd, uint16_t inst_start_sector)
  668. {
  669. flash_status_type status = FLASH_OPERATE_DONE;
  670. /* unlock slib cfg register */
  671. FLASH->slib_unlock = SLIB_UNLOCK_KEY;
  672. while(FLASH->slib_misc_sts_bit.slib_ulkf==RESET);
  673. /* configure slib, set pwd and range */
  674. FLASH->slib_set_pwd = pwd;
  675. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  676. if(status == FLASH_OPERATE_DONE)
  677. {
  678. FLASH->em_slib_set = (uint32_t)(inst_start_sector << 16) + (uint32_t)0x5AA5;
  679. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  680. }
  681. return status;
  682. }
  683. /**
  684. * @brief get the extension memory slib state.
  685. * @param none
  686. * @retval SET or RESET
  687. */
  688. flag_status flash_extension_memory_slib_state_get(void)
  689. {
  690. if(FLASH->slib_sts0_bit.em_slib_enf)
  691. return SET;
  692. else
  693. return RESET;
  694. }
  695. /**
  696. * @brief get the extension memory inst start sector of slib.
  697. * @param none
  698. * @retval uint16_t
  699. */
  700. uint16_t flash_em_slib_inststart_sector_get(void)
  701. {
  702. return (uint16_t)FLASH->slib_sts0_bit.em_slib_inst_ss;
  703. }
  704. /**
  705. * @}
  706. */
  707. #endif
  708. /**
  709. * @}
  710. */
  711. /**
  712. * @}
  713. */