at32f423_flash.h 16 KB

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