|
| 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 */ |
0 commit comments