at32f425_flash.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /**
  2. **************************************************************************
  3. * @file at32f425_flash.h
  4. * @brief at32f425 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 __AT32F425_FLASH_H
  26. #define __AT32F425_FLASH_H
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. /* Includes ------------------------------------------------------------------*/
  31. #include "at32f425.h"
  32. /** @addtogroup AT32F425_periph_driver
  33. * @{
  34. */
  35. /** @addtogroup FLASH
  36. * @{
  37. */
  38. /** @defgroup FLASH_keys
  39. * @brief flash 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 FAP_HIGH_LEVEL_KEY ((uint16_t)0x00CC) /*!< flash fap high level enable key val */
  46. #define SLIB_UNLOCK_KEY ((uint32_t)0xA35F6D24) /*!< flash slib operation unlock order key */
  47. /**
  48. * @}
  49. */
  50. /** @defgroup FLASH_flags
  51. * @brief flash flag
  52. * @{
  53. */
  54. #define FLASH_OBF_FLAG ((uint32_t)0x00000001) /*!< flash operate busy flag */
  55. #define FLASH_ODF_FLAG ((uint32_t)0x00000020) /*!< flash operate done flag */
  56. #define FLASH_PRGMERR_FLAG ((uint32_t)0x00000004) /*!< flash program error flag */
  57. #define FLASH_EPPERR_FLAG ((uint32_t)0x00000010) /*!< flash erase/program protection error flag */
  58. #define FLASH_USDERR_FLAG ((uint32_t)0x40000001) /*!< flash user system data error flag */
  59. /**
  60. * @}
  61. */
  62. /** @defgroup FLASH_interrupts
  63. * @brief flash interrupts
  64. * @{
  65. */
  66. #define FLASH_ERR_INT ((uint32_t)0x00000001) /*!< flash error interrupt */
  67. #define FLASH_ODF_INT ((uint32_t)0x00000002) /*!< flash operate done interrupt */
  68. /**
  69. * @}
  70. */
  71. /** @defgroup FLASH_slib_mask
  72. * @brief flash slib mask
  73. * @{
  74. */
  75. #define FLASH_SLIB_START_SECTOR ((uint32_t)0x000007FF) /*!< flash slib start sector */
  76. #define FLASH_SLIB_INST_START_SECTOR ((uint32_t)0x003FF800) /*!< flash slib i-bus area start sector */
  77. #define FLASH_SLIB_END_SECTOR ((uint32_t)0xFFC00000) /*!< flash slib end sector */
  78. /**
  79. * @}
  80. */
  81. /** @defgroup FLASH_user_system_data
  82. * @brief flash user system data
  83. * @{
  84. */
  85. #define USD_WDT_ATO_DISABLE ((uint16_t)0x0001) /*!< wdt auto start disabled */
  86. #define USD_WDT_ATO_ENABLE ((uint16_t)0x0000) /*!< wdt auto start enabled */
  87. #define USD_DEPSLP_NO_RST ((uint16_t)0x0002) /*!< no reset generated when entering in deepsleep */
  88. #define USD_DEPSLP_RST ((uint16_t)0x0000) /*!< reset generated when entering in deepsleep */
  89. #define USD_STDBY_NO_RST ((uint16_t)0x0004) /*!< no reset generated when entering in standby */
  90. #define USD_STDBY_RST ((uint16_t)0x0000) /*!< reset generated when entering in standby */
  91. #define USD_BOOT1_LOW ((uint16_t)0x0010) /*!< when boot0 is high level, boot from bootmem */
  92. #define USD_BOOT1_HIGH ((uint16_t)0x0000) /*!< when boot0 is high level, boot from sram */
  93. #define USD_DEPSLP_WDT_CONTINUE ((uint16_t)0x0020) /*!< wdt continue count when entering in deepsleep */
  94. #define USD_DEPSLP_WDT_STOP ((uint16_t)0x0000) /*!< wdt stop count when entering in deepsleep */
  95. #define USD_STDBY_WDT_CONTINUE ((uint16_t)0x0040) /*!< wdt continue count when entering in standby */
  96. #define USD_STDBY_WDT_STOP ((uint16_t)0x0000) /*!< wdt stop count when entering in standby */
  97. /**
  98. * @}
  99. */
  100. /** @defgroup FLASH_timeout_definition
  101. * @brief flash timeout definition
  102. * @{
  103. */
  104. #define ERASE_TIMEOUT ((uint32_t)0x40000000) /*!< internal flash erase operation timeout */
  105. #define PROGRAMMING_TIMEOUT ((uint32_t)0x00100000) /*!< internal flash program operation timeout */
  106. #define OPERATION_TIMEOUT ((uint32_t)0x10000000) /*!< flash common operation timeout */
  107. /**
  108. * @}
  109. */
  110. /**
  111. * @brief set the flash psr register
  112. * @param wtcyc: the flash wait cycle.
  113. * this parameter can be one of the following values:
  114. * - FLASH_WAIT_CYCLE_0
  115. * - FLASH_WAIT_CYCLE_1
  116. * - FLASH_WAIT_CYCLE_2
  117. * - FLASH_WAIT_CYCLE_3
  118. */
  119. #define flash_psr_set(wtcyc) (FLASH->psr = (uint32_t)(0x150 | wtcyc))
  120. /** @defgroup FLASH_exported_types
  121. * @{
  122. */
  123. /**
  124. * @brief flash status type
  125. */
  126. typedef enum
  127. {
  128. FLASH_OPERATE_BUSY = 0x00, /*!< flash status is operate busy */
  129. FLASH_PROGRAM_ERROR = 0x01, /*!< flash status is program error */
  130. FLASH_EPP_ERROR = 0x02, /*!< flash status is epp error */
  131. FLASH_OPERATE_DONE = 0x03, /*!< flash status is operate done */
  132. FLASH_OPERATE_TIMEOUT = 0x04 /*!< flash status is operate timeout */
  133. } flash_status_type;
  134. /**
  135. * @brief flash wait cycle type
  136. */
  137. typedef enum
  138. {
  139. FLASH_WAIT_CYCLE_0 = 0x00, /*!< sysclk 1~32mhz */
  140. FLASH_WAIT_CYCLE_1 = 0x01, /*!< sysclk 33~64mhz */
  141. FLASH_WAIT_CYCLE_2 = 0x02, /*!< sysclk 65~96mhz */
  142. FLASH_WAIT_CYCLE_3 = 0x03 /*!< sysclk 97~120mhz */
  143. } flash_wait_cycle_type;
  144. /**
  145. * @brief type define flash register all
  146. */
  147. typedef struct
  148. {
  149. /**
  150. * @brief flash psr register, offset:0x00
  151. */
  152. union
  153. {
  154. __IO uint32_t psr;
  155. struct
  156. {
  157. __IO uint32_t wtcyc : 3; /* [2:0] */
  158. __IO uint32_t hfcyc_en : 1; /* [3] */
  159. __IO uint32_t pft_en : 1; /* [4] */
  160. __IO uint32_t pft_enf : 1; /* [5] */
  161. __IO uint32_t pft_en2 : 1; /* [6] */
  162. __IO uint32_t pft_enf2 : 1; /* [7] */
  163. __IO uint32_t pft_lat_dis : 1; /* [8] */
  164. __IO uint32_t reserved1 : 23;/* [31:9] */
  165. } psr_bit;
  166. };
  167. /**
  168. * @brief flash unlock register, offset:0x04
  169. */
  170. union
  171. {
  172. __IO uint32_t unlock;
  173. struct
  174. {
  175. __IO uint32_t ukval : 32;/* [31:0] */
  176. } unlock_bit;
  177. };
  178. /**
  179. * @brief flash usd unlock register, offset:0x08
  180. */
  181. union
  182. {
  183. __IO uint32_t usd_unlock;
  184. struct
  185. {
  186. __IO uint32_t usd_ukval : 32;/* [31:0] */
  187. } usd_unlock_bit;
  188. };
  189. /**
  190. * @brief flash sts register, offset:0x0C
  191. */
  192. union
  193. {
  194. __IO uint32_t sts;
  195. struct
  196. {
  197. __IO uint32_t obf : 1; /* [0] */
  198. __IO uint32_t reserved1 : 1; /* [1] */
  199. __IO uint32_t prgmerr : 1; /* [2] */
  200. __IO uint32_t reserved2 : 1; /* [3] */
  201. __IO uint32_t epperr : 1; /* [4] */
  202. __IO uint32_t odf : 1; /* [5] */
  203. __IO uint32_t reserved3 : 26;/* [31:6] */
  204. } sts_bit;
  205. };
  206. /**
  207. * @brief flash ctrl register, offset:0x10
  208. */
  209. union
  210. {
  211. __IO uint32_t ctrl;
  212. struct
  213. {
  214. __IO uint32_t fprgm : 1; /* [0] */
  215. __IO uint32_t secers : 1; /* [1] */
  216. __IO uint32_t bankers : 1; /* [2] */
  217. __IO uint32_t reserved1 : 1; /* [3] */
  218. __IO uint32_t usdprgm : 1; /* [4] */
  219. __IO uint32_t usders : 1; /* [5] */
  220. __IO uint32_t erstr : 1; /* [6] */
  221. __IO uint32_t oplk : 1; /* [7] */
  222. __IO uint32_t reserved2 : 1; /* [8] */
  223. __IO uint32_t usdulks : 1; /* [9] */
  224. __IO uint32_t errie : 1; /* [10] */
  225. __IO uint32_t reserved3 : 1; /* [11] */
  226. __IO uint32_t odfie : 1; /* [12] */
  227. __IO uint32_t reserved4 : 4; /* [16:13] */
  228. __IO uint32_t lpmen : 1; /* [17] */
  229. __IO uint32_t reserved5 : 14;/* [31:18] */
  230. } ctrl_bit;
  231. };
  232. /**
  233. * @brief flash addr register, offset:0x14
  234. */
  235. union
  236. {
  237. __IO uint32_t addr;
  238. struct
  239. {
  240. __IO uint32_t fa : 32;/* [31:0] */
  241. } addr_bit;
  242. };
  243. /**
  244. * @brief flash reserved1 register, offset:0x18
  245. */
  246. __IO uint32_t reserved1;
  247. /**
  248. * @brief flash usd register, offset:0x1C
  249. */
  250. union
  251. {
  252. __IO uint32_t usd;
  253. struct
  254. {
  255. __IO uint32_t usderr : 1; /* [0] */
  256. __IO uint32_t fap : 1; /* [1] */
  257. __IO uint32_t wdt_ato_en : 1; /* [2] */
  258. __IO uint32_t depslp_rst : 1; /* [3] */
  259. __IO uint32_t stdby_rst : 1; /* [4] */
  260. __IO uint32_t reserved1 : 1; /* [5] */
  261. __IO uint32_t boot1 : 1; /* [6] */
  262. __IO uint32_t depslp_wdt : 1; /* [7] */
  263. __IO uint32_t stdby_wdt : 1; /* [8] */
  264. __IO uint32_t reserved2 : 1; /* [9] */
  265. __IO uint32_t user_d0 : 8; /* [17:10] */
  266. __IO uint32_t user_d1 : 8; /* [25:18] */
  267. __IO uint32_t fap_hl : 1; /* [26] */
  268. __IO uint32_t reserved3 : 5; /* [31:27] */
  269. } usd_bit;
  270. };
  271. /**
  272. * @brief flash epps register, offset:0x20
  273. */
  274. union
  275. {
  276. __IO uint32_t epps;
  277. struct
  278. {
  279. __IO uint32_t epps : 32;/* [31:0] */
  280. } epps_bit;
  281. };
  282. /**
  283. * @brief flash reserved2 register, offset:0x70~0x24
  284. */
  285. __IO uint32_t reserved2[20];
  286. /**
  287. * @brief flash slib_sts0 register, offset:0x74
  288. */
  289. union
  290. {
  291. __IO uint32_t slib_sts0;
  292. struct
  293. {
  294. __IO uint32_t btm_ap_enf : 1; /* [0] */
  295. __IO uint32_t reserved1 : 1; /* [1] */
  296. __IO uint32_t em_slib_enf : 1; /* [2] */
  297. __IO uint32_t slib_enf : 1; /* [3] */
  298. __IO uint32_t reserved2 : 12;/* [15:4] */
  299. __IO uint32_t em_slib_inst_ss : 8; /* [23:16] */
  300. __IO uint32_t reserved3 : 8; /* [31:24] */
  301. } slib_sts0_bit;
  302. };
  303. /**
  304. * @brief flash slib_sts1 register, offset:0x78
  305. */
  306. union
  307. {
  308. __IO uint32_t slib_sts1;
  309. struct
  310. {
  311. __IO uint32_t slib_ss : 11;/* [10:0] */
  312. __IO uint32_t slib_inst_ss : 11;/* [21:11] */
  313. __IO uint32_t slib_es : 10;/* [31:22] */
  314. } slib_sts1_bit;
  315. };
  316. /**
  317. * @brief flash slib_pwd_clr register, offset:0x7C
  318. */
  319. union
  320. {
  321. __IO uint32_t slib_pwd_clr;
  322. struct
  323. {
  324. __IO uint32_t slib_pclr_val : 32;/* [31:0] */
  325. } slib_pwd_clr_bit;
  326. };
  327. /**
  328. * @brief flash slib_misc_sts register, offset:0x80
  329. */
  330. union
  331. {
  332. __IO uint32_t slib_misc_sts;
  333. struct
  334. {
  335. __IO uint32_t slib_pwd_err : 1; /* [0] */
  336. __IO uint32_t slib_pwd_ok : 1; /* [1] */
  337. __IO uint32_t slib_ulkf : 1; /* [2] */
  338. __IO uint32_t reserved1 : 29;/* [31:3] */
  339. } slib_misc_sts_bit;
  340. };
  341. /**
  342. * @brief flash crc_addr register, offset:0x84
  343. */
  344. union
  345. {
  346. __IO uint32_t crc_addr;
  347. struct
  348. {
  349. __IO uint32_t crc_addr : 32;/* [31:0] */
  350. } crc_addr_bit;
  351. };
  352. /**
  353. * @brief flash crc_ctrl register, offset:0x88
  354. */
  355. union
  356. {
  357. __IO uint32_t crc_ctrl;
  358. struct
  359. {
  360. __IO uint32_t crc_sn : 16;/* [15:0] */
  361. __IO uint32_t crc_strt : 1; /* [16] */
  362. __IO uint32_t reserved1 : 15;/* [31:17] */
  363. } crc_ctrl_bit;
  364. };
  365. /**
  366. * @brief flash crc_chkr register, offset:0x8C
  367. */
  368. union
  369. {
  370. __IO uint32_t crc_chkr;
  371. struct
  372. {
  373. __IO uint32_t crc_chkr : 32;/* [31:0] */
  374. } crc_chkr_bit;
  375. };
  376. /**
  377. * @brief flash reserved3 register, offset:0x15C~0x90
  378. */
  379. __IO uint32_t reserved3[52];
  380. /**
  381. * @brief flash slib_set_pwd register, offset:0x160
  382. */
  383. union
  384. {
  385. __IO uint32_t slib_set_pwd;
  386. struct
  387. {
  388. __IO uint32_t slib_pset_val : 32;/* [31:0] */
  389. } slib_set_pwd_bit;
  390. };
  391. /**
  392. * @brief flash slib_set_range register, offset:0x164
  393. */
  394. union
  395. {
  396. __IO uint32_t slib_set_range;
  397. struct
  398. {
  399. __IO uint32_t slib_ss_set : 11;/* [10:0] */
  400. __IO uint32_t slib_iss_set : 11;/* [21:11] */
  401. __IO uint32_t slib_es_set : 10;/* [31:22] */
  402. } slib_set_range_bit;
  403. };
  404. /**
  405. * @brief flash em_slib_set register, offset:0x168
  406. */
  407. union
  408. {
  409. __IO uint32_t em_slib_set;
  410. struct
  411. {
  412. __IO uint32_t em_slib_set : 16;/* [15:0] */
  413. __IO uint32_t em_slib_iss_set : 8; /* [23:16] */
  414. __IO uint32_t reserved1 : 8; /* [31:24] */
  415. } em_slib_set_bit;
  416. };
  417. /**
  418. * @brief flash btm_mode_set register, offset:0x16C
  419. */
  420. union
  421. {
  422. __IO uint32_t btm_mode_set;
  423. struct
  424. {
  425. __IO uint32_t btm_mode_set : 8; /* [7:0] */
  426. __IO uint32_t reserved1 : 24;/* [31:8] */
  427. } btm_mode_set_bit;
  428. };
  429. /**
  430. * @brief flash slib_unlock register, offset:0x170
  431. */
  432. union
  433. {
  434. __IO uint32_t slib_unlock;
  435. struct
  436. {
  437. __IO uint32_t slib_ukval : 32;/* [31:0] */
  438. } slib_unlock_bit;
  439. };
  440. } flash_type;
  441. /**
  442. * @brief user system data
  443. */
  444. typedef struct
  445. {
  446. __IO uint16_t fap;
  447. __IO uint16_t ssb;
  448. __IO uint16_t data0;
  449. __IO uint16_t data1;
  450. __IO uint16_t epp0;
  451. __IO uint16_t epp1;
  452. __IO uint16_t epp2;
  453. __IO uint16_t epp3;
  454. } usd_type;
  455. /**
  456. * @}
  457. */
  458. #define FLASH ((flash_type *) FLASH_REG_BASE)
  459. #define USD ((usd_type *) USD_BASE)
  460. /** @defgroup FLASH_exported_functions
  461. * @{
  462. */
  463. flag_status flash_flag_get(uint32_t flash_flag);
  464. void flash_flag_clear(uint32_t flash_flag);
  465. flash_status_type flash_operation_status_get(void);
  466. flash_status_type flash_operation_wait_for(uint32_t time_out);
  467. void flash_unlock(void);
  468. void flash_lock(void);
  469. flash_status_type flash_sector_erase(uint32_t sector_address);
  470. flash_status_type flash_internal_all_erase(void);
  471. flash_status_type flash_user_system_data_erase(void);
  472. flash_status_type flash_word_program(uint32_t address, uint32_t data);
  473. flash_status_type flash_halfword_program(uint32_t address, uint16_t data);
  474. flash_status_type flash_byte_program(uint32_t address, uint8_t data);
  475. flash_status_type flash_user_system_data_program(uint32_t address, uint8_t data);
  476. flash_status_type flash_epp_set(uint32_t *sector_bits);
  477. void flash_epp_status_get(uint32_t *sector_bits);
  478. flash_status_type flash_fap_enable(confirm_state new_state);
  479. flag_status flash_fap_status_get(void);
  480. flash_status_type flash_fap_high_level_enable(void);
  481. flag_status flash_fap_high_level_status_get(void);
  482. flash_status_type flash_ssb_set(uint8_t usd_ssb);
  483. uint8_t flash_ssb_status_get(void);
  484. void flash_interrupt_enable(uint32_t flash_int, confirm_state new_state);
  485. flash_status_type flash_slib_enable(uint32_t pwd, uint16_t start_sector, uint16_t inst_start_sector, uint16_t end_sector);
  486. error_status flash_slib_disable(uint32_t pwd);
  487. flag_status flash_slib_state_get(void);
  488. uint16_t flash_slib_start_sector_get(void);
  489. uint16_t flash_slib_inststart_sector_get(void);
  490. uint16_t flash_slib_end_sector_get(void);
  491. uint32_t flash_crc_calibrate(uint32_t start_addr, uint32_t sector_cnt);
  492. void flash_boot_memory_extension_mode_enable(void);
  493. flash_status_type flash_extension_memory_slib_enable(uint32_t pwd, uint16_t inst_start_sector);
  494. flag_status flash_extension_memory_slib_state_get(void);
  495. uint16_t flash_em_slib_inststart_sector_get(void);
  496. void flash_low_power_mode_enable(confirm_state new_state);
  497. /**
  498. * @}
  499. */
  500. /**
  501. * @}
  502. */
  503. /**
  504. * @}
  505. */
  506. #ifdef __cplusplus
  507. }
  508. #endif
  509. #endif