Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat Jun 03 00:22:44 2017 +0000
Revision:
46:b156ef445742
Parent:
18:6a4db94011d3
Final code for internal battlebot competition.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sahilmgandhi 18:6a4db94011d3 1 /**************************************************************************//**
sahilmgandhi 18:6a4db94011d3 2 * @file usbd.h
sahilmgandhi 18:6a4db94011d3 3 * @version V1.00
sahilmgandhi 18:6a4db94011d3 4 * $Revision: 21 $
sahilmgandhi 18:6a4db94011d3 5 * $Date: 14/10/06 1:29p $
sahilmgandhi 18:6a4db94011d3 6 * @brief NUC472/NUC442 USBD driver header file
sahilmgandhi 18:6a4db94011d3 7 *
sahilmgandhi 18:6a4db94011d3 8 * @note
sahilmgandhi 18:6a4db94011d3 9 * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
sahilmgandhi 18:6a4db94011d3 10 *****************************************************************************/
sahilmgandhi 18:6a4db94011d3 11 #ifndef __USBD_H__
sahilmgandhi 18:6a4db94011d3 12 #define __USBD_H__
sahilmgandhi 18:6a4db94011d3 13
sahilmgandhi 18:6a4db94011d3 14 #ifdef __cplusplus
sahilmgandhi 18:6a4db94011d3 15 extern "C"
sahilmgandhi 18:6a4db94011d3 16 {
sahilmgandhi 18:6a4db94011d3 17 #endif
sahilmgandhi 18:6a4db94011d3 18
sahilmgandhi 18:6a4db94011d3 19
sahilmgandhi 18:6a4db94011d3 20 /** @addtogroup NUC472_442_Device_Driver NUC472/NUC442 Device Driver
sahilmgandhi 18:6a4db94011d3 21 @{
sahilmgandhi 18:6a4db94011d3 22 */
sahilmgandhi 18:6a4db94011d3 23
sahilmgandhi 18:6a4db94011d3 24 /** @addtogroup NUC472_442_USBD_Driver USBD Driver
sahilmgandhi 18:6a4db94011d3 25 @{
sahilmgandhi 18:6a4db94011d3 26 */
sahilmgandhi 18:6a4db94011d3 27
sahilmgandhi 18:6a4db94011d3 28 /** @addtogroup NUC472_442_USBD_EXPORTED_CONSTANTS USBD Exported Constants
sahilmgandhi 18:6a4db94011d3 29 @{
sahilmgandhi 18:6a4db94011d3 30 */
sahilmgandhi 18:6a4db94011d3 31 /// @cond HIDDEN_SYMBOLS
sahilmgandhi 18:6a4db94011d3 32 #define USBD_MAX_EP 12
sahilmgandhi 18:6a4db94011d3 33
sahilmgandhi 18:6a4db94011d3 34 #define Maximum(a,b) (a)>(b) ? (a) : (b)
sahilmgandhi 18:6a4db94011d3 35 #define Minimum(a,b) (a)<(b) ? (a) : (b)
sahilmgandhi 18:6a4db94011d3 36
sahilmgandhi 18:6a4db94011d3 37
sahilmgandhi 18:6a4db94011d3 38 #define CEP 0xff /*!< Control Endpoint \hideinitializer */
sahilmgandhi 18:6a4db94011d3 39 #define EPA 0 /*!< Endpoint A \hideinitializer */
sahilmgandhi 18:6a4db94011d3 40 #define EPB 1 /*!< Endpoint B \hideinitializer */
sahilmgandhi 18:6a4db94011d3 41 #define EPC 2 /*!< Endpoint C \hideinitializer */
sahilmgandhi 18:6a4db94011d3 42 #define EPD 3 /*!< Endpoint D \hideinitializer */
sahilmgandhi 18:6a4db94011d3 43 #define EPE 4 /*!< Endpoint E \hideinitializer */
sahilmgandhi 18:6a4db94011d3 44 #define EPF 5 /*!< Endpoint F \hideinitializer */
sahilmgandhi 18:6a4db94011d3 45 #define EPG 6 /*!< Endpoint G \hideinitializer */
sahilmgandhi 18:6a4db94011d3 46 #define EPH 7 /*!< Endpoint H \hideinitializer */
sahilmgandhi 18:6a4db94011d3 47 #define EPI 8 /*!< Endpoint I \hideinitializer */
sahilmgandhi 18:6a4db94011d3 48 #define EPJ 9 /*!< Endpoint J \hideinitializer */
sahilmgandhi 18:6a4db94011d3 49 #define EPK 10 /*!< Endpoint K \hideinitializer */
sahilmgandhi 18:6a4db94011d3 50 #define EPL 11 /*!< Endpoint L \hideinitializer */
sahilmgandhi 18:6a4db94011d3 51
sahilmgandhi 18:6a4db94011d3 52 /* USB Request Type */
sahilmgandhi 18:6a4db94011d3 53 #define REQ_STANDARD 0x00
sahilmgandhi 18:6a4db94011d3 54 #define REQ_CLASS 0x20
sahilmgandhi 18:6a4db94011d3 55 #define REQ_VENDOR 0x40
sahilmgandhi 18:6a4db94011d3 56
sahilmgandhi 18:6a4db94011d3 57 /* USB Standard Request */
sahilmgandhi 18:6a4db94011d3 58 #define USBD_GET_STATUS 0x00
sahilmgandhi 18:6a4db94011d3 59 #define USBD_CLEAR_FEATURE 0x01
sahilmgandhi 18:6a4db94011d3 60 #define USBD_SET_FEATURE 0x03
sahilmgandhi 18:6a4db94011d3 61 #define USBD_SET_ADDRESS 0x05
sahilmgandhi 18:6a4db94011d3 62 #define USBD_GET_DESCRIPTOR 0x06
sahilmgandhi 18:6a4db94011d3 63 #define USBD_SET_DESCRIPTOR 0x07
sahilmgandhi 18:6a4db94011d3 64 #define USBD_GET_CONFIGURATION 0x08
sahilmgandhi 18:6a4db94011d3 65 #define USBD_SET_CONFIGURATION 0x09
sahilmgandhi 18:6a4db94011d3 66 #define USBD_GET_INTERFACE 0x0A
sahilmgandhi 18:6a4db94011d3 67 #define USBD_SET_INTERFACE 0x0B
sahilmgandhi 18:6a4db94011d3 68 #define USBD_SYNC_FRAME 0x0C
sahilmgandhi 18:6a4db94011d3 69
sahilmgandhi 18:6a4db94011d3 70 /* USB Descriptor Type */
sahilmgandhi 18:6a4db94011d3 71 #define DESC_DEVICE 0x01
sahilmgandhi 18:6a4db94011d3 72 #define DESC_CONFIG 0x02
sahilmgandhi 18:6a4db94011d3 73 #define DESC_STRING 0x03
sahilmgandhi 18:6a4db94011d3 74 #define DESC_INTERFACE 0x04
sahilmgandhi 18:6a4db94011d3 75 #define DESC_ENDPOINT 0x05
sahilmgandhi 18:6a4db94011d3 76 #define DESC_QUALIFIER 0x06
sahilmgandhi 18:6a4db94011d3 77 #define DESC_OTHERSPEED 0x07
sahilmgandhi 18:6a4db94011d3 78 #define DESC_IFPOWER 0x08
sahilmgandhi 18:6a4db94011d3 79 #define DESC_OTG 0x09
sahilmgandhi 18:6a4db94011d3 80
sahilmgandhi 18:6a4db94011d3 81 /* USB HID Descriptor Type */
sahilmgandhi 18:6a4db94011d3 82 #define DESC_HID 0x21
sahilmgandhi 18:6a4db94011d3 83 #define DESC_HID_RPT 0x22
sahilmgandhi 18:6a4db94011d3 84
sahilmgandhi 18:6a4db94011d3 85 /* USB Descriptor Length */
sahilmgandhi 18:6a4db94011d3 86 #define LEN_DEVICE 18
sahilmgandhi 18:6a4db94011d3 87 #define LEN_QUALIFIER 10
sahilmgandhi 18:6a4db94011d3 88 #define LEN_CONFIG 9
sahilmgandhi 18:6a4db94011d3 89 #define LEN_INTERFACE 9
sahilmgandhi 18:6a4db94011d3 90 #define LEN_ENDPOINT 7
sahilmgandhi 18:6a4db94011d3 91 #define LEN_OTG 5
sahilmgandhi 18:6a4db94011d3 92 #define LEN_HID 9
sahilmgandhi 18:6a4db94011d3 93
sahilmgandhi 18:6a4db94011d3 94 /* USB Endpoint Type */
sahilmgandhi 18:6a4db94011d3 95 #define EP_ISO 0x01
sahilmgandhi 18:6a4db94011d3 96 #define EP_BULK 0x02
sahilmgandhi 18:6a4db94011d3 97 #define EP_INT 0x03
sahilmgandhi 18:6a4db94011d3 98
sahilmgandhi 18:6a4db94011d3 99 #define EP_INPUT 0x80
sahilmgandhi 18:6a4db94011d3 100 #define EP_OUTPUT 0x00
sahilmgandhi 18:6a4db94011d3 101
sahilmgandhi 18:6a4db94011d3 102 /* USB Feature Selector */
sahilmgandhi 18:6a4db94011d3 103 #define FEATURE_DEVICE_REMOTE_WAKEUP 0x01
sahilmgandhi 18:6a4db94011d3 104 #define FEATURE_ENDPOINT_HALT 0x00
sahilmgandhi 18:6a4db94011d3 105 /// @endcond HIDDEN_SYMBOLS
sahilmgandhi 18:6a4db94011d3 106 /********************* Bit definition of CEPCTL register **********************/
sahilmgandhi 18:6a4db94011d3 107 #define USB_CEPCTL_NAKCLR ((uint32_t)0x00000000) /*!<NAK clear \hideinitializer */
sahilmgandhi 18:6a4db94011d3 108 #define USB_CEPCTL_STALL ((uint32_t)0x00000002) /*!<Stall \hideinitializer */
sahilmgandhi 18:6a4db94011d3 109 #define USB_CEPCTL_ZEROLEN ((uint32_t)0x00000004) /*!<Zero length packet \hideinitializer */
sahilmgandhi 18:6a4db94011d3 110 #define USB_CEPCTL_FLUSH ((uint32_t)0x00000008) /*!<CEP flush \hideinitializer */
sahilmgandhi 18:6a4db94011d3 111
sahilmgandhi 18:6a4db94011d3 112 /********************* Bit definition of EPxRSPCTL register **********************/
sahilmgandhi 18:6a4db94011d3 113 #define USB_EP_RSPCTL_FLUSH ((uint32_t)0x00000001) /*!<Buffer Flush \hideinitializer */
sahilmgandhi 18:6a4db94011d3 114 #define USB_EP_RSPCTL_MODE_AUTO ((uint32_t)0x00000000) /*!<Auto-Validate Mode \hideinitializer */
sahilmgandhi 18:6a4db94011d3 115 #define USB_EP_RSPCTL_MODE_MANUAL ((uint32_t)0x00000002) /*!<Manual-Validate Mode \hideinitializer */
sahilmgandhi 18:6a4db94011d3 116 #define USB_EP_RSPCTL_MODE_FLY ((uint32_t)0x00000004) /*!<Fly Mode \hideinitializer */
sahilmgandhi 18:6a4db94011d3 117 #define USB_EP_RSPCTL_MODE_MASK ((uint32_t)0x00000006) /*!<Mode Mask \hideinitializer */
sahilmgandhi 18:6a4db94011d3 118 #define USB_EP_RSPCTL_TOGGLE ((uint32_t)0x00000008) /*!<Clear Toggle bit \hideinitializer */
sahilmgandhi 18:6a4db94011d3 119 #define USB_EP_RSPCTL_HALT ((uint32_t)0x00000010) /*!<Endpoint halt \hideinitializer */
sahilmgandhi 18:6a4db94011d3 120 #define USB_EP_RSPCTL_ZEROLEN ((uint32_t)0x00000020) /*!<Zero length packet IN \hideinitializer */
sahilmgandhi 18:6a4db94011d3 121 #define USB_EP_RSPCTL_SHORTTXEN ((uint32_t)0x00000040) /*!<Packet end \hideinitializer */
sahilmgandhi 18:6a4db94011d3 122 #define USB_EP_RSPCTL_DISBUF ((uint32_t)0x00000080) /*!<Disable buffer \hideinitializer */
sahilmgandhi 18:6a4db94011d3 123
sahilmgandhi 18:6a4db94011d3 124 /********************* Bit definition of EPxCFG register **********************/
sahilmgandhi 18:6a4db94011d3 125 #define USB_EP_CFG_VALID ((uint32_t)0x00000001) /*!<Endpoint Valid \hideinitializer */
sahilmgandhi 18:6a4db94011d3 126 #define USB_EP_CFG_TYPE_BULK ((uint32_t)0x00000002) /*!<Endpoint type - bulk \hideinitializer */
sahilmgandhi 18:6a4db94011d3 127 #define USB_EP_CFG_TYPE_INT ((uint32_t)0x00000004) /*!<Endpoint type - interrupt \hideinitializer */
sahilmgandhi 18:6a4db94011d3 128 #define USB_EP_CFG_TYPE_ISO ((uint32_t)0x00000006) /*!<Endpoint type - isochronous \hideinitializer */
sahilmgandhi 18:6a4db94011d3 129 #define USB_EP_CFG_TYPE_MASK ((uint32_t)0x00000006) /*!<Endpoint type mask \hideinitializer */
sahilmgandhi 18:6a4db94011d3 130 #define USB_EP_CFG_DIR_OUT ((uint32_t)0x00000000) /*!<OUT endpoint \hideinitializer */
sahilmgandhi 18:6a4db94011d3 131 #define USB_EP_CFG_DIR_IN ((uint32_t)0x00000008) /*!<IN endpoint \hideinitializer */
sahilmgandhi 18:6a4db94011d3 132
sahilmgandhi 18:6a4db94011d3 133
sahilmgandhi 18:6a4db94011d3 134 /*@}*/ /* end of group NUC472_442_USBD_EXPORTED_CONSTANTS */
sahilmgandhi 18:6a4db94011d3 135
sahilmgandhi 18:6a4db94011d3 136 /** @addtogroup NUC472_442_USBD_EXPORTED_STRUCT USBD Exported Struct
sahilmgandhi 18:6a4db94011d3 137 @{
sahilmgandhi 18:6a4db94011d3 138 */
sahilmgandhi 18:6a4db94011d3 139
sahilmgandhi 18:6a4db94011d3 140
sahilmgandhi 18:6a4db94011d3 141 typedef struct USBD_CMD_STRUCT {
sahilmgandhi 18:6a4db94011d3 142 uint8_t bmRequestType;
sahilmgandhi 18:6a4db94011d3 143 uint8_t bRequest;
sahilmgandhi 18:6a4db94011d3 144 uint16_t wValue;
sahilmgandhi 18:6a4db94011d3 145 uint16_t wIndex;
sahilmgandhi 18:6a4db94011d3 146 uint16_t wLength;
sahilmgandhi 18:6a4db94011d3 147
sahilmgandhi 18:6a4db94011d3 148 } S_USBD_CMD_T; /*!<USB Setup Packet Structure */
sahilmgandhi 18:6a4db94011d3 149
sahilmgandhi 18:6a4db94011d3 150
sahilmgandhi 18:6a4db94011d3 151
sahilmgandhi 18:6a4db94011d3 152
sahilmgandhi 18:6a4db94011d3 153 typedef struct s_usbd_info {
sahilmgandhi 18:6a4db94011d3 154 uint8_t *gu8DevDesc;
sahilmgandhi 18:6a4db94011d3 155 uint8_t *gu8ConfigDesc;
sahilmgandhi 18:6a4db94011d3 156 uint8_t *gu8StrLangDesc;
sahilmgandhi 18:6a4db94011d3 157 uint8_t *gu8StrVendorDesc;
sahilmgandhi 18:6a4db94011d3 158 uint8_t *gu8StrProductDesc;
sahilmgandhi 18:6a4db94011d3 159 uint8_t *gu8QualDesc;
sahilmgandhi 18:6a4db94011d3 160 uint8_t *gu8OtherConfigDesc;
sahilmgandhi 18:6a4db94011d3 161 uint8_t *gu8HidReportDesc[3];
sahilmgandhi 18:6a4db94011d3 162 uint32_t gu32HidReportSize[3];
sahilmgandhi 18:6a4db94011d3 163
sahilmgandhi 18:6a4db94011d3 164 } S_USBD_INFO_T; /*!<USB Information Structure */
sahilmgandhi 18:6a4db94011d3 165
sahilmgandhi 18:6a4db94011d3 166
sahilmgandhi 18:6a4db94011d3 167 /*@}*/ /* end of group NUC472_442_USBD_EXPORTED_STRUCT */
sahilmgandhi 18:6a4db94011d3 168
sahilmgandhi 18:6a4db94011d3 169 /// @cond HIDDEN_SYMBOLS
sahilmgandhi 18:6a4db94011d3 170 extern uint32_t volatile g_usbd_DmaDone;
sahilmgandhi 18:6a4db94011d3 171 extern uint32_t g_usbd_UsbAddr;
sahilmgandhi 18:6a4db94011d3 172 extern uint32_t g_usbd_CtrlInSize;
sahilmgandhi 18:6a4db94011d3 173 extern uint32_t g_usbd_ShortPacket;
sahilmgandhi 18:6a4db94011d3 174 extern uint32_t g_usbd_CtrlZero;
sahilmgandhi 18:6a4db94011d3 175 extern uint8_t g_usbd_EpHalt[];
sahilmgandhi 18:6a4db94011d3 176 extern S_USBD_INFO_T gsInfo;
sahilmgandhi 18:6a4db94011d3 177 extern S_USBD_CMD_T gUsbCmd;
sahilmgandhi 18:6a4db94011d3 178 extern uint32_t g_usbd_Configured;
sahilmgandhi 18:6a4db94011d3 179 /// @endcond /* HIDDEN_SYMBOLS */
sahilmgandhi 18:6a4db94011d3 180
sahilmgandhi 18:6a4db94011d3 181 /** @addtogroup NUC472_442_USBD_EXPORTED_MACROS USBD Exported Macros
sahilmgandhi 18:6a4db94011d3 182 @{
sahilmgandhi 18:6a4db94011d3 183 */
sahilmgandhi 18:6a4db94011d3 184
sahilmgandhi 18:6a4db94011d3 185 #define USBD_ENABLE_USB() ((uint32_t)(USBD->PHYCTL |= (USBD_PHYCTL_PHYEN_Msk|USBD_PHYCTL_DPPUEN_Msk))) /*!<Enable USB \hideinitializer */
sahilmgandhi 18:6a4db94011d3 186 #define USBD_DISABLE_USB() ((uint32_t)(USBD->PHYCTL &= ~USBD_PHYCTL_DPPUEN_Msk)) /*!<Disable USB \hideinitializer */
sahilmgandhi 18:6a4db94011d3 187 #define USBD_ENABLE_PHY() ((uint32_t)(USBD->PHYCTL |= USBD_PHYCTL_PHYEN_Msk)) /*!<Enable PHY \hideinitializer */
sahilmgandhi 18:6a4db94011d3 188 #define USBD_DISABLE_PHY() ((uint32_t)(USBD->PHYCTL &= ~USBD_PHYCTL_PHYEN_Msk)) /*!<Disable PHY \hideinitializer */
sahilmgandhi 18:6a4db94011d3 189 #define USBD_SET_SE0() ((uint32_t)(USBD->PHYCTL &= ~USBD_PHYCTL_DPPUEN_Msk)) /*!<Enable SE0, Force USB PHY Transceiver to Drive SE0 \hideinitializer */
sahilmgandhi 18:6a4db94011d3 190 #define USBD_CLR_SE0() ((uint32_t)(USBD->PHYCTL |= USBD_PHYCTL_DPPUEN_Msk)) /*!<Disable SE0 \hideinitializer */
sahilmgandhi 18:6a4db94011d3 191 #define USBD_SET_ADDR(addr) (USBD->FADDR = (addr)) /*!<Set USB address \hideinitializer */
sahilmgandhi 18:6a4db94011d3 192 #define USBD_GET_ADDR() ((uint32_t)(USBD->FADDR)) /*!<Get USB address \hideinitializer */
sahilmgandhi 18:6a4db94011d3 193 #define USBD_ENABLE_USB_INT(intr) (USBD->GINTEN = (intr)) /*!<Enable USB Interrupt \hideinitializer */
sahilmgandhi 18:6a4db94011d3 194 #define USBD_ENABLE_BUS_INT(intr) (USBD->BUSINTEN = (intr)) /*!<Enable BUS Interrupt \hideinitializer */
sahilmgandhi 18:6a4db94011d3 195 #define USBD_GET_BUS_INT_FLAG() (USBD->BUSINTSTS) /*!<Clear Bus interrupt flag \hideinitializer */
sahilmgandhi 18:6a4db94011d3 196 #define USBD_CLR_BUS_INT_FLAG(flag) (USBD->BUSINTSTS = flag) /*!<Clear Bus interrupt flag \hideinitializer */
sahilmgandhi 18:6a4db94011d3 197 #define USBD_ENABLE_CEP_INT(intr) (USBD->CEPINTEN = (intr)) /*!<Enable CEP Interrupt \hideinitializer */
sahilmgandhi 18:6a4db94011d3 198 #define USBD_CLR_CEP_INT_FLAG(flag) (USBD->CEPINTSTS = flag) /*!<Clear CEP interrupt flag \hideinitializer */
sahilmgandhi 18:6a4db94011d3 199 #define USBD_SET_CEP_STATE(flag) (USBD->CEPCTL = flag) /*!<Set CEP state \hideinitializer */
sahilmgandhi 18:6a4db94011d3 200 #define USBD_START_CEP_IN(size) (USBD->CEPTXCNT = size) /*!<Start CEP IN Transfer \hideinitializer */
sahilmgandhi 18:6a4db94011d3 201 #define USBD_SET_MAX_PAYLOAD(ep, size) (*((__IO uint32_t *) ((uint32_t)&USBD->EPAMPS + (uint32_t)((ep)*0x28))) = (size)) /*!<Set EPx Maximum Packet Size \hideinitializer */
sahilmgandhi 18:6a4db94011d3 202 #define USBD_ENABLE_EP_INT(ep, intr) (*((__IO uint32_t *) ((uint32_t)&USBD->EPAINTEN + (uint32_t)((ep)*0x28))) = (intr)) /*!<Enable EPx Interrupt \hideinitializer */
sahilmgandhi 18:6a4db94011d3 203 #define USBD_GET_EP_INT_FLAG(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EPAINTSTS + (uint32_t)((ep)*0x28)))) /*!<Get EPx interrupt flag \hideinitializer */
sahilmgandhi 18:6a4db94011d3 204 #define USBD_CLR_EP_INT_FLAG(ep, flag) (*((__IO uint32_t *) ((uint32_t)&USBD->EPAINTSTS + (uint32_t)((ep)*0x28))) = (flag)) /*!<Clear EPx interrupt flag \hideinitializer */
sahilmgandhi 18:6a4db94011d3 205 #define USBD_SET_DMA_LEN(len) (USBD->DMACNT = len) /*!<Set DMA transfer length \hideinitializer */
sahilmgandhi 18:6a4db94011d3 206 #define USBD_SET_DMA_ADDR(addr) (USBD->DMAADDR = addr) /*!<Set DMA transfer address \hideinitializer */
sahilmgandhi 18:6a4db94011d3 207 #define USBD_SET_DMA_READ(epnum) (USBD->DMACTL = (USBD->DMACTL & ~USBD_DMACTL_EPNUM_Msk) | USBD_DMACTL_DMARD_Msk | epnum) /*!<Set DMA transfer type to read \hideinitializer */
sahilmgandhi 18:6a4db94011d3 208 #define USBD_SET_DMA_WRITE(epnum) (USBD->DMACTL = (USBD->DMACTL & ~(USBD_DMACTL_EPNUM_Msk | USBD_DMACTL_DMARD_Msk)) | epnum) /*!<Set DMA transfer type to write \hideinitializer */
sahilmgandhi 18:6a4db94011d3 209 #define USBD_ENABLE_DMA() (USBD->DMACTL |= USBD_DMACTL_DMAEN_Msk) /*!<Enable DMA transfer \hideinitializer */
sahilmgandhi 18:6a4db94011d3 210 #define USBD_IS_ATTACHED() ((uint32_t)(USBD->PHYCTL & USBD_PHYCTL_VBUSDET_Msk)) /*!<Check cable connect state \hideinitializer */
sahilmgandhi 18:6a4db94011d3 211
sahilmgandhi 18:6a4db94011d3 212
sahilmgandhi 18:6a4db94011d3 213 /*@}*/ /* end of group NUC472_442_USBD_EXPORTED_MACROS */
sahilmgandhi 18:6a4db94011d3 214
sahilmgandhi 18:6a4db94011d3 215 /** @addtogroup NUC472_442_USBD_EXPORTED_FUNCTIONS USBD Exported Functions
sahilmgandhi 18:6a4db94011d3 216 @{
sahilmgandhi 18:6a4db94011d3 217 */
sahilmgandhi 18:6a4db94011d3 218 /**
sahilmgandhi 18:6a4db94011d3 219 * @brief USBD_memcpy, Copy bytes hardware limitation
sahilmgandhi 18:6a4db94011d3 220 * @param[in] u8Dst Destination pointer.
sahilmgandhi 18:6a4db94011d3 221 * @param[in] u8Src Source pointer.
sahilmgandhi 18:6a4db94011d3 222 * @param[in] i32Size Copy size.
sahilmgandhi 18:6a4db94011d3 223 * @retval None.
sahilmgandhi 18:6a4db94011d3 224 */
sahilmgandhi 18:6a4db94011d3 225 static __INLINE void USBD_MemCopy(uint8_t *u8Dst, uint8_t *u8Src, int32_t i32Size)
sahilmgandhi 18:6a4db94011d3 226 {
sahilmgandhi 18:6a4db94011d3 227 while (i32Size--) *u8Dst++ = *u8Src++;
sahilmgandhi 18:6a4db94011d3 228 }
sahilmgandhi 18:6a4db94011d3 229
sahilmgandhi 18:6a4db94011d3 230 /**
sahilmgandhi 18:6a4db94011d3 231 * @brief USBD_ResetDMA
sahilmgandhi 18:6a4db94011d3 232 * @param None
sahilmgandhi 18:6a4db94011d3 233 * @retval None.
sahilmgandhi 18:6a4db94011d3 234 */
sahilmgandhi 18:6a4db94011d3 235 static __INLINE void USBD_ResetDMA(void)
sahilmgandhi 18:6a4db94011d3 236 {
sahilmgandhi 18:6a4db94011d3 237 USBD->DMACNT = 0;
sahilmgandhi 18:6a4db94011d3 238 USBD->DMACTL = 0x80;
sahilmgandhi 18:6a4db94011d3 239 USBD->DMACTL = 0x00;
sahilmgandhi 18:6a4db94011d3 240 }
sahilmgandhi 18:6a4db94011d3 241 /**
sahilmgandhi 18:6a4db94011d3 242 * @brief USBD_SetEpBufAddr, Set Endpoint buffer address
sahilmgandhi 18:6a4db94011d3 243 * @param[in] u32Ep Endpoint Number
sahilmgandhi 18:6a4db94011d3 244 * @param[in] u32Base Buffer Start Address
sahilmgandhi 18:6a4db94011d3 245 * @param[in] u32Len Buffer length
sahilmgandhi 18:6a4db94011d3 246 * @retval None.
sahilmgandhi 18:6a4db94011d3 247 */
sahilmgandhi 18:6a4db94011d3 248 static __INLINE void USBD_SetEpBufAddr(uint32_t u32Ep, uint32_t u32Base, uint32_t u32Len)
sahilmgandhi 18:6a4db94011d3 249 {
sahilmgandhi 18:6a4db94011d3 250 if (u32Ep == CEP) {
sahilmgandhi 18:6a4db94011d3 251 USBD->CEPBUFSTART = u32Base;
sahilmgandhi 18:6a4db94011d3 252 USBD->CEPBUFEND = u32Base + u32Len - 1;
sahilmgandhi 18:6a4db94011d3 253 } else {
sahilmgandhi 18:6a4db94011d3 254 *((__IO uint32_t *) ((uint32_t)&USBD->EPABUFSTART + (uint32_t)(u32Ep*0x28))) = u32Base;
sahilmgandhi 18:6a4db94011d3 255 *((__IO uint32_t *) ((uint32_t)&USBD->EPABUFEND + (uint32_t)(u32Ep*0x28))) = u32Base + u32Len - 1;
sahilmgandhi 18:6a4db94011d3 256 }
sahilmgandhi 18:6a4db94011d3 257 }
sahilmgandhi 18:6a4db94011d3 258
sahilmgandhi 18:6a4db94011d3 259 /**
sahilmgandhi 18:6a4db94011d3 260 * @brief USBD_ConfigEp, Config Endpoint
sahilmgandhi 18:6a4db94011d3 261 * @param[in] u32Ep USB endpoint
sahilmgandhi 18:6a4db94011d3 262 * @param[in] u32EpNum Endpoint number
sahilmgandhi 18:6a4db94011d3 263 * @param[in] u32EpType Endpoint type
sahilmgandhi 18:6a4db94011d3 264 * @param[in] u32EpDir Endpoint direction
sahilmgandhi 18:6a4db94011d3 265 * @retval None.
sahilmgandhi 18:6a4db94011d3 266 */
sahilmgandhi 18:6a4db94011d3 267 static __INLINE void USBD_ConfigEp(uint32_t u32Ep, uint32_t u32EpNum, uint32_t u32EpType, uint32_t u32EpDir)
sahilmgandhi 18:6a4db94011d3 268 {
sahilmgandhi 18:6a4db94011d3 269 if (u32EpType == USB_EP_CFG_TYPE_BULK)
sahilmgandhi 18:6a4db94011d3 270 *((__IO uint32_t *)((uint32_t)&USBD->EPARSPCTL+(uint32_t)(u32Ep*0x28)))=(USB_EP_RSPCTL_FLUSH|USB_EP_RSPCTL_MODE_AUTO);
sahilmgandhi 18:6a4db94011d3 271 else if (u32EpType == USB_EP_CFG_TYPE_INT)
sahilmgandhi 18:6a4db94011d3 272 *((__IO uint32_t *)((uint32_t)&USBD->EPARSPCTL+(uint32_t)(u32Ep*0x28)))=(USB_EP_RSPCTL_FLUSH|USB_EP_RSPCTL_MODE_MANUAL);
sahilmgandhi 18:6a4db94011d3 273 else if (u32EpType == USB_EP_CFG_TYPE_ISO)
sahilmgandhi 18:6a4db94011d3 274 *((__IO uint32_t *)((uint32_t)&USBD->EPARSPCTL+(uint32_t)(u32Ep*0x28)))=(USB_EP_RSPCTL_FLUSH|USB_EP_RSPCTL_MODE_FLY);
sahilmgandhi 18:6a4db94011d3 275
sahilmgandhi 18:6a4db94011d3 276 *((__IO uint32_t *)((uint32_t)&USBD->EPACFG+(uint32_t)(u32Ep*0x28)))=(u32EpType|u32EpDir|USB_EP_CFG_VALID|(u32EpNum << 4));
sahilmgandhi 18:6a4db94011d3 277 }
sahilmgandhi 18:6a4db94011d3 278
sahilmgandhi 18:6a4db94011d3 279 /**
sahilmgandhi 18:6a4db94011d3 280 * @brief Set USB endpoint stall state
sahilmgandhi 18:6a4db94011d3 281 *
sahilmgandhi 18:6a4db94011d3 282 * @param[in] u32Ep USB endpoint
sahilmgandhi 18:6a4db94011d3 283 * @return None
sahilmgandhi 18:6a4db94011d3 284 *
sahilmgandhi 18:6a4db94011d3 285 * @details Set USB endpoint stall state, endpoint will return STALL token.
sahilmgandhi 18:6a4db94011d3 286 */
sahilmgandhi 18:6a4db94011d3 287 static __INLINE void USBD_SetStall(uint32_t u32Ep)
sahilmgandhi 18:6a4db94011d3 288 {
sahilmgandhi 18:6a4db94011d3 289 uint32_t u32CfgAddr;
sahilmgandhi 18:6a4db94011d3 290 uint32_t u32Cfg;
sahilmgandhi 18:6a4db94011d3 291 int i;
sahilmgandhi 18:6a4db94011d3 292
sahilmgandhi 18:6a4db94011d3 293 if (u32Ep == 0)
sahilmgandhi 18:6a4db94011d3 294 USBD_SET_CEP_STATE(USB_CEPCTL_STALL);
sahilmgandhi 18:6a4db94011d3 295 else {
sahilmgandhi 18:6a4db94011d3 296 for (i=0; i<USBD_MAX_EP; i++) {
sahilmgandhi 18:6a4db94011d3 297 u32CfgAddr = (uint32_t)&USBD->EPACFG + (uint32_t)(i * 0x28);
sahilmgandhi 18:6a4db94011d3 298 u32Cfg = *((__IO uint32_t *) (u32CfgAddr));
sahilmgandhi 18:6a4db94011d3 299
sahilmgandhi 18:6a4db94011d3 300 if (((u32Cfg & 0xf0) >> 4) == u32Ep)
sahilmgandhi 18:6a4db94011d3 301 {
sahilmgandhi 18:6a4db94011d3 302 u32CfgAddr = (uint32_t)&USBD->EPARSPCTL + (uint32_t)(i * 0x28);
sahilmgandhi 18:6a4db94011d3 303 u32Cfg = *((__IO uint32_t *) (u32CfgAddr)) & 0xf7; /* avoid clear TOGGLE bit */
sahilmgandhi 18:6a4db94011d3 304 *((__IO uint32_t *) (u32CfgAddr)) = (u32Cfg | USB_EP_RSPCTL_HALT);
sahilmgandhi 18:6a4db94011d3 305 }
sahilmgandhi 18:6a4db94011d3 306 }
sahilmgandhi 18:6a4db94011d3 307 }
sahilmgandhi 18:6a4db94011d3 308 }
sahilmgandhi 18:6a4db94011d3 309
sahilmgandhi 18:6a4db94011d3 310 /**
sahilmgandhi 18:6a4db94011d3 311 * @brief Clear USB endpoint stall state
sahilmgandhi 18:6a4db94011d3 312 *
sahilmgandhi 18:6a4db94011d3 313 * @param[in] u32Ep USB endpoint
sahilmgandhi 18:6a4db94011d3 314 * @return None
sahilmgandhi 18:6a4db94011d3 315 *
sahilmgandhi 18:6a4db94011d3 316 * @details Clear USB endpoint stall state, endpoint will return ACK/NAK token.
sahilmgandhi 18:6a4db94011d3 317 */
sahilmgandhi 18:6a4db94011d3 318 static __INLINE void USBD_ClearStall(uint32_t u32Ep)
sahilmgandhi 18:6a4db94011d3 319 {
sahilmgandhi 18:6a4db94011d3 320 uint32_t u32CfgAddr;
sahilmgandhi 18:6a4db94011d3 321 uint32_t u32Cfg;
sahilmgandhi 18:6a4db94011d3 322 int i;
sahilmgandhi 18:6a4db94011d3 323
sahilmgandhi 18:6a4db94011d3 324 for (i=0; i<USBD_MAX_EP; i++) {
sahilmgandhi 18:6a4db94011d3 325 u32CfgAddr = (uint32_t)&USBD->EPACFG + (uint32_t)(i * 0x28);
sahilmgandhi 18:6a4db94011d3 326 u32Cfg = *((__IO uint32_t *) (u32CfgAddr));
sahilmgandhi 18:6a4db94011d3 327
sahilmgandhi 18:6a4db94011d3 328 if (((u32Cfg & 0xf0) >> 4) == u32Ep)
sahilmgandhi 18:6a4db94011d3 329 {
sahilmgandhi 18:6a4db94011d3 330 u32CfgAddr = (uint32_t)&USBD->EPARSPCTL + (uint32_t)(i * 0x28);
sahilmgandhi 18:6a4db94011d3 331 *((__IO uint32_t *) (u32CfgAddr)) = USB_EP_RSPCTL_TOGGLE;
sahilmgandhi 18:6a4db94011d3 332 }
sahilmgandhi 18:6a4db94011d3 333 }
sahilmgandhi 18:6a4db94011d3 334 }
sahilmgandhi 18:6a4db94011d3 335
sahilmgandhi 18:6a4db94011d3 336 /**
sahilmgandhi 18:6a4db94011d3 337 * @brief Get USB endpoint stall state
sahilmgandhi 18:6a4db94011d3 338 *
sahilmgandhi 18:6a4db94011d3 339 * @param[in] u32Ep USB endpoint
sahilmgandhi 18:6a4db94011d3 340 * @retval 0: USB endpoint is not stalled.
sahilmgandhi 18:6a4db94011d3 341 * @retval non-0: USB endpoint is stalled.
sahilmgandhi 18:6a4db94011d3 342 *
sahilmgandhi 18:6a4db94011d3 343 * @details Get USB endpoint stall state.
sahilmgandhi 18:6a4db94011d3 344 */
sahilmgandhi 18:6a4db94011d3 345 static __INLINE uint32_t USBD_GetStall(uint32_t u32Ep)
sahilmgandhi 18:6a4db94011d3 346 {
sahilmgandhi 18:6a4db94011d3 347 uint32_t u32CfgAddr;
sahilmgandhi 18:6a4db94011d3 348 uint32_t u32Cfg;
sahilmgandhi 18:6a4db94011d3 349 int i;
sahilmgandhi 18:6a4db94011d3 350
sahilmgandhi 18:6a4db94011d3 351 for (i=0; i<USBD_MAX_EP; i++) {
sahilmgandhi 18:6a4db94011d3 352 u32CfgAddr = (uint32_t)&USBD->EPACFG + (uint32_t)(i * 0x28);
sahilmgandhi 18:6a4db94011d3 353 u32Cfg = *((__IO uint32_t *) (u32CfgAddr));
sahilmgandhi 18:6a4db94011d3 354
sahilmgandhi 18:6a4db94011d3 355 if (((u32Cfg & 0xf0) >> 4) == u32Ep)
sahilmgandhi 18:6a4db94011d3 356 {
sahilmgandhi 18:6a4db94011d3 357 u32CfgAddr = (uint32_t)&USBD->EPARSPCTL + (uint32_t)(i * 0x28);
sahilmgandhi 18:6a4db94011d3 358 return ((*((__IO uint32_t *) (u32CfgAddr))) & USB_EP_RSPCTL_HALT);
sahilmgandhi 18:6a4db94011d3 359 }
sahilmgandhi 18:6a4db94011d3 360 }
sahilmgandhi 18:6a4db94011d3 361 return 0;
sahilmgandhi 18:6a4db94011d3 362 }
sahilmgandhi 18:6a4db94011d3 363
sahilmgandhi 18:6a4db94011d3 364
sahilmgandhi 18:6a4db94011d3 365 /*-------------------------------------------------------------------------------------------*/
sahilmgandhi 18:6a4db94011d3 366 typedef void (*VENDOR_REQ)(void); /*!<USB Vendor request callback function */
sahilmgandhi 18:6a4db94011d3 367 typedef void (*CLASS_REQ)(void); /*!<USB Class request callback function */
sahilmgandhi 18:6a4db94011d3 368 typedef void (*SET_INTERFACE_REQ)(uint32_t u32AltInterface); /*!<USB Standard request "Set Interface" callback function */
sahilmgandhi 18:6a4db94011d3 369
sahilmgandhi 18:6a4db94011d3 370 void USBD_Open(S_USBD_INFO_T *param, CLASS_REQ pfnClassReq, SET_INTERFACE_REQ pfnSetInterface);
sahilmgandhi 18:6a4db94011d3 371 void USBD_Start(void);
sahilmgandhi 18:6a4db94011d3 372 void USBD_ProcessSetupPacket(void);
sahilmgandhi 18:6a4db94011d3 373 void USBD_StandardRequest(void);
sahilmgandhi 18:6a4db94011d3 374 void USBD_UpdateDeviceState(void);
sahilmgandhi 18:6a4db94011d3 375 void USBD_PrepareCtrlIn(uint8_t *pu8Buf, uint32_t u32Size);
sahilmgandhi 18:6a4db94011d3 376 void USBD_CtrlIn(void);
sahilmgandhi 18:6a4db94011d3 377 void USBD_CtrlOut(uint8_t *pu8Buf, uint32_t u32Size);
sahilmgandhi 18:6a4db94011d3 378 void USBD_SwReset(void);
sahilmgandhi 18:6a4db94011d3 379 void USBD_SetVendorRequest(VENDOR_REQ pfnVendorReq);
sahilmgandhi 18:6a4db94011d3 380
sahilmgandhi 18:6a4db94011d3 381
sahilmgandhi 18:6a4db94011d3 382
sahilmgandhi 18:6a4db94011d3 383 /*@}*/ /* end of group NUC472_442_USBD_EXPORTED_FUNCTIONS */
sahilmgandhi 18:6a4db94011d3 384
sahilmgandhi 18:6a4db94011d3 385 /*@}*/ /* end of group NUC472_442_USBD_Driver */
sahilmgandhi 18:6a4db94011d3 386
sahilmgandhi 18:6a4db94011d3 387 /*@}*/ /* end of group NUC472_442_Device_Driver */
sahilmgandhi 18:6a4db94011d3 388
sahilmgandhi 18:6a4db94011d3 389 #ifdef __cplusplus
sahilmgandhi 18:6a4db94011d3 390 }
sahilmgandhi 18:6a4db94011d3 391 #endif
sahilmgandhi 18:6a4db94011d3 392
sahilmgandhi 18:6a4db94011d3 393 #endif //__USBD_H__
sahilmgandhi 18:6a4db94011d3 394
sahilmgandhi 18:6a4db94011d3 395 /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/