usb_driver.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #ifndef __USB_DRIVER_H__
  22. #define __USB_DRIVER_H__
  23. #include "usb_define.h"
  24. #include "usb_host.h"
  25. #include "usb_device.h"
  26. #include "bsp_common.h"
  27. enum
  28. {
  29. USB_EP_STATE_DISABLE,
  30. USB_EP_STATE_ENABLE,
  31. USB_EP_STATE_STALL,
  32. USB_EP_STATE_NAK,
  33. USB_EP_STATE_ACK,
  34. USB_EP_STATE_NYET,
  35. USB_EP0_STAGE_SETUP = 0,
  36. USB_EP0_STAGE_DATA_TO_DEVICE,
  37. USB_EP0_STAGE_DATA_TO_HOST,
  38. SERV_USB_RESET_END = 0,
  39. SERV_USB_RESUME_END,
  40. SERV_USB_SUSPEND,
  41. SERV_USB_RESUME,
  42. };
  43. typedef struct
  44. {
  45. volatile Buffer_Struct RxBuf;
  46. volatile Buffer_Struct TxBuf;
  47. CBFuncEx_t CB;
  48. void *pData;
  49. uint32_t XferMaxLen;
  50. uint16_t MaxPacketLen;
  51. uint16_t TimeoutMS;
  52. union
  53. {
  54. uint16_t ToHostStatus;
  55. struct
  56. {
  57. uint16_t Halt:1;
  58. uint16_t Zero:15;
  59. }INSTATUS_b;
  60. };
  61. union
  62. {
  63. uint16_t ToDeviceStatus;
  64. struct
  65. {
  66. uint16_t Halt:1;
  67. uint16_t Zero:15;
  68. }OUTSTATUS_b;
  69. };
  70. uint8_t XferType;
  71. uint8_t ToHostEnable;
  72. uint8_t ToDeviceEnable;
  73. uint8_t ForceZeroPacket;
  74. uint8_t NeedZeroPacket;
  75. uint8_t TxZeroPacket;
  76. }USB_EndpointCtrlStruct;
  77. typedef struct
  78. {
  79. uint8_t *Data;
  80. uint32_t Len;
  81. usb_device_request_t *pLastRequest;
  82. uint8_t USB_ID;
  83. uint8_t EpIndex;
  84. uint8_t IsToDevice;
  85. uint8_t IsDataStage;
  86. }USB_EndpointDataStruct;
  87. typedef struct
  88. {
  89. uint32_t EpBufMaxLen;
  90. union
  91. {
  92. uint32_t Feature;
  93. struct
  94. {
  95. uint32_t Suspend:1;
  96. uint32_t RemoteWakeup:1;
  97. uint32_t U1:1;
  98. uint32_t U2:1;
  99. uint32_t LTM:1;
  100. uint32_t FullSpeed:1;
  101. uint32_t HighSpeed:1;
  102. uint32_t SuperSpeed:1;
  103. }FEATURE_b;
  104. };
  105. uint8_t CtrlMode; //see usb_hc_mode
  106. }USB_HWCapsStruct;
  107. /*************usb common api****************/
  108. void USB_StackSetControl(uint8_t USB_ID, HANDLE pHWCtrl,USB_EndpointCtrlStruct *pEpCtrl, USB_HWCapsStruct *Caps);
  109. int32_t USB_StackStop(uint8_t USB_ID);
  110. void USB_StackPutRxData(uint8_t USB_ID, uint8_t EpIndex, const uint8_t *Data, uint32_t Len);
  111. void USB_StackResetEpBuffer(uint8_t USB_ID, uint8_t Index);
  112. void USB_StackSetEpStatus(uint8_t USB_ID, uint8_t EpIndex, uint8_t IsToDevice, uint8_t Status);
  113. int32_t USB_StackTxEpData(uint8_t USB_ID, uint8_t EpIndex, void *pData, uint32_t Len, uint32_t MaxLen, uint8_t ForceZeroPacket);
  114. void USB_StackSetRxEpDataLen(uint8_t USB_ID, uint8_t EpIndex, uint32_t Len);
  115. void USB_StackEpIntOnOff(uint8_t USB_ID, uint8_t EpIndex, uint8_t IsToDevice, uint8_t OnOff);
  116. /*************usb class api*******************/
  117. /*************usb device api******************/
  118. void USB_StackClearSetup(uint8_t ID);
  119. void USB_StackSetDeviceSelfPower(uint8_t USB_ID, uint8_t OnOff);
  120. void USB_StackSetDeviceRemoteWakeupCapable(uint8_t USB_ID, uint8_t OnOff);
  121. void USB_StackSetDeviceSpeed(uint8_t USB_ID, uint8_t Speed);
  122. void USB_StackSetDeviceConfig(uint8_t USB_ID, usb_device_descriptor_t *pDeviceDesc, USB_FullConfigStruct *pConfigInfo, uint8_t ConfigNum, uint8_t StringNum, CBFuncEx_t CB, void *pUserData);
  123. void USB_StackSetString(uint8_t USB_ID, uint8_t Index, const uint8_t *Data, uint16_t Len);
  124. void USB_StackSetCharString(uint8_t USB_ID, uint8_t Index, const char *Data, uint16_t Len);
  125. void USB_StackSetEpCB(uint8_t USB_ID, uint8_t Index, CBFuncEx_t CB, void *pUserData);
  126. int32_t USB_StackStartDevice(uint8_t USB_ID);
  127. void USB_StackDeviceBusChange(uint8_t USB_ID, uint8_t Type);
  128. void USB_StackDeviceEp0TxDone(uint8_t USB_ID);
  129. void USB_StackSetEp0Stage(uint8_t USB_ID, uint8_t Stage);
  130. void USB_StackAnalyzeDeviceEpRx(uint8_t USB_ID, uint8_t EpIndex);
  131. void USB_StackDeviceAfterDisconnect(uint8_t USB_ID);
  132. void USB_StackStopDeviceTx(uint8_t USB_ID, uint8_t EpIndex, uint8_t IsNeedNotify);
  133. //void USB_StackAnalyzeDeviceEpTx(uint8_t USB_ID, uint8_t EpIndex);
  134. /*************usb host api******************/
  135. void USB_StackHostBusChange(uint8_t USB_ID, uint8_t Type);
  136. #endif