1 /*!
2     \file    drv_usb_host.h
3     \brief   USB host mode 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_HOST_H
36 #define __DRV_USB_HOST_H
37 
38 #include "drv_usb_regs.h"
39 #include "usb_ch9_std.h"
40 #include "drv_usb_core.h"
41 
42 typedef enum _usb_pipe_status
43 {
44     PIPE_IDLE = 0U,
45     PIPE_XF,
46     PIPE_HALTED,
47     PIPE_NAK,
48     PIPE_NYET,
49     PIPE_STALL,
50     PIPE_TRACERR,
51     PIPE_BBERR,
52     PIPE_REQOVR,
53     PIPE_DTGERR,
54 } usb_pipe_staus;
55 
56 typedef enum _usb_pipe_mode
57 {
58     PIPE_PERIOD     = 0U,
59     PIPE_NON_PERIOD = 1U
60 } usb_pipe_mode;
61 
62 typedef enum _usb_urb_state
63 {
64     URB_IDLE = 0U,
65     URB_DONE,
66     URB_NOTREADY,
67     URB_ERROR,
68     URB_STALL,
69     URB_PING
70 } usb_urb_state;
71 
72 typedef struct _usb_pipe
73 {
74     uint8_t              in_used;
75     uint8_t              dev_addr;
76     uint32_t             dev_speed;
77 
78     struct {
79         uint8_t          num;
80         uint8_t          dir;
81         uint8_t          type;
82         uint16_t         mps;
83     } ep;
84 
85     uint8_t              ping;
86     uint32_t             DPID;
87 
88     uint8_t             *xfer_buf;
89     uint32_t             xfer_len;
90     uint32_t             xfer_count;
91 
92     uint8_t              data_toggle_in;
93     uint8_t              data_toggle_out;
94 
95     __IO uint32_t        err_count;
96     __IO usb_pipe_staus  pp_status;
97     __IO usb_urb_state   urb_state;
98 } usb_pipe;
99 
100 
101 typedef struct _usb_host_drv
102 {
103     __IO uint32_t            connect_status;
104     __IO uint32_t            port_enabled;
105     __IO uint32_t            backup_xfercount[USBFS_MAX_TX_FIFOS];
106 
107     usb_pipe                 pipe[USBFS_MAX_TX_FIFOS];
108     void                    *data;
109 } usb_host_drv;
110 
111 typedef struct _usb_core_driver
112 {
113     usb_core_basic       bp;
114     usb_core_regs        regs;
115     usb_host_drv         host;
116 } usb_core_driver;
117 
118 /*!
119     \brief      get USB even frame
120     \param[in]  pudev: pointer to USB device
121     \param[out] none
122     \retval     none
123 */
usb_frame_even(usb_core_driver * pudev)124 static inline uint8_t usb_frame_even (usb_core_driver *pudev)
125 {
126     return (uint8_t)!(pudev->regs.hr->HFINFR & 0x01U);
127 }
128 
129 /*!
130     \brief      configure USB clock of PHY
131     \param[in]  pudev: pointer to USB device
132     \param[in]  clock: PHY clock
133     \param[out] none
134     \retval     none
135 */
usb_phyclock_config(usb_core_driver * pudev,uint8_t clock)136 static inline void usb_phyclock_config (usb_core_driver *pudev, uint8_t clock)
137 {
138     pudev->regs.hr->HCTL &= ~HCTL_CLKSEL;
139     pudev->regs.hr->HCTL |= clock;
140 }
141 
142 /*!
143     \brief      read USB port
144     \param[in]  pudev: pointer to USB device
145     \param[out] none
146     \retval     port status
147 */
usb_port_read(usb_core_driver * pudev)148 static inline uint32_t usb_port_read (usb_core_driver *pudev)
149 {
150     return *pudev->regs.HPCS & ~(HPCS_PE | HPCS_PCD | HPCS_PEDC);
151 }
152 
153 /*!
154     \brief      get USB current speed
155     \param[in]  pudev: pointer to USB device
156     \param[out] none
157     \retval     USB current speed
158 */
usb_curspeed_get(usb_core_driver * pudev)159 static inline uint32_t usb_curspeed_get (usb_core_driver *pudev)
160 {
161     return *pudev->regs.HPCS & HPCS_PS;
162 }
163 
164 /*!
165     \brief      get USB current frame
166     \param[in]  pudev: pointer to USB device
167     \param[out] none
168     \retval     USB current frame
169 */
usb_curframe_get(usb_core_driver * pudev)170 static inline uint32_t usb_curframe_get (usb_core_driver *pudev)
171 {
172     return (pudev->regs.hr->HFINFR & 0xFFFFU);
173 }
174 
175 /* function declarations */
176 /* initializes USB core for host mode */
177 usb_status usb_host_init (usb_core_driver *pudev);
178 /* control the VBUS to power */
179 void usb_portvbus_switch (usb_core_driver *pudev, uint8_t state);
180 /* reset host port */
181 uint32_t usb_port_reset (usb_core_driver *pudev);
182 /* initialize host pipe */
183 usb_status usb_pipe_init (usb_core_driver *pudev, uint8_t pipe_num);
184 /* prepare host pipe for transferring packets */
185 usb_status usb_pipe_xfer (usb_core_driver *pudev, uint8_t pipe_num);
186 /* halt host pipe */
187 usb_status usb_pipe_halt (usb_core_driver *pudev, uint8_t pipe_num);
188 /* configure host pipe to do ping operation */
189 usb_status usb_pipe_ping (usb_core_driver *pudev, uint8_t pipe_num);
190 /* stop the USB host and clean up FIFO */
191 void usb_host_stop (usb_core_driver *pudev);
192 
193 #endif /* __DRV_USB_HOST_H */
194