core_rv32.h 34 KB

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