at32f403a_407_flash.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. /**
  2. **************************************************************************
  3. * @file at32f403a_407_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 "at32f403a_407_conf.h"
  25. /** @addtogroup AT32F403A_407_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_BANK1_OBF_FLAG
  45. * - FLASH_BANK1_ODF_FLAG
  46. * - FLASH_BANK1_PRGMERR_FLAG
  47. * - FLASH_BANK1_EPPERR_FLAG
  48. * - FLASH_BANK2_OBF_FLAG
  49. * - FLASH_BANK2_ODF_FLAG
  50. * - FLASH_BANK2_PRGMERR_FLAG
  51. * - FLASH_BANK2_EPPERR_FLAG
  52. * - FLASH_SPIM_OBF_FLAG
  53. * - FLASH_SPIM_ODF_FLAG
  54. * - FLASH_SPIM_PRGMERR_FLAG
  55. * - FLASH_SPIM_EPPERR_FLAG
  56. * - FLASH_USDERR_FLAG
  57. * @retval the new state of flash_flag (SET or RESET).
  58. */
  59. flag_status flash_flag_get(uint32_t flash_flag)
  60. {
  61. flag_status status = RESET;
  62. uint32_t flag_position;
  63. flag_position = flash_flag & 0x70000000;
  64. flash_flag &= 0x8FFFFFFF;
  65. switch(flag_position)
  66. {
  67. case 0x00000000:
  68. if(FLASH->sts & flash_flag)
  69. status = SET;
  70. break;
  71. case 0x10000000:
  72. if(FLASH->sts2 & flash_flag)
  73. status = SET;
  74. break;
  75. case 0x20000000:
  76. if(FLASH->sts3 & flash_flag)
  77. status = SET;
  78. break;
  79. case 0x40000000:
  80. if(FLASH->usd & flash_flag)
  81. status = SET;
  82. break;
  83. default:
  84. break;
  85. }
  86. /* return the new state of flash_flag (SET or RESET) */
  87. return status;
  88. }
  89. /**
  90. * @brief clear the flash flag.
  91. * @param flash_flag: specifies the flash flags to clear.
  92. * this parameter can be any combination of the following values:
  93. * - FLASH_ODF_FLAG
  94. * - FLASH_PRGMERR_FLAG
  95. * - FLASH_EPPERR_FLAG
  96. * - FLASH_BANK1_ODF_FLAG
  97. * - FLASH_BANK1_PRGMERR_FLAG
  98. * - FLASH_BANK1_EPPERR_FLAG
  99. * - FLASH_BANK2_ODF_FLAG
  100. * - FLASH_BANK2_PRGMERR_FLAG
  101. * - FLASH_BANK2_EPPERR_FLAG
  102. * - FLASH_SPIM_ODF_FLAG
  103. * - FLASH_SPIM_PRGMERR_FLAG
  104. * - FLASH_SPIM_EPPERR_FLAG
  105. * @retval none
  106. */
  107. void flash_flag_clear(uint32_t flash_flag)
  108. {
  109. uint32_t flag_position;
  110. flag_position = flash_flag & 0x70000000;
  111. flash_flag &= 0x8FFFFFFF;
  112. switch(flag_position)
  113. {
  114. case 0x00000000:
  115. FLASH->sts = flash_flag;
  116. break;
  117. case 0x10000000:
  118. FLASH->sts2 = flash_flag;
  119. break;
  120. case 0x20000000:
  121. FLASH->sts3 = flash_flag;
  122. break;
  123. default:
  124. break;
  125. }
  126. }
  127. /**
  128. * @brief return the flash operation status.
  129. * @param none
  130. * @retval status: the returned value can be: FLASH_OPERATE_BUSY,
  131. * FLASH_PROGRAM_ERROR, FLASH_EPP_ERROR or FLASH_OPERATE_DONE.
  132. */
  133. flash_status_type flash_operation_status_get(void)
  134. {
  135. flash_status_type flash_status = FLASH_OPERATE_DONE;
  136. if(FLASH->sts_bit.obf != RESET)
  137. {
  138. flash_status = FLASH_OPERATE_BUSY;
  139. }
  140. else if(FLASH->sts_bit.prgmerr != RESET)
  141. {
  142. flash_status = FLASH_PROGRAM_ERROR;
  143. }
  144. else if(FLASH->sts_bit.epperr != RESET)
  145. {
  146. flash_status = FLASH_EPP_ERROR;
  147. }
  148. else
  149. {
  150. flash_status = FLASH_OPERATE_DONE;
  151. }
  152. /* return the flash status */
  153. return flash_status;
  154. }
  155. /**
  156. * @brief return the flash bank1 operation status.
  157. * @param none
  158. * @retval status: the returned value can be: FLASH_OPERATE_BUSY,
  159. * FLASH_PROGRAM_ERROR, FLASH_EPP_ERROR or FLASH_OPERATE_DONE.
  160. */
  161. flash_status_type flash_bank1_operation_status_get(void)
  162. {
  163. flash_status_type flash_status = FLASH_OPERATE_DONE;
  164. if(FLASH->sts_bit.obf != RESET)
  165. {
  166. flash_status = FLASH_OPERATE_BUSY;
  167. }
  168. else if(FLASH->sts_bit.prgmerr != RESET)
  169. {
  170. flash_status = FLASH_PROGRAM_ERROR;
  171. }
  172. else if(FLASH->sts_bit.epperr != RESET)
  173. {
  174. flash_status = FLASH_EPP_ERROR;
  175. }
  176. else
  177. {
  178. flash_status = FLASH_OPERATE_DONE;
  179. }
  180. /* return the flash status */
  181. return flash_status;
  182. }
  183. /**
  184. * @brief return the flash bank2 operation status.
  185. * @param none
  186. * @retval status: the returned value can be: FLASH_OPERATE_BUSY,
  187. * FLASH_PROGRAM_ERROR, FLASH_EPP_ERROR or FLASH_OPERATE_DONE.
  188. */
  189. flash_status_type flash_bank2_operation_status_get(void)
  190. {
  191. flash_status_type flash_status = FLASH_OPERATE_DONE;
  192. if(FLASH->sts2_bit.obf != RESET)
  193. {
  194. flash_status = FLASH_OPERATE_BUSY;
  195. }
  196. else if(FLASH->sts2_bit.prgmerr != RESET)
  197. {
  198. flash_status = FLASH_PROGRAM_ERROR;
  199. }
  200. else if(FLASH->sts2_bit.epperr != RESET)
  201. {
  202. flash_status = FLASH_EPP_ERROR;
  203. }
  204. else
  205. {
  206. flash_status = FLASH_OPERATE_DONE;
  207. }
  208. /* return the flash status */
  209. return flash_status;
  210. }
  211. /**
  212. * @brief return the flash spim operation status.
  213. * @param none
  214. * @retval status: the returned value can be: FLASH_OPERATE_BUSY,
  215. * FLASH_PROGRAM_ERROR, FLASH_EPP_ERROR or FLASH_OPERATE_DONE.
  216. */
  217. flash_status_type flash_spim_operation_status_get(void)
  218. {
  219. flash_status_type flash_status = FLASH_OPERATE_DONE;
  220. if(FLASH->sts3_bit.obf != RESET)
  221. {
  222. flash_status = FLASH_OPERATE_BUSY;
  223. }
  224. else if(FLASH->sts3_bit.prgmerr != RESET)
  225. {
  226. flash_status = FLASH_PROGRAM_ERROR;
  227. }
  228. else if(FLASH->sts3_bit.epperr != RESET)
  229. {
  230. flash_status = FLASH_EPP_ERROR;
  231. }
  232. else
  233. {
  234. flash_status = FLASH_OPERATE_DONE;
  235. }
  236. /* return the flash status */
  237. return flash_status;
  238. }
  239. /**
  240. * @brief wait for flash operation complete or timeout.
  241. * @param time_out: flash operation timeout
  242. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  243. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  244. */
  245. flash_status_type flash_operation_wait_for(uint32_t time_out)
  246. {
  247. flash_status_type status = FLASH_OPERATE_DONE;
  248. /* check for the flash status */
  249. status = flash_operation_status_get();
  250. while((status == FLASH_OPERATE_BUSY) && (time_out != 0x00))
  251. {
  252. status = flash_operation_status_get();
  253. time_out--;
  254. }
  255. if(time_out == 0x00)
  256. {
  257. status = FLASH_OPERATE_TIMEOUT;
  258. }
  259. /* return the status */
  260. return status;
  261. }
  262. /**
  263. * @brief wait for flash bank1 operation complete or timeout.
  264. * @param time_out: flash operation timeout
  265. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  266. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  267. */
  268. flash_status_type flash_bank1_operation_wait_for(uint32_t time_out)
  269. {
  270. flash_status_type status = FLASH_OPERATE_DONE;
  271. /* check for the flash status */
  272. status = flash_bank1_operation_status_get();
  273. while((status == FLASH_OPERATE_BUSY) && (time_out != 0x00))
  274. {
  275. status = flash_bank1_operation_status_get();
  276. time_out--;
  277. }
  278. if(time_out == 0x00)
  279. {
  280. status = FLASH_OPERATE_TIMEOUT;
  281. }
  282. /* return the operation status */
  283. return status;
  284. }
  285. /**
  286. * @brief wait for flash bank2 operation complete or timeout.
  287. * @param time_out: flash operation timeout
  288. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  289. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  290. */
  291. flash_status_type flash_bank2_operation_wait_for(uint32_t time_out)
  292. {
  293. flash_status_type status = FLASH_OPERATE_DONE;
  294. /* check for the flash status */
  295. status = flash_bank2_operation_status_get();
  296. while((status == FLASH_OPERATE_BUSY) && (time_out != 0x00))
  297. {
  298. status = flash_bank2_operation_status_get();
  299. time_out--;
  300. }
  301. if(time_out == 0x00)
  302. {
  303. status = FLASH_OPERATE_TIMEOUT;
  304. }
  305. /* return the operation status */
  306. return status;
  307. }
  308. /**
  309. * @brief wait for flash spim operation complete or timeout.
  310. * @param time_out: flash operation timeout
  311. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  312. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  313. */
  314. flash_status_type flash_spim_operation_wait_for(uint32_t time_out)
  315. {
  316. flash_status_type status = FLASH_OPERATE_DONE;
  317. /* check for the flash status */
  318. status = flash_spim_operation_status_get();
  319. while((status == FLASH_OPERATE_BUSY) && (time_out != 0x00))
  320. {
  321. status = flash_spim_operation_status_get();
  322. time_out--;
  323. }
  324. if(time_out == 0x00)
  325. {
  326. status = FLASH_OPERATE_TIMEOUT;
  327. }
  328. /* return the operation status */
  329. return status;
  330. }
  331. /**
  332. * @brief unlock the flash controller.
  333. * @param none
  334. * @retval none
  335. */
  336. void flash_unlock(void)
  337. {
  338. FLASH->unlock = FLASH_UNLOCK_KEY1;
  339. FLASH->unlock = FLASH_UNLOCK_KEY2;
  340. FLASH->unlock2 = FLASH_UNLOCK_KEY1;
  341. FLASH->unlock2 = FLASH_UNLOCK_KEY2;
  342. }
  343. /**
  344. * @brief unlock the flash bank1 controller.
  345. * @param none
  346. * @retval none
  347. */
  348. void flash_bank1_unlock(void)
  349. {
  350. FLASH->unlock = FLASH_UNLOCK_KEY1;
  351. FLASH->unlock = FLASH_UNLOCK_KEY2;
  352. }
  353. /**
  354. * @brief unlock the flash bank2 controller.
  355. * @param none
  356. * @retval none
  357. */
  358. void flash_bank2_unlock(void)
  359. {
  360. FLASH->unlock2 = FLASH_UNLOCK_KEY1;
  361. FLASH->unlock2 = FLASH_UNLOCK_KEY2;
  362. }
  363. /**
  364. * @brief unlock the flash spim controller.
  365. * @param none
  366. * @retval none
  367. */
  368. void flash_spim_unlock(void)
  369. {
  370. FLASH->unlock3 = FLASH_UNLOCK_KEY1;
  371. FLASH->unlock3 = FLASH_UNLOCK_KEY2;
  372. }
  373. /**
  374. * @brief lock the flash controller.
  375. * @param none
  376. * @retval none
  377. */
  378. void flash_lock(void)
  379. {
  380. FLASH->ctrl_bit.oplk = TRUE;
  381. FLASH->ctrl2_bit.oplk = TRUE;
  382. }
  383. /**
  384. * @brief lock the flash bank1 controller.
  385. * @param none
  386. * @retval none
  387. */
  388. void flash_bank1_lock(void)
  389. {
  390. FLASH->ctrl_bit.oplk = TRUE;
  391. }
  392. /**
  393. * @brief lock the flash bank2 controller.
  394. * @param none
  395. * @retval none
  396. */
  397. void flash_bank2_lock(void)
  398. {
  399. FLASH->ctrl2_bit.oplk = TRUE;
  400. }
  401. /**
  402. * @brief lock the flash spim controller.
  403. * @param none
  404. * @retval none
  405. */
  406. void flash_spim_lock(void)
  407. {
  408. FLASH->ctrl3_bit.oplk = TRUE;
  409. }
  410. /**
  411. * @brief erase a specified flash sector.
  412. * @param sector_address: the sector address to be erased.
  413. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  414. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  415. */
  416. flash_status_type flash_sector_erase(uint32_t sector_address)
  417. {
  418. flash_status_type status = FLASH_OPERATE_DONE;
  419. if((sector_address >= FLASH_BANK1_START_ADDR) && (sector_address <= FLASH_BANK1_END_ADDR))
  420. {
  421. FLASH->ctrl_bit.secers = TRUE;
  422. FLASH->addr = sector_address;
  423. FLASH->ctrl_bit.erstr = TRUE;
  424. /* wait for operation to be completed */
  425. status = flash_bank1_operation_wait_for(ERASE_TIMEOUT);
  426. /* disable the secers bit */
  427. FLASH->ctrl_bit.secers = FALSE;
  428. }
  429. else if((sector_address >= FLASH_BANK2_START_ADDR) && (sector_address <= FLASH_BANK2_END_ADDR))
  430. {
  431. FLASH->ctrl2_bit.secers = TRUE;
  432. FLASH->addr2 = sector_address;
  433. FLASH->ctrl2_bit.erstr = TRUE;
  434. /* wait for operation to be completed */
  435. status = flash_bank2_operation_wait_for(ERASE_TIMEOUT);
  436. /* disable the secers bit */
  437. FLASH->ctrl2_bit.secers = FALSE;
  438. }
  439. /* spim : external flash */
  440. else if(sector_address >= FLASH_SPIM_START_ADDR)
  441. {
  442. FLASH->ctrl3_bit.secers = TRUE;
  443. FLASH->addr3 = sector_address;
  444. FLASH->ctrl3_bit.erstr = TRUE;
  445. /* wait for operation to be completed */
  446. status = flash_spim_operation_wait_for(SPIM_ERASE_TIMEOUT);
  447. /* disable the secers bit */
  448. FLASH->ctrl3_bit.secers = FALSE;
  449. /* dummy read */
  450. flash_spim_dummy_read();
  451. }
  452. /* return the erase status */
  453. return status;
  454. }
  455. /**
  456. * @brief erase flash all internal sectors.
  457. * @param none
  458. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  459. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  460. */
  461. flash_status_type flash_internal_all_erase(void)
  462. {
  463. flash_status_type status = FLASH_OPERATE_DONE;
  464. FLASH->ctrl_bit.bankers = TRUE;
  465. FLASH->ctrl_bit.erstr = TRUE;
  466. /* wait for operation to be completed */
  467. status = flash_bank1_operation_wait_for(ERASE_TIMEOUT);
  468. /* disable the bankers bit */
  469. FLASH->ctrl_bit.bankers = FALSE;
  470. if(status == FLASH_OPERATE_DONE)
  471. {
  472. /* if the previous operation is completed, continue to erase bank2 */
  473. FLASH->ctrl2_bit.bankers = TRUE;
  474. FLASH->ctrl2_bit.erstr = TRUE;
  475. /* wait for operation to be completed */
  476. status = flash_bank2_operation_wait_for(ERASE_TIMEOUT);
  477. /* disable the bankers bit */
  478. FLASH->ctrl2_bit.bankers = FALSE;
  479. }
  480. /* return the erase status */
  481. return status;
  482. }
  483. /**
  484. * @brief erase flash bank1 sectors.
  485. * @param none
  486. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  487. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  488. */
  489. flash_status_type flash_bank1_erase(void)
  490. {
  491. flash_status_type status = FLASH_OPERATE_DONE;
  492. FLASH->ctrl_bit.bankers = TRUE;
  493. FLASH->ctrl_bit.erstr = TRUE;
  494. /* wait for operation to be completed */
  495. status = flash_bank1_operation_wait_for(ERASE_TIMEOUT);
  496. /* disable the bankers bit */
  497. FLASH->ctrl_bit.bankers = FALSE;
  498. /* return the erase status */
  499. return status;
  500. }
  501. /**
  502. * @brief erase flash bank2 sectors.
  503. * @param none
  504. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  505. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  506. */
  507. flash_status_type flash_bank2_erase(void)
  508. {
  509. flash_status_type status = FLASH_OPERATE_DONE;
  510. FLASH->ctrl2_bit.bankers = TRUE;
  511. FLASH->ctrl2_bit.erstr = TRUE;
  512. /* wait for operation to be completed */
  513. status = flash_bank2_operation_wait_for(ERASE_TIMEOUT);
  514. /* disable the bankers bit */
  515. FLASH->ctrl2_bit.bankers = FALSE;
  516. /* return the erase status */
  517. return status;
  518. }
  519. /**
  520. * @brief erase flash spim sectors.
  521. * @param none
  522. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  523. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  524. */
  525. flash_status_type flash_spim_all_erase(void)
  526. {
  527. flash_status_type status = FLASH_OPERATE_DONE;
  528. FLASH->ctrl3_bit.chpers = TRUE;
  529. FLASH->ctrl3_bit.erstr = TRUE;
  530. /* wait for operation to be completed */
  531. status = flash_spim_operation_wait_for(SPIM_ERASE_TIMEOUT);
  532. /* disable the chpers bit */
  533. FLASH->ctrl3_bit.chpers = FALSE;
  534. /* dummy read */
  535. flash_spim_dummy_read();
  536. /* return the erase status */
  537. return status;
  538. }
  539. /**
  540. * @brief erase the flash user system data.
  541. * @note this functions erases all user system data except the fap byte.
  542. * @param none
  543. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  544. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  545. */
  546. flash_status_type flash_user_system_data_erase(void)
  547. {
  548. flash_status_type status = FLASH_OPERATE_DONE;
  549. uint16_t fap_val = FAP_RELIEVE_KEY;
  550. /* get the flash access protection status */
  551. if(flash_fap_status_get() != RESET)
  552. {
  553. fap_val = 0x0000;
  554. }
  555. /* unlock the user system data */
  556. FLASH->usd_unlock = FLASH_UNLOCK_KEY1;
  557. FLASH->usd_unlock = FLASH_UNLOCK_KEY2;
  558. while(FLASH->ctrl_bit.usdulks==RESET);
  559. /* erase the user system data */
  560. FLASH->ctrl_bit.usders = TRUE;
  561. FLASH->ctrl_bit.erstr = TRUE;
  562. #if 1
  563. /* wait for operation to be completed */
  564. status = flash_operation_wait_for(ERASE_TIMEOUT);
  565. /* disable the usders bit */
  566. FLASH->ctrl_bit.usders = FALSE;
  567. if((status == FLASH_OPERATE_DONE) && (fap_val == FAP_RELIEVE_KEY))
  568. {
  569. /* enable the user system data programming operation */
  570. FLASH->ctrl_bit.usdprgm = TRUE;
  571. /* restore the last flash access protection value */
  572. USD->fap = (uint16_t)fap_val;
  573. /* wait for operation to be completed */
  574. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  575. /*disable the usdprgm bit */
  576. FLASH->ctrl_bit.usdprgm = FALSE;
  577. }
  578. #endif
  579. /* return the erase status */
  580. return status;
  581. }
  582. /**
  583. * @brief program a word at a specified address.
  584. * @param address: specifies the address to be programmed, word alignment is recommended.
  585. * @param data: specifies the data to be programmed.
  586. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  587. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  588. */
  589. flash_status_type flash_word_program(uint32_t address, uint32_t data)
  590. {
  591. flash_status_type status = FLASH_OPERATE_DONE;
  592. if((address >= FLASH_BANK1_START_ADDR) && (address <= FLASH_BANK1_END_ADDR))
  593. {
  594. FLASH->ctrl_bit.fprgm = TRUE;
  595. *(__IO uint32_t*)address = data;
  596. /* wait for operation to be completed */
  597. status = flash_bank1_operation_wait_for(PROGRAMMING_TIMEOUT);
  598. /* disable the fprgm bit */
  599. FLASH->ctrl_bit.fprgm = FALSE;
  600. }
  601. else if((address >= FLASH_BANK2_START_ADDR) && (address <= FLASH_BANK2_END_ADDR))
  602. {
  603. FLASH->ctrl2_bit.fprgm = TRUE;
  604. *(__IO uint32_t*)address = data;
  605. /* wait for operation to be completed */
  606. status = flash_bank2_operation_wait_for(PROGRAMMING_TIMEOUT);
  607. /* disable the fprgm bit */
  608. FLASH->ctrl2_bit.fprgm = FALSE;
  609. }
  610. /* spim : external flash */
  611. else if(address >= FLASH_SPIM_START_ADDR)
  612. {
  613. FLASH->ctrl3_bit.fprgm = TRUE;
  614. *(__IO uint32_t*)address = data;
  615. /* wait for operation to be completed */
  616. status = flash_spim_operation_wait_for(SPIM_PROGRAMMING_TIMEOUT);
  617. /* disable the fprgm bit */
  618. FLASH->ctrl3_bit.fprgm = FALSE;
  619. /* dummy read */
  620. flash_spim_dummy_read();
  621. }
  622. /* return the program status */
  623. return status;
  624. }
  625. /**
  626. * @brief program a halfword at a specified address.
  627. * @param address: specifies the address to be programmed, halfword alignment is recommended.
  628. * @param data: specifies the data to be programmed.
  629. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  630. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  631. */
  632. flash_status_type flash_halfword_program(uint32_t address, uint16_t data)
  633. {
  634. flash_status_type status = FLASH_OPERATE_DONE;
  635. if((address >= FLASH_BANK1_START_ADDR) && (address <= FLASH_BANK1_END_ADDR))
  636. {
  637. FLASH->ctrl_bit.fprgm = TRUE;
  638. *(__IO uint16_t*)address = data;
  639. /* wait for operation to be completed */
  640. status = flash_bank1_operation_wait_for(PROGRAMMING_TIMEOUT);
  641. /* disable the fprgm bit */
  642. FLASH->ctrl_bit.fprgm = FALSE;
  643. }
  644. else if((address >= FLASH_BANK2_START_ADDR) && (address <= FLASH_BANK2_END_ADDR))
  645. {
  646. FLASH->ctrl2_bit.fprgm = TRUE;
  647. *(__IO uint16_t*)address = data;
  648. /* wait for operation to be completed */
  649. status = flash_bank2_operation_wait_for(PROGRAMMING_TIMEOUT);
  650. /* disable the fprgm bit */
  651. FLASH->ctrl2_bit.fprgm = FALSE;
  652. }
  653. /* spim : external flash */
  654. else if(address >= FLASH_SPIM_START_ADDR)
  655. {
  656. FLASH->ctrl3_bit.fprgm = TRUE;
  657. *(__IO uint16_t*)address = data;
  658. /* wait for operation to be completed */
  659. status = flash_spim_operation_wait_for(SPIM_PROGRAMMING_TIMEOUT);
  660. /* disable the fprgm bit */
  661. FLASH->ctrl3_bit.fprgm = FALSE;
  662. /* dummy read */
  663. flash_spim_dummy_read();
  664. }
  665. /* return the program status */
  666. return status;
  667. }
  668. /**
  669. * @brief program a byte at a specified address.
  670. * @note this function cannot be used to program spim.
  671. * @param address: specifies the address to be programmed.
  672. * @param data: specifies the data to be programmed.
  673. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  674. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  675. */
  676. flash_status_type flash_byte_program(uint32_t address, uint8_t data)
  677. {
  678. flash_status_type status = FLASH_OPERATE_DONE;
  679. if((address >= FLASH_BANK1_START_ADDR) && (address <= FLASH_BANK1_END_ADDR))
  680. {
  681. FLASH->ctrl_bit.fprgm = TRUE;
  682. *(__IO uint8_t*)address = data;
  683. /* wait for operation to be completed */
  684. status = flash_bank1_operation_wait_for(PROGRAMMING_TIMEOUT);
  685. /* disable the fprgm bit */
  686. FLASH->ctrl_bit.fprgm = FALSE;
  687. }
  688. else if((address >= FLASH_BANK2_START_ADDR) && (address <= FLASH_BANK2_END_ADDR))
  689. {
  690. FLASH->ctrl2_bit.fprgm = TRUE;
  691. *(__IO uint8_t*)address = data;
  692. /* wait for operation to be completed */
  693. status = flash_bank2_operation_wait_for(PROGRAMMING_TIMEOUT);
  694. /* disable the fprgm bit */
  695. FLASH->ctrl2_bit.fprgm = FALSE;
  696. }
  697. /* return the program status */
  698. return status;
  699. }
  700. /**
  701. * @brief program a halfword at a specified user system data address.
  702. * @param address: specifies the address to be programmed.
  703. * @param data: specifies the data to be programmed.
  704. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  705. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  706. */
  707. flash_status_type flash_user_system_data_program(uint32_t address, uint8_t data)
  708. {
  709. flash_status_type status = FLASH_OPERATE_DONE;
  710. if(address == USD_BASE)
  711. {
  712. if(data != 0xA5)
  713. return FLASH_OPERATE_DONE;
  714. }
  715. /* unlock the user system data */
  716. FLASH->usd_unlock = FLASH_UNLOCK_KEY1;
  717. FLASH->usd_unlock = FLASH_UNLOCK_KEY2;
  718. while(FLASH->ctrl_bit.usdulks==RESET);
  719. /* enable the user system data programming operation */
  720. FLASH->ctrl_bit.usdprgm = TRUE;
  721. *(__IO uint16_t*)address = data;
  722. /* wait for operation to be completed */
  723. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  724. /* disable the usdprgm bit */
  725. FLASH->ctrl_bit.usdprgm = FALSE;
  726. /* return the user system data program status */
  727. return status;
  728. }
  729. /**
  730. * @brief config erase/program protection for the desired sectors.
  731. * @param sector_bits:
  732. * the pointer of the address of the sectors to be erase/program protected.
  733. * general every bit is used to protect the 4KB bytes, and the last one bit
  734. * is used to protect the rest.
  735. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  736. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  737. */
  738. flash_status_type flash_epp_set(uint32_t *sector_bits)
  739. {
  740. uint16_t epp_data[4] = {0xFFFF,0xFFFF,0xFFFF,0xFFFF};
  741. flash_status_type status = FLASH_OPERATE_DONE;
  742. sector_bits[0] = (uint32_t)(~sector_bits[0]);
  743. epp_data[0] = (uint16_t)((sector_bits[0] >> 0) & 0xFF);
  744. epp_data[1] = (uint16_t)((sector_bits[0] >> 8) & 0xFF);
  745. epp_data[2] = (uint16_t)((sector_bits[0] >> 16) & 0xFF);
  746. epp_data[3] = (uint16_t)((sector_bits[0] >> 24) & 0xFF);
  747. /* unlock the user system data */
  748. FLASH->usd_unlock = FLASH_UNLOCK_KEY1;
  749. FLASH->usd_unlock = FLASH_UNLOCK_KEY2;
  750. while(FLASH->ctrl_bit.usdulks==RESET);
  751. FLASH->ctrl_bit.usdprgm = TRUE;
  752. USD->epp0 = epp_data[0];
  753. /* wait for operation to be completed */
  754. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  755. if(status == FLASH_OPERATE_DONE)
  756. {
  757. USD->epp1 = epp_data[1];
  758. /* wait for operation to be completed */
  759. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  760. }
  761. if(status == FLASH_OPERATE_DONE)
  762. {
  763. USD->epp2 = epp_data[2];
  764. /* wait for operation to be completed */
  765. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  766. }
  767. if(status == FLASH_OPERATE_DONE)
  768. {
  769. USD->epp3 = epp_data[3];
  770. /* wait for operation to be completed */
  771. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  772. }
  773. /* disable the usdprgm bit */
  774. FLASH->ctrl_bit.usdprgm = FALSE;
  775. /* return the erase/program protection operation status */
  776. return status;
  777. }
  778. /**
  779. * @brief return the flash erase/program protection status.
  780. * @param sector_bits: pointer to get the epps register.
  781. * @retval none
  782. */
  783. void flash_epp_status_get(uint32_t *sector_bits)
  784. {
  785. /* return the flash erase/program protection register value */
  786. sector_bits[0] = (uint32_t)(FLASH->epps);
  787. }
  788. /**
  789. * @brief enable or disable the flash access protection.
  790. * @note if the user has already programmed the other user system data before calling
  791. * this function, must re-program them since this function erase all user system data.
  792. * @param new_state: new state of the flash access protection.
  793. * this parameter can be: TRUE or FALSE.
  794. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  795. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  796. */
  797. flash_status_type flash_fap_enable(confirm_state new_state)
  798. {
  799. flash_status_type status = FLASH_OPERATE_DONE;
  800. /* unlock the user system data */
  801. FLASH->usd_unlock = FLASH_UNLOCK_KEY1;
  802. FLASH->usd_unlock = FLASH_UNLOCK_KEY2;
  803. while(FLASH->ctrl_bit.usdulks==RESET);
  804. FLASH->ctrl_bit.usders = TRUE;
  805. FLASH->ctrl_bit.erstr = TRUE;
  806. /* wait for operation to be completed */
  807. status = flash_operation_wait_for(ERASE_TIMEOUT);
  808. /* disable the usders bit */
  809. FLASH->ctrl_bit.usders = FALSE;
  810. if(status == FLASH_OPERATE_DONE)
  811. {
  812. if(new_state == FALSE)
  813. {
  814. /* enable the user system data programming operation */
  815. FLASH->ctrl_bit.usdprgm = TRUE;
  816. USD->fap = FAP_RELIEVE_KEY;
  817. /* wait for operation to be completed */
  818. status = flash_operation_wait_for(ERASE_TIMEOUT);
  819. /* disable the usdprgm bit */
  820. FLASH->ctrl_bit.usdprgm = FALSE;
  821. }
  822. }
  823. /* return the flash access protection operation status */
  824. return status;
  825. }
  826. /**
  827. * @brief check the flash access protection status.
  828. * @param none
  829. * @retval flash access protection status(SET or RESET)
  830. */
  831. flag_status flash_fap_status_get(void)
  832. {
  833. return (flag_status)FLASH->usd_bit.fap;
  834. }
  835. /**
  836. * @brief program the flash system setting byte in usd: wdt_ato_en / depslp_rst / stdby_rst / btopt.
  837. * @param usd_ssb: the system setting byte
  838. * @note this parameter usd_ssb must contain a combination of all the following 4 types of data
  839. * type 1: wdt_ato_en, select the wdt auto start
  840. * this data can be one of the following values:
  841. * - USD_WDT_ATO_DISABLE: disable wdt auto start
  842. * - USD_WDT_ATO_ENABLE: enable wdt auto start
  843. * type 2: depslp_rst, reset event when entering deepsleep mode.
  844. * this data can be one of the following values:
  845. * - USD_DEPSLP_NO_RST: no reset generated when entering in deepsleep
  846. * - USD_DEPSLP_RST: reset generated when entering in deepsleep
  847. * type 3: stdby_rst, reset event when entering standby mode.
  848. * this data can be one of the following values:
  849. * - USD_STDBY_NO_RST: no reset generated when entering in standby
  850. * - USD_STDBY_RST: reset generated when entering in standby
  851. * type 4: btopt, at startup,if boot pins are set in boot from user flash position,selected the device boot from bank1/bank2.
  852. * this data can be one of the following values:
  853. * - FLASH_BOOT_FROM_BANK1:boot from bank1
  854. * - FLASH_BOOT_FROM_BANK2:boot from bank 2 or bank 1
  855. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  856. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  857. */
  858. flash_status_type flash_ssb_set(uint8_t usd_ssb)
  859. {
  860. flash_status_type status = FLASH_OPERATE_DONE;
  861. /* unlock the user system data */
  862. FLASH->usd_unlock = FLASH_UNLOCK_KEY1;
  863. FLASH->usd_unlock = FLASH_UNLOCK_KEY2;
  864. while(FLASH->ctrl_bit.usdulks==RESET);
  865. /* enable the user system data programming operation */
  866. FLASH->ctrl_bit.usdprgm = TRUE;
  867. USD->ssb = usd_ssb;
  868. /* wait for operation to be completed */
  869. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  870. /* disable the usdprgm bit */
  871. FLASH->ctrl_bit.usdprgm = FALSE;
  872. /* return the user system data program status */
  873. return status;
  874. }
  875. /**
  876. * @brief return the flash system setting byte status.
  877. * @param none
  878. * @retval values from flash_usd register: wdt_ato_en(bit0), depslp_rst(bit1),
  879. * stdby_rst(bit2) and btopt(bit3).
  880. */
  881. uint8_t flash_ssb_status_get(void)
  882. {
  883. /* return the system setting byte status */
  884. return (uint8_t)(FLASH->usd >> 2);
  885. }
  886. /**
  887. * @brief enable or disable the specified flash interrupts.
  888. * @param flash_int: specifies the flash interrupt sources to be enabled or disabled.
  889. * this parameter can be any combination of the following values:
  890. * - FLASH_ERR_INT
  891. * - FLASH_ODF_INT
  892. * - FLASH_BANK1_ERR_INT
  893. * - FLASH_BANK1_ODF_INT
  894. * - FLASH_BANK2_ERR_INT
  895. * - FLASH_BANK2_ODF_INT
  896. * - FLASH_SPIM_ERR_INT
  897. * - FLASH_SPIM_ODF_INT
  898. * @param new_state: new state of the specified flash interrupts.
  899. * this parameter can be: TRUE or FALSE.
  900. * @retval none
  901. */
  902. void flash_interrupt_enable(uint32_t flash_int, confirm_state new_state)
  903. {
  904. if(flash_int & FLASH_BANK1_ERR_INT)
  905. FLASH->ctrl_bit.errie = new_state;
  906. if(flash_int & FLASH_BANK1_ODF_INT)
  907. FLASH->ctrl_bit.odfie = new_state;
  908. if(flash_int & FLASH_BANK2_ERR_INT)
  909. FLASH->ctrl2_bit.errie = new_state;
  910. if(flash_int & FLASH_BANK2_ODF_INT)
  911. FLASH->ctrl2_bit.odfie = new_state;
  912. if(flash_int & FLASH_SPIM_ERR_INT)
  913. FLASH->ctrl3_bit.errie = new_state;
  914. if(flash_int & FLASH_SPIM_ODF_INT)
  915. FLASH->ctrl3_bit.odfie = new_state;
  916. }
  917. /**
  918. * @brief select spim supports extended spi flash chip model.
  919. * @param mode: the extended spi flash model
  920. * @retval none
  921. */
  922. void flash_spim_model_select(flash_spim_model_type mode)
  923. {
  924. FLASH->select = mode;
  925. /* dummy read */
  926. flash_spim_dummy_read();
  927. }
  928. /**
  929. * @brief set the range of encryption in spim flash.
  930. * when the address is larger than this value, the writing data will be
  931. * directly written to spim without encryption.
  932. * @param decode_address: the end address of encrypted data in spim
  933. * @retval none
  934. */
  935. void flash_spim_encryption_range_set(uint32_t decode_address)
  936. {
  937. FLASH->da = decode_address;
  938. }
  939. /**
  940. * @brief operate the flash spim dummy read.
  941. * @param none
  942. * @retval none
  943. */
  944. void flash_spim_dummy_read(void)
  945. {
  946. UNUSED(*(__IO uint32_t*)FLASH_SPIM_START_ADDR);
  947. UNUSED(*(__IO uint32_t*)(FLASH_SPIM_START_ADDR + 0x1000));
  948. UNUSED(*(__IO uint32_t*)(FLASH_SPIM_START_ADDR + 0x2000));
  949. }
  950. /**
  951. * @brief mass program for flash spim.
  952. * @param address: specifies the start address to be programmed, word or halfword alignment is recommended.
  953. * @param buf: specifies the pointer of data to be programmed.
  954. * @param cnt: specifies the data counter to be programmed.
  955. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  956. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  957. */
  958. flash_status_type flash_spim_mass_program(uint32_t address, uint8_t *buf, uint32_t cnt)
  959. {
  960. flash_status_type status = FLASH_OPERATE_DONE;
  961. uint32_t index, temp_offset;
  962. if(address >= FLASH_SPIM_START_ADDR)
  963. {
  964. temp_offset = cnt % 4;
  965. if((temp_offset != 0) && (temp_offset != 2))
  966. return status;
  967. FLASH->ctrl3_bit.fprgm = TRUE;
  968. for(index = 0; index < cnt / 4; index++)
  969. {
  970. *(__IO uint32_t*)(address + index * 4) = *(uint32_t*)(buf + index * 4);
  971. /* wait for operation to be completed */
  972. status = flash_spim_operation_wait_for(SPIM_PROGRAMMING_TIMEOUT);
  973. if(status != FLASH_OPERATE_DONE)
  974. return status;
  975. }
  976. if(temp_offset == 2)
  977. {
  978. *(__IO uint16_t*)(address + index * 4) = *(uint16_t*)(buf + index * 4);
  979. /* wait for operation to be completed */
  980. status = flash_spim_operation_wait_for(SPIM_PROGRAMMING_TIMEOUT);
  981. }
  982. /* disable the fprgm bit */
  983. FLASH->ctrl3_bit.fprgm = FALSE;
  984. /* dummy read */
  985. flash_spim_dummy_read();
  986. }
  987. /* return the program status */
  988. return status;
  989. }
  990. /**
  991. * @brief enable security library function.
  992. * @param pwd: slib password
  993. * start_sector: security library start sector
  994. * data_start_sector: security library d-bus area start sector
  995. * end_sector: security library end sector
  996. * @retval status: the returned value can be: FLASH_PROGRAM_ERROR,
  997. * FLASH_EPP_ERROR, FLASH_OPERATE_DONE or FLASH_OPERATE_TIMEOUT.
  998. */
  999. flash_status_type flash_slib_enable(uint32_t pwd, uint16_t start_sector, uint16_t data_start_sector, uint16_t end_sector)
  1000. {
  1001. uint32_t slib_range;
  1002. flash_status_type status = FLASH_OPERATE_DONE;
  1003. /*check range param limits*/
  1004. if((start_sector>=data_start_sector) || ((data_start_sector > end_sector) && \
  1005. (data_start_sector != 0x7FF)) || (start_sector > end_sector))
  1006. return FLASH_PROGRAM_ERROR;
  1007. /* unlock slib cfg register */
  1008. FLASH->slib_unlock = SLIB_UNLOCK_KEY;
  1009. while(FLASH->slib_misc_sts_bit.slib_ulkf==RESET);
  1010. slib_range = ((uint32_t)(data_start_sector << 11) & FLASH_SLIB_DATA_START_SECTOR) | \
  1011. ((uint32_t)(end_sector << 22) & FLASH_SLIB_END_SECTOR) | \
  1012. (start_sector & FLASH_SLIB_START_SECTOR);
  1013. /* configure slib, set pwd and range */
  1014. FLASH->slib_set_pwd = pwd;
  1015. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  1016. FLASH->slib_set_range = slib_range;
  1017. if(status == FLASH_OPERATE_DONE)
  1018. {
  1019. status = flash_operation_wait_for(PROGRAMMING_TIMEOUT);
  1020. }
  1021. return status;
  1022. }
  1023. /**
  1024. * @brief disable slib when slib enabled.
  1025. * @param pwd: slib password
  1026. * @retval success or error
  1027. */
  1028. error_status flash_slib_disable(uint32_t pwd)
  1029. {
  1030. flash_status_type status = FLASH_OPERATE_DONE;
  1031. /* write password to disable slib */
  1032. FLASH->slib_pwd_clr = pwd;
  1033. status = flash_operation_wait_for(ERASE_TIMEOUT);
  1034. if(status == FLASH_OPERATE_DONE)
  1035. {
  1036. if(FLASH->slib_misc_sts_bit.slib_pwd_ok)
  1037. return SUCCESS;
  1038. else
  1039. return ERROR;
  1040. }
  1041. return ERROR;
  1042. }
  1043. /**
  1044. * @brief get remaining count of slib(range: 256~0).
  1045. * @param none
  1046. * @retval uint32_t
  1047. */
  1048. uint32_t flash_slib_remaining_count_get(void)
  1049. {
  1050. return (uint32_t)FLASH->slib_misc_sts_bit.slib_rcnt;
  1051. }
  1052. /**
  1053. * @brief get the slib state.
  1054. * @param none
  1055. * @retval SET or RESET
  1056. */
  1057. flag_status flash_slib_state_get(void)
  1058. {
  1059. if(FLASH->slib_sts0_bit.slib_enf)
  1060. return SET;
  1061. else
  1062. return RESET;
  1063. }
  1064. /**
  1065. * @brief get the start sector of slib.
  1066. * @param none
  1067. * @retval uint16_t
  1068. */
  1069. uint16_t flash_slib_start_sector_get(void)
  1070. {
  1071. return (uint16_t)FLASH->slib_sts1_bit.slib_ss;
  1072. }
  1073. /**
  1074. * @brief get the data start sector of slib.
  1075. * @param none
  1076. * @retval uint16_t
  1077. */
  1078. uint16_t flash_slib_datastart_sector_get(void)
  1079. {
  1080. return (uint16_t)FLASH->slib_sts1_bit.slib_dat_ss;
  1081. }
  1082. /**
  1083. * @brief get the end sector of slib.
  1084. * @param none
  1085. * @retval uint16_t
  1086. */
  1087. uint16_t flash_slib_end_sector_get(void)
  1088. {
  1089. return (uint16_t)FLASH->slib_sts1_bit.slib_es;
  1090. }
  1091. /**
  1092. * @brief flash crc calibration in main block.
  1093. * @param start_sector: crc calibration start sector number
  1094. * sector_cnt: crc calibration sector count
  1095. * @retval uint32: crc calibration result
  1096. */
  1097. uint32_t flash_crc_calibrate(uint32_t start_sector, uint32_t sector_cnt)
  1098. {
  1099. FLASH->crc_ctrl_bit.crc_ss = start_sector;
  1100. FLASH->crc_ctrl_bit.crc_sn = sector_cnt;
  1101. FLASH->crc_ctrl_bit.crc_strt = TRUE;
  1102. flash_operation_wait_for(OPERATION_TIMEOUT);
  1103. return FLASH->crc_chkr;
  1104. }
  1105. /**
  1106. * @}
  1107. */
  1108. #endif
  1109. /**
  1110. * @}
  1111. */
  1112. /**
  1113. * @}
  1114. */