Chap_9.c
上传用户:lyfy_2008
上传日期:2014-07-13
资源大小:3016k
文件大小:7k
源码类别:

USB编程

开发平台:

Visual C++

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <reg51.h>                /* special function register declarations   */
  4. //#include "epphal.h"
  5. #include "d12ci.h"
  6. #include "mainloop.h"
  7. #include "usb100.h"
  8. #include "chap_9.h"
  9. #define NUM_ENDPOINTS 4
  10. #define CONFIG_DESCRIPTOR_LENGTH    sizeof(USB_CONFIGURATION_DESCRIPTOR) 
  11. + sizeof(USB_INTERFACE_DESCRIPTOR) 
  12. + (NUM_ENDPOINTS * sizeof(USB_ENDPOINT_DESCRIPTOR))
  13. extern CONTROL_XFER ControlData;
  14. extern IO_REQUEST idata ioRequest;
  15. extern EPPFLAGS bEPPflags;
  16. //设备描述符
  17. code USB_DEVICE_DESCRIPTOR DeviceDescr =
  18. {
  19. sizeof(USB_DEVICE_DESCRIPTOR),
  20.     USB_DEVICE_DESCRIPTOR_TYPE,
  21.     SWAP(0x0110),
  22.     USB_CLASS_CODE_TEST_CLASS_DEVICE,
  23.     0, 0,
  24. EP0_PACKET_SIZE,
  25. SWAP(0x0471),
  26. SWAP(0x0677),
  27. SWAP(0x0100),
  28.     0, 0, 0,
  29. 1
  30. };
  31. //配置描述符
  32. code USB_CONFIGURATION_DESCRIPTOR ConfigDescr =
  33. {
  34.     sizeof(USB_CONFIGURATION_DESCRIPTOR),
  35.     USB_CONFIGURATION_DESCRIPTOR_TYPE,
  36.     SWAP(CONFIG_DESCRIPTOR_LENGTH),
  37. 1,
  38. 1,
  39.     0,
  40. 0x60,//自供电
  41. 0x32
  42. };
  43. //接口描述符
  44. code USB_INTERFACE_DESCRIPTOR InterfaceDescr =
  45. {
  46.     sizeof(USB_INTERFACE_DESCRIPTOR),
  47.     USB_INTERFACE_DESCRIPTOR_TYPE,
  48.     0,
  49.     0,
  50. NUM_ENDPOINTS,
  51. USB_CLASS_CODE_TEST_CLASS_DEVICE,
  52. USB_SUBCLASS_CODE_TEST_CLASS_D12,
  53. USB_PROTOCOL_CODE_TEST_CLASS_D12,
  54. 0
  55. };
  56. //端点描述符
  57. code USB_ENDPOINT_DESCRIPTOR EP1_TXDescr =
  58. {
  59. sizeof(USB_ENDPOINT_DESCRIPTOR),
  60. USB_ENDPOINT_DESCRIPTOR_TYPE,
  61. 0x81,
  62. USB_ENDPOINT_TYPE_INTERRUPT,
  63. SWAP(EP1_PACKET_SIZE),
  64. 1
  65. };
  66. code USB_ENDPOINT_DESCRIPTOR EP1_RXDescr =
  67. {
  68. sizeof(USB_ENDPOINT_DESCRIPTOR),
  69. USB_ENDPOINT_DESCRIPTOR_TYPE,
  70. 0x1,
  71. USB_ENDPOINT_TYPE_INTERRUPT,
  72. SWAP(EP1_PACKET_SIZE),
  73. 1
  74. };
  75. code USB_ENDPOINT_DESCRIPTOR EP2_TXDescr =
  76. {
  77. sizeof(USB_ENDPOINT_DESCRIPTOR),
  78. USB_ENDPOINT_DESCRIPTOR_TYPE,
  79. 0x82,
  80. USB_ENDPOINT_TYPE_BULK,
  81. SWAP(EP2_PACKET_SIZE),
  82. 10
  83. };
  84. code USB_ENDPOINT_DESCRIPTOR EP2_RXDescr =
  85. {
  86. sizeof(USB_ENDPOINT_DESCRIPTOR),
  87. USB_ENDPOINT_DESCRIPTOR_TYPE,
  88. 0x2,
  89. USB_ENDPOINT_TYPE_BULK,
  90. SWAP(EP2_PACKET_SIZE),
  91. 10
  92. };
  93. /*
  94.    //*************************************************************************
  95.    // USB Protocol Layer
  96.    //*************************************************************************
  97. */
  98. void reserved(void)
  99. {
  100. stall_ep0();
  101. }
  102. /*
  103.    //*************************************************************************
  104.    // USB standard device requests
  105.    //*************************************************************************
  106. */
  107. //获取设备状态
  108. void get_status(void)
  109. {
  110. unsigned char endp, txdat[2];
  111. unsigned char bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
  112. unsigned char c;
  113. if (bRecipient == USB_RECIPIENT_DEVICE) {
  114. if(bEPPflags.bits.remote_wakeup == 1)//获取远程唤醒状态
  115. txdat[0] = 3;
  116. else
  117. txdat[0] = 1;
  118. txdat[1]=0;
  119. single_transmit(txdat, 2);
  120. } else if (bRecipient == USB_RECIPIENT_INTERFACE) {//获取接口状态
  121. txdat[0]=0;
  122. txdat[1]=0;
  123. single_transmit(txdat, 2);
  124. } else if (bRecipient == USB_RECIPIENT_ENDPOINT) {//获取端点状态
  125. endp = (unsigned char)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
  126. if (ControlData.DeviceRequest.wIndex & (unsigned char)USB_ENDPOINT_DIRECTION_MASK)
  127. c = D12_SelectEndpoint(endp*2 + 1); /* Control-in */
  128. else
  129. c = D12_SelectEndpoint(endp*2); /* Control-out */
  130. if(c & D12_STALL)
  131. txdat[0] = 1;
  132. else
  133. txdat[0] = 0;
  134. txdat[1] = 0;
  135. single_transmit(txdat, 2);
  136. } else
  137. stall_ep0();
  138. }
  139. //特性清除
  140. void clear_feature(void)
  141. {
  142. unsigned char endp;
  143. unsigned char bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
  144. if (bRecipient == USB_RECIPIENT_DEVICE
  145. && ControlData.DeviceRequest.wValue == USB_FEATURE_REMOTE_WAKEUP) {//清除远程唤醒功能
  146. DISABLE;
  147. bEPPflags.bits.remote_wakeup = 0;
  148. ENABLE;
  149. single_transmit(0, 0);
  150. }
  151. else if (bRecipient == USB_RECIPIENT_ENDPOINT
  152. && ControlData.DeviceRequest.wValue == USB_FEATURE_ENDPOINT_STALL) {//清除端点stall
  153. endp = (unsigned char)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
  154. if (ControlData.DeviceRequest.wIndex & (unsigned char)USB_ENDPOINT_DIRECTION_MASK)
  155. /* clear TX stall for IN on EPn. */
  156. D12_SetEndpointStatus(endp*2 + 1, 0);
  157. else
  158. /* clear RX stall for OUT on EPn. */
  159. D12_SetEndpointStatus(endp*2, 0);
  160. single_transmit(0, 0);
  161. } else
  162. stall_ep0();
  163. }
  164. //特性设置
  165. void set_feature(void)
  166. {
  167. unsigned char endp;
  168. unsigned char bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
  169. if (bRecipient == USB_RECIPIENT_DEVICE
  170. && ControlData.DeviceRequest.wValue == USB_FEATURE_REMOTE_WAKEUP) {//设置远程唤醒功能
  171. DISABLE;
  172. bEPPflags.bits.remote_wakeup = 1;
  173. ENABLE;
  174. single_transmit(0, 0);
  175. }
  176. else if (bRecipient == USB_RECIPIENT_ENDPOINT
  177. && ControlData.DeviceRequest.wValue == USB_FEATURE_ENDPOINT_STALL) {//设置端点stall
  178. endp = (unsigned char)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
  179. if (ControlData.DeviceRequest.wIndex & (unsigned char)USB_ENDPOINT_DIRECTION_MASK)
  180. /* clear TX stall for IN on EPn. */
  181. D12_SetEndpointStatus(endp*2 + 1, 1);
  182. else
  183. /* clear RX stall for OUT on EPn. */
  184. D12_SetEndpointStatus(endp*2, 1);
  185. single_transmit(0, 0);
  186. } else
  187. stall_ep0();
  188. }
  189. //设置地址
  190. void set_address(void)
  191. {
  192. D12_SetAddressEnable((unsigned char)(ControlData.DeviceRequest.wValue &
  193. DEVICE_ADDRESS_MASK), 1);
  194. single_transmit(0, 0);
  195. }
  196. //获取描述符
  197. void get_descriptor(void)
  198. {
  199. unsigned char bDescriptor = MSB(ControlData.DeviceRequest.wValue);
  200. if (bDescriptor == USB_DEVICE_DESCRIPTOR_TYPE) {//获取设备描述符
  201. code_transmit((unsigned char code *)&DeviceDescr, sizeof(USB_DEVICE_DESCRIPTOR));
  202. } else if (bDescriptor == USB_CONFIGURATION_DESCRIPTOR_TYPE) {//获取配置描述符
  203. code_transmit((unsigned char code *)&ConfigDescr, CONFIG_DESCRIPTOR_LENGTH);
  204. } else
  205. stall_ep0();
  206. }
  207. //获取配置状态
  208. void get_configuration(void)
  209. {
  210. unsigned char c = bEPPflags.bits.configuration;
  211. single_transmit(&c, 1);
  212. }
  213. //设置配置状态
  214. void set_configuration(void)
  215. {
  216. if (ControlData.DeviceRequest.wValue == 0) {
  217. //设备进入未配置状态
  218. single_transmit(0, 0);
  219. DISABLE;
  220. bEPPflags.bits.configuration = 0;
  221. ENABLE;
  222. init_unconfig();
  223. } else if (ControlData.DeviceRequest.wValue == 1) {
  224. //配置设备
  225. single_transmit(0, 0);
  226. init_unconfig();
  227. init_config();
  228. DISABLE;
  229. bEPPflags.bits.configuration = 1;
  230. ENABLE;
  231. } else
  232. stall_ep0();
  233. }
  234. //获取接口状态
  235. void get_interface(void)
  236. {
  237. unsigned char txdat = 0;        /* Only/Current interface = 0 */
  238. single_transmit(&txdat, 1);
  239. }
  240. //设置接口状态
  241. void set_interface(void)
  242. {
  243. if (ControlData.DeviceRequest.wValue == 0 && ControlData.DeviceRequest.wIndex == 0)
  244. single_transmit(0, 0);
  245. else
  246. stall_ep0();
  247. }