at32f421_flash.h 16 KB

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