usbd_core.h 9.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
/*!
 * @file        usbd_core.h
 *
 * @brief       USB protocol core handler head file
 *
 * @version     V1.0.1
 *
 * @date        2022-09-20
 *
 * @attention
 *
 *  Copyright (C) 2020-2022 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

/* Define to prevent recursive inclusion */
#ifndef __USBD_CORE_H_
#define __USBD_CORE_H_

/* Includes */
#include "drv_usb_device.h"

/** @addtogroup USB_Driver_Library USB Driver Library
  @{
*/

/** @addtogroup Core_Device Core Device
  @{
*/

/** @addtogroup Standrad
  @{
*/

/** @addtogroup Core
  @{
*/

/** @defgroup Core_Macros Macros
  @{
*/

/* Get minimum value */
#define USB_MIN(a, b)      (a >= b ? b : a)

/* Get maximum value */
#define USB_MAX(a, b)      (a >= b ? a : b)

/* control status function */
#define USBD_CtrlTxStatus()         USBD_CtrlInData(NULL, 0)
#define USBD_CtrlRxStatus()         USBD_CtrlOutData(NULL, 0)

/**@} end of group Core_Macros */

/** @defgroup Core_Enumerations Enumerations
  @{
*/

/**
 * @brief   USB request type
 */
enum
{
    USBD_REQ_TYPE_STANDARD   = 0,
    USBD_REQ_TYPE_CLASS      = 1,
    USBD_REQ_TYPE_VENDOR     = 2,
    USBD_REQ_TYPE_RESERVED   = 3
};

/**
 * @brief   USB recipient
 */
enum
{
    USBD_RECIPIENT_DEVICE    = 0,
    USBD_RECIPIENT_INTERFACE = 1,
    USBD_RECIPIENT_ENDPOINT  = 2,
    USBD_RECIPIENT_OTHER     = 3,
};

/**
 * @brief   USB standard device requests
 */
enum
{
    USBD_GET_STATUS          = 0,
    USBD_CLEAR_FEATURE       = 1,
    USBD_SET_FEATURE         = 3,
    USBD_SET_ADDRESS         = 5,
    USBD_GET_DESCRIPTOR      = 6,
    USBD_SET_DESCRIPTOR      = 7,
    USBD_GET_CONFIGURATION   = 8,
    USBD_SET_CONFIGURATION   = 9,
    USBD_GET_INTERFACE       = 10,
    USBD_SET_INTERFACE       = 11,
    USBD_SYNCH_FRAME         = 12,
};

/**
 * @brief   USB descriptor types
 */
enum
{
    USBD_DESC_DEVICE             = 1,
    USBD_DESC_CONFIGURATION      = 2,
    USBD_DESC_STRING             = 3,
    USBD_DESC_INTERFACE          = 4,
    USBD_DESC_ENDPOINT           = 5,
    USBD_DESC_DEVICE_QUALIFIER   = 6,
    USBD_DESC_OTHER_SPEED        = 7,
    USBD_INTERFACE_POWER         = 8,
};

/**
 * @brief   USB standard feature
 */
enum
{
    USBD_FEATURE_ENDPOINT_HALT   = 0,
    USBD_FEATURE_REMOTE_WAKEUP   = 1,
    USBD_FEATURE_TEST_MODE       = 2
};

/**
 * @brief   USB internal state machine
 */
typedef enum
{
    USBD_CTRL_STATE_WAIT_SETUP,
    USBD_CTRL_STATE_DATA_IN,
    USBD_CTRL_STATE_DATA_OUT,
    USBD_CTRL_STATE_WAIT_STATUS_IN,
    USBD_CTRL_STATE_WAIT_STATUS_OUT,
    USBD_CTRL_STATE_STALLED,
} USBD_CTRL_STATE_T;

/**
 * @brief   USBD Endpoint type for USB protocol
 */
typedef enum
{
    USBD_EP_TYPE_CONTROL,
    USBD_EP_TYPE_ISO,
    USBD_EP_TYPE_BULK,
    USBD_EP_TYPE_INTERRUPT
} USBD_EP_TYPE_T;

/**@} end of group Core_Enumerations */

/** @defgroup Core_Structures Structures
  @{
*/

/**
 * @brief   USB request type
 */
typedef union
{
    uint8_t byte;

    struct
    {
        uint8_t recipient       : 5;
        uint8_t type            : 2;
        uint8_t dir             : 1;
    } bit;
} USBD_REQ_TYPE_T;

/**
 * @brief   USB device request data
 */
typedef struct
{
    union
    {
        uint8_t pack[8];

        struct
        {
            USBD_REQ_TYPE_T bmRequestType;
            uint8_t         bRequest;
            uint8_t         wValue[2];
            uint8_t         wIndex[2];
            uint8_t         wLength[2];
        } byte;
    };
} USBD_DevReqData_T;


/* USB standard request callback handler */
typedef void (*USBD_StdReqHandler_T)(void);

/* USB request handler */
typedef void (*USBD_ReqHandler_T)(USBD_DevReqData_T*);

/* Ctrl Tx Status handler function define */
typedef void (*USBD_CtrlTxStatusHandler_T)(void);

/* Ctrl Rx Status handler function define */
typedef void (*USBD_CtrlRxStatusHandler_T)(void);

/* Endpoint handler */
typedef void (*USBD_EPHandler_T)(uint8_t ep);

/* Reset handler */
typedef void (*USBD_ResetHandler_T)(void);

/* Interrupt handler function define */
typedef void (*USBD_InterruptHandler_T)(void);

/**
 * @brief   Descriptor structure
 */
