at32f415_flash.h 16 KB

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