core_rv32.h 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. /*
  2. * Copyright (C) 2017-2019 Alibaba Group Holding Limited
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-08-20 zx.chen CSI RV32 Core Peripheral Access Layer Header File
  9. */
  10. #ifndef __CORE_RV32_H_GENERIC
  11. #define __CORE_RV32_H_GENERIC
  12. #include <stdint.h>
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /*******************************************************************************
  17. * CSI definitions
  18. ******************************************************************************/
  19. /**
  20. \ingroup RV32
  21. @{
  22. */
  23. #ifndef __RV32
  24. #define __RV32 (0x01U)
  25. #endif
  26. /** __FPU_USED indicates whether an FPU is used or not.
  27. This core does not support an FPU at all
  28. */
  29. #define __FPU_USED 0U
  30. #if defined ( __GNUC__ )
  31. #if defined (__VFP_FP__) && !defined(__SOFTFP__)
  32. #error "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
  33. #endif
  34. #endif
  35. #ifdef __cplusplus
  36. }
  37. #endif
  38. #endif /* __CORE_RV32_H_GENERIC */
  39. #ifndef __CSI_GENERIC
  40. #ifndef __CORE_RV32_H_DEPENDANT
  41. #define __CORE_RV32_H_DEPENDANT
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. /* check device defines and use defaults */
  46. #ifndef __RV32_REV
  47. #define __RV32_REV 0x0000U
  48. #endif
  49. #ifndef __VIC_PRIO_BITS
  50. #define __VIC_PRIO_BITS 2U
  51. #endif
  52. #ifndef __Vendor_SysTickConfig
  53. #define __Vendor_SysTickConfig 1U
  54. #endif
  55. #ifndef __MPU_PRESENT
  56. #define __MPU_PRESENT 1U
  57. #endif
  58. #ifndef __ICACHE_PRESENT
  59. #define __ICACHE_PRESENT 1U
  60. #endif
  61. #ifndef __DCACHE_PRESENT
  62. #define __DCACHE_PRESENT 1U
  63. #endif
  64. #include <csi_rv32_gcc.h>
  65. /* IO definitions (access restrictions to peripheral registers) */
  66. /**
  67. \defgroup CSI_glob_defs CSI Global Defines
  68. <strong>IO Type Qualifiers</strong> are used
  69. \li to specify the access to peripheral variables.
  70. \li for automatic generation of peripheral register debug information.
  71. */
  72. #ifdef __cplusplus
  73. #define __I volatile /*!< Defines 'read only' permissions */
  74. #else
  75. #define __I volatile const /*!< Defines 'read only' permissions */
  76. #endif
  77. #define __O volatile /*!< Defines 'write only' permissions */
  78. #define __IO volatile /*!< Defines 'read / write' permissions */
  79. /* following defines should be used for structure members */
  80. #define __IM volatile const /*! Defines 'read only' structure member permissions */
  81. #define __OM volatile /*! Defines 'write only' structure member permissions */
  82. #define __IOM volatile /*! Defines 'read / write' structure member permissions */
  83. /*@} end of group CK802 */
  84. /*******************************************************************************
  85. * Register Abstraction
  86. Core Register contain:
  87. - Core Register
  88. - Core CLIC Register
  89. ******************************************************************************/
  90. /**
  91. \defgroup CSI_core_register Defines and Type Definitions
  92. \brief Type definitions and defines for CK80X processor based devices.
  93. */
  94. /**
  95. \ingroup CSI_core_register
  96. \defgroup CSI_CORE Status and Control Registers
  97. \brief Core Register type definitions.
  98. @{
  99. */
  100. /**
  101. \ingroup CSI_core_register
  102. \defgroup CSI_CLIC Core-Local Interrupt Controller (CLIC)
  103. \brief Type definitions for the CLIC Registers
  104. @{
  105. */
  106. /**
  107. \brief Access to the structure of a vector interrupt controller.
  108. */
  109. typedef struct
  110. {
  111. __IOM uint8_t IP; /*!< Offset: 0x000 (R/W) Interrupt set pending register */
  112. __IOM uint8_t IE; /*!< Offset: 0x004 (R/W) Interrupt set enable register */
  113. __IOM uint8_t ATTR; /*!< Offset: 0x008 (R/W) Interrupt set attribute register */
  114. __IOM uint8_t CTL; /*!< Offset: 0x00C (R/W) Interrupt control register */
  115. } CLIC_INT_Control;
  116. typedef struct
  117. {
  118. __IOM uint32_t CLICCFG:8; /*!< Offset: 0x000 (R/W) CLIC configure register */
  119. __IM uint32_t CLICINFO;
  120. __IOM uint32_t MINTTHRESH;
  121. uint32_t RESERVED[1021];
  122. CLIC_INT_Control CLICINT[4096];
  123. } CLIC_Type;
  124. #define CLIC_INFO_CLICINTCTLBITS_Pos 21U
  125. #define CLIC_INFO_CLICINTCTLBITS_Msk (0xFUL << CLIC_INFO_CLICINTCTLBITS_Pos)
  126. #define CLIC_INTIP_IP_Pos 0U /*!< CLIC INTIP: IP Position */
  127. #define CLIC_INTIP_IP_Msk (0x1UL << CLIC_INTIP_IP_Pos) /*!< CLIC INTIP: IP Mask */
  128. #define CLIC_INTIE_IE_Pos 0U /*!< CLIC INTIE: IE Position */
  129. #define CLIC_INTIE_IE_Msk (0x1UL << CLIC_INTIE_IE_Pos) /*!< CLIC INTIE: IE Mask */
  130. #define CLIC_INTIE_T_Pos 7U /*!< CLIC INTIE: T Position */
  131. #define CLIC_INTIE_T_Msk (0x1UL << CLIC_INTIE_T_Pos) /*!< CLIC INTIE: T Mask */
  132. #define CLIC_INTATTR_TRIG_Pos 1U /*!< CLIC INTATTR: TRIG Position */
  133. #define CLIC_INTATTR_TRIG_Msk (0x3UL << CLIC_INTATTR_TRIG_Pos) /*!< CLIC INTATTR: TRIG Mask */
  134. #define CLIC_INTATTR_SHV_Pos 0U /*!< CLIC INTATTR: SHV Position */
  135. #define CLIC_INTATTR_SHV_Msk (0x1UL << CLIC_INTATTR_SHV_Pos) /*!< CLIC INTATTR: SHV Mask */
  136. #define CLIC_INTCFG_NVBIT_Pos 5U /*!< CLIC INTCFG: NVBIT Position */
  137. #define CLIC_INTCFG_NVBIT_Msk (0x1UL << CLIC_INTCFG_NVBIT_Pos) /*!< CLIC INTCFG: NVBIT Mask */
  138. #define CLIC_INTCFG_PRIO_Pos 5U /*!< CLIC INTCFG: INTCFG Position */
  139. #define CLIC_INTCFG_PRIO_Msk (0x7UL << CLIC_INTCFG_PRIO_Pos) /*!< CLIC INTCFG: INTCFG Mask */
  140. #define CLIC_CLICCFG_NVBIT_Pos 0U /*!< CLIC CLICCFG: NVBIT Position */
  141. #define CLIC_CLICCFG_NVBIT_Msk (0x1UL << CLIC_CLICCFG_NVBIT_Pos) /*!< CLIC CLICCFG: NVBIT Mask */
  142. #define CLIC_CLICCFG_NLBIT_Pos 1U /*!< CLIC CLICCFG: NLBIT Position */
  143. #define CLIC_CLICCFG_NLBIT_Msk (0xFUL << CLIC_CLICCFG_NLBIT_Pos) /*!< CLIC CLICCFG: NLBIT Mask */
  144. #define CLIC_CLICCFG_NMBIT_Pos 5U /*!< CLIC CLICCFG: NMBIT Position */
  145. #define CLIC_CLICCFG_NMBIT_Msk (0x3UL << CLIC_CLICCFG_NMBIT_Pos) /*!< CLIC CLICCFG: NMBIT Mask */
  146. /*@} end of group CSI_CLIC */
  147. /**
  148. \ingroup CSI_core_register
  149. \defgroup CSI_PMP Physical Memory Protection (PMP)
  150. \brief Type definitions for the PMP Registers
  151. @{
  152. */
  153. #define PMP_PMPCFG_R_Pos 0U /*!< PMP PMPCFG: R Position */
  154. #define PMP_PMPCFG_R_Msk (0x1UL << PMP_PMPCFG_R_Pos) /*!< PMP PMPCFG: R Mask */
  155. #define PMP_PMPCFG_W_Pos 1U /*!< PMP PMPCFG: W Position */
  156. #define PMP_PMPCFG_W_Msk (0x1UL << PMP_PMPCFG_W_Pos) /*!< PMP PMPCFG: W Mask */
  157. #define PMP_PMPCFG_X_Pos 2U /*!< PMP PMPCFG: X Position */
  158. #define PMP_PMPCFG_X_Msk (0x1UL << PMP_PMPCFG_X_Pos) /*!< PMP PMPCFG: X Mask */
  159. #define PMP_PMPCFG_A_Pos 3U /*!< PMP PMPCFG: A Position */
  160. #define PMP_PMPCFG_A_Msk (0x3UL << PMP_PMPCFG_A_Pos) /*!< PMP PMPCFG: A Mask */
  161. #define PMP_PMPCFG_L_Pos 7U /*!< PMP PMPCFG: L Position */
  162. #define PMP_PMPCFG_L_Msk (0x1UL << PMP_PMPCFG_L_Pos) /*!< PMP PMPCFG: L Mask */
  163. typedef enum
  164. {
  165. REGION_SIZE_4B = -1,
  166. REGION_SIZE_8B = 0,
  167. REGION_SIZE_16B = 1,
  168. REGION_SIZE_32B = 2,
  169. REGION_SIZE_64B = 3,
  170. REGION_SIZE_128B = 4,
  171. REGION_SIZE_256B = 5,
  172. REGION_SIZE_512B = 6,
  173. REGION_SIZE_1KB = 7,
  174. REGION_SIZE_2KB = 8,
  175. REGION_SIZE_4KB = 9,
  176. REGION_SIZE_8KB = 10,
  177. REGION_SIZE_16KB = 11,
  178. REGION_SIZE_32KB = 12,
  179. REGION_SIZE_64KB = 13,
  180. REGION_SIZE_128KB = 14,
  181. REGION_SIZE_256KB = 15,
  182. REGION_SIZE_512KB = 16,
  183. REGION_SIZE_1MB = 17,
  184. REGION_SIZE_2MB = 18,
  185. REGION_SIZE_4MB = 19,
  186. REGION_SIZE_8MB = 20,
  187. REGION_SIZE_16MB = 21,
  188. REGION_SIZE_32MB = 22,
  189. REGION_SIZE_64MB = 23,
  190. REGION_SIZE_128MB = 24,
  191. REGION_SIZE_256MB = 25,
  192. REGION_SIZE_512MB = 26,
  193. REGION_SIZE_1GB = 27,
  194. REGION_SIZE_2GB = 28,
  195. REGION_SIZE_4GB = 29,
  196. REGION_SIZE_8GB = 30,
  197. REGION_SIZE_16GB = 31
  198. } region_size_e;
  199. typedef enum
  200. {
  201. ADDRESS_MATCHING_TOR = 1,
  202. ADDRESS_MATCHING_NAPOT = 3
  203. } address_matching_e;
  204. typedef struct
  205. {
  206. uint32_t r: 1; /* readable enable */
  207. uint32_t w: 1; /* writeable enable */
  208. uint32_t x: 1; /* execable enable */
  209. address_matching_e a: 2; /* address matching mode */
  210. uint32_t reserved: 2; /* reserved */
  211. uint32_t l: 1; /* lock enable */
  212. } mpu_region_attr_t;
  213. /*@} end of group CSI_PMP */
  214. /* CACHE Register Definitions */
  215. #define CACHE_MHCR_L0BTB_Pos 12U /*!< CACHE MHCR: L0BTB Position */
  216. #define CACHE_MHCR_L0BTB_Msk (0x1UL << CACHE_MHCR_L0BTB_Pos) /*!< CACHE MHCR: WA Mask */
  217. #define CACHE_MHCR_BPE_Pos 5U /*!< CACHE MHCR: BPE Position */
  218. #define CACHE_MHCR_BPE_Msk (0x1UL << CACHE_MHCR_BPE_Pos) /*!< CACHE MHCR: BPE Mask */
  219. #define CACHE_MHCR_RS_Pos 4U /*!< CACHE MHCR: RS Position */
  220. #define CACHE_MHCR_RS_Msk (0x1UL << CACHE_MHCR_RS_Pos) /*!< CACHE MHCR: RS Mask */
  221. #define CACHE_MHCR_WA_Pos 3U /*!< CACHE MHCR: WA Position */
  222. #define CACHE_MHCR_WA_Msk (0x1UL << CACHE_MHCR_WA_Pos) /*!< CACHE MHCR: WA Mask */
  223. #define CACHE_MHCR_WB_Pos 2U /*!< CACHE MHCR: WB Position */
  224. #define CACHE_MHCR_WB_Msk (0x1UL << CACHE_MHCR_WB_Pos) /*!< CACHE MHCR: WB Mask */
  225. #define CACHE_MHCR_DE_Pos 1U /*!< CACHE MHCR: DE Position */
  226. #define CACHE_MHCR_DE_Msk (0x1UL << CACHE_MHCR_DE_Pos) /*!< CACHE MHCR: DE Mask */
  227. #define CACHE_MHCR_IE_Pos 0U /*!< CACHE MHCR: IE Position */
  228. #define CACHE_MHCR_IE_Msk (0x1UL << CACHE_MHCR_IE_Pos) /*!< CACHE MHCR: IE Mask */
  229. #define CACHE_INV_ADDR_Pos 5U
  230. #define CACHE_INV_ADDR_Msk (0xFFFFFFFFUL << CACHE_INV_ADDR_Pos)
  231. /*@} end of group CSI_CACHE */
  232. /**
  233. \ingroup CSI_core_register
  234. \defgroup CSI_SysTick System Tick Timer (CORET)
  235. \brief Type definitions for the System Timer Registers.
  236. @{
  237. */
  238. /**
  239. \brief The data structure of the access system timer.
  240. */
  241. typedef struct
  242. {
  243. __IOM unsigned long long MTIMECMP; /*!< Offset: 0x000 (R/W) Timer compare register */
  244. uint32_t RESERVED[8187];
  245. __IM unsigned long long MTIME; /*!< Offset: 0x7FFC (R) Timer current register */
  246. } CORET_Type;
  247. /*@} end of group CSI_SysTick */
  248. /**
  249. \ingroup CSI_core_register
  250. \defgroup CSI_DCC
  251. \brief Type definitions for the DCC.
  252. @{
  253. */
  254. /**
  255. \brief Access to the data structure of DCC.
  256. */
  257. typedef struct
  258. {
  259. uint32_t RESERVED0[13U];
  260. __IOM uint32_t HCR; /*!< Offset: 0x034 (R/W) */
  261. __IM uint32_t EHSR; /*!< Offset: 0x03C (R/ ) */
  262. uint32_t RESERVED1[6U];
  263. union {
  264. __IM uint32_t DERJW; /*!< Offset: 0x058 (R/ ) Data exchange register CPU read*/
  265. __OM uint32_t DERJR; /*!< Offset: 0x058 ( /W) Data exchange register CPU writer*/
  266. };
  267. } DCC_Type;
  268. #define DCC_HCR_JW_Pos 18U /*!< DCC HCR: jw_int_en Position */
  269. #define DCC_HCR_JW_Msk (1UL << DCC_HCR_JW_Pos) /*!< DCC HCR: jw_int_en Mask */
  270. #define DCC_HCR_JR_Pos 19U /*!< DCC HCR: jr_int_en Position */
  271. #define DCC_HCR_JR_Msk (1UL << DCC_HCR_JR_Pos) /*!< DCC HCR: jr_int_en Mask */
  272. #define DCC_EHSR_JW_Pos 1U /*!< DCC EHSR: jw_vld Position */
  273. #define DCC_EHSR_JW_Msk (1UL << DCC_EHSR_JW_Pos) /*!< DCC EHSR: jw_vld Mask */
  274. #define DCC_EHSR_JR_Pos 2U /*!< DCC EHSR: jr_vld Position */
  275. #define DCC_EHSR_JR_Msk (1UL << DCC_EHSR_JR_Pos) /*!< DCC EHSR: jr_vld Mask */
  276. /*@} end of group CSI_DCC */
  277. /**
  278. \ingroup CSI_core_register
  279. \defgroup CSI_core_bitfield Core register bit field macros
  280. \brief Macros for use with bit field definitions (xxx_Pos, xxx_Msk).
  281. @{
  282. */
  283. /**
  284. \brief Mask and shift a bit field value for use in a register bit range.
  285. \param[in] field Name of the register bit field.
  286. \param[in] value Value of the bit field.
  287. \return Masked and shifted value.
  288. */
  289. #define _VAL2FLD(field, value) ((value << field ## _Pos) & field ## _Msk)
  290. /**
  291. \brief Mask and shift a register value to extract a bit filed value.
  292. \param[in] field Name of the register bit field.
  293. \param[in] value Value of register.
  294. \return Masked and shifted bit field value.
  295. */
  296. #define _FLD2VAL(field, value) ((value & field ## _Msk) >> field ## _Pos)
  297. /*@} end of group CSI_core_bitfield */
  298. /**
  299. \ingroup CSI_core_register
  300. \defgroup CSI_core_base Core Definitions
  301. \brief Definitions for base addresses, unions, and structures.
  302. @{
  303. */
  304. /* Memory mapping of CK802 Hardware */
  305. #define TCIP_BASE (0xE000E000UL) /*!< Titly Coupled IP Base Address */
  306. #define CORET_BASE (0xE0004000UL) /*!< CORET Base Address */
  307. #define CLIC_BASE (0xE0800000UL) /*!< CLIC Base Address */
  308. #define DCC_BASE (0xE4010000UL) /*!< DCC Base Address */
  309. #define CACHE_BASE (TCIP_BASE + 0x1000UL) /*!< CACHE Base Address */
  310. #define CORET ((CORET_Type *) CORET_BASE ) /*!< SysTick configuration struct */
  311. #define CLIC ((CLIC_Type *) CLIC_BASE ) /*!< CLIC configuration struct */
  312. #define DCC ((DCC_Type *) DCC_BASE ) /*!< DCC configuration struct */
  313. #define CACHE ((CACHE_Type *) CACHE_BASE ) /*!< cache configuration struct */
  314. /*@} */
  315. /*******************************************************************************
  316. * Hardware Abstraction Layer
  317. Core Function Interface contains:
  318. - Core VIC Functions
  319. - Core CORET Functions
  320. - Core Register Access Functions
  321. ******************************************************************************/
  322. /**
  323. \defgroup CSI_Core_FunctionInterface Functions and Instructions Reference
  324. */
  325. /* ########################## VIC functions #################################### */
  326. /**
  327. \ingroup CSI_Core_FunctionInterface
  328. \defgroup CSI_Core_VICFunctions VIC Functions
  329. \brief Functions that manage interrupts and exceptions via the VIC.
  330. @{
  331. */
  332. /* The following MACROS handle generation of the register offset and byte masks */
  333. #define _BIT_SHIFT(IRQn) ( ((((uint32_t)(int32_t)(IRQn)) ) & 0x03UL) * 8UL)
  334. #define _IP_IDX(IRQn) ( (((uint32_t)(int32_t)(IRQn)) >> 5UL) )
  335. #define _IP2_IDX(IRQn) ( (((uint32_t)(int32_t)(IRQn)) >> 2UL) )
  336. /**
  337. \brief Enable External Interrupt
  338. \details Enable a device-specific interrupt in the VIC interrupt controller.
  339. \param [in] IRQn External interrupt number. Value cannot be negative.
  340. */
  341. __STATIC_INLINE void csi_vic_enable_irq(int32_t IRQn)
  342. {
  343. CLIC->CLICINT[IRQn].IE |= CLIC_INTIE_IE_Msk;
  344. }
  345. /**
  346. \brief Disable External Interrupt
  347. \details Disable a device-specific interrupt in the VIC interrupt controller.
  348. \param [in] IRQn External interrupt number. Value cannot be negative.
  349. */
  350. __STATIC_INLINE void csi_vic_disable_irq(int32_t IRQn)
  351. {
  352. CLIC->CLICINT[IRQn].IE &= ~CLIC_INTIE_IE_Msk;
  353. }
  354. /**
  355. \brief Enable External Secure Interrupt
  356. \details Enable a secure device-specific interrupt in the VIC interrupt controller.
  357. \param [in] IRQn External interrupt number. Value cannot be negative.
  358. */
  359. __STATIC_INLINE void csi_vic_enable_sirq(int32_t IRQn)
  360. {
  361. CLIC->CLICINT[IRQn].IE |= (CLIC_INTIE_IE_Msk | CLIC_INTIE_T_Msk);
  362. }
  363. /**
  364. \brief Disable External Secure Interrupt
  365. \details Disable a secure device-specific interrupt in the VIC interrupt controller.
  366. \param [in] IRQn External interrupt number. Value cannot be negative.
  367. */
  368. __STATIC_INLINE void csi_vic_disable_sirq(int32_t IRQn)
  369. {
  370. CLIC->CLICINT[IRQn].IE &= ~(CLIC_INTIE_IE_Msk | CLIC_INTIE_T_Msk);
  371. }
  372. /**
  373. \brief Check Interrupt is Enabled or not
  374. \details Read the enabled register in the VIC and returns the pending bit for the specified interrupt.
  375. \param [in] IRQn Interrupt number.
  376. \return 0 Interrupt status is not enabled.
  377. \return 1 Interrupt status is enabled.
  378. */
  379. __STATIC_INLINE uint32_t csi_vic_get_enabled_irq(int32_t IRQn)
  380. {
  381. return (uint32_t)(CLIC->CLICINT[IRQn].IE & CLIC_INTIE_IE_Msk);
  382. }
  383. /**
  384. \brief Check Interrupt is Pending or not
  385. \details Read the pending register in the VIC and returns the pending bit for the specified interrupt.
  386. \param [in] IRQn Interrupt number.
  387. \return 0 Interrupt status is not pending.
  388. \return 1 Interrupt status is pending.
  389. */
  390. __STATIC_INLINE uint32_t csi_vic_get_pending_irq(int32_t IRQn)
  391. {
  392. return (uint32_t)(CLIC->CLICINT[IRQn].IP & CLIC_INTIP_IP_Msk);
  393. }
  394. /**
  395. \brief Set Pending Interrupt
  396. \details Set the pending bit of an external interrupt.
  397. \param [in] IRQn Interrupt number. Value cannot be negative.
  398. */
  399. __STATIC_INLINE void csi_vic_set_pending_irq(int32_t IRQn)
  400. {
  401. CLIC->CLICINT[IRQn].IP |= CLIC_INTIP_IP_Msk;
  402. }
  403. /**
  404. \brief Clear Pending Interrupt
  405. \details Clear the pending bit of an external interrupt.
  406. \param [in] IRQn External interrupt number. Value cannot be negative.
  407. */
  408. __STATIC_INLINE void csi_vic_clear_pending_irq(int32_t IRQn)
  409. {
  410. CLIC->CLICINT[IRQn].IP &= ~CLIC_INTIP_IP_Msk;
  411. }
  412. /**
  413. \brief Set Interrupt Priority
  414. \details Set the priority of an interrupt.
  415. \note The priority cannot be set for every core interrupt.
  416. \param [in] IRQn Interrupt number.
  417. \param [in] priority Priority to set.
  418. */
  419. __STATIC_INLINE void csi_vic_set_prio(int32_t IRQn, uint32_t priority)
  420. {
  421. uint8_t nlbits = (CLIC->CLICINFO & CLIC_INFO_CLICINTCTLBITS_Msk) >> CLIC_INFO_CLICINTCTLBITS_Pos;
  422. CLIC->CLICINT[IRQn].CTL = (CLIC->CLICINT[IRQn].CTL & (~CLIC_INTCFG_PRIO_Msk)) | (priority << (8 - nlbits));
  423. }
  424. /**
  425. \brief Get Interrupt Priority
  426. \details Read the priority of an interrupt.
  427. The interrupt number can be positive to specify an external (device specific) interrupt,
  428. or negative to specify an internal (core) interrupt.
  429. \param [in] IRQn Interrupt number.
  430. \return Interrupt Priority.
  431. Value is aligned automatically to the implemented priority bits of the microcontroller.
  432. */
  433. __STATIC_INLINE uint32_t csi_vic_get_prio(int32_t IRQn)
  434. {
  435. uint8_t nlbits = (CLIC->CLICINFO & CLIC_INFO_CLICINTCTLBITS_Msk) >> CLIC_INFO_CLICINTCTLBITS_Pos;
  436. return CLIC->CLICINT[IRQn].CTL >> (8 - nlbits);
  437. }
  438. /**
  439. \brief Set interrupt handler
  440. \details Set the interrupt handler according to the interrupt num, the handler will be filled in irq vectors.
  441. \param [in] IRQn Interrupt number.
  442. \param [in] handler Interrupt handler.
  443. */
  444. __STATIC_INLINE void csi_vic_set_vector(int32_t IRQn, uint32_t handler)
  445. {
  446. if (IRQn >= 0 && IRQn < 1024) {
  447. uint32_t *vectors = (uint32_t *)__get_MTVT();
  448. vectors[IRQn] = handler;
  449. }
  450. }
  451. /**
  452. \brief Get interrupt handler
  453. \details Get the address of interrupt handler function.
  454. \param [in] IRQn Interrupt number.
  455. */
  456. __STATIC_INLINE uint32_t csi_vic_get_vector(int32_t IRQn)
  457. {
  458. if (IRQn >= 0 && IRQn < 1024) {
  459. uint32_t *vectors = (uint32_t *)__get_MTVT();
  460. return (uint32_t)vectors[IRQn];
  461. }
  462. return 0;
  463. }
  464. /*@} end of CSI_Core_VICFunctions */
  465. /* ########################## PMP functions #################################### */
  466. /**
  467. \ingroup CSI_Core_FunctionInterface
  468. \defgroup CSI_Core_PMPFunctions PMP Functions
  469. \brief Functions that manage interrupts and exceptions via the VIC.
  470. @{
  471. */
  472. /**
  473. \brief configure memory protected region.
  474. \details
  475. \param [in] idx memory protected region (0, 1, 2, ..., 15).
  476. \param [in] base_addr base address must be aligned with page size.
  477. \param [in] size \ref region_size_e. memory protected region size.
  478. \param [in] attr \ref region_size_t. memory protected region attribute.
  479. \param [in] enable enable or disable memory protected region.
  480. */
  481. __STATIC_INLINE void csi_mpu_config_region(uint32_t idx, uint32_t base_addr, region_size_e size,
  482. mpu_region_attr_t attr, uint32_t enable)
  483. {
  484. uint8_t pmpxcfg = 0;
  485. uint32_t addr = 0;
  486. if (idx > 15) {
  487. return;
  488. }
  489. if (!enable) {
  490. attr.a = 0;
  491. }
  492. if (attr.a == ADDRESS_MATCHING_TOR) {
  493. addr = base_addr >> 2;
  494. } else {
  495. if (size == REGION_SIZE_4B) {
  496. addr = base_addr >> 2;
  497. attr.a = 2;
  498. } else {
  499. addr = ((base_addr >> 2) & (0xFFFFFFFFU - ((1 << (size + 1)) - 1))) | ((1 << size) - 1);
  500. }
  501. }
  502. __set_PMPADDRx(idx, addr);
  503. pmpxcfg |= (attr.r << PMP_PMPCFG_R_Pos) | (attr.w << PMP_PMPCFG_W_Pos) |
  504. (attr.x << PMP_PMPCFG_X_Pos) | (attr.a << PMP_PMPCFG_A_Pos) |
  505. (attr.l << PMP_PMPCFG_L_Pos);
  506. __set_PMPxCFG(idx, pmpxcfg);
  507. }
  508. /**
  509. \brief disable mpu region by idx.
  510. \details
  511. \param [in] idx memory protected region (0, 1, 2, ..., 15).
  512. */
  513. __STATIC_INLINE void csi_mpu_disable_region(uint32_t idx)
  514. {
  515. __set_PMPxCFG(idx, __get_PMPxCFG(idx) & (~PMP_PMPCFG_A_Msk));
  516. }
  517. /*@} end of CSI_Core_PMPFunctions */
  518. /* ################################## SysTick function ############################################ */
  519. /**
  520. \ingroup CSI_Core_FunctionInterface
  521. \defgroup CSI_Core_SysTickFunctions SysTick Functions
  522. \brief Functions that configure the System.
  523. @{
  524. */
  525. /**
  526. \brief CORE timer Configuration
  527. \details Initializes the System Timer and its interrupt, and starts the System Tick Timer.
  528. Counter is in free running mode to generate periodic interrupts.
  529. \param [in] ticks Number of ticks between two interrupts.
  530. \param [in] IRQn core timer Interrupt number.
  531. \return 0 Function succeeded.
  532. \return 1 Function failed.
  533. \note When the variable <b>__Vendor_SysTickConfig</b> is set to 1, then the
  534. function <b>SysTick_Config</b> is not included. In this case, the file <b><i>device</i>.h</b>
  535. must contain a vendor-specific implementation of this function.
  536. */
  537. __STATIC_INLINE uint32_t csi_coret_config(uint32_t ticks, int32_t IRQn)
  538. {
  539. if (CORET->MTIMECMP) {
  540. CORET->MTIMECMP = CORET->MTIMECMP + ticks;
  541. } else {
  542. CORET->MTIMECMP = CORET->MTIME + ticks;
  543. }
  544. return (0UL);
  545. }
  546. /**
  547. \brief get CORE timer reload value
  548. \return CORE timer counter value.
  549. */
  550. __STATIC_INLINE uint32_t csi_coret_get_load(void)
  551. {
  552. return CORET->MTIMECMP & 0xFFFFFFFF;
  553. }
  554. /**
  555. \brief get CORE timer reload high value
  556. \return CORE timer counter value.
  557. */
  558. __STATIC_INLINE uint32_t csi_coret_get_loadh(void)
  559. {
  560. return (CORET->MTIMECMP >> 32) & 0xFFFFFFFF;
  561. }
  562. /**
  563. \brief get CORE timer counter value
  564. \return CORE timer counter value.
  565. */
  566. __STATIC_INLINE uint32_t csi_coret_get_value(void)
  567. {
  568. return CORET->MTIME & 0xFFFFFFFF;
  569. }
  570. /**
  571. \brief get CORE timer counter high value
  572. \return CORE timer counter value.
  573. */
  574. __STATIC_INLINE uint32_t csi_coret_get_valueh(void)
  575. {
  576. return (CORET->MTIME >> 32) & 0xFFFFFFFF;
  577. }
  578. /*@} end of CSI_Core_SysTickFunctions */
  579. /* ##################################### DCC function ########################################### */
  580. /**
  581. \ingroup CSI_Core_FunctionInterface
  582. \defgroup CSI_core_DebugFunctions HAD Functions
  583. \brief Functions that access the HAD debug interface.
  584. @{
  585. */
  586. /**
  587. \brief HAD Send Character
  588. \details Transmits a character via the HAD channel 0, and
  589. \li Just returns when no debugger is connected that has booked the output.
  590. \li Is blocking when a debugger is connected, but the previous character sent has not been transmitted.
  591. \param [in] ch Character to transmit.
  592. \returns Character to transmit.
  593. */
  594. __STATIC_INLINE uint32_t csi_had_send_char(uint32_t ch)
  595. {
  596. DCC->DERJR = (uint8_t)ch;
  597. return (ch);
  598. }
  599. /**
  600. \brief HAD Receive Character
  601. \details Inputs a character via the external variable \ref HAD_RxBuffer.
  602. \return Received character.
  603. \return -1 No character pending.
  604. */
  605. __STATIC_INLINE int32_t csi_had_receive_char(void)
  606. {
  607. int32_t ch = -1; /* no character available */
  608. if (_FLD2VAL(DCC_EHSR_JW, DCC->EHSR)) {
  609. ch = DCC->DERJW;
  610. }
  611. return (ch);
  612. }
  613. /**
  614. \brief HAD Check Character
  615. \details Check whether a character is pending for reading in the variable \ref HAD_RxBuffer.
  616. \return 0 No character available.
  617. \return 1 Character available.
  618. */
  619. __STATIC_INLINE int32_t csi_had_check_char(void)
  620. {
  621. return _FLD2VAL(DCC_EHSR_JW, DCC->EHSR); /* no character available */
  622. }
  623. /*@} end of CSI_core_DebugFunctions */
  624. /* ########################## sleep and Reset functions #################################### */
  625. /**
  626. \ingroup CSI_Core_FunctionInterface
  627. \defgroup CSI_Core_SleepAndResetFunctions Sleep and Reset Functions
  628. \brief Functions that configure Sleep and Reset.
  629. @{
  630. */
  631. /* MEXSTATUS Register Definitions */
  632. #define MEXSTATUS_RESET_Pos 0U
  633. #define MEXSTATUS_RESET_Msk (0x3UL << MEXSTATUS_RESET_Pos) /*!< MEXSTATUS: RESET Position */
  634. #define MEXSTATUS_SLEEP_Pos 2U
  635. #define MEXSTATUS_SLEEP_Msk (0x3UL << MEXSTATUS_SLEEP_Pos) /*!< MEXSTATUS: RESET Position */
  636. /*@} end of group CSI_MEXSTATUS */
  637. /**
  638. \brief CPU System Reset
  639. \details Triggle CPU System Reset
  640. */
  641. __STATIC_INLINE void csi_system_reset (void)
  642. {
  643. uint32_t mexstatus;
  644. mexstatus = __get_MEXSTATUS();
  645. mexstatus &= (~(MEXSTATUS_RESET_Msk));
  646. mexstatus |= (uint32_t)(0x2 << MEXSTATUS_RESET_Pos);
  647. __set_MEXSTATUS(mexstatus);
  648. }
  649. /**
  650. \brief CPU Core Reset
  651. \details Triggle CPU Core Reset
  652. */
  653. __STATIC_INLINE void csi_core_reset (void)
  654. {
  655. uint32_t mexstatus;
  656. mexstatus = __get_MEXSTATUS();
  657. mexstatus &= (~(MEXSTATUS_RESET_Msk));
  658. mexstatus |= (uint32_t)(0x1 << MEXSTATUS_RESET_Pos);
  659. __set_MEXSTATUS(mexstatus);
  660. }
  661. /* ########################## Cache functions #################################### */
  662. /**
  663. \ingroup CSI_Core_FunctionInterface
  664. \defgroup CSI_Core_CacheFunctions Cache Functions
  665. \brief Functions that configure Instruction and Data cache.
  666. @{
  667. */
  668. /**
  669. \brief Enable I-Cache
  670. \details Turns on I-Cache
  671. */
  672. __STATIC_INLINE void csi_icache_enable (void)
  673. {
  674. #if (__ICACHE_PRESENT == 1U)
  675. uint32_t cache;
  676. __DSB();
  677. __ISB();
  678. __ICACHE_IALL();
  679. cache = __get_MHCR();
  680. cache |= CACHE_MHCR_IE_Msk;
  681. __set_MHCR(cache);
  682. __DSB();
  683. __ISB();
  684. #endif
  685. }
  686. /**
  687. \brief Disable I-Cache
  688. \details Turns off I-Cache
  689. */
  690. __STATIC_INLINE void csi_icache_disable (void)
  691. {
  692. #if (__ICACHE_PRESENT == 1U)
  693. uint32_t cache;
  694. __DSB();
  695. __ISB();
  696. cache = __get_MHCR();
  697. cache &= ~CACHE_MHCR_IE_Msk; /* disable icache */
  698. __set_MHCR(cache);
  699. __ICACHE_IALL(); /* invalidate all icache */
  700. __DSB();
  701. __ISB();
  702. #endif
  703. }
  704. /**
  705. \brief Invalidate I-Cache
  706. \details Invalidates I-Cache
  707. */
  708. __STATIC_INLINE void csi_icache_invalid (void)
  709. {
  710. #if (__ICACHE_PRESENT == 1U)
  711. __DSB();
  712. __ISB();
  713. __ICACHE_IALL(); /* invalidate all icache */
  714. __DSB();
  715. __ISB();
  716. #endif
  717. }
  718. /**
  719. \brief Enable D-Cache
  720. \details Turns on D-Cache
  721. \note I-Cache also turns on.
  722. */
  723. __STATIC_INLINE void csi_dcache_enable (void)
  724. {
  725. #if (__DCACHE_PRESENT == 1U)
  726. uint32_t cache;
  727. __DSB();
  728. __ISB();
  729. __DCACHE_IALL(); /* invalidate all dcache */
  730. cache = __get_MHCR();
  731. cache |= (CACHE_MHCR_DE_Msk | CACHE_MHCR_WB_Msk | CACHE_MHCR_WA_Msk | CACHE_MHCR_RS_Msk | CACHE_MHCR_BPE_Msk | CACHE_MHCR_L0BTB_Msk); /* enable all Cache */
  732. __set_MHCR(cache);
  733. __DSB();
  734. __ISB();
  735. #endif
  736. }
  737. /**
  738. \brief Disable D-Cache
  739. \details Turns off D-Cache
  740. \note I-Cache also turns off.
  741. */
  742. __STATIC_INLINE void csi_dcache_disable (void)
  743. {
  744. #if (__DCACHE_PRESENT == 1U)
  745. uint32_t cache;
  746. __DSB();
  747. __ISB();
  748. cache = __get_MHCR();
  749. cache &= ~(uint32_t)CACHE_MHCR_DE_Msk; /* disable all Cache */
  750. __set_MHCR(cache);
  751. __DCACHE_IALL(); /* invalidate all Cache */
  752. __DSB();
  753. __ISB();
  754. #endif
  755. }
  756. /**
  757. \brief Invalidate D-Cache
  758. \details Invalidates D-Cache
  759. \note I-Cache also invalid
  760. */
  761. __STATIC_INLINE void csi_dcache_invalid (void)
  762. {
  763. #if (__DCACHE_PRESENT == 1U)
  764. __DSB();
  765. __ISB();
  766. __DCACHE_IALL(); /* invalidate all Cache */
  767. __DSB();
  768. __ISB();
  769. #endif
  770. }
  771. /**
  772. \brief Clean D-Cache
  773. \details Cleans D-Cache
  774. \note I-Cache also cleans
  775. */
  776. __STATIC_INLINE void csi_dcache_clean (void)
  777. {
  778. #if (__DCACHE_PRESENT == 1U)
  779. __DSB();
  780. __ISB();
  781. __DCACHE_CALL(); /* clean all Cache */
  782. __DSB();
  783. __ISB();
  784. #endif
  785. }
  786. /**
  787. \brief Clean & Invalidate D-Cache
  788. \details Cleans and Invalidates D-Cache
  789. \note I-Cache also flush.
  790. */
  791. __STATIC_INLINE void csi_dcache_clean_invalid (void)
  792. {
  793. #if (__DCACHE_PRESENT == 1U)
  794. __DSB();
  795. __ISB();
  796. __DCACHE_CIALL(); /* clean and inv all Cache */
  797. __DSB();
  798. __ISB();
  799. #endif
  800. }
  801. /**
  802. \brief D-Cache Invalidate by address
  803. \details Invalidates D-Cache for the given address
  804. \param[in] addr address (aligned to 32-byte boundary)
  805. \param[in] dsize size of memory block (in number of bytes)
  806. */
  807. __STATIC_INLINE void csi_dcache_invalid_range (uint32_t *addr, int32_t dsize)
  808. {
  809. #if (__DCACHE_PRESENT == 1U)
  810. int32_t op_size = dsize + (uint32_t)addr % 32;
  811. uint32_t op_addr = (uint32_t)addr;
  812. int32_t linesize = 32;
  813. __DSB();
  814. while (op_size > 0) {
  815. __DCACHE_IPA(op_addr);
  816. op_addr += linesize;
  817. op_size -= linesize;
  818. }
  819. __DSB();
  820. __ISB();
  821. #endif
  822. }
  823. /**
  824. \brief D-Cache Clean by address
  825. \details Cleans D-Cache for the given address
  826. \param[in] addr address (aligned to 32-byte boundary)
  827. \param[in] dsize size of memory block (in number of bytes)
  828. */
  829. __STATIC_INLINE void csi_dcache_clean_range (uint32_t *addr, int32_t dsize)
  830. {
  831. #if (__DCACHE_PRESENT == 1)
  832. int32_t op_size = dsize + (uint32_t)addr % 32;
  833. uint32_t op_addr = (uint32_t) addr & CACHE_INV_ADDR_Msk;
  834. int32_t linesize = 32;
  835. __DSB();
  836. while (op_size > 0) {
  837. __DCACHE_CPA(op_addr);
  838. op_addr += linesize;
  839. op_size -= linesize;
  840. }
  841. __DSB();
  842. __ISB();
  843. #endif
  844. }
  845. /**
  846. \brief D-Cache Clean and Invalidate by address
  847. \details Cleans and invalidates D_Cache for the given address
  848. \param[in] addr address (aligned to 32-byte boundary)
  849. \param[in] dsize size of memory block (aligned to 16-byte boundary)
  850. */
  851. __STATIC_INLINE void csi_dcache_clean_invalid_range (uint32_t *addr, int32_t dsize)
  852. {
  853. #if (__DCACHE_PRESENT == 1U)
  854. int32_t op_size = dsize + (uint32_t)addr % 32;
  855. uint32_t op_addr = (uint32_t) addr;
  856. int32_t linesize = 32;
  857. __DSB();
  858. while (op_size > 0) {
  859. __DCACHE_CIPA(op_addr);
  860. op_addr += linesize;
  861. op_size -= linesize;
  862. }
  863. __DSB();
  864. __ISB();
  865. #endif
  866. }
  867. /**
  868. \brief setup cacheable range Cache
  869. \details setup Cache range
  870. */
  871. __STATIC_INLINE void csi_cache_set_range (uint32_t index, uint32_t baseAddr, uint32_t size, uint32_t enable)
  872. {
  873. ;
  874. }
  875. /**
  876. \brief Enable cache profile
  877. \details Turns on Cache profile
  878. */
  879. __STATIC_INLINE void csi_cache_enable_profile (void)
  880. {
  881. ;
  882. }
  883. /**
  884. \brief Disable cache profile
  885. \details Turns off Cache profile
  886. */
  887. __STATIC_INLINE void csi_cache_disable_profile (void)
  888. {
  889. ;
  890. }
  891. /**
  892. \brief Reset cache profile
  893. \details Reset Cache profile
  894. */
  895. __STATIC_INLINE void csi_cache_reset_profile (void)
  896. {
  897. ;
  898. }
  899. /**
  900. \brief cache access times
  901. \details Cache access times
  902. \note every 256 access add 1.
  903. \return cache access times, actual times should be multiplied by 256
  904. */
  905. __STATIC_INLINE uint32_t csi_cache_get_access_time (void)
  906. {
  907. return 0;
  908. }
  909. /**
  910. \brief cache miss times
  911. \details Cache miss times
  912. \note every 256 miss add 1.
  913. \return cache miss times, actual times should be multiplied by 256
  914. */
  915. __STATIC_INLINE uint32_t csi_cache_get_miss_time (void)
  916. {
  917. return 0;
  918. }
  919. /*@} end of CSI_Core_CacheFunctions */
  920. /*@} end of CSI_core_DebugFunctions */
  921. /* ################################## IRQ Functions ############################################ */
  922. /**
  923. \brief Save the Irq context
  924. \details save the psr result before disable irq.
  925. */
  926. __STATIC_INLINE uint32_t csi_irq_save(void)
  927. {
  928. uint32_t result;
  929. result = __get_MSTATUS();
  930. __disable_irq();
  931. return(result);
  932. }
  933. /**
  934. \brief Restore the Irq context
  935. \details restore saved primask state.
  936. \param [in] irq_state psr irq state.
  937. */
  938. __STATIC_INLINE void csi_irq_restore(uint32_t irq_state)
  939. {
  940. __set_MSTATUS(irq_state);
  941. }
  942. /*@} end of IRQ Functions */
  943. #ifdef __cplusplus
  944. }
  945. #endif
  946. #endif /* __CORE_RV32_H_DEPENDANT */
  947. #endif /* __CSI_GENERIC */