typedef struct
{
    const uint8_t* pDesc;
    uint8_t size;
} USBD_Descriptor_T;

/**
 * @brief   USB Class Request handler
 */
typedef struct
{
    USBD_StdReqHandler_T getConfigurationHandler;
    USBD_StdReqHandler_T getDescriptorHandler;
    USBD_StdReqHandler_T getInterfaceHandler;
    USBD_StdReqHandler_T getStatusHandler;
    USBD_StdReqHandler_T setAddressHandler;

    USBD_StdReqHandler_T setConfigurationHandler;
    USBD_StdReqHandler_T setDescriptorHandler;
    USBD_StdReqHandler_T setFeatureHandler;
    USBD_StdReqHandler_T setInterfaceHandler;
    USBD_StdReqHandler_T clearFeatureHandler;
} USBD_StdReqCallback_T;

/**
 * @brief   Control transfer buffer
 */
typedef struct
{
    uint8_t* pBuf;         /*!< Data buffer */
    uint32_t bufLen;       /*!< Length of the data buffer */
    uint8_t  packNum;      /*!< Packet number of the data */
    uint8_t  zeroPackFill; /*!< Fill a zero pack for IN transfer or not */
    uint16_t maxPackSize;  /*!< Max pack size of this endpoint */
    uint32_t xferCnt;      /*!< Data count of one pack on from tansfer */
} USBD_CtrlBuf_T;

/**
 * @brief   USB init parameter
 */
typedef struct
{
    USBD_Descriptor_T* pDeviceDesc;              /*!< Device descriptor pointer */
    USBD_Descriptor_T* pConfigurationDesc;       /*!< Configuration descriptor pointer */
    USBD_Descriptor_T* pStringDesc;              /*!< String descriptor pointer */
    USBD_Descriptor_T* pQualifierDesc;           /*!< Device Qualifier descriptor pointer */
    USBD_Descriptor_T* pHidReportDesc;           /*!< HID report descriptor pointer */


    USBD_StdReqCallback_T* pStdReqCallback;
    USBD_ReqHandler_T stdReqExceptionHandler;    /*!< Standard request exception handler */
    USBD_ReqHandler_T classReqHandler;           /*!< Class request handler */
    USBD_ReqHandler_T vendorReqHandler;          /*!< vendor request handler */

    USBD_CtrlTxStatusHandler_T txStatusHandler;  /*!< Send IN status early handler */
    USBD_CtrlRxStatusHandler_T rxStatusHandler;  /*!< Receive OUT status early handler */

    USBD_EPHandler_T    outEpHandler;            /*!< OUT EP transfer done handler except EP0 */
    USBD_EPHandler_T    inEpHandler;             /*!< IN EP transfer done handler except EP0 */
    USBD_ResetHandler_T resetHandler;            /*!< Reset handler */
    USBD_InterruptHandler_T intHandler;          /*!< Hadler the rest of interrupt. */
} USBD_InitParam_T;

/**
 * @brief   USB infomation
 */
typedef struct
{
    USBD_CTRL_STATE_T ctrlState;

    uint8_t curFeature;
    uint8_t curInterface;
    uint8_t curAlternateSetting;
    uint8_t curConfiguration;
    uint8_t configurationNum;

    /* Setup request data buffer */
    USBD_DevReqData_T reqData;

    /* Endpoint buffer management */
    USBD_CtrlBuf_T inBuf[USB_EP_MAX_NUM];
    USBD_CtrlBuf_T outBuf[USB_EP_MAX_NUM];

    /* Descriptor pointer */
    USBD_Descriptor_T* pDeviceDesc;
    USBD_Descriptor_T* pConfigurationDesc;
    USBD_Descriptor_T* pStringDesc;
    USBD_Descriptor_T* pQualifierDesc;
    USBD_Descriptor_T* pHidReportDesc;

    /* Setup request callback handler */
    USBD_StdReqCallback_T* pStdReqCallback;
    USBD_ReqHandler_T     stdReqExceptionHandler;
    USBD_ReqHandler_T     classReqHandler;
    USBD_ReqHandler_T     vendorReqHandler;

    /* Control transfer status stage handler */
    USBD_CtrlTxStatusHandler_T txStatusHandler;
    USBD_CtrlRxStatusHandler_T rxStatusHandler;

    /* Endpoint transfer done handler */
    USBD_EPHandler_T      outEpHandler;
    USBD_EPHandler_T      inEpHandler;

    USBD_ResetHandler_T   resetHandler;
    USBD_InterruptHandler_T intHandler;
} USBD_Info_T;

/**@} end of group Core_Structures */

/** @defgroup Core_Variables Variables
  @{
*/


extern USBD_Info_T g_usbDev;

/**@} end of group Core_Variables */

/** @defgroup Core_Functions Functions
  @{
*/

/* Handler Endpoint 0 control transfer */
void USBD_SetupProcess(void);
void USBD_CtrlInProcess(void);
void USBD_CtrlOutProcess(void);
void USBD_CtrlOutData(uint8_t* buf, uint32_t len);
void USBD_CtrlInData(uint8_t* buf, uint32_t len);

/* Handler other Endpoint data transfer */
void USBD_DataInProcess(USBD_EP_T ep);
void USBD_DataOutProcess(USBD_EP_T ep);
void USBD_TxData(uint8_t ep, uint8_t* buf, uint32_t len);
void USBD_RxData(uint8_t ep, uint8_t* buf, uint32_t len);

#endif

/**@} end of group Core_Functions */
/**@} end of group Core */
/**@} end of group Standard */
/**@} end of group Core_Device */
/**@} end of group USB_Driver_Library */