[Add] First commit
This commit is contained in:
68
NSPE/Example/USBFS/usb_device/cdc_acm/src/app.c
Normal file
68
NSPE/Example/USBFS/usb_device/cdc_acm/src/app.c
Normal file
@ -0,0 +1,68 @@
|
||||
/*!
|
||||
\file app.c
|
||||
\brief main routine
|
||||
|
||||
\version 2021-10-30, V1.0.0, firmware for GD32W51x
|
||||
*/
|
||||
|
||||
/*
|
||||
Copyright (c) 2021, 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"
|
||||
#include "cdc_acm_core.h"
|
||||
|
||||
usb_core_driver cdc_acm;
|
||||
|
||||
/*!
|
||||
\brief main routine will construct a USB virtual ComPort device
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
usb_gpio_config();
|
||||
|
||||
usb_rcu_config();
|
||||
|
||||
usb_timer_init();
|
||||
|
||||
usbd_init (&cdc_acm, &cdc_desc, &cdc_class);
|
||||
|
||||
usb_intr_config();
|
||||
|
||||
/* main loop */
|
||||
while (1) {
|
||||
if (USBD_CONFIGURED == cdc_acm.dev.cur_status) {
|
||||
if (0U == cdc_acm_check_ready(&cdc_acm)) {
|
||||
cdc_acm_data_receive(&cdc_acm);
|
||||
} else {
|
||||
cdc_acm_data_send(&cdc_acm);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
229
NSPE/Example/USBFS/usb_device/cdc_acm/src/gd32w51x_hw.c
Normal file
229
NSPE/Example/USBFS/usb_device/cdc_acm/src/gd32w51x_hw.c
Normal file
@ -0,0 +1,229 @@
|
||||
/*!
|
||||
\file gd32w51x_hw.c
|
||||
\brief USB hardware configuration for GD32W51x
|
||||
|
||||
\version 2021-10-30, V1.0.0, firmware for GD32W51x
|
||||
*/
|
||||
|
||||
/*
|
||||
Copyright (c) 2021, 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 = 5U;
|
||||
uint32_t usb_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 data line gpio
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void usb_gpio_config(void)
|
||||
{
|
||||
rcu_periph_clock_enable(RCU_SYSCFG);
|
||||
rcu_periph_clock_enable(RCU_GPIOB);
|
||||
|
||||
/* USBFS_DM(PB12) and USBFS_DP(PB13) GPIO pin configuration */
|
||||
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_12 | GPIO_PIN_13);
|
||||
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_166MHZ, GPIO_PIN_12 | GPIO_PIN_13);
|
||||
|
||||
gpio_af_set(GPIOB, GPIO_AF_10, GPIO_PIN_12 | GPIO_PIN_13);
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief configure USB clock
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void usb_rcu_config(void)
|
||||
{
|
||||
rcu_usbfs_clock_config(RCU_USBFSSRC_PLL);
|
||||
|
||||
rcu_usbfs_div_config(RCU_USBFS_DIV7);
|
||||
|
||||
// rcu_usbfs_div_config(RCU_USBFS_DIV5);
|
||||
|
||||
// rcu_plldig_config(RCU_PLLDIG_240M);
|
||||
|
||||
// PMU_CTL1 |= PMU_CTL1_WLPEN;
|
||||
// RCU_APB2EN |= RCU_APB2EN_RFEN;
|
||||
// rcu_bandgap_poweron();
|
||||
// RCU_CTL |= RCU_CTL_PLLDIGPU;
|
||||
// RCU_CTL |= RCU_CTL_PLLDIGEN;
|
||||
|
||||
// rcu_usbfs_clock_config(RCU_USBFSSRC_PLLDIG);
|
||||
|
||||
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);
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
||||
/*!
|
||||
\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 TIM2 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 milliseconds
|
||||
\param[in] msec: value of delay required in milliseconds
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void usb_mdelay (const uint32_t msec)
|
||||
{
|
||||
hw_delay(msec, TIM_MSEC_DELAY);
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief time 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 = milliseconds / microseconds
|
||||
\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 = 6U;
|
||||
} else if(TIM_MSEC_DELAY == unit) {
|
||||
timer_basestructure.period = 6999U;
|
||||
} 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);
|
||||
|
||||
/* TIMER IT enable */
|
||||
timer_interrupt_enable(TIMER2, TIMER_INT_UP);
|
||||
|
||||
/* TIMER2 enable counter */
|
||||
timer_enable(TIMER2);
|
||||
}
|
||||
217
NSPE/Example/USBFS/usb_device/cdc_acm/src/gd32w51x_it.c
Normal file
217
NSPE/Example/USBFS/usb_device/cdc_acm/src/gd32w51x_it.c
Normal file
@ -0,0 +1,217 @@
|
||||
/*!
|
||||
\file gd32w51x_it.c
|
||||
\brief main interrupt service routines
|
||||
|
||||
\version 2021-10-30, V1.0.0, firmware for GD32W51x
|
||||
*/
|
||||
|
||||
/*
|
||||
Copyright (c) 2021, 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 "gd32w51x_it.h"
|
||||
#include "drv_usbd_int.h"
|
||||
|
||||
extern usb_core_driver cdc_acm;
|
||||
extern uint32_t usb_prescaler;
|
||||
|
||||
void usb_timer_irq (void);
|
||||
|
||||
/* local function prototypes ('static') */
|
||||
static void resume_mcu_clk(void);
|
||||
|
||||
/*!
|
||||
\brief this function handles NMI exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void NMI_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles HardFault exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void HardFault_Handler(void)
|
||||
{
|
||||
/* if Hard Fault exception occurs, go to infinite loop */
|
||||
while (1){
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles MemManage exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void MemManage_Handler(void)
|
||||
{
|
||||
/* if Memory Manage exception occurs, go to infinite loop */
|
||||
while (1){
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles BusFault exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void BusFault_Handler(void)
|
||||
{
|
||||
/* if Bus Fault exception occurs, go to infinite loop */
|
||||
while (1){
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles UsageFault exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void UsageFault_Handler(void)
|
||||
{
|
||||
/* if Usage Fault exception occurs, go to infinite loop */
|
||||
while (1){
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles SVC exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void SVC_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles DebugMon exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void DebugMon_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles PendSV exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void PendSV_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles SysTick exception
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void SysTick_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles USBFS interrupt
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void USBFS_IRQHandler (void)
|
||||
{
|
||||
usbd_isr (&cdc_acm);
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles USBFS wakeup interrupt request.
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void USBFS_WKUP_IRQHandler(void)
|
||||
{
|
||||
if (cdc_acm.bp.low_power) {
|
||||
resume_mcu_clk();
|
||||
|
||||
rcu_periph_clock_enable(RCU_USBFS);
|
||||
|
||||
usb_clock_active(&cdc_acm);
|
||||
}
|
||||
|
||||
exti_interrupt_flag_clear(EXTI_18);
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief this function handles Timer2 update interrupt request.
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void TIMER2_IRQHandler(void)
|
||||
{
|
||||
usb_timer_irq();
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief resume mcu clock
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
static void resume_mcu_clk(void)
|
||||
{
|
||||
/* enable HSE */
|
||||
rcu_osci_on(RCU_HXTAL);
|
||||
|
||||
/* wait till HSE is ready */
|
||||
while(RESET == rcu_flag_get(RCU_FLAG_HXTALSTB)){
|
||||
}
|
||||
|
||||
/* enable PLL */
|
||||
rcu_osci_on(RCU_PLL_CK);
|
||||
|
||||
/* wait till PLL is ready */
|
||||
while(RESET == rcu_flag_get(RCU_FLAG_PLLSTB)){
|
||||
}
|
||||
|
||||
/* select PLL as system clock source */
|
||||
rcu_system_clock_source_config(RCU_CKSYSSRC_PLLP);
|
||||
|
||||
/* wait till PLL is used as system clock source */
|
||||
while(RCU_SCSS_PLLP != rcu_system_clock_source_get()){
|
||||
}
|
||||
}
|
||||
1071
NSPE/Example/USBFS/usb_device/cdc_acm/src/system_gd32w51x.c
Normal file
1071
NSPE/Example/USBFS/usb_device/cdc_acm/src/system_gd32w51x.c
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user