usbhspecific.c 16 KB

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