1 /*!
2     \file    drv_usb_core.h
3     \brief   USB core low level driver header file
4 
5     \version 2020-08-04, V1.1.0, firmware for GD32VF103
6 */
7 
8 /*
9     Copyright (c) 2020, GigaDevice Semiconductor Inc.
10 
11     Redistribution and use in source and binary forms, with or without modification,
12 are permitted provided that the following conditions are met:
13 
14     1. Redistributions of source code must retain the above copyright notice, this
15        list of conditions and the following disclaimer.
16     2. Redistributions in binary form must reproduce the above copyright notice,
17        this list of conditions and the following disclaimer in the documentation
18        and/or other materials provided with the distribution.
19     3. Neither the name of the copyright holder nor the names of its contributors
20        may be used to endorse or promote products derived from this software without
21        specific prior written permission.
22 
23     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 OF SUCH DAMAGE.
33 */
34 
35 #ifndef __DRV_USB_CORE_H
36 #define __DRV_USB_CORE_H
37 
38 #include "drv_usb_regs.h"
39 #include "usb_ch9_std.h"
40 
41 #define USB_FS_EP0_MAX_LEN                  64U                         /*!< maximum packet size of endpoint 0 */
42 #define HC_MAX_PACKET_COUNT                 140U                        /*!< maximum packet count */
43 
44 #define EP_ID(x)                            ((uint8_t)((x) & 0x7FU))    /*!< endpoint number */
45 #define EP_DIR(x)                           ((uint8_t)((x) >> 7))       /*!< endpoint direction */
46 
47 enum _usb_mode {
48     DEVICE_MODE = 0U,                                                   /*!< device mode */
49     HOST_MODE,                                                          /*!< host mode */
50     OTG_MODE                                                            /*!< OTG mode */
51 };
52 
53 enum _usb_eptype {
54     USB_EPTYPE_CTRL = 0U,                                               /*!< control endpoint type */
55     USB_EPTYPE_ISOC = 1U,                                               /*!< isochronous endpoint type */
56     USB_EPTYPE_BULK = 2U,                                               /*!< bulk endpoint type */
57     USB_EPTYPE_INTR = 3U,                                               /*!< interrupt endpoint type */
58     USB_EPTYPE_MASK = 3U                                                /*!< endpoint type mask */
59 };
60 
61 typedef enum
62 {
63     USB_OTG_OK = 0U,                                                    /*!< USB OTG status OK*/
64     USB_OTG_FAIL                                                        /*!< USB OTG status fail*/
65 } usb_otg_status;
66 
67 typedef enum
68 {
69     USB_OK = 0U,                                                        /*!< USB status OK*/
70     USB_FAIL                                                            /*!< USB status fail*/
71 } usb_status;
72 
73 typedef enum
74 {
75     USB_USE_FIFO,                                                       /*!< USB use FIFO transfer mode */
76     USB_USE_DMA                                                         /*!< USB use DMA transfer mode */
77 } usb_transfer_mode;
78 
79 typedef struct
80 {
81     uint8_t        core_enum;                                           /*!< USB core type */
82     uint8_t        core_speed;                                          /*!< USB core speed */
83     uint8_t        num_pipe;                                            /*!< USB host channel numbers */
84     uint8_t        num_ep;                                              /*!< USB device endpoint numbers */
85     uint8_t        transfer_mode;                                       /*!< USB transfer mode */
86     uint8_t        phy_itf;                                             /*!< USB core PHY interface */
87     uint8_t        sof_enable;                                          /*!< USB SOF output */
88     uint8_t        low_power;                                           /*!< USB low power */
89     uint8_t        lpm_enable;                                          /*!< USB link power mode(LPM) */
90     uint8_t        vbus_sensing_enable;                                 /*!< USB VBUS sensing feature */
91     uint8_t        use_dedicated_ep1;                                   /*!< USB dedicated endpoint1 interrupt */
92     uint8_t        use_external_vbus;                                   /*!< enable or disable the use of the external VBUS */
93     uint32_t       base_reg;                                            /*!< base register address */
94 } usb_core_basic;
95 
96 /* static inline function definitions */
97 
98 /*!
99     \brief      get the global interrupts
100     \param[in]  usb_regs: pointer to USB core registers
101     \param[out] none
102     \retval     interrupt status
103 */
usb_coreintr_get(usb_core_regs * usb_regs)104 static inline uint32_t usb_coreintr_get(usb_core_regs *usb_regs)
105 {
106     return usb_regs->gr->GINTEN & usb_regs->gr->GINTF;
107 }
108 
109 /*!
110     \brief      set USB RX FIFO size
111     \param[in]  usb_regs: pointer to USB core registers
112     \param[in]  size: assigned FIFO size
113     \param[out] none
114     \retval     none
115 */
usb_set_rxfifo(usb_core_regs * usb_regs,uint16_t size)116 static inline void usb_set_rxfifo(usb_core_regs *usb_regs, uint16_t size)
117 {
118     usb_regs->gr->GRFLEN = size;
119 }
120 
121 /*!
122     \brief      enable the global interrupts
123     \param[in]  usb_regs: pointer to USB core registers
124     \param[out] none
125     \retval     none
126 */
usb_globalint_enable(usb_core_regs * usb_regs)127 static inline void usb_globalint_enable(usb_core_regs *usb_regs)
128 {
129     /* enable USB global interrupt */
130     usb_regs->gr->GAHBCS |= GAHBCS_GINTEN;
131 }
132 
133 /*!
134     \brief      disable the global interrupts
135     \param[in]  usb_regs: pointer to USB core registers
136     \param[out] none
137     \retval     none
138 */
usb_globalint_disable(usb_core_regs * usb_regs)139 static inline void usb_globalint_disable(usb_core_regs *usb_regs)
140 {
141     /* disable USB global interrupt */
142     usb_regs->gr->GAHBCS &= ~GAHBCS_GINTEN;
143 }
144 
145 /* function declarations */
146 /* configure core capabilities */
147 usb_status usb_basic_init (usb_core_basic *usb_basic, usb_core_regs *usb_regs, usb_core_enum usb_core);
148 /* initializes the USB controller registers and prepares the core device mode or host mode operation */
149 usb_status usb_core_init (usb_core_basic usb_basic, usb_core_regs *usb_regs);
150 /* write a packet into the Tx FIFO associated with the endpoint */
151 usb_status usb_txfifo_write (usb_core_regs *usb_regs, uint8_t *src_buf, uint8_t  fifo_num, uint16_t byte_count);
152 /* read a packet from the Rx FIFO associated with the endpoint */
153 void *usb_rxfifo_read (usb_core_regs *usb_regs, uint8_t *dest_buf, uint16_t byte_count);
154 /* flush a Tx FIFO or all Tx FIFOs */
155 usb_status usb_txfifo_flush (usb_core_regs *usb_regs, uint8_t fifo_num);
156 /* flush the entire Rx FIFO */
157 usb_status usb_rxfifo_flush (usb_core_regs *usb_regs);
158 /* set endpoint or channel TX FIFO size */
159 void usb_set_txfifo(usb_core_regs *usb_regs, uint8_t fifo, uint16_t size);
160 /* set USB current mode */
161 void usb_curmode_set(usb_core_regs *usb_regs, uint8_t mode);
162 
163 #endif /* __DRV_USB_CORE_H */
164