at32f415_flash.h 16 KB

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