e310x.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. use riscv_peripheral::{
  2. aclint::HartIdNumber,
  3. plic::{ContextNumber, InterruptNumber, PriorityNumber},
  4. };
  5. #[repr(u16)]
  6. #[derive(Clone, Copy, Debug, Eq, PartialEq)]
  7. pub enum HartId {
  8. H0 = 0,
  9. }
  10. unsafe impl HartIdNumber for HartId {
  11. const MAX_HART_ID_NUMBER: u16 = 0;
  12. #[inline]
  13. fn number(self) -> u16 {
  14. self as _
  15. }
  16. #[inline]
  17. fn from_number(number: u16) -> Result<Self, u16> {
  18. if number > Self::MAX_HART_ID_NUMBER {
  19. Err(number)
  20. } else {
  21. // SAFETY: valid context number
  22. Ok(unsafe { core::mem::transmute(number) })
  23. }
  24. }
  25. }
  26. unsafe impl ContextNumber for HartId {
  27. const MAX_CONTEXT_NUMBER: u16 = 0;
  28. #[inline]
  29. fn number(self) -> u16 {
  30. self as _
  31. }
  32. #[inline]
  33. fn from_number(number: u16) -> Result<Self, u16> {
  34. if number > Self::MAX_CONTEXT_NUMBER {
  35. Err(number)
  36. } else {
  37. // SAFETY: valid context number
  38. Ok(unsafe { core::mem::transmute(number) })
  39. }
  40. }
  41. }
  42. #[derive(Copy, Clone, Debug, PartialEq, Eq)]
  43. #[repr(u16)]
  44. pub enum Interrupt {
  45. WATCHDOG = 1,
  46. RTC = 2,
  47. UART0 = 3,
  48. UART1 = 4,
  49. QSPI0 = 5,
  50. QSPI1 = 6,
  51. QSPI2 = 7,
  52. GPIO0 = 8,
  53. GPIO1 = 9,
  54. GPIO2 = 10,
  55. GPIO3 = 11,
  56. GPIO4 = 12,
  57. GPIO5 = 13,
  58. GPIO6 = 14,
  59. GPIO7 = 15,
  60. GPIO8 = 16,
  61. GPIO9 = 17,
  62. GPIO10 = 18,
  63. GPIO11 = 19,
  64. GPIO12 = 20,
  65. GPIO13 = 21,
  66. GPIO14 = 22,
  67. GPIO15 = 23,
  68. GPIO16 = 24,
  69. GPIO17 = 25,
  70. GPIO18 = 26,
  71. GPIO19 = 27,
  72. GPIO20 = 28,
  73. GPIO21 = 29,
  74. GPIO22 = 30,
  75. GPIO23 = 31,
  76. GPIO24 = 32,
  77. GPIO25 = 33,
  78. GPIO26 = 34,
  79. GPIO27 = 35,
  80. GPIO28 = 36,
  81. GPIO29 = 37,
  82. GPIO30 = 38,
  83. GPIO31 = 39,
  84. PWM0CMP0 = 40,
  85. PWM0CMP1 = 41,
  86. PWM0CMP2 = 42,
  87. PWM0CMP3 = 43,
  88. PWM1CMP0 = 44,
  89. PWM1CMP1 = 45,
  90. PWM1CMP2 = 46,
  91. PWM1CMP3 = 47,
  92. PWM2CMP0 = 48,
  93. PWM2CMP1 = 49,
  94. PWM2CMP2 = 50,
  95. PWM2CMP3 = 51,
  96. I2C0 = 52,
  97. }
  98. unsafe impl InterruptNumber for Interrupt {
  99. const MAX_INTERRUPT_NUMBER: u16 = 52;
  100. #[inline]
  101. fn number(self) -> u16 {
  102. self as _
  103. }
  104. #[inline]
  105. fn from_number(number: u16) -> Result<Self, u16> {
  106. if number == 0 || number > Self::MAX_INTERRUPT_NUMBER {
  107. Err(number)
  108. } else {
  109. // SAFETY: valid interrupt number
  110. Ok(unsafe { core::mem::transmute(number) })
  111. }
  112. }
  113. }
  114. #[derive(Copy, Clone, Debug, PartialEq, Eq)]
  115. #[repr(u8)]
  116. pub enum Priority {
  117. P0 = 0,
  118. P1 = 1,
  119. P2 = 2,
  120. P3 = 3,
  121. P4 = 4,
  122. P5 = 5,
  123. P6 = 6,
  124. P7 = 7,
  125. }
  126. unsafe impl PriorityNumber for Priority {
  127. const MAX_PRIORITY_NUMBER: u8 = 7;
  128. #[inline]
  129. fn number(self) -> u8 {
  130. self as _
  131. }
  132. #[inline]
  133. fn from_number(number: u8) -> Result<Self, u8> {
  134. if number > Self::MAX_PRIORITY_NUMBER {
  135. Err(number)
  136. } else {
  137. // SAFETY: valid priority number
  138. Ok(unsafe { core::mem::transmute(number) })
  139. }
  140. }
  141. }
  142. riscv_peripheral::clint_codegen!(
  143. base 0x0200_0000,
  144. freq 32_768,
  145. mtimecmps [mtimecmp0=(HartId::H0,"`H0`")],
  146. msips [msip0=(HartId::H0,"`H0`")],
  147. );
  148. fn main() {}