gd32f30x_hw.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*!
  2. \file gd32f30x_hw.c
  3. \brief USB hardware configuration for GD32F30x
  4. \version 2023-06-30, V2.1.6, firmware for GD32F30x
  5. */
  6. /*
  7. Copyright (c) 2023, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #include "drv_usb_hw.h"
  30. #define TIM_MSEC_DELAY 0x01U
  31. #define TIM_USEC_DELAY 0x02U
  32. __IO uint32_t delay_time = 0U;
  33. __IO uint16_t timer_prescaler = 9U;
  34. uint32_t usbfs_prescaler = 0U;
  35. /* local function prototypes ('static') */
  36. static void hw_time_set (uint8_t unit);
  37. static void hw_delay (uint32_t ntime, uint8_t unit);
  38. /*!
  39. \brief configure USB clock
  40. \param[in] none
  41. \param[out] none
  42. \retval none
  43. */
  44. void usb_rcu_config(void)
  45. {
  46. uint32_t system_clock = rcu_clock_freq_get(CK_SYS);
  47. if (48000000U == system_clock) {
  48. usbfs_prescaler = RCU_CKUSB_CKPLL_DIV1;
  49. timer_prescaler = 3U;
  50. } else if (72000000U == system_clock) {
  51. usbfs_prescaler = RCU_CKUSB_CKPLL_DIV1_5;
  52. timer_prescaler = 5U;
  53. } else if (96000000U == system_clock) {
  54. usbfs_prescaler = RCU_CKUSB_CKPLL_DIV2;
  55. timer_prescaler = 7U;
  56. } else if (120000000U == system_clock) {
  57. usbfs_prescaler = RCU_CKUSB_CKPLL_DIV2_5;
  58. timer_prescaler = 9U;
  59. } else {
  60. /* reserved */
  61. }
  62. rcu_usb_clock_config(usbfs_prescaler);
  63. rcu_periph_clock_enable(RCU_USBFS);
  64. }
  65. /*!
  66. \brief configure USB interrupt
  67. \param[in] none
  68. \param[out] none
  69. \retval none
  70. */
  71. void usb_intr_config(void)
  72. {
  73. nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
  74. nvic_irq_enable((uint8_t)USBFS_IRQn, 2U, 0U);
  75. #ifdef USB_LOW_POWER
  76. /* enable the power module clock */
  77. rcu_periph_clock_enable(RCU_PMU);
  78. /* USB wakeup EXTI line configuration */
  79. exti_interrupt_flag_clear(EXTI_18);
  80. exti_init(EXTI_18, EXTI_INTERRUPT, EXTI_TRIG_RISING);
  81. exti_interrupt_enable(EXTI_18);
  82. nvic_irq_enable((uint8_t)USBFS_WKUP_IRQn, 1U, 0U);
  83. #endif
  84. }
  85. /*!
  86. \brief initializes delay unit using Timer2
  87. \param[in] none
  88. \param[out] none
  89. \retval none
  90. */
  91. void usb_timer_init (void)
  92. {
  93. /* configure the priority group to 2 bits */
  94. nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
  95. /* enable the TIMER2 global interrupt */
  96. nvic_irq_enable((uint8_t)TIMER2_IRQn, 1U, 0U);
  97. rcu_periph_clock_enable(RCU_TIMER2);
  98. }
  99. /*!
  100. \brief delay in micro seconds
  101. \param[in] usec: value of delay required in micro seconds
  102. \param[out] none
  103. \retval none
  104. */
  105. void usb_udelay (const uint32_t usec)
  106. {
  107. hw_delay(usec, TIM_USEC_DELAY);
  108. }
  109. /*!
  110. \brief delay in milli seconds
  111. \param[in] msec: value of delay required in milli seconds
  112. \param[out] none
  113. \retval none
  114. */
  115. void usb_mdelay (const uint32_t msec)
  116. {
  117. hw_delay(msec, TIM_MSEC_DELAY);
  118. }
  119. /*!
  120. \brief timer base IRQ
  121. \param[in] none
  122. \param[out] none
  123. \retval none
  124. */
  125. void usb_timer_irq (void)
  126. {
  127. if (RESET != timer_interrupt_flag_get(TIMER2, TIMER_INT_UP)){
  128. timer_interrupt_flag_clear(TIMER2, TIMER_INT_UP);
  129. if (delay_time > 0x00U) {
  130. delay_time--;
  131. } else {
  132. timer_disable(TIMER2);
  133. }
  134. }
  135. }
  136. /*!
  137. \brief delay routine based on TIMER2
  138. \param[in] nTime: delay Time
  139. \param[in] unit: delay Time unit = mili sec / micro sec
  140. \param[out] none
  141. \retval none
  142. */
  143. static void hw_delay(uint32_t ntime, uint8_t unit)
  144. {
  145. delay_time = ntime;
  146. hw_time_set(unit);
  147. while (0U != delay_time) {
  148. }
  149. timer_disable(TIMER2);
  150. }
  151. /*!
  152. \brief configures TIMER2 for delay routine based on TIMER2
  153. \param[in] unit: msec /usec
  154. \param[out] none
  155. \retval none
  156. */
  157. static void hw_time_set(uint8_t unit)
  158. {
  159. timer_parameter_struct timer_basestructure;
  160. timer_disable(TIMER2);
  161. timer_interrupt_disable(TIMER2, TIMER_INT_UP);
  162. if (TIM_USEC_DELAY == unit) {
  163. timer_basestructure.period = 11U;
  164. } else if(TIM_MSEC_DELAY == unit) {
  165. timer_basestructure.period = 11999U;
  166. } else {
  167. /* no operation */
  168. }
  169. timer_basestructure.prescaler = timer_prescaler;
  170. timer_basestructure.alignedmode = TIMER_COUNTER_EDGE;
  171. timer_basestructure.counterdirection = TIMER_COUNTER_UP;
  172. timer_basestructure.clockdivision = TIMER_CKDIV_DIV1;
  173. timer_basestructure.repetitioncounter = 0U;
  174. timer_init(TIMER2, &timer_basestructure);
  175. timer_interrupt_flag_clear(TIMER2, TIMER_INT_UP);
  176. timer_auto_reload_shadow_enable(TIMER2);
  177. /* TIMER2 interrupt enable */
  178. timer_interrupt_enable(TIMER2, TIMER_INT_UP);
  179. /* TIMER2 enable counter */
  180. timer_enable(TIMER2);
  181. }