at32f413_flash.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /**
  2. **************************************************************************
  3. * @file at32f413_flash.h
  4. * @version v2.0.5
  5. * @date 2022-05-20
  6. * @brief at32f413 flash header file
  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. /* Define to prevent recursive inclusion -------------------------------------*/
  27. #ifndef __AT32F413_FLASH_H
  28. #define __AT32F413_FLASH_H
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /* Includes ------------------------------------------------------------------*/
  33. #include "at32f413.h"
  34. /** @addtogroup AT32F413_periph_driver
  35. * @{
  36. */
  37. /** @addtogroup FLASH
  38. * @{
  39. */
  40. /** @defgroup FLASH_unlock_keys
  41. * @brief flash unlock keys
  42. * @{
  43. */
  44. #define FLASH_UNLOCK_KEY1 ((uint32_t)0x45670123) /*!< flash operation unlock order key1 */
  45. #define FLASH_UNLOCK_KEY2 ((uint32_t)0xCDEF89AB) /*!< flash operation unlock order key2 */
  46. #define FAP_RELIEVE_KEY ((uint16_t)0x00A5) /*!< flash fap relieve key val */
  47. #define SLIB_UNLOCK_KEY ((uint32_t)0xA35F6D24) /*!< flash slib operation unlock order key */
  48. /**
  49. * @}
  50. */
  51. /** @defgroup FLASH_spim_address
  52. * @brief flash spim address
  53. * @{
  54. */
  55. #define FLASH_SPIM_START_ADDR ((uint32_t)0x08400000) /*!< flash start address of spim */
  56. /**
  57. * @}
  58. */
  59. /** @defgroup FLASH_flags
  60. * @brief flash flag
  61. * @{
  62. */
  63. #define FLASH_OBF_FLAG ((uint32_t)0x00000001) /*!< flash bank1 operate busy flag */
  64. #define FLASH_ODF_FLAG ((uint32_t)0x00000020) /*!< flash bank1 operate done flag */
  65. #define FLASH_PRGMERR_FLAG ((uint32_t)0x00000004) /*!< flash bank1 program error flag */
  66. #define FLASH_EPPERR_FLAG ((uint32_t)0x00000010) /*!< flash bank1 erase/program protection error flag */
  67. #define FLASH_SPIM_OBF_FLAG ((uint32_t)0x20000001) /*!< flash spim operate busy flag */
  68. #define FLASH_SPIM_ODF_FLAG ((uint32_t)0x20000020) /*!< flash spim operate done flag */
  69. #define FLASH_SPIM_PRGMERR_FLAG ((uint32_t)0x20000004) /*!< flash spim program error flag */
  70. #define FLASH_SPIM_EPPERR_FLAG ((uint32_t)0x20000010) /*!< flash spim erase/program protection error flag */
  71. #define FLASH_USDERR_FLAG ((uint32_t)0x40000001) /*!< flash user system data error flag */
  72. /**
  73. * @}
  74. */
  75. /** @defgroup FLASH_interrupts
  76. * @brief flash interrupts
  77. * @{
  78. */
  79. #define FLASH_ERR_INT ((uint32_t)0x00000001) /*!< flash bank1 error interrupt */
  80. #define FLASH_ODF_INT ((uint32_t)0x00000002) /*!< flash bank1 operate done interrupt */
  81. #define FLASH_SPIM_ERR_INT ((uint32_t)0x00000010) /*!< flash spim error interrupt */
  82. #define FLASH_SPIM_ODF_INT ((uint32_t)0x00000020) /*!< flash spim operate done interrupt */
  83. /**
  84. * @}
  85. */
  86. /** @defgroup FLASH_slib_mask
  87. * @brief flash slib mask
  88. * @{
  89. */
  90. #define FLASH_SLIB_START_SECTOR ((uint32_t)0x000007FF) /*!< flash slib start sector */
  91. #define FLASH_SLIB_DATA_START_SECTOR ((uint32_t)0x003FF800) /*!< flash slib d-bus area start sector */
  92. #define FLASH_SLIB_END_SECTOR ((uint32_t)0xFFC00000) /*!< flash slib end sector */
  93. /**
  94. * @}
  95. */
  96. /** @defgroup FLASH_user_system_data
  97. * @brief flash user system data
  98. * @{
  99. */
  100. #define USD_WDT_ATO_DISABLE ((uint16_t)0x0001) /*!< wdt auto start disabled */
  101. #define USD_WDT_ATO_ENABLE ((uint16_t)0x0000) /*!< wdt auto start enabled */
  102. #define USD_DEPSLP_NO_RST ((uint16_t)0x0002) /*!< no reset generated when entering in deepsleep */
  103. #define USD_DEPSLP_RST ((uint16_t)0x0000) /*!< reset generated when entering in deepsleep */
  104. #define USD_STDBY_NO_RST ((uint16_t)0x0004) /*!< no reset generated when entering in standby */
  105. #define USD_STDBY_RST ((uint16_t)0x0000) /*!< reset generated when entering in standby */
  106. /**
  107. * @}
  108. */
  109. /** @defgroup FLASH_timeout_definition
  110. * @brief flash timeout definition
  111. * @{
  112. */
  113. #define ERASE_TIMEOUT ((uint32_t)0x40000000) /*!< internal flash erase operation timeout */
  114. #define PROGRAMMING_TIMEOUT ((uint32_t)0x00100000) /*!< internal flash program operation timeout */
  115. #define SPIM_ERASE_TIMEOUT ((uint32_t)0xFFFFFFFF) /*!< spim erase operation timeout */
  116. #define SPIM_PROGRAMMING_TIMEOUT ((uint32_t)0x00100000) /*!< spim program operation timeout */
  117. #define OPERATION_TIMEOUT ((uint32_t)0x10000000) /*!< flash common operation timeout */
  118. /**
  119. * @}
  120. */
  121. /** @defgroup FLASH_exported_types
  122. * @{
  123. */
  124. /**
  125. * @brief flash status type
  126. */
  127. typedef enum
  128. {
  129. FLASH_OPERATE_BUSY = 0x00, /*!< flash status is operate busy */
  130. FLASH_PROGRAM_ERROR = 0x01, /*!< flash status is program error */
  131. FLASH_EPP_ERROR = 0x02, /*!< flash status is epp error */
  132. FLASH_OPERATE_DONE = 0x03, /*!< flash status is operate done */
  133. FLASH_OPERATE_TIMEOUT = 0x04 /*!< flash status is operate timeout */
  134. } flash_status_type;
  135. /**
  136. * @brief flash spim model type
  137. */
  138. typedef enum
  139. {
  140. FLASH_SPIM_MODEL1 = 0x01, /*!< spim model 1 */
  141. FLASH_SPIM_MODEL2 = 0x02, /*!< spim model 2 */
  142. } flash_spim_model_type;
  143. /**
  144. * @brief type define flash register all
  145. */
  146. typedef struct
  147. {
  148. /**
  149. * @brief flash psr register, offset:0x00
  150. */
  151. union
  152. {
  153. __IO uint32_t psr;
  154. struct
  155. {
  156. __IO uint32_t reserved1 : 32; /* [31:0] */
  157. } psr_bit;
  158. };
  159. /**
  160. * @brief flash unlock register, offset:0x04
  161. */
  162. union
  163. {
  164. __IO uint32_t unlock;
  165. struct
  166. {
  167. __IO uint32_t ukval : 32;/* [31:0] */
  168. } unlock_bit;
  169. };
  170. /**
  171. * @brief flash usd unlock register, offset:0x08
  172. */
  173. union
  174. {
  175. __IO uint32_t usd_unlock;
  176. struct
  177. {
  178. __IO uint32_t usd_ukval : 32;/* [31:0] */
  179. } usd_unlock_bit;
  180. };
  181. /**
  182. * @brief flash sts register, offset:0x0C
  183. */
  184. union
  185. {
  186. __IO uint32_t sts;
  187. struct
  188. {
  189. __IO uint32_t obf : 1; /* [0] */
  190. __IO uint32_t reserved1 : 1; /* [1] */
  191. __IO uint32_t prgmerr : 1; /* [2] */
  192. __IO uint32_t reserved2 : 1; /* [3] */
  193. __IO uint32_t epperr : 1; /* [4] */
  194. __IO uint32_t odf : 1; /* [5] */
  195. __IO uint32_t reserved3 : 26;/* [31:6] */
  196. } sts_bit;
  197. };
  198. /**
  199. * @brief flash ctrl register, offset:0x10
  200. */
  201. union
  202. {
  203. __IO uint32_t ctrl;
  204. struct
  205. {
  206. __IO uint32_t fprgm : 1; /* [0] */
  207. __IO uint32_t secers : 1; /* [1] */
  208. __IO uint32_t bankers : 1; /* [2] */
  209. __IO uint32_t reserved1 : 1; /* [3] */
  210. __IO uint32_t usdprgm : 1; /* [4] */
  211. __IO uint32_t usders : 1; /* [5] */
  212. __IO uint32_t erstr : 1; /* [6] */
  213. __IO uint32_t oplk : 1; /* [7] */
  214. __IO uint32_t reserved2 : 1; /* [8] */
  215. __IO uint32_t usdulks : 1; /* [9] */
  216. __IO uint32_t errie : 1; /* [10] */
  217. __IO uint32_t reserved3 : 1; /* [11] */
  218. __IO uint32_t odfie : 1; /* [12] */
  219. __IO uint32_t reserved4 : 19;/* [31:13] */
  220. } ctrl_bit;
  221. };
  222. /**
  223. * @brief flash addr register, offset:0x14
  224. */
  225. union
  226. {
  227. __IO uint32_t addr;
  228. struct
  229. {
  230. __IO uint32_t fa : 32;/* [31:0] */
  231. } addr_bit;
  232. };
  233. /**
  234. * @brief flash reserved1 register, offset:0x18
  235. */
  236. __IO uint32_t reserved1;
  237. /**
  238. * @brief flash usd register, offset:0x1C
  239. */
  240. union
  241. {
  242. __IO uint32_t usd;
  243. struct
  244. {
  245. __IO uint32_t usderr : 1; /* [0] */
  246. __IO uint32_t fap : 1; /* [1] */
  247. __IO uint32_t wdt_ato_en : 1; /* [2] */
  248. __IO uint32_t depslp_rst : 1; /* [3] */
  249. __IO uint32_t stdby_rst : 1; /* [4] */
  250. __IO uint32_t reserved1 : 5; /* [9:5] */
  251. __IO uint32_t user_d0 : 8; /* [17:10] */
  252. __IO uint32_t user_d1 : 8; /* [25:18] */
  253. __IO uint32_t reserved2 : 6; /* [31:26] */
  254. } usd_bit;
  255. };
  256. /**
  257. * @brief flash epps register, offset:0x20
  258. */
  259. union
  260. {
  261. __IO uint32_t epps;
  262. struct
  263. {
  264. __IO uint32_t epps : 32;/* [31:0] */
  265. } epps_bit;
  266. };
  267. /**
  268. * @brief flash reserved2 register, offset:0x80~0x24
  269. */
  270. __IO uint32_t reserved2[24];
  271. /**
  272. * @brief flash unlock3 register, offset:0x84
  273. */
  274. union
  275. {
  276. __IO uint32_t unlock3;
  277. struct
  278. {
  279. __IO uint32_t ukval : 32;/* [31:0] */
  280. } unlock3_bit;
  281. };
  282. /**
  283. * @brief flash select register, offset:0x88
  284. */
  285. union
  286. {
  287. __IO uint32_t select;
  288. struct
  289. {
  290. __IO uint32_t select : 32;/* [31:0] */
  291. } select_bit;
  292. };
  293. /**
  294. * @brief flash sts3 register, offset:0x8C
  295. */
  296. union
  297. {
  298. __IO uint32_t sts3;
  299. struct
  300. {
  301. __IO uint32_t obf : 1; /* [0] */
  302. __IO uint32_t reserved1 : 1; /* [1] */
  303. __IO uint32_t prgmerr : 1; /* [2] */
  304. __IO uint32_t reserved2 : 1; /* [3] */
  305. __IO uint32_t epperr : 1; /* [4] */
  306. __IO uint32_t odf : 1; /* [5] */
  307. __IO uint32_t reserved3 : 26;/* [31:6] */
  308. } sts3_bit;
  309. };
  310. /**
  311. * @brief flash ctrl3 register, offset:0x90
  312. */
  313. union
  314. {
  315. __IO uint32_t ctrl3;
  316. struct
  317. {
  318. __IO uint32_t fprgm : 1; /* [0] */
  319. __IO uint32_t secers : 1; /* [1] */
  320. __IO uint32_t chpers : 1; /* [2] */
  321. __IO uint32_t reserved1 : 3; /* [5:3] */
  322. __IO uint32_t erstr : 1; /* [6] */
  323. __IO uint32_t oplk : 1; /* [7] */
  324. __IO uint32_t reserved2 : 2; /* [9:8] */
  325. __IO uint32_t errie : 1; /* [10] */
  326. __IO uint32_t reserved3 : 1; /* [11] */
  327. __IO uint32_t odfie : 1; /* [12] */
  328. __IO uint32_t reserved4 : 19;/* [31:13] */
  329. } ctrl3_bit;
  330. };
  331. /**
  332. * @brief flash addr3 register, offset:0x94
  333. */
  334. union
  335. {
  336. __IO uint32_t addr3;
  337. struct
  338. {
  339. __IO uint32_t fa : 32;/* [31:0] */
  340. } addr3_bit;
  341. };
  342. /**
  343. * @brief flash da register, offset:0x98
  344. */
  345. union
  346. {
  347. __IO uint32_t da;
  348. struct
  349. {
  350. __IO uint32_t fda : 32;/* [31:0] */
  351. } da_bit;
  352. };
  353. /**
  354. * @brief flash reserved5 register, offset:0xC8~0x9C
  355. */
  356. __IO uint32_t reserved5[12];
  357. /**
  358. * @brief flash slib_sts0 register, offset:0xCC
  359. */
  360. union
  361. {
  362. __IO uint32_t slib_sts0;
  363. struct
  364. {
  365. __IO uint32_t reserved1 : 3; /* [2:0] */
  366. __IO uint32_t slib_enf : 1; /* [3] */
  367. __IO uint32_t reserved2 : 28;/* [31:4] */
  368. } slib_sts0_bit;
  369. };
  370. /**
  371. * @brief flash slib_sts1 register, offset:0xD0
  372. */
  373. union
  374. {
  375. __IO uint32_t slib_sts1;
  376. struct
  377. {
  378. __IO uint32_t slib_ss : 11;/* [10:0] */
  379. __IO uint32_t slib_dat_ss : 11;/* [21:11] */
  380. __IO uint32_t slib_es : 10;/* [31:22] */
  381. } slib_sts1_bit;
  382. };
  383. /**
  384. * @brief flash slib_pwd_clr register, offset:0xD4
  385. */
  386. union
  387. {
  388. __IO uint32_t slib_pwd_clr;
  389. struct
  390. {
  391. __IO uint32_t slib_pclr_val : 32;/* [31:0] */
  392. } slib_pwd_clr_bit;
  393. };
  394. /**
  395. * @brief flash slib_misc_sts register, offset:0xD8
  396. */
  397. union
  398. {
  399. __IO uint32_t slib_misc_sts;
  400. struct
  401. {
  402. __IO uint32_t slib_pwd_err : 1; /* [0] */
  403. __IO uint32_t slib_pwd_ok : 1; /* [1] */
  404. __IO uint32_t slib_ulkf : 1; /* [2] */
  405. __IO uint32_t reserved1 : 13;/* [15:3] */
  406. __IO uint32_t slib_rcnt : 9; /* [24:16] */
  407. __IO uint32_t reserved2 : 7; /* [31:25] */
  408. } slib_misc_sts_bit;
  409. };
  410. /**
  411. * @brief flash slib_set_pwd register, offset:0xDC
  412. */
  413. union
  414. {
  415. __IO uint32_t slib_set_pwd;
  416. struct
  417. {
  418. __IO uint32_t slib_pset_val : 32;/* [31:0] */
  419. } slib_set_pwd_bit;
  420. };
  421. /**
  422. * @brief flash slib_set_range register, offset:0xE0
  423. */
  424. union
  425. {
  426. __IO uint32_t slib_set_range;
  427. struct
  428. {
  429. __IO uint32_t slib_ss_set : 11;/* [10:0] */
  430. __IO uint32_t slib_dss_set : 11;/* [21:11] */
  431. __IO uint32_t slib_es_set : 10;/* [31:22] */
  432. } slib_set_range_bit;
  433. };
  434. /**
  435. * @brief flash reserved6 register, offset:0xEC~0xE4
  436. */
  437. __IO uint32_t reserved6[3];
  438. /**
  439. * @brief flash slib_unlock register, offset:0xF0
  440. */
  441. union
  442. {
  443. __IO uint32_t slib_unlock;
  444. struct
  445. {
  446. __IO uint32_t slib_ukval : 32;/* [31:0] */
  447. } slib_unlock_bit;
  448. };
  449. /**
  450. * @brief flash crc_ctrl register, offset:0xF4
  451. */
  452. union
  453. {
  454. __IO uint32_t crc_ctrl;
  455. struct
  456. {
  457. __IO uint32_t crc_ss : 12;/* [11:0] */
  458. __IO uint32_t crc_sn : 12;/* [23:12] */
  459. __IO uint32_t reserved1 : 7; /* [30:24] */
  460. __IO uint32_t crc_strt : 1; /* [31] */
  461. } crc_ctrl_bit;
  462. };
  463. /**
  464. * @brief flash crc_chkr register, offset:0xF8
  465. */
  466. union
  467. {
  468. __IO uint32_t crc_chkr;
  469. struct
  470. {
  471. __IO uint32_t crc_chkr : 32;/* [31:0] */
  472. } crc_chkr_bit;
  473. };
  474. } flash_type;
  475. /**
  476. * @brief user system data
  477. */
  478. typedef struct
  479. {
  480. __IO uint16_t fap;
  481. __IO uint16_t ssb;
  482. __IO uint16_t data0;
  483. __IO uint16_t data1;
  484. __IO uint16_t epp0;
  485. __IO uint16_t epp1;
  486. __IO uint16_t epp2;
  487. __IO uint16_t epp3;
  488. __IO uint16_t eopb0;
  489. __IO uint16_t reserved;
  490. __IO uint16_t data2;
  491. __IO uint16_t data3;
  492. __IO uint16_t data4;
  493. __IO uint16_t data5;
  494. __IO uint16_t data6;
  495. __IO uint16_t data7;
  496. __IO uint16_t ext_flash_key[8];
  497. } usd_type;
  498. /**
  499. * @}
  500. */
  501. #define FLASH ((flash_type *) FLASH_REG_BASE)
  502. #define USD ((usd_type *) USD_BASE)
  503. /** @defgroup FLASH_exported_functions
  504. * @{
  505. */
  506. flag_status flash_flag_get(uint32_t flash_flag);
  507. void flash_flag_clear(uint32_t flash_flag);
  508. flash_status_type flash_operation_status_get(void);
  509. flash_status_type flash_spim_operation_status_get(void);
  510. flash_status_type flash_operation_wait_for(uint32_t time_out);
  511. flash_status_type flash_spim_operation_wait_for(uint32_t time_out);
  512. void flash_unlock(void);
  513. void flash_spim_unlock(void);
  514. void flash_lock(void);
  515. void flash_spim_lock(void);
  516. flash_status_type flash_sector_erase(uint32_t sector_address);
  517. flash_status_type flash_internal_all_erase(void);
  518. flash_status_type flash_spim_all_erase(void);
  519. flash_status_type flash_user_system_data_erase(void);
  520. flash_status_type flash_word_program(uint32_t address, uint32_t data);
  521. flash_status_type flash_halfword_program(uint32_t address, uint16_t data);
  522. flash_status_type flash_byte_program(uint32_t address, uint8_t data);
  523. flash_status_type flash_user_system_data_program(uint32_t address, uint8_t data);
  524. flash_status_type flash_epp_set(uint32_t *sector_bits);
  525. void flash_epp_status_get(uint32_t *sector_bits);
  526. flash_status_type flash_fap_enable(confirm_state new_state);
  527. flag_status flash_fap_status_get(void);
  528. flash_status_type flash_ssb_set(uint8_t usd_ssb);
  529. uint8_t flash_ssb_status_get(void);
  530. void flash_interrupt_enable(uint32_t flash_int, confirm_state new_state);
  531. void flash_spim_model_select(flash_spim_model_type mode);
  532. void flash_spim_encryption_range_set(uint32_t decode_address);
  533. flash_status_type flash_slib_enable(uint32_t pwd, uint16_t start_sector, uint16_t data_start_sector, uint16_t end_sector);
  534. error_status flash_slib_disable(uint32_t pwd);
  535. uint32_t flash_slib_remaining_count_get(void);
  536. flag_status flash_slib_state_get(void);
  537. uint16_t flash_slib_start_sector_get(void);
  538. uint16_t flash_slib_datstart_sector_get(void);
  539. uint16_t flash_slib_end_sector_get(void);
  540. uint32_t flash_crc_calibrate(uint32_t start_sector, uint32_t sector_cnt);
  541. /**
  542. * @}
  543. */
  544. /**
  545. * @}
  546. */
  547. /**
  548. * @}
  549. */
  550. #ifdef __cplusplus
  551. }
  552. #endif
  553. #endif