hc32_ll_efm.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /**
  2. *******************************************************************************
  3. * @file hc32_ll_efm.h
  4. * @brief This file contains all the functions prototypes of the EFM driver
  5. * library.
  6. @verbatim
  7. Change Logs:
  8. Date Author Notes
  9. 2022-12-31 CDT First version
  10. @endverbatim
  11. *******************************************************************************
  12. * Copyright (C) 2022, Xiaohua Semiconductor Co., Ltd. All rights reserved.
  13. *
  14. * This software component is licensed by XHSC under BSD 3-Clause license
  15. * (the "License"); You may not use this file except in compliance with the
  16. * License. You may obtain a copy of the License at:
  17. * opensource.org/licenses/BSD-3-Clause
  18. *
  19. *******************************************************************************
  20. */
  21. #ifndef __HC32_LL_EFM_H__
  22. #define __HC32_LL_EFM_H__
  23. /* C binding of definitions if building with C++ compiler */
  24. #ifdef __cplusplus
  25. extern "C"
  26. {
  27. #endif
  28. /*******************************************************************************
  29. * Include files
  30. ******************************************************************************/
  31. #include "hc32_ll_def.h"
  32. #include "hc32f4xx.h"
  33. #include "hc32f4xx_conf.h"
  34. /**
  35. * @addtogroup LL_Driver
  36. * @{
  37. */
  38. /**
  39. * @addtogroup LL_EFM
  40. * @{
  41. */
  42. #if (LL_EFM_ENABLE == DDL_ON)
  43. /*******************************************************************************
  44. * Global type definitions ('typedef')
  45. ******************************************************************************/
  46. /**
  47. * @defgroup EFM_Global_Types EFM Global Types
  48. * @{
  49. */
  50. /**
  51. * @brief EFM unique ID definition
  52. */
  53. typedef struct {
  54. uint32_t u32UniqueID0; /*!< unique ID 0. */
  55. uint32_t u32UniqueID1; /*!< unique ID 1. */
  56. uint32_t u32UniqueID2; /*!< unique ID 2. */
  57. } stc_efm_unique_id_t;
  58. typedef struct {
  59. uint32_t u32State;
  60. uint32_t u32Addr;
  61. uint32_t u32Size;
  62. } stc_efm_remap_init_t;
  63. /**
  64. * @}
  65. */
  66. /*******************************************************************************
  67. * Global pre-processor symbols/macros ('#define')
  68. ******************************************************************************/
  69. /**
  70. * @defgroup EFM_Global_Macros EFM Global Macros
  71. * @{
  72. */
  73. /**
  74. * @defgroup EFM_Address EFM Address Area
  75. * @{
  76. */
  77. #define EFM_START_ADDR (0x00000000UL) /*!< Flash start address */
  78. #define EFM_END_ADDR (0x0003FFFFUL) /*!< Flash end address */
  79. #define EFM_OTP_START_ADDR1 (0x00000000UL) /*!< OTP start address1 */
  80. #define EFM_OTP_END_ADDR1 (0x00001FFFUL) /*!< OTP end address1 */
  81. #define EFM_OTP_START_ADDR (0x03000C00UL) /*!< OTP start address */
  82. #define EFM_OTP_END_ADDR (0x03000FFFUL) /*!< OTP end address */
  83. #define EFM_OTP_LOCK_ADDR_START (0x03000A80UL) /*!< OTP lock address */
  84. #define EFM_OTP_LOCK_ADDR_START1 (0x03000AC0UL) /*!< OTP lock address 1 */
  85. #define EFM_OTP_LOCK_ADDR_END (0x03000AFFUL) /*!< OTP lock address */
  86. #define EFM_OTP_ENABLE_ADDR (0x03000A00UL)
  87. /**
  88. * @}
  89. */
  90. /**
  91. * @defgroup EFM_Chip_Sel EFM Chip Selection
  92. * @{
  93. */
  94. #define EFM_CHIP_ALL (EFM_FSTP_FSTP)
  95. /**
  96. * @}
  97. */
  98. /**
  99. * @defgroup EFM_Bus_Status EFM Bus Status
  100. * @{
  101. */
  102. #define EFM_BUS_HOLD (0x0UL) /*!< Bus busy while flash program or erase */
  103. #define EFM_BUS_RELEASE (0x1UL) /*!< Bus release while flash program or erase */
  104. /**
  105. * @}
  106. */
  107. /**
  108. * @defgroup EFM_Wait_Cycle EFM Wait Cycle
  109. * @{
  110. */
  111. #define EFM_WAIT_CYCLE0 (0U << EFM_FRMC_FLWT_POS) /*!< Don't insert read wait cycle */
  112. #define EFM_WAIT_CYCLE1 (1U << EFM_FRMC_FLWT_POS) /*!< Insert 1 read wait cycle */
  113. #define EFM_WAIT_CYCLE2 (2U << EFM_FRMC_FLWT_POS) /*!< Insert 2 read wait cycles */
  114. #define EFM_WAIT_CYCLE3 (3U << EFM_FRMC_FLWT_POS) /*!< Insert 3 read wait cycles */
  115. #define EFM_WAIT_CYCLE4 (4U << EFM_FRMC_FLWT_POS) /*!< Insert 4 read wait cycles */
  116. #define EFM_WAIT_CYCLE5 (5U << EFM_FRMC_FLWT_POS) /*!< Insert 5 read wait cycles */
  117. #define EFM_WAIT_CYCLE6 (6U << EFM_FRMC_FLWT_POS) /*!< Insert 6 read wait cycles */
  118. #define EFM_WAIT_CYCLE7 (7U << EFM_FRMC_FLWT_POS) /*!< Insert 7 read wait cycles */
  119. #define EFM_WAIT_CYCLE8 (8U << EFM_FRMC_FLWT_POS) /*!< Insert 8 read wait cycles */
  120. #define EFM_WAIT_CYCLE9 (9U << EFM_FRMC_FLWT_POS) /*!< Insert 9 read wait cycles */
  121. #define EFM_WAIT_CYCLE10 (10U << EFM_FRMC_FLWT_POS) /*!< Insert 10 read wait cycles */
  122. #define EFM_WAIT_CYCLE11 (11U << EFM_FRMC_FLWT_POS) /*!< Insert 11 read wait cycles */
  123. #define EFM_WAIT_CYCLE12 (12U << EFM_FRMC_FLWT_POS) /*!< Insert 12 read wait cycles */
  124. #define EFM_WAIT_CYCLE13 (13U << EFM_FRMC_FLWT_POS) /*!< Insert 13 read wait cycles */
  125. #define EFM_WAIT_CYCLE14 (14U << EFM_FRMC_FLWT_POS) /*!< Insert 14 read wait cycles */
  126. #define EFM_WAIT_CYCLE15 (15U << EFM_FRMC_FLWT_POS) /*!< Insert 15 read wait cycles */
  127. /**
  128. * @}
  129. */
  130. /**
  131. * @defgroup EFM_Swap_Address EFM Swap Address
  132. * @{
  133. */
  134. #define EFM_SWAP_ADDR (0x03002000UL)
  135. #define EFM_SWAP_DATA (0x005A5A5AUL)
  136. /**
  137. * @}
  138. */
  139. /**
  140. * @defgroup EFM_OperateMode_Sel EFM Operate Mode Selection
  141. * @{
  142. */
  143. #define EFM_MD_READONLY (0x0UL << EFM_FWMC_PEMOD_POS) /*!< Read only mode */
  144. #define EFM_MD_PGM_SINGLE (0x1UL << EFM_FWMC_PEMOD_POS) /*!< Program single mode */
  145. #define EFM_MD_PGM_READBACK (0x2UL << EFM_FWMC_PEMOD_POS) /*!< Program and read back mode */
  146. #define EFM_MD_PGM_SEQ (0x3UL << EFM_FWMC_PEMOD_POS) /*!< Program sequence mode */
  147. #define EFM_MD_ERASE_SECTOR (0x4UL << EFM_FWMC_PEMOD_POS) /*!< Sector erase mode */
  148. #define EFM_MD_ERASE_ALL_CHIP (0x5UL << EFM_FWMC_PEMOD_POS) /*!< Chip erase mode */
  149. /**
  150. * @}
  151. */
  152. /**
  153. * @defgroup EFM_Flag_Sel EFM Flag Selection
  154. * @{
  155. */
  156. #define EFM_FLAG_OTPWERR (EFM_FSR_OTPWERR) /*!< EFM Flash otp Programming/erase error flag. */
  157. #define EFM_FLAG_PEPRTERR (EFM_FSR_PRTWERR) /*!< EFM Flash write protect address error flag. */
  158. #define EFM_FLAG_PGSZERR (EFM_FSR_PGSZERR) /*!< EFM Flash programming size error flag. */
  159. #define EFM_FLAG_PGMISMTCH (EFM_FSR_MISMTCH) /*!< EFM Flash programming missing match error flag. */
  160. #define EFM_FLAG_OPTEND (EFM_FSR_OPTEND) /*!< EFM Flash end of operation flag. */
  161. #define EFM_FLAG_COLERR (EFM_FSR_COLERR) /*!< EFM Flash read collide error flag. */
  162. #define EFM_FLAG_RDY (EFM_FSR_RDY) /*!< EFM Flash ready flag. */
  163. #define EFM_FLAG_ALL (EFM_FLAG_OTPWERR | EFM_FLAG_PEPRTERR | EFM_FLAG_PGSZERR | EFM_FLAG_PGMISMTCH | \
  164. EFM_FLAG_OPTEND | EFM_FLAG_COLERR | EFM_FLAG_RDY)
  165. /**
  166. * @}
  167. */
  168. /**
  169. * @defgroup EFM_Interrupt_Sel EFM Interrupt Selection
  170. * @{
  171. */
  172. #define EFM_INT_PEERR (EFM_FITE_PEERRITE) /*!< Program/erase error Interrupt source */
  173. #define EFM_INT_OPTEND (EFM_FITE_OPTENDITE) /*!< End of EFM operation Interrupt source */
  174. #define EFM_INT_COLERR (EFM_FITE_COLERRITE) /*!< Read collide error Interrupt source */
  175. #define EFM_INT_ALL (EFM_FITE_PEERRITE | EFM_FITE_OPTENDITE | EFM_FITE_COLERRITE)
  176. /**
  177. * @}
  178. */
  179. /**
  180. * @defgroup EFM_Keys EFM Keys
  181. * @{
  182. */
  183. #define EFM_REG_UNLOCK_KEY1 (0x0123UL)
  184. #define EFM_REG_UNLOCK_KEY2 (0x3210UL)
  185. #define EFM_REG_LOCK_KEY (0x0000UL)
  186. /**
  187. * @}
  188. */
  189. /**
  190. * @defgroup EFM_Sector_Size EFM Sector Size
  191. * @{
  192. */
  193. #define SECTOR_SIZE (0x2000UL)
  194. /**
  195. * @}
  196. */
  197. /**
  198. * @defgroup EFM_Sector_Address EFM Sector Address
  199. * @{
  200. */
  201. #define EFM_SECTOR_ADDR(x) (uint32_t)(SECTOR_SIZE * (x))
  202. /**
  203. * @}
  204. */
  205. /**
  206. * @defgroup EFM_OTP_Base_Address EFM Otp Base Address
  207. * @{
  208. */
  209. #define EFM_OTP_BASE1_ADDR (0x00000000UL)
  210. #define EFM_OTP_BASE1_SIZE (0x2000UL)
  211. #define EFM_OTP_BASE2_ADDR (0x03000C00UL)
  212. #define EFM_OTP_BASE2_SIZE (0x40UL)
  213. #define EFM_OTP_LOCK_ADDR0 (0x03000A80UL)
  214. #define EFM_OTP_LOCK_ADDR1 (0x03000AC0UL)
  215. /**
  216. * @}
  217. */
  218. /**
  219. * @defgroup EFM_OTP_Address EFM Otp Address
  220. * @{
  221. */
  222. #define EFM_OTP_BLOCK0 (EFM_OTP_BASE1_ADDR)
  223. #define EFM_OTP_BLOCK1 (EFM_OTP_BASE2_ADDR + (0UL * EFM_OTP_BASE2_SIZE))
  224. #define EFM_OTP_BLOCK2 (EFM_OTP_BASE2_ADDR + (1UL * EFM_OTP_BASE2_SIZE))
  225. #define EFM_OTP_BLOCK3 (EFM_OTP_BASE2_ADDR + (2UL * EFM_OTP_BASE2_SIZE))
  226. #define EFM_OTP_BLOCK4 (EFM_OTP_BASE2_ADDR + (3UL * EFM_OTP_BASE2_SIZE))
  227. #define EFM_OTP_BLOCK5 (EFM_OTP_BASE2_ADDR + (4UL * EFM_OTP_BASE2_SIZE))
  228. #define EFM_OTP_BLOCK6 (EFM_OTP_BASE2_ADDR + (5UL * EFM_OTP_BASE2_SIZE))
  229. #define EFM_OTP_BLOCK7 (EFM_OTP_BASE2_ADDR + (6UL * EFM_OTP_BASE2_SIZE))
  230. #define EFM_OTP_BLOCK8 (EFM_OTP_BASE2_ADDR + (7UL * EFM_OTP_BASE2_SIZE))
  231. #define EFM_OTP_BLOCK9 (EFM_OTP_BASE2_ADDR + (8UL * EFM_OTP_BASE2_SIZE))
  232. #define EFM_OTP_BLOCK10 (EFM_OTP_BASE2_ADDR + (9UL * EFM_OTP_BASE2_SIZE))
  233. #define EFM_OTP_BLOCK11 (EFM_OTP_BASE2_ADDR + (10UL * EFM_OTP_BASE2_SIZE))
  234. #define EFM_OTP_BLOCK12 (EFM_OTP_BASE2_ADDR + (11UL * EFM_OTP_BASE2_SIZE))
  235. #define EFM_OTP_BLOCK13 (EFM_OTP_BASE2_ADDR + (12UL * EFM_OTP_BASE2_SIZE))
  236. #define EFM_OTP_BLOCK14 (EFM_OTP_BASE2_ADDR + (13UL * EFM_OTP_BASE2_SIZE))
  237. #define EFM_OTP_BLOCK15 (EFM_OTP_BASE2_ADDR + (14UL * EFM_OTP_BASE2_SIZE))
  238. #define EFM_OTP_BLOCK16 (EFM_OTP_BASE2_ADDR + (15UL * EFM_OTP_BASE2_SIZE))
  239. /**
  240. * @}
  241. */
  242. /**
  243. * @defgroup EFM_OTP_Lock_Address EFM Otp Lock_address
  244. * x at range of 0~181 [-HC32F4A0, HC32F472-]
  245. * x at range of 0~16 [-HC32F448-]
  246. * @{
  247. */
  248. #define EFM_OTP_BLOCK_LOCKADDR(x) ((x == 0UL) ? EFM_OTP_LOCK_ADDR0 : (EFM_OTP_LOCK_ADDR1 + (x - 1UL) * 0x04UL))
  249. /**
  250. * @}
  251. */
  252. #define EFM_REMAP_REG_LOCK_KEY (0x0000UL)
  253. #define EFM_REMAP_REG_UNLOCK_KEY1 (0x0123UL)
  254. #define EFM_REMAP_REG_UNLOCK_KEY2 (0x3210UL)
  255. /**
  256. * @defgroup EFM_Remap_State EFM remap function state
  257. * @{
  258. */
  259. #define EFM_REMAP_OFF (0UL)
  260. #define EFM_REMAP_ON (EFM_MMF_REMCR_EN)
  261. /**
  262. * @}
  263. */
  264. /**
  265. * @defgroup EFM_Remap_Size EFM remap size definition
  266. * @note refer to chip user manual for details size spec.
  267. * @{
  268. */
  269. #define EFM_REMAP_4K (12UL)
  270. #define EFM_REMAP_8K (13UL)
  271. #define EFM_REMAP_16K (14UL)
  272. #define EFM_REMAP_32K (15UL)
  273. #define EFM_REMAP_64K (16UL)
  274. #define EFM_REMAP_128K (17UL)
  275. #define EFM_REMAP_256K (18UL)
  276. #define EFM_REMAP_512K (19UL)
  277. /**
  278. * @}
  279. */
  280. /**
  281. * @defgroup EFM_Remap_Index EFM remap index
  282. * @{
  283. */
  284. #define EFM_REMAP_IDX0 (0U)
  285. #define EFM_REMAP_IDX1 (1U)
  286. /**
  287. * @}
  288. */
  289. /**
  290. * @defgroup EFM_Remap_BaseAddr EFM remap base address
  291. * @{
  292. */
  293. #define EFM_REMAP_BASE_ADDR0 (0x2000000UL)
  294. #define EFM_REMAP_BASE_ADDR1 (0x2080000UL)
  295. /**
  296. * @}
  297. */
  298. /**
  299. * @defgroup EFM_Remap_Region EFM remap ROM/RAM region
  300. * @{
  301. */
  302. #define EFM_REMAP_ROM_END_ADDR EFM_END_ADDR
  303. #define EFM_REMAP_RAM_START_ADDR (0x1FFF8000UL)
  304. #define EFM_REMAP_RAM_END_ADDR (0x1FFFFFFFUL)
  305. /**
  306. * @}
  307. */
  308. /**
  309. * @defgroup EFM_Protect_Level EFM protect level
  310. * @{
  311. */
  312. #define EFM_PROTECT_LEVEL1 (1U)
  313. #define EFM_PROTECT_LEVEL2 (2U)
  314. #define EFM_PROTECT_LEVEL3 (4U)
  315. /**
  316. * @}
  317. */
  318. /**
  319. * @defgroup EFM_MCU_Status EFM protect level
  320. * @{
  321. */
  322. #define EFM_MCU_PROTECT1_FREE (0U)
  323. #define EFM_MCU_PROTECT1_LOCK (1U)
  324. #define EFM_MCU_PROTECT1_UNLOCK (2U)
  325. #define EFM_MCU_PROTECT2_LOCK (4U)
  326. /**
  327. * @}
  328. */
  329. /**
  330. * @}
  331. */
  332. /*******************************************************************************
  333. * Global variable definitions ('extern')
  334. ******************************************************************************/
  335. /*******************************************************************************
  336. Global function prototypes (definition in C source)
  337. ******************************************************************************/
  338. /**
  339. * @addtogroup EFM_Global_Functions
  340. * @{
  341. */
  342. /**
  343. * @brief EFM Protect Unlock.
  344. * @param None
  345. * @retval None
  346. */
  347. __STATIC_INLINE void EFM_REG_Unlock(void)
  348. {
  349. WRITE_REG32(CM_EFM->FAPRT, EFM_REG_UNLOCK_KEY1);
  350. WRITE_REG32(CM_EFM->FAPRT, EFM_REG_UNLOCK_KEY2);
  351. }
  352. /**
  353. * @brief EFM Protect Lock.
  354. * @param None
  355. * @retval None
  356. */
  357. __STATIC_INLINE void EFM_REG_Lock(void)
  358. {
  359. WRITE_REG32(CM_EFM->FAPRT, EFM_REG_LOCK_KEY);
  360. }
  361. /**
  362. * @brief EFM remap Unlock.
  363. * @param None
  364. * @retval None
  365. */
  366. __STATIC_INLINE void EFM_REMAP_Unlock(void)
  367. {
  368. WRITE_REG32(CM_EFM->MMF_REMPRT, EFM_REMAP_REG_UNLOCK_KEY1);
  369. WRITE_REG32(CM_EFM->MMF_REMPRT, EFM_REMAP_REG_UNLOCK_KEY2);
  370. }
  371. /**
  372. * @brief EFM remap Lock.
  373. * @param None
  374. * @retval None
  375. */
  376. __STATIC_INLINE void EFM_REMAP_Lock(void)
  377. {
  378. WRITE_REG32(CM_EFM->MMF_REMPRT, EFM_REMAP_REG_LOCK_KEY);
  379. }
  380. void EFM_Cmd(uint32_t u32Flash, en_functional_state_t enNewState);
  381. void EFM_FWMC_Cmd(en_functional_state_t enNewState);
  382. void EFM_SetBusStatus(uint32_t u32Status);
  383. void EFM_IntCmd(uint32_t u32EfmInt, en_functional_state_t enNewState);
  384. void EFM_ClearStatus(uint32_t u32Flag);
  385. int32_t EFM_SetWaitCycle(uint32_t u32WaitCycle);
  386. int32_t EFM_SetOperateMode(uint32_t u32Mode);
  387. int32_t EFM_ReadByte(uint32_t u32Addr, uint8_t *pu8ReadBuf, uint32_t u32ByteLen);
  388. int32_t EFM_Program(uint32_t u32Addr, uint8_t *pu8Buf, uint32_t u32Len);
  389. int32_t EFM_ProgramWord(uint32_t u32Addr, uint32_t u32Data);
  390. int32_t EFM_ProgramWordReadBack(uint32_t u32Addr, uint32_t u32Data);
  391. int32_t EFM_SequenceProgram(uint32_t u32Addr, uint8_t *pu8Buf, uint32_t u32Len);
  392. int32_t EFM_SectorErase(uint32_t u32Addr);
  393. int32_t EFM_ChipErase(uint8_t u8Chip);
  394. en_flag_status_t EFM_GetAnyStatus(uint32_t u32Flag);
  395. en_flag_status_t EFM_GetStatus(uint32_t u32Flag);
  396. void EFM_GetUID(stc_efm_unique_id_t *pstcUID);
  397. void EFM_DataCacheResetCmd(en_functional_state_t enNewState);
  398. void EFM_PrefetchCmd(en_functional_state_t enNewState);
  399. void EFM_DCacheCmd(en_functional_state_t enNewState);
  400. void EFM_ICacheCmd(en_functional_state_t enNewState);
  401. void EFM_LowVoltageReadCmd(en_functional_state_t enNewState);
  402. int32_t EFM_SwapCmd(en_functional_state_t enNewState);
  403. en_flag_status_t EFM_GetSwapStatus(void);
  404. int32_t EFM_OTP_Lock(uint32_t u32Addr);
  405. int32_t EFM_REMAP_StructInit(stc_efm_remap_init_t *pstcEfmRemapInit);
  406. int32_t EFM_REMAP_Init(uint8_t u8RemapIdx, stc_efm_remap_init_t *pstcEfmRemapInit);
  407. void EFM_REMAP_DeInit(void);
  408. void EFM_REMAP_Cmd(uint8_t u8RemapIdx, en_functional_state_t enNewState);
  409. void EFM_REMAP_SetAddr(uint8_t u8RemapIdx, uint32_t u32Addr);
  410. void EFM_REMAP_SetSize(uint8_t u8RemapIdx, uint32_t u32Size);
  411. uint32_t EFM_GetCID(void);
  412. void EFM_OTP_WP_Unlock(void);
  413. void EFM_OTP_WP_Lock(void);
  414. int32_t EFM_OTP_Enable(void);
  415. void EFM_SectorProtectRegLock(uint32_t u32RegLock);
  416. void EFM_SingleSectorOperateCmd(uint8_t u8SectorNum, en_functional_state_t enNewState);
  417. void EFM_SequenceSectorOperateCmd(uint32_t u32StartSectorNum, uint16_t u16Count, en_functional_state_t enNewState);
  418. void EFM_Protect_Enable(uint8_t u8Level);
  419. int32_t EFM_WriteSecurityCode(uint8_t *pu8Buf, uint32_t u32Len);
  420. /**
  421. * @}
  422. */
  423. #endif /* LL_EFM_ENABLE */
  424. /**
  425. * @}
  426. */
  427. /**
  428. * @}
  429. */
  430. #ifdef __cplusplus
  431. }
  432. #endif
  433. #endif /* __HC32_LL_EFM_H__ */
  434. /*******************************************************************************
  435. * EOF (not truncated)
  436. ******************************************************************************/