core_ck802.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-01-01 Urey first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <stdint.h>
  13. #include <core_ck802.h>
  14. /* flag in interrupt handling */
  15. rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
  16. rt_uint32_t rt_thread_switch_interrupt_flag;
  17. /*******************************************************************************
  18. * Hardware Abstraction Layer
  19. Core Function Interface contains:
  20. - Core VIC Functions
  21. - Core CORET Functions
  22. - Core Register Access Functions
  23. ******************************************************************************/
  24. /**
  25. \defgroup CSI_Core_FunctionInterface Functions and Instructions Reference
  26. */
  27. /* ########################## NVIC functions #################################### */
  28. /**
  29. \ingroup CSI_Core_FunctionInterface
  30. \defgroup CSI_Core_NVICFunctions NVIC Functions
  31. \brief Functions that manage interrupts and exceptions via the NVIC.
  32. @{
  33. */
  34. /* Interrupt Priorities are WORD accessible only under CSKYv6M */
  35. /* The following MACROS handle generation of the register offset and byte masks */
  36. #define _BIT_SHIFT(IRQn) ( ((((uint32_t)(int32_t)(IRQn)) ) & 0x03UL) * 8UL)
  37. #define _IP_IDX(IRQn) ( (((uint32_t)(int32_t)(IRQn)) >> 2UL) )
  38. static uint32_t s_nvic_prio_bits = __NVIC_PRIO_BITS;
  39. /**
  40. \brief initialize the NVIC interrupt controller
  41. \param [in] prio_bits the priority bits of NVIC interrupt controller.
  42. */
  43. void drv_nvic_init(uint32_t prio_bits)
  44. {
  45. if (s_nvic_prio_bits >= 8U)
  46. {
  47. return;
  48. }
  49. s_nvic_prio_bits = prio_bits;
  50. }
  51. /**
  52. \brief Enable External Interrupt
  53. \details Enables a device-specific interrupt in the NVIC interrupt controller.
  54. \param [in] IRQn External interrupt number. Value cannot be negative.
  55. */
  56. void drv_nvic_enable_irq(int32_t IRQn)
  57. {
  58. NVIC->ISER[0U] = (uint32_t)(1UL << (((uint32_t)(int32_t)IRQn) & 0x1FUL));
  59. #ifdef CONFIG_SYSTEM_SECURE
  60. NVIC->ISSR[0U] = (uint32_t)(1UL << (((uint32_t)(int32_t)IRQn) & 0x1FUL));
  61. #endif
  62. }
  63. /**
  64. \brief Disable External Interrupt
  65. \details Disables a device-specific interrupt in the NVIC interrupt controller.
  66. \param [in] IRQn External interrupt number. Value cannot be negative.
  67. */
  68. void drv_nvic_disable_irq(int32_t IRQn)
  69. {
  70. NVIC->ICER[0U] = (uint32_t)(1UL << (((uint32_t)(int32_t)IRQn) & 0x1FUL));
  71. }
  72. /**
  73. \brief Enable External Secure Interrupt
  74. \details Enables a secure device-specific interrupt in the NVIC interrupt controller.
  75. \param [in] IRQn External interrupt number. Value cannot be negative.
  76. */
  77. void drv_nvic_enable_sirq(int32_t IRQn)
  78. {
  79. NVIC->ISSR[0U] = (uint32_t)(1UL << (((uint32_t)(int32_t)IRQn) & 0x1FUL));
  80. }
  81. /**
  82. \brief Get Pending Interrupt
  83. \details Reads the pending register in the NVIC and returns the pending bit for the specified interrupt.
  84. \param [in] IRQn Interrupt number.
  85. \return 0 Interrupt status is not pending.
  86. \return 1 Interrupt status is pending.
  87. */
  88. uint32_t drv_nvic_get_pending_irq(int32_t IRQn)
  89. {
  90. return ((uint32_t)(((NVIC->ISPR[0U] & (1UL << (((uint32_t)(int32_t)IRQn) & 0x1FUL))) != 0UL) ? 1UL : 0UL));
  91. }
  92. /**
  93. \brief Set Pending Interrupt
  94. \details Sets the pending bit of an external interrupt.
  95. \param [in] IRQn Interrupt number. Value cannot be negative.
  96. */
  97. void drv_nvic_set_pending_irq(int32_t IRQn)
  98. {
  99. NVIC->ISPR[0U] = (uint32_t)(1UL << (((uint32_t)(int32_t)IRQn) & 0x1FUL));
  100. }
  101. /**
  102. \brief Clear Pending Interrupt
  103. \details Clears the pending bit of an external interrupt.
  104. \param [in] IRQn External interrupt number. Value cannot be negative.
  105. */
  106. void drv_nvic_clear_pending_irq(int32_t IRQn)
  107. {
  108. NVIC->ICPR[0U] = (uint32_t)(1UL << (((uint32_t)(int32_t)IRQn) & 0x1FUL));
  109. }
  110. /**
  111. \brief Get Wake up Interrupt
  112. \details Reads the wake up register in the NVIC and returns the pending bit for the specified interrupt.
  113. \param [in] IRQn Interrupt number.
  114. \return 0 Interrupt is not set as wake up interrupt.
  115. \return 1 Interrupt is set as wake up interrupt.
  116. */
  117. uint32_t drv_nvic_get_wakeup_irq(int32_t IRQn)
  118. {
  119. return ((uint32_t)(((NVIC->IWER[0U] & (1UL << (((uint32_t)(int32_t)IRQn) & 0x1FUL))) != 0UL) ? 1UL : 0UL));
  120. }
  121. /**
  122. \brief Set Wake up Interrupt
  123. \details Sets the wake up bit of an external interrupt.
  124. \param [in] IRQn Interrupt number. Value cannot be negative.
  125. */
  126. void drv_nvic_set_wakeup_irq(int32_t IRQn)
  127. {
  128. NVIC->IWER[0U] = (uint32_t)(1UL << (((uint32_t)(int32_t)IRQn) & 0x1FUL));
  129. }
  130. /**
  131. \brief Clear Wake up Interrupt
  132. \details Clears the wake up bit of an external interrupt.
  133. \param [in] IRQn External interrupt number. Value cannot be negative.
  134. */
  135. void drv_nvic_clear_wakeup_irq(int32_t IRQn)
  136. {
  137. NVIC->IWDR[0U] = (uint32_t)(1UL << (((uint32_t)(int32_t)IRQn) & 0x1FUL));
  138. }
  139. /**
  140. \brief Get Active Interrupt
  141. \details Reads the active register in the NVIC and returns the active bit for the device specific interrupt.
  142. \param [in] IRQn Device specific interrupt number.
  143. \return 0 Interrupt status is not active.
  144. \return 1 Interrupt status is active.
  145. \note IRQn must not be negative.
  146. */
  147. uint32_t drv_nvic_get_active(int32_t IRQn)
  148. {
  149. return ((uint32_t)(((NVIC->IABR[0] & (1UL << (((uint32_t)(int32_t)IRQn) & 0x1FUL))) != 0UL) ? 1UL : 0UL));
  150. }
  151. /**
  152. \brief Set Threshold register
  153. \details set the threshold register in the NVIC.
  154. \param [in] VectThreshold specific vecter threshold.
  155. \param [in] PrioThreshold specific priority threshold.
  156. */
  157. void drv_nvic_set_threshold(uint32_t VectThreshold, uint32_t PrioThreshold)
  158. {
  159. NVIC->IPTR = 0x80000000 | (((VectThreshold + 32) & 0xFF) << 8) | ((PrioThreshold & 0x3) << 6);
  160. }
  161. /**
  162. \brief Set Interrupt Priority
  163. \details Sets the priority of an interrupt.
  164. \note The priority cannot be set for every core interrupt.
  165. \param [in] IRQn Interrupt number.
  166. \param [in] priority Priority to set.
  167. */
  168. void drv_nvic_set_prio(int32_t IRQn, uint32_t priority)
  169. {
  170. NVIC->IPR[_IP_IDX(IRQn)] = ((uint32_t)(NVIC->IPR[_IP_IDX(IRQn)] & ~(0xFFUL << _BIT_SHIFT(IRQn))) |
  171. (((priority << (8U - s_nvic_prio_bits)) & (uint32_t)0xFFUL) << _BIT_SHIFT(IRQn)));
  172. }
  173. /**
  174. \brief Get Interrupt Priority
  175. \details Reads the priority of an interrupt.
  176. The interrupt number can be positive to specify an external (device specific) interrupt,
  177. or negative to specify an internal (core) interrupt.
  178. \param [in] IRQn Interrupt number.
  179. \return Interrupt Priority.
  180. Value is aligned automatically to the implemented priority bits of the microcontroller.
  181. */
  182. uint32_t drv_nvic_get_prio(int32_t IRQn)
  183. {
  184. return ((uint32_t)(((NVIC->IPR[ _IP_IDX(IRQn)] >> _BIT_SHIFT(IRQn)) & (uint32_t)0xFFUL) >> (8U - s_nvic_prio_bits)));
  185. }
  186. /*@} end of CSI_Core_NVICFunctions */
  187. /* ################################## SysTick function ############################################ */
  188. /**
  189. \ingroup CSI_Core_FunctionInterface
  190. \defgroup CSI_Core_SysTickFunctions SysTick Functions
  191. \brief Functions that configure the System.
  192. @{
  193. */
  194. /**
  195. \brief CORE timer Configuration
  196. \details Initializes the System Timer and its interrupt, and starts the System Tick Timer.
  197. Counter is in free running mode to generate periodic interrupts.
  198. \param [in] ticks Number of ticks between two interrupts.
  199. \param [in] IRQn core timer Interrupt number.
  200. \return 0 Function succeeded.
  201. \return 1 Function failed.
  202. \note When the variable <b>__Vendor_SysTickConfig</b> is set to 1, then the
  203. function <b>SysTick_Config</b> is not included. In this case, the file <b><i>device</i>.h</b>
  204. must contain a vendor-specific implementation of this function.
  205. */
  206. uint32_t drv_coret_config(uint32_t ticks, int32_t IRQn)
  207. {
  208. if ((ticks - 1UL) > CORET_LOAD_RELOAD_Msk)
  209. {
  210. return (1UL); /* Reload value impossible */
  211. }
  212. CORET->LOAD = (uint32_t)(ticks - 1UL); /* set reload register */
  213. drv_nvic_set_prio(IRQn, (1UL << s_nvic_prio_bits) - 1UL); /* set Priority for Systick Interrupt */
  214. CORET->VAL = 0UL; /* Load the CORET Counter Value */
  215. CORET->CTRL = CORET_CTRL_CLKSOURCE_Msk |
  216. CORET_CTRL_TICKINT_Msk |
  217. CORET_CTRL_ENABLE_Msk; /* Enable CORET IRQ and CORET Timer */
  218. return (0UL); /* Function successful */
  219. }
  220. /**
  221. \brief get CORE timer reload value
  222. \return CORE timer counter value.
  223. */
  224. uint32_t drv_coret_get_load(void)
  225. {
  226. return CORET->LOAD;
  227. }
  228. /**
  229. \brief get CORE timer counter value
  230. \return CORE timer counter value.
  231. */
  232. uint32_t drv_coret_get_value(void)
  233. {
  234. return CORET->VAL;
  235. }
  236. /*@} end of CSI_Core_SysTickFunctions */
  237. #if 0
  238. /* ##################################### DCC function ########################################### */
  239. /**
  240. \ingroup CSI_Core_FunctionInterface
  241. \defgroup CSI_core_DebugFunctions HAD Functions
  242. \brief Functions that access the HAD debug interface.
  243. @{
  244. */
  245. /**
  246. \brief HAD Send Character
  247. \details Transmits a character via the HAD channel 0, and
  248. \li Just returns when no debugger is connected that has booked the output.
  249. \li Is blocking when a debugger is connected, but the previous character sent has not been transmitted.
  250. \param [in] ch Character to transmit.
  251. \returns Character to transmit.
  252. */
  253. uint32_t HAD_SendChar(uint32_t ch)
  254. {
  255. DCC->DERJR = (uint8_t)ch;
  256. return (ch);
  257. }
  258. /**
  259. \brief HAD Receive Character
  260. \details Inputs a character via the external variable \ref HAD_RxBuffer.
  261. \return Received character.
  262. \return -1 No character pending.
  263. */
  264. int32_t HAD_ReceiveChar(void)
  265. {
  266. int32_t ch = -1; /* no character available */
  267. if (_FLD2VAL(DCC_EHSR_JW, DCC->EHSR))
  268. {
  269. ch = DCC->DERJW;
  270. }
  271. return (ch);
  272. }
  273. /**
  274. \brief HAD Check Character
  275. \details Checks whether a character is pending for reading in the variable \ref HAD_RxBuffer.
  276. \return 0 No character available.
  277. \return 1 Character available.
  278. */
  279. int32_t HAD_CheckChar(void)
  280. {
  281. return _FLD2VAL(DCC_EHSR_JW, DCC->EHSR); /* no character available */
  282. }
  283. #endif