Skip to content

Commit 8790803

Browse files
committed
gd32f4xx: add usb hal
gd32 provide a usb implement in hal usb_library directory. Since zephyr use it's own usb stack, this only import low level register wrapper. This is not the final sulution, suggestion are welcomed.
1 parent e5c45f4 commit 8790803

File tree

12 files changed

+4257
-0
lines changed

12 files changed

+4257
-0
lines changed

gd32f4xx/usb_drivers/include/drv_usb_core.h

Lines changed: 358 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/*!
2+
\file drv_usb_dev.h
3+
\brief USB device low level driver header file
4+
5+
\version 2020-08-01, V3.0.0, firmware for GD32F4xx
6+
\version 2022-03-09, V3.1.0, firmware for GD32F4xx
7+
\version 2022-06-30, V3.2.0, firmware for GD32F4xx
8+
*/
9+
10+
/*
11+
Copyright (c) 2022, GigaDevice Semiconductor Inc.
12+
13+
Redistribution and use in source and binary forms, with or without modification,
14+
are permitted provided that the following conditions are met:
15+
16+
1. Redistributions of source code must retain the above copyright notice, this
17+
list of conditions and the following disclaimer.
18+
2. Redistributions in binary form must reproduce the above copyright notice,
19+
this list of conditions and the following disclaimer in the documentation
20+
and/or other materials provided with the distribution.
21+
3. Neither the name of the copyright holder nor the names of its contributors
22+
may be used to endorse or promote products derived from this software without
23+
specific prior written permission.
24+
25+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28+
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30+
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34+
OF SUCH DAMAGE.
35+
*/
36+
37+
#ifndef __DRV_USB_DEV_H
38+
#define __DRV_USB_DEV_H
39+
40+
#include "usbd_conf.h"
41+
#include "drv_usb_core.h"
42+
43+
#define EP_IN(x) ((uint8_t)(0x80U | (x))) /*!< device IN endpoint */
44+
#define EP_OUT(x) ((uint8_t)(x)) /*!< device OUT endpoint */
45+
46+
enum usb_ctl_status {
47+
USB_CTL_IDLE = 0U, /*!< USB control transfer idle state */
48+
USB_CTL_DATA_IN, /*!< USB control transfer data in state */
49+
USB_CTL_LAST_DATA_IN, /*!< USB control transfer last data in state */
50+
USB_CTL_DATA_OUT, /*!< USB control transfer data out state */
51+
USB_CTL_LAST_DATA_OUT, /*!< USB control transfer last data out state */
52+
USB_CTL_STATUS_IN, /*!< USB control transfer status in state*/
53+
USB_CTL_STATUS_OUT /*!< USB control transfer status out state */
54+
};
55+
56+
/* static inline function definitions */
57+
58+
/*!
59+
\brief configure the USB device to be disconnected
60+
\param[in] udev: pointer to USB device
61+
\param[out] none
62+
\retval operation status
63+
*/
64+
__STATIC_INLINE void usb_dev_disconnect (usb_core_driver *udev)
65+
{
66+
udev->regs.dr->DCTL |= DCTL_SD;
67+
}
68+
69+
/*!
70+
\brief configure the USB device to be connected
71+
\param[in] udev: pointer to USB device
72+
\param[out] none
73+
\retval operation status
74+
*/
75+
__STATIC_INLINE void usb_dev_connect (usb_core_driver *udev)
76+
{
77+
udev->regs.dr->DCTL &= ~DCTL_SD;
78+
}
79+
80+
/*!
81+
\brief set the USB device address
82+
\param[in] udev: pointer to USB device
83+
\param[in] dev_addr: device address for setting
84+
\param[out] none
85+
\retval operation status
86+
*/
87+
__STATIC_INLINE void usb_devaddr_set (usb_core_driver *udev, uint8_t dev_addr)
88+
{
89+
udev->regs.dr->DCFG &= ~DCFG_DAR;
90+
udev->regs.dr->DCFG |= (uint32_t)dev_addr << 4U;
91+
}
92+
93+
/*!
94+
\brief read device all OUT endpoint interrupt register
95+
\param[in] udev: pointer to USB device
96+
\param[out] none
97+
\retval interrupt status
98+
*/
99+
__STATIC_INLINE uint32_t usb_oepintnum_read (usb_core_driver *udev)
100+
{
101+
uint32_t value = udev->regs.dr->DAEPINT;
102+
103+
value &= udev->regs.dr->DAEPINTEN;
104+
105+
return (value & DAEPINT_OEPITB) >> 16U;
106+
}
107+
108+
/*!
109+
\brief read device OUT endpoint interrupt flag register
110+
\param[in] udev: pointer to USB device
111+
\param[in] ep_num: endpoint number
112+
\param[out] none
113+
\retval interrupt status
114+
*/
115+
__STATIC_INLINE uint32_t usb_oepintr_read (usb_core_driver *udev, uint8_t ep_num)
116+
{
117+
uint32_t value = udev->regs.er_out[ep_num]->DOEPINTF;
118+
119+
value &= udev->regs.dr->DOEPINTEN;
120+
121+
return value;
122+
}
123+
124+
/*!
125+
\brief read device all IN endpoint interrupt register
126+
\param[in] udev: pointer to USB device
127+
\param[out] none
128+
\retval interrupt status
129+
*/
130+
__STATIC_INLINE uint32_t usb_iepintnum_read (usb_core_driver *udev)
131+
{
132+
uint32_t value = udev->regs.dr->DAEPINT;
133+
134+
value &= udev->regs.dr->DAEPINTEN;
135+
136+
return value & DAEPINT_IEPITB;
137+
}
138+
139+
/*!
140+
\brief set remote wakeup signaling
141+
\param[in] udev: pointer to USB device
142+
\param[out] none
143+
\retval none
144+
*/
145+
__STATIC_INLINE void usb_rwkup_set (usb_core_driver *udev)
146+
{
147+
if (udev->dev.pm.dev_remote_wakeup) {
148+
/* enable remote wakeup signaling */
149+
udev->regs.dr->DCTL |= DCTL_RWKUP;
150+
}
151+
}
152+
153+
/*!
154+
\brief reset remote wakeup signaling
155+
\param[in] udev: pointer to USB device
156+
\param[out] none
157+
\retval none
158+
*/
159+
__STATIC_INLINE void usb_rwkup_reset (usb_core_driver *udev)
160+
{
161+
if (udev->dev.pm.dev_remote_wakeup) {
162+
/* disable remote wakeup signaling */
163+
udev->regs.dr->DCTL &= ~DCTL_RWKUP;
164+
}
165+
}
166+
167+
/* function declarations */
168+
/* initialize USB core registers for device mode */
169+
usb_status usb_devcore_init (usb_core_driver *udev);
170+
/* enable the USB device mode interrupts */
171+
usb_status usb_devint_enable (usb_core_driver *udev);
172+
/* active the USB endpoint 0 transaction */
173+
usb_status usb_transc0_active (usb_core_driver *udev, usb_transc *transc);
174+
/* active the USB transaction */
175+
usb_status usb_transc_active (usb_core_driver *udev, usb_transc *transc);
176+
/* deactivate the USB transaction */
177+
usb_status usb_transc_deactivate (usb_core_driver *udev, usb_transc *transc);
178+
/* configure USB transaction to start IN transfer */
179+
usb_status usb_transc_inxfer (usb_core_driver *udev, usb_transc *transc);
180+
/* configure USB transaction to start OUT transfer */
181+
usb_status usb_transc_outxfer (usb_core_driver *udev, usb_transc *transc);
182+
/* set the USB transaction STALL status */
183+
usb_status usb_transc_stall (usb_core_driver *udev, usb_transc *transc);
184+
/* clear the USB transaction STALL status */
185+
usb_status usb_transc_clrstall (usb_core_driver *udev, usb_transc *transc);
186+
/* read device IN endpoint interrupt flag register */
187+
uint32_t usb_iepintr_read (usb_core_driver *udev, uint8_t ep_num);
188+
/* configures OUT endpoint 0 to receive SETUP packets */
189+
void usb_ctlep_startout (usb_core_driver *udev);
190+
/* active remote wakeup signaling */
191+
void usb_rwkup_active (usb_core_driver *udev);
192+
/* active USB core clock */
193+
void usb_clock_active (usb_core_driver *udev);
194+
/* USB device suspend */
195+
void usb_dev_suspend (usb_core_driver *udev);
196+
/* stop the device and clean up FIFOs */
197+
void usb_dev_stop (usb_core_driver *udev);
198+
199+
#endif /* __DRV_USB_DEV_H */
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*!
2+
\file drv_usb_host.h
3+
\brief USB host mode low level driver header file
4+
5+
\version 2020-08-01, V3.0.0, firmware for GD32F4xx
6+
\version 2022-03-09, V3.1.0, firmware for GD32F4xx
7+
\version 2022-06-30, V3.2.0, firmware for GD32F4xx
8+
*/
9+
10+
/*
11+
Copyright (c) 2022, GigaDevice Semiconductor Inc.
12+
13+
Redistribution and use in source and binary forms, with or without modification,
14+
are permitted provided that the following conditions are met:
15+
16+
1. Redistributions of source code must retain the above copyright notice, this
17+
list of conditions and the following disclaimer.
18+
2. Redistributions in binary form must reproduce the above copyright notice,
19+
this list of conditions and the following disclaimer in the documentation
20+
and/or other materials provided with the distribution.
21+
3. Neither the name of the copyright holder nor the names of its contributors
22+
may be used to endorse or promote products derived from this software without
23+
specific prior written permission.
24+
25+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28+
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30+
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34+
OF SUCH DAMAGE.
35+
*/
36+
37+
#ifndef __DRV_USB_HOST_H
38+
#define __DRV_USB_HOST_H
39+
40+
#include "drv_usb_regs.h"
41+
#include "usb_ch9_std.h"
42+
#include "drv_usb_core.h"
43+
44+
typedef enum _usb_pipe_mode
45+
{
46+
PIPE_PERIOD = 0U,
47+
PIPE_NON_PERIOD = 1U
48+
} usb_pipe_mode;
49+
50+
/*!
51+
\brief get USB even frame
52+
\param[in] udev: pointer to USB device
53+
\param[out] none
54+
\retval none
55+
*/
56+
__STATIC_INLINE uint8_t usb_frame_even (usb_core_driver *udev)
57+
{
58+
return (uint8_t)!(udev->regs.hr->HFINFR & 0x01U);
59+
}
60+
61+
/*!
62+
\brief configure USB clock of PHY
63+
\param[in] udev: pointer to USB device
64+
\param[in] clock: PHY clock
65+
\param[out] none
66+
\retval none
67+
*/
68+
__STATIC_INLINE void usb_phyclock_config (usb_core_driver *udev, uint8_t clock)
69+
{
70+
udev->regs.hr->HCTL &= ~HCTL_CLKSEL;
71+
udev->regs.hr->HCTL |= clock;
72+
}
73+
74+
/*!
75+
\brief read USB port
76+
\param[in] udev: pointer to USB device
77+
\param[out] none
78+
\retval port status
79+
*/
80+
__STATIC_INLINE uint32_t usb_port_read (usb_core_driver *udev)
81+
{
82+
return *udev->regs.HPCS & ~(HPCS_PE | HPCS_PCD | HPCS_PEDC);
83+
}
84+
85+
/*!
86+
\brief get USB current speed
87+
\param[in] udev: pointer to USB device
88+
\param[out] none
89+
\retval USB current speed
90+
*/
91+
__STATIC_INLINE uint32_t usb_curspeed_get (usb_core_driver *udev)
92+
{
93+
return *udev->regs.HPCS & HPCS_PS;
94+
}
95+
96+
/*!
97+
\brief get USB current frame
98+
\param[in] udev: pointer to USB device
99+
\param[out] none
100+
\retval USB current frame
101+
*/
102+
__STATIC_INLINE uint32_t usb_curframe_get (usb_core_driver *udev)
103+
{
104+
return (udev->regs.hr->HFINFR & 0xFFFFU);
105+
}
106+
107+
/* function declarations */
108+
/* initializes USB core for host mode */
109+
usb_status usb_host_init (usb_core_driver *udev);
110+
/* control the VBUS to power */
111+
void usb_portvbus_switch (usb_core_driver *udev, uint8_t state);
112+
/* reset host port */
113+
uint32_t usb_port_reset (usb_core_driver *udev);
114+
/* initialize host pipe */
115+
usb_status usb_pipe_init (usb_core_driver *udev, uint8_t pipe_num);
116+
/* prepare host pipe for transferring packets */
117+
usb_status usb_pipe_xfer (usb_core_driver *udev, uint8_t pipe_num);
118+
/* halt host pipe */
119+
usb_status usb_pipe_halt (usb_core_driver *udev, uint8_t pipe_num);
120+
/* configure host pipe to do ping operation */
121+
usb_status usb_pipe_ping (usb_core_driver *udev, uint8_t pipe_num);
122+
/* stop the USB host and clean up FIFO */
123+
void usb_host_stop (usb_core_driver *udev);
124+
125+
#endif /* __DRV_USB_HOST_H */
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*!
2+
\file drv_usb_hw.h
3+
\brief usb hardware configuration header file
4+
5+
\version 2020-08-01, V3.0.0, firmware for GD32F4xx
6+
\version 2022-03-09, V3.1.0, firmware for GD32F4xx
7+
\version 2022-06-30, V3.2.0, firmware for GD32F4xx
8+
*/
9+
10+
/*
11+
Copyright (c) 2022, GigaDevice Semiconductor Inc.
12+
13+
Redistribution and use in source and binary forms, with or without modification,
14+
are permitted provided that the following conditions are met:
15+
16+
1. Redistributions of source code must retain the above copyright notice, this
17+
list of conditions and the following disclaimer.
18+
2. Redistributions in binary form must reproduce the above copyright notice,
19+
this list of conditions and the following disclaimer in the documentation
20+
and/or other materials provided with the distribution.
21+
3. Neither the name of the copyright holder nor the names of its contributors
22+
may be used to endorse or promote products derived from this software without
23+
specific prior written permission.
24+
25+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28+
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30+
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34+
OF SUCH DAMAGE.
35+
*/
36+
37+
#ifndef __DRV_USB_HW_H
38+
#define __DRV_USB_HW_H
39+
40+
#include "usb_conf.h"
41+
42+
/* function declarations */
43+
/* configure USB clock */
44+
void usb_rcu_config (void);
45+
/* configure USB data line gpio */
46+
void usb_gpio_config (void);
47+
/* configure USB interrupt */
48+
void usb_intr_config (void);
49+
/* initializes delay unit using Timer2 */
50+
void usb_timer_init (void);
51+
/* delay in micro seconds */
52+
void usb_udelay (const uint32_t usec);
53+
/* delay in milliseconds */
54+
void usb_mdelay (const uint32_t msec);
55+
/* configures system clock after wakeup from STOP mode */
56+
void system_clk_config_stop(void);
57+
#ifdef USE_HOST_MODE
58+
/* configure USB VBus */
59+
void usb_vbus_config (void);
60+
/* drive USB VBus */
61+
void usb_vbus_drive (uint8_t State);
62+
#endif /* USE_HOST_MODE */
63+
64+
#endif /* __DRV_USB_HW_H */

0 commit comments

Comments
 (0)