core_riscv.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : core_riscv.c
  3. * Author : WCH
  4. * Version : V1.0.1
  5. * Date : 2023/11/11
  6. * Description : RISC-V V4 Core Peripheral Access Layer Source File for CH32V30x
  7. *********************************************************************************
  8. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  9. * Attention: This software (modified or not) and binary are used for
  10. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  11. *******************************************************************************/
  12. #include <stdint.h>
  13. /* define compiler specific symbols */
  14. #if defined ( __CC_ARM )
  15. #define __ASM __asm /*!< asm keyword for ARM Compiler */
  16. #define __INLINE __inline /*!< inline keyword for ARM Compiler */
  17. #elif defined ( __ICCARM__ )
  18. #define __ASM __asm /*!< asm keyword for IAR Compiler */
  19. #define __INLINE inline /*!< inline keyword for IAR Compiler. Only avaiable in High optimization mode! */
  20. #elif defined ( __GNUC__ )
  21. #define __ASM __asm /*!< asm keyword for GNU Compiler */
  22. #define __INLINE inline /*!< inline keyword for GNU Compiler */
  23. #elif defined ( __TASKING__ )
  24. #define __ASM __asm /*!< asm keyword for TASKING Compiler */
  25. #define __INLINE inline /*!< inline keyword for TASKING Compiler */
  26. #endif
  27. /*********************************************************************
  28. * @fn __get_FFLAGS
  29. *
  30. * @brief Return the Floating-Point Accrued Exceptions
  31. *
  32. * @return fflags value
  33. */
  34. uint32_t __get_FFLAGS(void)
  35. {
  36. uint32_t result;
  37. __ASM volatile ( "csrr %0," "fflags" : "=r" (result) );
  38. return (result);
  39. }
  40. /*********************************************************************
  41. * @fn __set_FFLAGS
  42. *
  43. * @brief Set the Floating-Point Accrued Exceptions
  44. *
  45. * @param value - set FFLAGS value
  46. *
  47. * @return none
  48. */
  49. void __set_FFLAGS(uint32_t value)
  50. {
  51. __ASM volatile ("csrw fflags, %0" : : "r" (value) );
  52. }
  53. /*********************************************************************
  54. * @fn __get_FRM
  55. *
  56. * @brief Return the Floating-Point Dynamic Rounding Mode
  57. *
  58. * @return frm value
  59. */
  60. uint32_t __get_FRM(void)
  61. {
  62. uint32_t result;
  63. __ASM volatile ( "csrr %0," "frm" : "=r" (result) );
  64. return (result);
  65. }
  66. /*********************************************************************
  67. * @fn __set_FRM
  68. *
  69. * @brief Set the Floating-Point Dynamic Rounding Mode
  70. *
  71. * @param value - set frm value
  72. *
  73. * @return none
  74. */
  75. void __set_FRM(uint32_t value)
  76. {
  77. __ASM volatile ("csrw frm, %0" : : "r" (value) );
  78. }
  79. /*********************************************************************
  80. * @fn __get_FCSR
  81. *
  82. * @brief Return the Floating-Point Control and Status Register
  83. *
  84. * @return fcsr value
  85. */
  86. uint32_t __get_FCSR(void)
  87. {
  88. uint32_t result;
  89. __ASM volatile ( "csrr %0," "fcsr" : "=r" (result) );
  90. return (result);
  91. }
  92. /*********************************************************************
  93. * @fn __set_FCSR
  94. *
  95. * @brief Set the Floating-Point Dynamic Rounding Mode
  96. *
  97. * @param value - set fcsr value
  98. *
  99. * @return none
  100. */
  101. void __set_FCSR(uint32_t value)
  102. {
  103. __ASM volatile ("csrw fcsr, %0" : : "r" (value) );
  104. }
  105. /*********************************************************************
  106. * @fn __get_MSTATUS
  107. *
  108. * @brief Return the Machine Status Register
  109. *
  110. * @return mstatus value
  111. */
  112. uint32_t __get_MSTATUS(void)
  113. {
  114. uint32_t result;
  115. __ASM volatile ( "csrr %0," "mstatus" : "=r" (result) );
  116. return (result);
  117. }
  118. /*********************************************************************
  119. * @fn __set_MSTATUS
  120. *
  121. * @brief Set the Machine Status Register
  122. *
  123. * @param value - set mstatus value
  124. *
  125. * @return none
  126. */
  127. void __set_MSTATUS(uint32_t value)
  128. {
  129. __ASM volatile ("csrw mstatus, %0" : : "r" (value) );
  130. }
  131. /*********************************************************************
  132. * @fn __get_MISA
  133. *
  134. * @brief Return the Machine ISA Register
  135. *
  136. * @return misa value
  137. */
  138. uint32_t __get_MISA(void)
  139. {
  140. uint32_t result;
  141. __ASM volatile ( "csrr %0," "misa" : "=r" (result) );
  142. return (result);
  143. }
  144. /*********************************************************************
  145. * @fn __set_MISA
  146. *
  147. * @brief Set the Machine ISA Register
  148. *
  149. * @param value - set misa value
  150. *
  151. * @return none
  152. */
  153. void __set_MISA(uint32_t value)
  154. {
  155. __ASM volatile ("csrw misa, %0" : : "r" (value) );
  156. }
  157. /*********************************************************************
  158. * @fn __get_MTVEC
  159. *
  160. * @brief Return the Machine Trap-Vector Base-Address Register
  161. *
  162. * @return mtvec value
  163. */
  164. uint32_t __get_MTVEC(void)
  165. {
  166. uint32_t result;
  167. __ASM volatile ( "csrr %0," "mtvec" : "=r" (result) );
  168. return (result);
  169. }
  170. /*********************************************************************
  171. * @fn __set_MTVEC
  172. *
  173. * @brief Set the Machine Trap-Vector Base-Address Register
  174. *
  175. * @param value - set mtvec value
  176. *
  177. * @return none
  178. */
  179. void __set_MTVEC(uint32_t value)
  180. {
  181. __ASM volatile ("csrw mtvec, %0" : : "r" (value) );
  182. }
  183. /*********************************************************************
  184. * @fn __get_MSCRATCH
  185. *
  186. * @brief Return the Machine Seratch Register
  187. *
  188. * @return mscratch value
  189. */
  190. uint32_t __get_MSCRATCH(void)
  191. {
  192. uint32_t result;
  193. __ASM volatile ( "csrr %0," "mscratch" : "=r" (result) );
  194. return (result);
  195. }
  196. /*********************************************************************
  197. * @fn __set_MSCRATCH
  198. *
  199. * @brief Set the Machine Seratch Register
  200. *
  201. * @param value - set mscratch value
  202. *
  203. * @return none
  204. */
  205. void __set_MSCRATCH(uint32_t value)
  206. {
  207. __ASM volatile ("csrw mscratch, %0" : : "r" (value) );
  208. }
  209. /*********************************************************************
  210. * @fn __get_MEPC
  211. *
  212. * @brief Return the Machine Exception Program Register
  213. *
  214. * @return mepc value
  215. */
  216. uint32_t __get_MEPC(void)
  217. {
  218. uint32_t result;
  219. __ASM volatile ( "csrr %0," "mepc" : "=r" (result) );
  220. return (result);
  221. }
  222. /*********************************************************************
  223. * @fn __set_MEPC
  224. *
  225. * @brief Set the Machine Exception Program Register
  226. *
  227. * @return mepc value
  228. */
  229. void __set_MEPC(uint32_t value)
  230. {
  231. __ASM volatile ("csrw mepc, %0" : : "r" (value) );
  232. }
  233. /*********************************************************************
  234. * @fn __get_MCAUSE
  235. *
  236. * @brief Return the Machine Cause Register
  237. *
  238. * @return mcause value
  239. */
  240. uint32_t __get_MCAUSE(void)
  241. {
  242. uint32_t result;
  243. __ASM volatile ( "csrr %0," "mcause" : "=r" (result) );
  244. return (result);
  245. }
  246. /*********************************************************************
  247. * @fn __set_MEPC
  248. *
  249. * @brief Set the Machine Cause Register
  250. *
  251. * @return mcause value
  252. */
  253. void __set_MCAUSE(uint32_t value)
  254. {
  255. __ASM volatile ("csrw mcause, %0" : : "r" (value) );
  256. }
  257. /*********************************************************************
  258. * @fn __get_MTVAL
  259. *
  260. * @brief Return the Machine Trap Value Register
  261. *
  262. * @return mtval value
  263. */
  264. uint32_t __get_MTVAL(void)
  265. {
  266. uint32_t result;
  267. __ASM volatile ( "csrr %0," "mtval" : "=r" (result) );
  268. return (result);
  269. }
  270. /*********************************************************************
  271. * @fn __set_MTVAL
  272. *
  273. * @brief Set the Machine Trap Value Register
  274. *
  275. * @return mtval value
  276. */
  277. void __set_MTVAL(uint32_t value)
  278. {
  279. __ASM volatile ("csrw mtval, %0" : : "r" (value) );
  280. }
  281. /*********************************************************************
  282. * @fn __get_MVENDORID
  283. *
  284. * @brief Return Vendor ID Register
  285. *
  286. * @return mvendorid value
  287. */
  288. uint32_t __get_MVENDORID(void)
  289. {
  290. uint32_t result;
  291. __ASM volatile ( "csrr %0," "mvendorid" : "=r" (result) );
  292. return (result);
  293. }
  294. /*********************************************************************
  295. * @fn __get_MARCHID
  296. *
  297. * @brief Return Machine Architecture ID Register
  298. *
  299. * @return marchid value
  300. */
  301. uint32_t __get_MARCHID(void)
  302. {
  303. uint32_t result;
  304. __ASM volatile ( "csrr %0," "marchid" : "=r" (result) );
  305. return (result);
  306. }
  307. /*********************************************************************
  308. * @fn __get_MIMPID
  309. *
  310. * @brief Return Machine Implementation ID Register
  311. *
  312. * @return mimpid value
  313. */
  314. uint32_t __get_MIMPID(void)
  315. {
  316. uint32_t result;
  317. __ASM volatile ( "csrr %0," "mimpid" : "=r" (result) );
  318. return (result);
  319. }
  320. /*********************************************************************
  321. * @fn __get_MHARTID
  322. *
  323. * @brief Return Hart ID Register
  324. *
  325. * @return mhartid value
  326. */
  327. uint32_t __get_MHARTID(void)
  328. {
  329. uint32_t result;
  330. __ASM volatile ( "csrr %0," "mhartid" : "=r" (result) );
  331. return (result);
  332. }
  333. /*********************************************************************
  334. * @fn __get_SP
  335. *
  336. * @brief Return SP Register
  337. *
  338. * @return SP value
  339. */
  340. uint32_t __get_SP(void)
  341. {
  342. uint32_t result;
  343. __ASM volatile ( "mv %0," "sp" : "=r"(result) : );
  344. return (result);
  345. }