usbhspecific.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. if(g_SPECIFICInstance.isConnected){
  207. return USBHCDPipeWrite(g_SPECIFICInstance.ulIndex, g_SPECIFICInstance.ulBulkOutPipe, pucData, ulSize);
  208. }else{
  209. return 0;
  210. }
  211. }
  212. //*****************************************************************************
  213. //
  214. //! This function is used to open an instance of the SPECIFIC driver.
  215. //!
  216. //! \param pDevice is a pointer to the device information structure.
  217. //!
  218. //! This function will attempt to open an instance of the SPECIFIC driver based on
  219. //! the information contained in the pDevice structure. This call can fail if
  220. //! there are not sufficient resources to open the device. The function will
  221. //! return a value that should be passed back into USBSPECIFICClose() when the
  222. //! driver is no longer needed.
  223. //!
  224. //! \return The function will return a pointer to a SPECIFIC driver instance.
  225. //
  226. //*****************************************************************************
  227. #include <stdio.h>
  228. #include "device/console.h"
  229. unsigned char DataBuffer1[64] = {'\0'};
  230. static void *
  231. SPECIFICDriverOpen(tUSBHostDevice *pDevice, unsigned int ulInstance)
  232. {
  233. ConsolePuts("SPECIFICDriverOpen...\r\n",-1);
  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. ConsolePuts("SPECIFICDriverClose...\r\n",-1);
  266. printf("SPECIFICDriverClose...\r\n");
  267. // tSPECIFICInstance *pSPECIFICInstance;
  268. // pSPECIFICInstance = (tSPECIFICInstance *)pvInstance;
  269. //
  270. // No device so just exit.
  271. //
  272. // if(g_SPECIFICInstance->pDevice == 0)
  273. // {
  274. // return;
  275. // }
  276. //
  277. // Reset the device pointer.
  278. //
  279. g_SPECIFICInstance.pDevice = 0;
  280. //
  281. // Free the Interrupt IN pipe.
  282. //
  283. if(g_SPECIFICInstance.ulIntInPipe != 0)
  284. {
  285. USBHCDPipeFree(g_SPECIFICInstance.ulIndex, g_SPECIFICInstance.ulIntInPipe);
  286. }
  287. if(g_SPECIFICInstance.ulBulkInPipe != 0)
  288. {
  289. USBHCDPipeFree(g_SPECIFICInstance.ulIndex, g_SPECIFICInstance.ulBulkInPipe);
  290. }
  291. if(g_SPECIFICInstance.ulBulkOutPipe != 0)
  292. {
  293. USBHCDPipeFree(g_SPECIFICInstance.ulIndex, g_SPECIFICInstance.ulBulkOutPipe);
  294. }
  295. //
  296. // If the callback exists, call it with an Open event.
  297. //
  298. if(g_SPECIFICInstance.pfnCallback != 0)
  299. {
  300. g_SPECIFICInstance.pfnCallback((void *)g_SPECIFICInstance.ulCBData,
  301. USB_EVENT_DISCONNECTED,
  302. (unsigned int)&g_SPECIFICInstance, 0);
  303. }
  304. }
  305. //*****************************************************************************
  306. //
  307. //! This function is used to set the idle timeout for a SPECIFIC device.
  308. //!
  309. //! \param ulInstance is the value that was returned from the call to
  310. //! USBHSPECIFICOpen().
  311. //! \param ucDuration is the duration of the timeout in milliseconds.
  312. //! \param ucReportID is the report identifier to set the timeout on.
  313. //!
  314. //! This function will send the Set Idle command to a SPECIFIC device to set the
  315. //! idle timeout for a given report. The length of the timeout is specified
  316. //! by the \e ucDuration parameter and the report the timeout for is in the
  317. //! \e ucReportID value.
  318. //!
  319. //! \return Always returns 0.
  320. //
  321. //*****************************************************************************
  322. unsigned int
  323. USBHSPECIFICSetIdle(unsigned int ulInstance, unsigned char ucDuration,
  324. unsigned char ucReportID)
  325. {
  326. return (0);
  327. }
  328. //*****************************************************************************
  329. //
  330. //! This function can be used to retrieve the report descriptor for a given
  331. //! device instance.
  332. //!
  333. //! \param ulInstance is the value that was returned from the call to
  334. //! USBHSPECIFICOpen().
  335. //! \param pucBuffer is the memory buffer to use to store the report
  336. //! descriptor.
  337. //! \param ulSize is the size in bytes of the buffer pointed to by
  338. //! \e pucBuffer.
  339. //!
  340. //! This function is used to return a report descriptor from a SPECIFIC device
  341. //! instance so that it can determine how to interpret reports that are
  342. //! returned from the device indicated by the \e ulInstance parameter.
  343. //! This call is blocking and will return the number of bytes read into the
  344. //! \e pucBuffer.
  345. //!
  346. //! \return Returns the number of bytes read into the \e pucBuffer.
  347. //
  348. //*****************************************************************************
  349. unsigned int
  350. USBHSPECIFICGetReportDescriptor(unsigned int ulInstance, unsigned char *pucBuffer,
  351. unsigned int ulSize)
  352. {
  353. return (ulSize);
  354. }
  355. //*****************************************************************************
  356. //
  357. //! This function is used to set or clear the boot protocol state of a device.
  358. //!
  359. //! \param ulInstance is the value that was returned from the call to
  360. //! USBHSPECIFICOpen().
  361. //! \param ulBootProtocol is either zero or non-zero to indicate which protocol
  362. //! to use for the device.
  363. //!
  364. //! A USB host device can use this function to set the protocol for a connected
  365. //! SPECIFIC device. This is commonly used to set keyboards and mice into their
  366. //! simplified boot protocol modes to fix the report structure to a know
  367. //! state.
  368. //!
  369. //! \return This function returns 0.
  370. //
  371. //*****************************************************************************
  372. unsigned int
  373. USBHSPECIFICSetProtocol(unsigned int ulInstance, unsigned int ulBootProtocol)
  374. {
  375. return (0);
  376. }
  377. //*****************************************************************************
  378. //
  379. //! This function is used to retrieve a report from a SPECIFIC device.
  380. //!
  381. //! \param ulInstance is the value that was returned from the call to
  382. //! USBHSPECIFICOpen().
  383. //! \param ulInterface is the interface to retrieve the report from.
  384. //! \param pucData is the memory buffer to use to store the report.
  385. //! \param ulSize is the size in bytes of the buffer pointed to by
  386. //! \e pucBuffer.
  387. //!
  388. //! This function is used to retrieve a report from a USB pipe. It is usually
  389. //! called when the USB SPECIFIC layer has detected a new data available in a USB
  390. //! pipe. The USB SPECIFIC host device code will receive a
  391. //! \b USB_EVENT_RX_AVAILABLE event when data is available, allowing the
  392. //! callback function to retrieve the data.
  393. //!
  394. //! \return Returns the number of bytes read from report.
  395. //
  396. //*****************************************************************************
  397. unsigned int
  398. USBHSPECIFICGetReport(unsigned int ulInstance,
  399. unsigned int ulInterface,
  400. unsigned char *pucData,
  401. unsigned int ulSize)
  402. {
  403. tSPECIFICInstance *pSPECIFICInstance;
  404. //
  405. // Cast the instance pointer to the correct type for ease of use.
  406. //
  407. pSPECIFICInstance = (tSPECIFICInstance *)ulInstance;
  408. //
  409. // Read the Data out.
  410. //
  411. ulSize = USBHCDPipeReadNonBlocking(pSPECIFICInstance->ulIndex, pSPECIFICInstance
  412. ->ulIntInPipe, pucData, ulSize);
  413. //
  414. // Return the number of bytes read from the interrupt in pipe.
  415. //
  416. return(ulSize);
  417. }
  418. //*****************************************************************************
  419. //
  420. //! This function is used to send a report to a SPECIFIC device.
  421. //!
  422. //! \param ulInstance is the value that was returned from the call to
  423. //! USBHSPECIFICOpen().
  424. //! \param ulInterface is the interface to send the report to.
  425. //! \param pucData is the memory buffer to use to store the report.
  426. //! \param ulSize is the size in bytes of the buffer pointed to by
  427. //! \e pucBuffer.
  428. //!
  429. //! This function is used to send a report to a USB SPECIFIC device. It can be
  430. //! only be called from outside the callback context as this function will not
  431. //! return from the call until the data has been sent successfully.
  432. //!
  433. //! \return Returns the number of bytes sent to the device.
  434. //
  435. //*****************************************************************************
  436. unsigned int
  437. USBHSPECIFICSetReport(unsigned int ulInstance, unsigned int ulInterface,
  438. unsigned char *pucData, unsigned int ulSize)
  439. {
  440. return (ulSize);
  441. }
  442. //*****************************************************************************
  443. //
  444. //! @}
  445. //
  446. //*****************************************************************************