at32f413_flash.h 18 KB

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