usb_prop.c
上传用户:yyyd609
上传日期:2022-07-18
资源大小:183k
文件大小:7k
源码类别:

微处理器开发

开发平台:

C/C++

  1. /******************** (C) COPYRIGHT 2003 STMicroelectronics ********************
  2. * File Name          : usb_prop.c
  3. * Author             : MCD Application Team
  4. * Date First Issued  : 27/10/2003
  5. * Description        : All processings related to Mouse device
  6. *
  7. ********************************************************************************/
  8. #include "71x_lib.h"
  9. #include "USB_lib.h"
  10. #include "USB_conf.h"
  11. #include "USB_prop.h"
  12. #include "USB_desc.h"
  13. #include "USB_pwr.h"
  14. BYTE *Mouse_GetHidDescriptor(WORD Length);
  15. BYTE *Mouse_GetReportDescriptor(WORD Length);
  16. BYTE *Mouse_GetProtocolValue(WORD Length);
  17. RESULT Mouse_SetProtocol(void);
  18. BYTE ProtocolValue;
  19. #define USBHPIRQchannel 6
  20. #define USBLPIRQchannel 26
  21. /* ---------------------------------------------------------------------------- */
  22. /*  Structures initializations */
  23. /* ---------------------------------------------------------------------------- */
  24. DEVICE Device_Table = {
  25. /* ENDP0, */
  26. EP_NUM,
  27. 1
  28. };
  29. DEVICE_PROP Device_Property = {
  30. MOUSE_init,
  31. MOUSE_Reset,
  32. MOUSE_Status_In,
  33. MOUSE_Status_Out,
  34. MOUSE_Data_Setup,
  35. MOUSE_NoData_Setup,
  36. MOUSE_GetDeviceDescriptor,
  37. MOUSE_GetConfigDescriptor,
  38. MOUSE_GetStringDescriptor,
  39. /*MOUSE_EP0Buffer*/0,
  40. STD_MAXPACKETSIZE
  41. };
  42. /* ---------------------------------------------------------------------------- */
  43. /* ---------------------------------------------------------------------------- */
  44. void MOUSE_init()
  45. {
  46.  DEVICE_INFO *pInfo = &Device_Info;
  47. pInfo->Current_Configuration = 0;
  48. /* Connect the device */
  49. PowerOn();
  50.     /* USB interrupts initialization */
  51.     _SetISTR(0);               /* clear pending interrupts */
  52.     wInterrupt_Mask = IMR_MSK;
  53.     _SetCNTR(wInterrupt_Mask); /* set interrupts mask */
  54.         EIC_IRQChannelPriorityConfig(USBHP_IRQChannel,2);
  55.         EIC_IRQChannelPriorityConfig(USBLP_IRQChannel,2);
  56.         EIC_IRQChannelConfig(USBHP_IRQChannel,ENABLE);
  57.         EIC_IRQChannelConfig(USBLP_IRQChannel,ENABLE);
  58.         EIC_IRQConfig(ENABLE);
  59.         /* Wait until device is configured */
  60. while (pInfo->Current_Configuration == 0) NOP_Process();
  61. bDeviceState = CONFIGURED;
  62. } /* MOUSE_init() */
  63. /* ---------------------------------------------------------------------------- */
  64. /* ---------------------------------------------------------------------------- */
  65. void MOUSE_Reset()
  66. {
  67. /* Set MOUSE_DEVICE as not configured */
  68. Device_Info.Current_Configuration = 0;
  69. SetBTABLE(BTABLE_ADDRESS);
  70. /* Initialize Endpoint 0 */
  71. SetEPType(ENDP0, EP_CONTROL);
  72. SetEPTxStatus(ENDP0, EP_TX_NAK);
  73. SetEPRxAddr(ENDP0, ENDP0_RXADDR);
  74. SetEPRxCount(ENDP0, STD_MAXPACKETSIZE);
  75. SetEPTxAddr(ENDP0, ENDP0_TXADDR);
  76. SetEPTxCount(ENDP0, STD_MAXPACKETSIZE);
  77. Clear_Status_Out(ENDP0);
  78. SetEPRxValid(ENDP0);
  79. /* Initialize Endpoint 1 */
  80. SetEPType(ENDP1, EP_INTERRUPT);
  81. SetEPRxStatus(ENDP1, EP_RX_NAK);
  82. SetEPTxStatus(ENDP1, EP_TX_NAK);
  83. /* Set this device to response on default address */
  84. SetDeviceAddress(0);
  85. } /* MOUSE_Reset() */
  86. /* ---------------------------------------------------------------------------- */
  87. /* ---------------------------------------------------------------------------- */
  88. #define MOUSE_SetConfiguration Standard_SetConfiguration
  89. #define MOUSE_SetInterface Standard_SetInterface
  90. /* ---------------------------------------------------------------------------- */
  91. /* ---------------------------------------------------------------------------- */
  92. RESULT MOUSE_Data_Setup(BYTE RequestNo)
  93. {
  94. DEVICE_INFO *pInfo = &Device_Info;
  95. BYTE *(*CopyRoutine)(WORD);
  96. BYTE *pbLen;
  97. WORD wLen;
  98.     CopyRoutine = NULL;
  99. if (RequestNo == GET_CONFIGURATION) {
  100. if ( Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT) )
  101. CopyRoutine = Standard_GetConfiguration;
  102. }
  103. else
  104. if (RequestNo == GET_INTERFACE) {
  105. if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT))
  106. CopyRoutine = Standard_GetInterface;
  107. }
  108. else
  109. if (RequestNo == GET_DESCRIPTOR && Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) {
  110. if (pInfo->USBwIndex0 != 0)
  111. return UNSUPPORT;
  112. switch (pInfo->USBwValue1) {
  113. case HID_DESCRIPTOR:
  114. CopyRoutine = Mouse_GetHidDescriptor;
  115. break;
  116. case REPORT_DESCRIPTOR:
  117. CopyRoutine = Mouse_GetReportDescriptor;
  118. break;
  119. default:
  120. return UNSUPPORT;
  121. }
  122. } /* End of GET_DESCRIPTOR */
  123. else
  124. /*** GET_IDLE/GET_PROTOCOL ***/
  125. if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT)) {
  126.    if (RequestNo == GET_IDLE)
  127.    {
  128.         // Do something to return the idle time
  129.         return UNSUPPORT;
  130.    }
  131.        else
  132.    if (RequestNo == GET_PROTOCOL) {
  133. CopyRoutine = Mouse_GetProtocolValue;
  134.    }
  135.    else return UNSUPPORT;
  136. }
  137. else return UNSUPPORT;
  138. if (CopyRoutine == NULL) return UNSUPPORT;
  139. pInfo->Ctrl_Info.CopyData = CopyRoutine;
  140. pInfo->Ctrl_Info.Usb_wOffset = 0;
  141. pbLen = (*CopyRoutine)(0);
  142. wLen = (WORD)((DWORD)pbLen);
  143. pInfo->Ctrl_Info.Usb_wLength = wLen;
  144. return SUCCESS;
  145. } /* MOUSE_Data_Setup */
  146. /* ---------------------------------------------------------------------------- */
  147. /* ---------------------------------------------------------------------------- */
  148. RESULT MOUSE_NoData_Setup(BYTE RequestNo)
  149. {
  150. DEVICE_INFO *pInfo = &Device_Info;
  151. if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) {
  152. if (RequestNo == SET_CONFIGURATION)
  153. return MOUSE_SetConfiguration();
  154. }
  155. else
  156. if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) {
  157. if (RequestNo == SET_INTERFACE)
  158. return MOUSE_SetInterface();
  159. }
  160. /*** SET_IDLE/SET_PROTOCOL ***/
  161. else
  162. if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT)) {
  163.    if (RequestNo == SET_IDLE)
  164.    {
  165.         //Do something to set the idle time
  166.         return UNSUPPORT;
  167.    }
  168.        else
  169.    if (RequestNo == SET_PROTOCOL) {
  170.         return Mouse_SetProtocol();
  171.    }
  172.        else return UNSUPPORT;
  173. }
  174. return UNSUPPORT;
  175. } /* MOUSE_NoData_Setup */
  176. /* ---------------------------------------------------------------------------- */
  177. /* ---------------------------------------------------------------------------- */
  178. ONE_DESCRIPTOR Device_Descriptor = {
  179. (BYTE*)MOUSE_DeviceDescriptor,
  180. MOUSE_SIZ_DEVICE_DESC
  181. };
  182. BYTE *MOUSE_GetDeviceDescriptor(WORD Length)
  183. {
  184. return Standard_GetDescriptorData( Length, &Device_Descriptor );
  185. }
  186. ONE_DESCRIPTOR Config_Descriptor = {
  187. (BYTE*)MOUSE_ConfigDescriptor,
  188. MOUSE_SIZ_CONFIG_DESC
  189. };
  190. BYTE *MOUSE_GetConfigDescriptor(WORD Length)
  191. {
  192. return Standard_GetDescriptorData( Length, &Config_Descriptor );
  193. }
  194. ONE_DESCRIPTOR String_Descriptor = {
  195. (BYTE*)MOUSE_StringDescriptor,
  196. MOUSE_SIZ_STRING_DESC
  197. };
  198. BYTE *MOUSE_GetStringDescriptor(WORD Length)
  199. {
  200. return Standard_GetStringDescriptor( Length, &String_Descriptor );
  201. }
  202. ONE_DESCRIPTOR Mouse_Hid_Descriptor = {
  203. (BYTE *)MOUSE_ConfigDescriptor + MOUSE_OFF_HID_DESC,
  204. MOUSE_SIZ_HID_DESC
  205. };
  206. BYTE *Mouse_GetHidDescriptor(WORD Length)
  207. {
  208. return Standard_GetDescriptorData( Length, &Mouse_Hid_Descriptor );
  209. }
  210. ONE_DESCRIPTOR Mouse_Report_Descriptor = {
  211. (BYTE *)MOUSE_ReportDescriptor,
  212. MOUSE_SIZ_REPORT_DESC
  213. };
  214. BYTE *Mouse_GetReportDescriptor(WORD Length)
  215. {
  216. return Standard_GetDescriptorData( Length, &Mouse_Report_Descriptor );
  217. }
  218. BYTE *Mouse_GetProtocolValue(WORD Length)
  219. {
  220.   return (BYTE *)&ProtocolValue;
  221. }
  222. RESULT Mouse_SetProtocol(void)
  223. {
  224. BYTE wValue0 = pInformation->USBwValue0;
  225. ProtocolValue = wValue0;
  226. return SUCCESS;
  227. }