123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- /*!
- \file gd32f30x_hw.c
- \brief USB hardware configuration for GD32F30x
- \version 2023-06-30, V2.1.6, firmware for GD32F30x
- */
- /*
- Copyright (c) 2023, GigaDevice Semiconductor Inc.
- Redistribution and use in source and binary forms, with or without modification,
- are permitted provided that the following conditions are met:
- 1. Redistributions of source code must retain the above copyright notice, this
- list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
- 3. Neither the name of the copyright holder nor the names of its contributors
- may be used to endorse or promote products derived from this software without
- specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
- OF SUCH DAMAGE.
- */
- #include "drv_usb_hw.h"
- #define TIM_MSEC_DELAY 0x01U
- #define TIM_USEC_DELAY 0x02U
- __IO uint32_t delay_time = 0U;
- __IO uint16_t timer_prescaler = 9U;
- uint32_t usbfs_prescaler = 0U;
- /* local function prototypes ('static') */
- static void hw_time_set (uint8_t unit);
- static void hw_delay (uint32_t ntime, uint8_t unit);
- /*!
- \brief configure USB clock
- \param[in] none
- \param[out] none
- \retval none
- */
- void usb_rcu_config(void)
- {
- uint32_t system_clock = rcu_clock_freq_get(CK_SYS);
- if (48000000U == system_clock) {
- usbfs_prescaler = RCU_CKUSB_CKPLL_DIV1;
- timer_prescaler = 3U;
- } else if (72000000U == system_clock) {
- usbfs_prescaler = RCU_CKUSB_CKPLL_DIV1_5;
- timer_prescaler = 5U;
- } else if (96000000U == system_clock) {
- usbfs_prescaler = RCU_CKUSB_CKPLL_DIV2;
- timer_prescaler = 7U;
- } else if (120000000U == system_clock) {
- usbfs_prescaler = RCU_CKUSB_CKPLL_DIV2_5;
- timer_prescaler = 9U;
- } else {
- /* reserved */
- }
- rcu_usb_clock_config(usbfs_prescaler);
- rcu_periph_clock_enable(RCU_USBFS);
- }
- /*!
- \brief configure USB interrupt
- \param[in] none
- \param[out] none
- \retval none
- */
- void usb_intr_config(void)
- {
- nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
- nvic_irq_enable((uint8_t)USBFS_IRQn, 2U, 0U);
- #ifdef USB_LOW_POWER
- /* enable the power module clock */
- rcu_periph_clock_enable(RCU_PMU);
- /* USB wakeup EXTI line configuration */
- exti_interrupt_flag_clear(EXTI_18);
- exti_init(EXTI_18, EXTI_INTERRUPT, EXTI_TRIG_RISING);
- exti_interrupt_enable(EXTI_18);
- nvic_irq_enable((uint8_t)USBFS_WKUP_IRQn, 1U, 0U);
- #endif
- }
- /*!
- \brief initializes delay unit using Timer2
- \param[in] none
- \param[out] none
- \retval none
- */
- void usb_timer_init (void)
- {
- /* configure the priority group to 2 bits */
- nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
- /* enable the TIMER2 global interrupt */
- nvic_irq_enable((uint8_t)TIMER2_IRQn, 1U, 0U);
- rcu_periph_clock_enable(RCU_TIMER2);
- }
- /*!
- \brief delay in micro seconds
- \param[in] usec: value of delay required in micro seconds
- \param[out] none
- \retval none
- */
- void usb_udelay (const uint32_t usec)
- {
- hw_delay(usec, TIM_USEC_DELAY);
- }
- /*!
- \brief delay in milli seconds
- \param[in] msec: value of delay required in milli seconds
- \param[out] none
- \retval none
- */
- void usb_mdelay (const uint32_t msec)
- {
- hw_delay(msec, TIM_MSEC_DELAY);
- }
- /*!
- \brief timer base IRQ
- \param[in] none
- \param[out] none
- \retval none
- */
- void usb_timer_irq (void)
- {
- if (RESET != timer_interrupt_flag_get(TIMER2, TIMER_INT_UP)){
- timer_interrupt_flag_clear(TIMER2, TIMER_INT_UP);
- if (delay_time > 0x00U) {
- delay_time--;
- } else {
- timer_disable(TIMER2);
- }
- }
- }
- /*!
- \brief delay routine based on TIMER2
- \param[in] nTime: delay Time
- \param[in] unit: delay Time unit = mili sec / micro sec
- \param[out] none
- \retval none
- */
- static void hw_delay(uint32_t ntime, uint8_t unit)
- {
- delay_time = ntime;
- hw_time_set(unit);
- while (0U != delay_time) {
- }
- timer_disable(TIMER2);
- }
- /*!
- \brief configures TIMER2 for delay routine based on TIMER2
- \param[in] unit: msec /usec
- \param[out] none
- \retval none
- */
- static void hw_time_set(uint8_t unit)
- {
- timer_parameter_struct timer_basestructure;
- timer_disable(TIMER2);
- timer_interrupt_disable(TIMER2, TIMER_INT_UP);
- if (TIM_USEC_DELAY == unit) {
- timer_basestructure.period = 11U;
- } else if(TIM_MSEC_DELAY == unit) {
- timer_basestructure.period = 11999U;
- } else {
- /* no operation */
- }
- timer_basestructure.prescaler = timer_prescaler;
- timer_basestructure.alignedmode = TIMER_COUNTER_EDGE;
- timer_basestructure.counterdirection = TIMER_COUNTER_UP;
- timer_basestructure.clockdivision = TIMER_CKDIV_DIV1;
- timer_basestructure.repetitioncounter = 0U;
- timer_init(TIMER2, &timer_basestructure);
- timer_interrupt_flag_clear(TIMER2, TIMER_INT_UP);
- timer_auto_reload_shadow_enable(TIMER2);
- /* TIMER2 interrupt enable */
- timer_interrupt_enable(TIMER2, TIMER_INT_UP);
- /* TIMER2 enable counter */
- timer_enable(TIMER2);
- }
|