usbh_stdreq.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /******************************************************************************************************************************************
  2. * 文件名称: usbh_stdreq.c
  3. * 功能说明:
  4. * 技术支持: http://www.synwit.com.cn/e/tool/gbook/?bid=1
  5. * 注意事项:
  6. * 版本日期: V1.1.0 2020年11月3日
  7. * 升级记录:
  8. *
  9. *
  10. *******************************************************************************************************************************************
  11. * @attention
  12. *
  13. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH CODING INFORMATION
  14. * REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, SYNWIT SHALL NOT BE HELD LIABLE
  15. * FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
  16. * OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONN-
  17. * -ECTION WITH THEIR PRODUCTS.
  18. *
  19. * COPYRIGHT 2012 Synwit Technology
  20. *******************************************************************************************************************************************/
  21. #include <string.h>
  22. #include "SWM341.h"
  23. #include "usbh_core.h"
  24. #include "usbh_stdreq.h"
  25. /******************************************************************************************************************************************
  26. * 函数名称: USBH_GetDescriptor()
  27. * 功能说明: 获取描述符
  28. * 输 入: 无
  29. * 输 出: 无
  30. * 注意事项: 无
  31. ******************************************************************************************************************************************/
  32. USBH_Status USBH_GetDescriptor(USBH_Info_t *phost, uint8_t type, uint8_t index, uint8_t *buff, int size)
  33. {
  34. phost->Ctrl.setup.bRequestType = USB_REQ_D2H | USB_REQ_STANDARD | USB_REQ_TO_DEVICE;
  35. phost->Ctrl.setup.bRequest = USB_GET_DESCRIPTOR;
  36. phost->Ctrl.setup.wValue = (type << 8) | index;
  37. phost->Ctrl.setup.wIndex = (type == USB_DESC_STRING) ? 0x0409 : 0;
  38. phost->Ctrl.setup.wLength = size;
  39. return USBH_CtrlTransfer(phost, buff, size);
  40. }
  41. /******************************************************************************************************************************************
  42. * 函数名称: USBH_GetDescriptorEx()
  43. * 功能说明: 获取描述符
  44. * 输 入: uint8_t reqType USB_REQ_STANDARD、USB_REQ_CLASS、USB_REQ_VENDOR
  45. * uint8_t reqTarget USB_REQ_TO_DEVICE、USB_REQ_TO_INTERFACE、USB_REQ_TO_ENDPOINT
  46. * uint8_t type
  47. * uint8_t index
  48. * uint16_t wIndex
  49. * 输 出: 无
  50. * 注意事项: 无
  51. ******************************************************************************************************************************************/
  52. USBH_Status USBH_GetDescriptorEx(USBH_Info_t *phost, uint8_t reqType, uint8_t reqTarget, uint8_t type, uint8_t index, uint16_t wIndex, uint8_t *buff, int size)
  53. {
  54. phost->Ctrl.setup.bRequestType = USB_REQ_D2H | reqType | reqTarget;
  55. phost->Ctrl.setup.bRequest = USB_GET_DESCRIPTOR;
  56. phost->Ctrl.setup.wValue = (type << 8) | index;
  57. phost->Ctrl.setup.wIndex = wIndex;
  58. phost->Ctrl.setup.wLength = size;
  59. return USBH_CtrlTransfer(phost, buff, size);
  60. }
  61. /******************************************************************************************************************************************
  62. * 函数名称: USBH_SetAddress()
  63. * 功能说明: 设置设备地址
  64. * 输 入: 无
  65. * 输 出: 无
  66. * 注意事项: 无
  67. ******************************************************************************************************************************************/
  68. USBH_Status USBH_SetAddress(USBH_Info_t *phost, uint8_t addr)
  69. {
  70. phost->Ctrl.setup.bRequestType = USB_REQ_H2D | USB_REQ_STANDARD | USB_REQ_TO_DEVICE;
  71. phost->Ctrl.setup.bRequest = USB_SET_ADDRESS;
  72. phost->Ctrl.setup.wValue = addr;
  73. phost->Ctrl.setup.wIndex = 0;
  74. phost->Ctrl.setup.wLength = 0;
  75. return USBH_CtrlTransfer(phost, 0, 0);
  76. }
  77. /******************************************************************************************************************************************
  78. * 函数名称: USBH_SetConfiguration()
  79. * 功能说明: 设置 configuration
  80. * 输 入: 无
  81. * 输 出: 无
  82. * 注意事项: 无
  83. ******************************************************************************************************************************************/
  84. USBH_Status USBH_SetConfiguration(USBH_Info_t *phost, uint8_t cfg)
  85. {
  86. phost->Ctrl.setup.bRequestType = USB_REQ_H2D | USB_REQ_STANDARD | USB_REQ_TO_DEVICE;
  87. phost->Ctrl.setup.bRequest = USB_SET_CONFIGURATION;
  88. phost->Ctrl.setup.wValue = cfg;
  89. phost->Ctrl.setup.wIndex = 0;
  90. phost->Ctrl.setup.wLength = 0;
  91. return USBH_CtrlTransfer(phost, 0, 0);
  92. }
  93. /******************************************************************************************************************************************
  94. * 函数名称: USBH_SetInterface()
  95. * 功能说明: 设置 interface altSetting
  96. * 输 入: 无
  97. * 输 出: 无
  98. * 注意事项: 无
  99. ******************************************************************************************************************************************/
  100. USBH_Status USBH_SetInterface(USBH_Info_t *phost, uint8_t intf, uint8_t altSetting)
  101. {
  102. phost->Ctrl.setup.bRequestType = USB_REQ_H2D | USB_REQ_STANDARD | USB_REQ_TO_INTERFACE;
  103. phost->Ctrl.setup.bRequest = USB_SET_INTERFACE;
  104. phost->Ctrl.setup.wValue = altSetting;
  105. phost->Ctrl.setup.wIndex = intf;
  106. phost->Ctrl.setup.wLength = 0;
  107. return USBH_CtrlTransfer(phost, 0, 0);
  108. }
  109. /******************************************************************************************************************************************
  110. * 函数名称: USBH_ClrFeature()
  111. * 功能说明: 清除端点 STALL
  112. * 输 入: 无
  113. * 输 出: 无
  114. * 注意事项: 无
  115. ******************************************************************************************************************************************/
  116. USBH_Status USBH_ClrFeature(USBH_Info_t *phost, uint8_t ep)
  117. {
  118. phost->Ctrl.setup.bRequestType = USB_REQ_H2D | USB_REQ_STANDARD | USB_REQ_TO_ENDPOINT;
  119. phost->Ctrl.setup.bRequest = USB_CLEAR_FEATURE;
  120. phost->Ctrl.setup.wValue = 0x00;
  121. phost->Ctrl.setup.wIndex = ep;
  122. phost->Ctrl.setup.wLength = 0;
  123. return USBH_CtrlTransfer(phost, 0, 0);
  124. }
  125. /******************************************************************************************************************************************
  126. * 函数名称: USBH_ParseCfgDesc()
  127. * 功能说明: 解析配置描述符
  128. * 输 入: 无
  129. * 输 出: 无
  130. * 注意事项: 无
  131. ******************************************************************************************************************************************/
  132. USBH_Status USBH_ParseCfgDesc(USBH_Info_t *phost, uint8_t *buff, uint16_t size)
  133. {
  134. USB_DescHeader_t *pdesc = (USB_DescHeader_t *)buff;
  135. uint8_t if_ix, ep_ix;
  136. memcpy(&phost->Device.Cfg_Desc, pdesc, sizeof(USB_CfgDesc_t));
  137. if(phost->Device.Cfg_Desc.bNumInterfaces > USBH_MAX_NUM_INTERFACES)
  138. {
  139. return USBH_NOT_SUPPORTED;
  140. }
  141. while((uint8_t *)pdesc < &buff[size])
  142. {
  143. pdesc = (USB_DescHeader_t *)((uint8_t *)pdesc + pdesc->bLength);
  144. switch(pdesc->bDescriptorType)
  145. {
  146. case USB_DESC_INTERFACE:
  147. if_ix = ((USB_IntfDesc_t *)pdesc)->bInterfaceNumber;
  148. if(if_ix >= USBH_MAX_NUM_INTERFACES)
  149. {
  150. return USBH_NOT_SUPPORTED;
  151. }
  152. memcpy(&phost->Device.Intf_Desc[if_ix], pdesc, sizeof(USB_IntfDesc_t));
  153. if(phost->Device.Intf_Desc[if_ix].bNumEndpoints > USBH_MAX_NUM_ENDPOINTS)
  154. {
  155. return USBH_NOT_SUPPORTED;
  156. }
  157. ep_ix = 0;
  158. break;
  159. case USB_DESC_ENDPOINT:
  160. memcpy(&phost->Device.Ep_Desc[if_ix][ep_ix], pdesc, sizeof(USB_EpDesc_t));
  161. if(++ep_ix >= USBH_MAX_NUM_ENDPOINTS)
  162. {
  163. return USBH_NOT_SUPPORTED;
  164. }
  165. break;
  166. case USB_DESC_HID:
  167. memcpy(&phost->Device.HID_Desc[if_ix], pdesc, sizeof(USB_HIDDesc_t));
  168. break;
  169. default:
  170. break;
  171. }
  172. }
  173. return USBH_OK ;
  174. }