usbhspecific.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. //*****************************************************************************
  2. //
  3. // usbhSPECIFIC.c - This file contains the host SPECIFIC driver.
  4. //
  5. // Copyright (c) 2008-2010 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Texas Instruments (TI) is supplying this software for use solely and
  9. // exclusively on TI's microcontroller products. The software is owned by
  10. // TI and/or its suppliers, and is protected under applicable copyright
  11. // laws. You may not combine this software with "viral" open-source
  12. // software in order to form a larger program.
  13. //
  14. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  15. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  16. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  18. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  19. // DAMAGES, FOR ANY REASON WHATSOEVER.
  20. //
  21. // This is part of AM1808 StarterWare USB Library, resused from revision 6288 of the
  22. // stellaris USB Library
  23. //
  24. //*****************************************************************************
  25. #include "usbhspecific.h"
  26. extern tUSBHCD g_sUSBHCD[];
  27. static void * SPECIFICDriverOpen(tUSBHostDevice *pDevice, unsigned int ulDeviceInst);
  28. static void SPECIFICDriverClose(void *pvInstance);
  29. //*****************************************************************************
  30. //
  31. //! \addtogroup usblib_host_class
  32. //! @{
  33. //
  34. //*****************************************************************************
  35. //*****************************************************************************
  36. //
  37. // The instance data storage for attached SPECIFIC devices.
  38. //
  39. //*****************************************************************************
  40. tSPECIFICInstance g_SPECIFICDevice[] =
  41. {
  42. {
  43. false,
  44. 0,
  45. 0,
  46. 0,
  47. USBH_SPECIFIC_DEV_NONE,
  48. 0,
  49. 0,
  50. 0,
  51. 0
  52. },
  53. {
  54. false,
  55. 0,
  56. 0,
  57. 0,
  58. USBH_SPECIFIC_DEV_NONE,
  59. 0,
  60. 0,
  61. 0,
  62. 1
  63. }
  64. };
  65. tSPECIFICInstance g_SPECIFICInstance =
  66. {
  67. false,
  68. 0,
  69. 0,
  70. 0,
  71. USBH_SPECIFIC_DEV_NONE,
  72. 0,
  73. 0,
  74. 0,
  75. 0
  76. };
  77. //*****************************************************************************
  78. //
  79. //! This constant global structure defines the SPECIFIC Class Driver that is
  80. //! provided with the USB library.
  81. //
  82. //*****************************************************************************
  83. const tUSBHostClassDriver g_USBSPECIFICClassDriver =
  84. {
  85. USB_CLASS_CDC,
  86. SPECIFICDriverOpen,
  87. SPECIFICDriverClose,
  88. 0
  89. };
  90. //*****************************************************************************
  91. //
  92. //! This function is used to open an instance of a SPECIFIC device.
  93. //!
  94. //! \param eDeviceType is the type of device that should be loaded for this
  95. //! instance of the SPECIFIC device.
  96. //! \param pfnCallback is the function that will be called whenever changes
  97. //! are detected for this device.
  98. //! \param ulCBData is the data that will be returned in when the pfnCallback
  99. //! function is called.
  100. //!
  101. //! This function creates an instance of an specific type of SPECIFIC device. The
  102. //! \e eDeviceType parameter is one subclass/protocol values of the types
  103. //! specified in enumerated types tSPECIFICSubClassProtocol. Only devices that
  104. //! enumerate with this type will be called back via the \e pfnCallback
  105. //! function. The \e pfnCallback parameter is the callback function for any
  106. //! events that occur for this device type. The \e pfnCallback function must
  107. //! point to a valid function of type \e tUSBCallback for this call to complete
  108. //! successfully. To release this device instance the caller of USBHSPECIFICOpen()
  109. //! should call USBHSPECIFICClose() and pass in the value returned from the
  110. //! USBHSPECIFICOpen() call.
  111. //!
  112. //! \return This function returns and instance value that should be used with
  113. //! any other APIs that require an instance value. If a value of 0 is returned
  114. //! then the device instance could not be created.
  115. //
  116. //*****************************************************************************
  117. unsigned int
  118. USBHSPECIFICOpen(unsigned int ulIndex,tSPECIFICSubClassProtocol eDeviceType,
  119. tUSBCallback pfnCallback, unsigned int ulCBData)
  120. {
  121. //
  122. // Only one callback is supported.
  123. //
  124. if(g_SPECIFICDevice[ulIndex].pfnCallback)
  125. {
  126. return (0);
  127. }
  128. //
  129. // Save the instance data for this device.
  130. //
  131. g_SPECIFICDevice[ulIndex].ulIndex = ulIndex;
  132. g_SPECIFICDevice[ulIndex].pfnCallback = pfnCallback;
  133. g_SPECIFICDevice[ulIndex].eDeviceType = eDeviceType;
  134. g_SPECIFICDevice[ulIndex].ulCBData = ulCBData;
  135. //
  136. // Return the device instance pointer.
  137. //
  138. return((unsigned int)&g_SPECIFICDevice[ulIndex]);
  139. }
  140. //*****************************************************************************
  141. //
  142. //! This function is used to release an instance of a SPECIFIC device.
  143. //!
  144. //! \param ulSPECIFICInstance is the instance value for a SPECIFIC device to release.
  145. //!
  146. //! This function releases an instance of a SPECIFIC device that was created by a
  147. //! call to USBHSPECIFICOpen(). This call is required to allow other SPECIFIC devices
  148. //! to be enumerated after another SPECIFIC device has been disconnected. The
  149. //! \e ulSPECIFICInstance parameter should hold the value that was returned from the
  150. //! previous call to USBHSPECIFICOpen().
  151. //!
  152. //! \return None.
  153. //
  154. //*****************************************************************************
  155. void
  156. USBHSPECIFICClose(unsigned int ulSPECIFICInstance)
  157. {
  158. tSPECIFICInstance *pSPECIFICInstance;
  159. pSPECIFICInstance = (tSPECIFICInstance *)ulSPECIFICInstance;
  160. //
  161. // Disable any more notification from the SPECIFIC layer.
  162. //
  163. pSPECIFICInstance->pfnCallback = 0;
  164. pSPECIFICInstance->eDeviceType = USBH_SPECIFIC_DEV_NONE;
  165. }
  166. //*****************************************************************************
  167. //
  168. // This function handles callbacks for the interrupt IN endpoint.
  169. //
  170. //*****************************************************************************
  171. #include <stdio.h>
  172. #include "tr_queue.h"
  173. unsigned char pucBuffer[64];
  174. static void
  175. SPECIFICIntINCallback(unsigned int ulIndex, unsigned int ulPipe, unsigned int ulEvent)
  176. {
  177. switch (ulEvent)
  178. {
  179. //
  180. // Handles a request to schedule a new request on the interrupt IN
  181. // pipe.
  182. //
  183. case USB_EVENT_SCHEDULER:
  184. {
  185. USBHCDPipeSchedule(ulIndex, ulPipe, 0, 1);
  186. break;
  187. }
  188. //
  189. // Called when new data is available on the interrupt IN pipe.
  190. //
  191. case USB_EVENT_RX_AVAILABLE:
  192. {
  193. //
  194. // Send the report data to the USB host SPECIFIC device class driver.
  195. //
  196. unsigned int ulSize = USBHCDPipeReadNonBlocking(ulIndex, ulPipe, pucBuffer, 64);
  197. if(enqueueFromUsb(pucBuffer, ulSize)){
  198. // printf(",添加到队列");
  199. }
  200. break;
  201. }
  202. }
  203. }
  204. unsigned int USBHSPECIFICWrite(unsigned char *pucData, unsigned int ulSize)
  205. {
  206. return USBHCDPipeWrite(g_SPECIFICInstance.ulIndex, g_SPECIFICInstance.ulBulkOutPipe, pucData, ulSize);
  207. }
  208. //*****************************************************************************
  209. //
  210. //! This function is used to open an instance of the SPECIFIC driver.
  211. //!
  212. //! \param pDevice is a pointer to the device information structure.
  213. //!
  214. //! This function will attempt to open an instance of the SPECIFIC driver based on
  215. //! the information contained in the pDevice structure. This call can fail if
  216. //! there are not sufficient resources to open the device. The function will
  217. //! return a value that should be passed back into USBSPECIFICClose() when the
  218. //! driver is no longer needed.
  219. //!
  220. //! \return The function will return a pointer to a SPECIFIC driver instance.
  221. //
  222. //*****************************************************************************
  223. #include <stdio.h>
  224. unsigned char DataBuffer1[64] = {'\0'};
  225. static void *
  226. SPECIFICDriverOpen(tUSBHostDevice *pDevice, unsigned int ulInstance)
  227. {
  228. printf("SPECIFICDriverOpen...\r\n");
  229. unsigned int ulIndex = 0;
  230. // 端点2,中断,IN
  231. // g_SPECIFICInstance.ulIntInPipe = USBHCDPipeAlloc(ulIndex, USBHCD_PIPE_INTR_IN, 0x01, SPECIFICIntINCallback);
  232. // USBHCDPipeConfig(ulIndex, g_SPECIFICInstance.ulIntInPipe, 0x0A, 0xff, (0x82 & USB_EP_DESC_NUM_M));
  233. // 端点1,批量,IN
  234. g_SPECIFICInstance.ulBulkInPipe = USBHCDPipeAllocSize(ulIndex, USBHCD_PIPE_BULK_IN, pDevice->ulAddress, 0x40, SPECIFICIntINCallback);
  235. USBHCDPipeConfig(ulIndex, g_SPECIFICInstance.ulBulkInPipe, 0x40, 0, (0x81 & USB_EP_DESC_NUM_M));
  236. // 端点1,批量,OUT
  237. g_SPECIFICInstance.ulBulkOutPipe = USBHCDPipeAllocSize(ulIndex, USBHCD_PIPE_BULK_OUT, pDevice->ulAddress, 0x40, 0);
  238. USBHCDPipeConfig(ulIndex, g_SPECIFICInstance.ulBulkOutPipe, 0x40, 0, (0x01 & USB_EP_DESC_NUM_M));
  239. g_SPECIFICInstance.isConnected = true;
  240. return ((void *)1);
  241. }
  242. //*****************************************************************************
  243. //
  244. //! This function is used to release an instance of the SPECIFIC driver.
  245. //!
  246. //! \param pvInstance is an instance pointer that needs to be released.
  247. //!
  248. //! This function will free up any resources in use by the SPECIFIC driver instance
  249. //! that is passed in. The \e pvInstance pointer should be a valid value that
  250. //! was returned from a call to USBSPECIFICOpen().
  251. //!
  252. //! \return None.
  253. //
  254. //*****************************************************************************
  255. static void
  256. SPECIFICDriverClose(void *pvInstance)
  257. {
  258. g_SPECIFICInstance.isConnected = false;
  259. printf("SPECIFICDriverClose...\r\n");
  260. // tSPECIFICInstance *pSPECIFICInstance;
  261. // pSPECIFICInstance = (tSPECIFICInstance *)pvInstance;
  262. //
  263. // No device so just exit.
  264. //
  265. // if(g_SPECIFICInstance->pDevice == 0)
  266. // {
  267. // return;
  268. // }
  269. //
  270. // Reset the device pointer.
  271. //
  272. g_SPECIFICInstance.pDevice = 0;
  273. //
  274. // Free the Interrupt IN pipe.
  275. //
  276. if(g_SPECIFICInstance.ulIntInPipe != 0)
  277. {
  278. USBHCDPipeFree(g_SPECIFICInstance.ulIndex, g_SPECIFICInstance.ulIntInPipe);
  279. }
  280. if(g_SPECIFICInstance.ulBulkInPipe != 0)
  281. {
  282. USBHCDPipeFree(g_SPECIFICInstance.ulIndex, g_SPECIFICInstance.ulBulkInPipe);
  283. }
  284. if(g_SPECIFICInstance.ulBulkOutPipe != 0)
  285. {
  286. USBHCDPipeFree(g_SPECIFICInstance.ulIndex, g_SPECIFICInstance.ulBulkOutPipe);
  287. }
  288. //
  289. // If the callback exists, call it with an Open event.
  290. //
  291. if(g_SPECIFICInstance.pfnCallback != 0)
  292. {
  293. g_SPECIFICInstance.pfnCallback((void *)g_SPECIFICInstance.ulCBData,
  294. USB_EVENT_DISCONNECTED,
  295. (unsigned int)&g_SPECIFICInstance, 0);
  296. }
  297. }
  298. //*****************************************************************************
  299. //
  300. //! This function is used to set the idle timeout for a SPECIFIC device.
  301. //!
  302. //! \param ulInstance is the value that was returned from the call to
  303. //! USBHSPECIFICOpen().
  304. //! \param ucDuration is the duration of the timeout in milliseconds.
  305. //! \param ucReportID is the report identifier to set the timeout on.
  306. //!
  307. //! This function will send the Set Idle command to a SPECIFIC device to set the
  308. //! idle timeout for a given report. The length of the timeout is specified
  309. //! by the \e ucDuration parameter and the report the timeout for is in the
  310. //! \e ucReportID value.
  311. //!
  312. //! \return Always returns 0.
  313. //
  314. //*****************************************************************************
  315. unsigned int
  316. USBHSPECIFICSetIdle(unsigned int ulInstance, unsigned char ucDuration,
  317. unsigned char ucReportID)
  318. {
  319. return (0);
  320. }
  321. //*****************************************************************************
  322. //
  323. //! This function can be used to retrieve the report descriptor for a given
  324. //! device instance.
  325. //!
  326. //! \param ulInstance is the value that was returned from the call to
  327. //! USBHSPECIFICOpen().
  328. //! \param pucBuffer is the memory buffer to use to store the report
  329. //! descriptor.
  330. //! \param ulSize is the size in bytes of the buffer pointed to by
  331. //! \e pucBuffer.
  332. //!
  333. //! This function is used to return a report descriptor from a SPECIFIC device
  334. //! instance so that it can determine how to interpret reports that are
  335. //! returned from the device indicated by the \e ulInstance parameter.
  336. //! This call is blocking and will return the number of bytes read into the
  337. //! \e pucBuffer.
  338. //!
  339. //! \return Returns the number of bytes read into the \e pucBuffer.
  340. //
  341. //*****************************************************************************
  342. unsigned int
  343. USBHSPECIFICGetReportDescriptor(unsigned int ulInstance, unsigned char *pucBuffer,
  344. unsigned int ulSize)
  345. {
  346. return (ulSize);
  347. }
  348. //*****************************************************************************
  349. //
  350. //! This function is used to set or clear the boot protocol state of a device.
  351. //!
  352. //! \param ulInstance is the value that was returned from the call to
  353. //! USBHSPECIFICOpen().
  354. //! \param ulBootProtocol is either zero or non-zero to indicate which protocol
  355. //! to use for the device.
  356. //!
  357. //! A USB host device can use this function to set the protocol for a connected
  358. //! SPECIFIC device. This is commonly used to set keyboards and mice into their
  359. //! simplified boot protocol modes to fix the report structure to a know
  360. //! state.
  361. //!
  362. //! \return This function returns 0.
  363. //
  364. //*****************************************************************************
  365. unsigned int
  366. USBHSPECIFICSetProtocol(unsigned int ulInstance, unsigned int ulBootProtocol)
  367. {
  368. return (0);
  369. }
  370. //*****************************************************************************
  371. //
  372. //! This function is used to retrieve a report from a SPECIFIC device.
  373. //!
  374. //! \param ulInstance is the value that was returned from the call to
  375. //! USBHSPECIFICOpen().
  376. //! \param ulInterface is the interface to retrieve the report from.
  377. //! \param pucData is the memory buffer to use to store the report.
  378. //! \param ulSize is the size in bytes of the buffer pointed to by
  379. //! \e pucBuffer.
  380. //!
  381. //! This function is used to retrieve a report from a USB pipe. It is usually
  382. //! called when the USB SPECIFIC layer has detected a new data available in a USB
  383. //! pipe. The USB SPECIFIC host device code will receive a
  384. //! \b USB_EVENT_RX_AVAILABLE event when data is available, allowing the
  385. //! callback function to retrieve the data.
  386. //!
  387. //! \return Returns the number of bytes read from report.
  388. //
  389. //*****************************************************************************
  390. unsigned int
  391. USBHSPECIFICGetReport(unsigned int ulInstance,
  392. unsigned int ulInterface,
  393. unsigned char *pucData,
  394. unsigned int ulSize)
  395. {
  396. tSPECIFICInstance *pSPECIFICInstance;
  397. //
  398. // Cast the instance pointer to the correct type for ease of use.
  399. //
  400. pSPECIFICInstance = (tSPECIFICInstance *)ulInstance;
  401. //
  402. // Read the Data out.
  403. //
  404. ulSize = USBHCDPipeReadNonBlocking(pSPECIFICInstance->ulIndex, pSPECIFICInstance
  405. ->ulIntInPipe, pucData, ulSize);
  406. //
  407. // Return the number of bytes read from the interrupt in pipe.
  408. //
  409. return(ulSize);
  410. }
  411. //*****************************************************************************
  412. //
  413. //! This function is used to send a report to a SPECIFIC device.
  414. //!
  415. //! \param ulInstance is the value that was returned from the call to
  416. //! USBHSPECIFICOpen().
  417. //! \param ulInterface is the interface to send the report to.
  418. //! \param pucData is the memory buffer to use to store the report.
  419. //! \param ulSize is the size in bytes of the buffer pointed to by
  420. //! \e pucBuffer.
  421. //!
  422. //! This function is used to send a report to a USB SPECIFIC device. It can be
  423. //! only be called from outside the callback context as this function will not
  424. //! return from the call until the data has been sent successfully.
  425. //!
  426. //! \return Returns the number of bytes sent to the device.
  427. //
  428. //*****************************************************************************
  429. unsigned int
  430. USBHSPECIFICSetReport(unsigned int ulInstance, unsigned int ulInterface,
  431. unsigned char *pucData, unsigned int ulSize)
  432. {
  433. return (ulSize);
  434. }
  435. //*****************************************************************************
  436. //
  437. //! @}
  438. //
  439. //*****************************************************************************