usbdbulk.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  1. //*****************************************************************************
  2. //
  3. // usbdbulk.c - USB bulk device class 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 revision 6288 of the Stellaris USB Library.
  22. //
  23. //*****************************************************************************
  24. #include "hw_usb.h"
  25. #include "hw_types.h"
  26. #include "debug.h"
  27. #include "interrupt.h"
  28. #include "usb.h"
  29. #include "usblib.h"
  30. #include "usbdevice.h"
  31. #include "usbdcomp.h"
  32. #include "usbdbulk.h"
  33. #include "usblibpriv.h"
  34. //*****************************************************************************
  35. //
  36. //! \addtogroup bulk_device_class_api
  37. //! @{
  38. //
  39. //*****************************************************************************
  40. //*****************************************************************************
  41. //
  42. // The subset of endpoint status flags that we consider to be reception
  43. // errors. These are passed to the client via USB_EVENT_ERROR if seen.
  44. //
  45. //*****************************************************************************
  46. #define USB_RX_ERROR_FLAGS (USBERR_DEV_RX_DATA_ERROR | \
  47. USBERR_DEV_RX_OVERRUN | \
  48. USBERR_DEV_RX_FIFO_FULL)
  49. //*****************************************************************************
  50. //
  51. // Flags that may appear in usDeferredOpFlags to indicate some operation that
  52. // has been requested but could not be processed at the time it was received.
  53. // Each deferred operation is defined as the bit number that should be set in
  54. // tBulkInstance->usDeferredOpFlags to indicate that the operation is pending.
  55. //
  56. //*****************************************************************************
  57. #define BULK_DO_PACKET_RX 5
  58. //*****************************************************************************
  59. //
  60. // Macros to convert between USB controller base address and an index. These
  61. // are currently trivial but are included to allow for the possibility of
  62. // supporting more than one controller in the future.
  63. //
  64. //*****************************************************************************
  65. #if (USB_NUM_INSTANCE == 2)
  66. #define USB_BASE_TO_INDEX(BaseAddr, index) do{ \
  67. if(USB0_BASE==BaseAddr) \
  68. index = 0; \
  69. else if(USB1_BASE==BaseAddr) \
  70. index = 1; \
  71. else \
  72. index = -1; \
  73. }while(0)
  74. #define USB_INDEX_TO_BASE(Index, BaseAddr) ( \
  75. if(0==Index) \
  76. BaseAddr = USB0_BASE; \
  77. else if(1==Index) \
  78. BaseAddr = USB1_BASE; \
  79. else \
  80. BaseAddr = -1; \
  81. )
  82. #else
  83. #define USB_BASE_TO_INDEX(BaseAddr, index) do{ \
  84. if(USB0_BASE==BaseAddr) \
  85. index = 0; \
  86. else \
  87. index = -1; \
  88. }while(0)
  89. #define USB_INDEX_TO_BASE(Index, BaseAddr) ( \
  90. if(0==Index) \
  91. BaseAddr = USB0_BASE; \
  92. else \
  93. BaseAddr = -1; \
  94. )
  95. #endif
  96. //*****************************************************************************
  97. //
  98. // Endpoints to use for each of the required endpoints in the driver.
  99. //
  100. //*****************************************************************************
  101. #define DATA_IN_ENDPOINT USB_EP_1
  102. #define DATA_OUT_ENDPOINT USB_EP_1
  103. //*****************************************************************************
  104. //
  105. // Maximum packet size for the bulk endpoints used for serial data
  106. // transmission and reception and the associated FIFO sizes to set aside
  107. // for each endpoint.
  108. //
  109. //*****************************************************************************
  110. #define DATA_IN_EP_FIFO_SIZE USB_FIFO_SZ_64
  111. #define DATA_OUT_EP_FIFO_SIZE USB_FIFO_SZ_64
  112. #define DATA_IN_EP_MAX_SIZE USB_FIFO_SZ_TO_BYTES(DATA_IN_EP_FIFO_SIZE)
  113. #define DATA_OUT_EP_MAX_SIZE USB_FIFO_SZ_TO_BYTES(DATA_IN_EP_FIFO_SIZE)
  114. //*****************************************************************************
  115. //
  116. // USB instance Object
  117. //
  118. //*****************************************************************************
  119. extern tUSBInstanceObject g_USBInstance[];
  120. //*****************************************************************************
  121. //
  122. // Device Descriptor. This is stored in RAM to allow several fields to be
  123. // changed at runtime based on the client's requirements.
  124. //
  125. //*****************************************************************************
  126. unsigned char g_pBulkDeviceDescriptor[] =
  127. {
  128. 18, // Size of this structure.
  129. USB_DTYPE_DEVICE, // Type of this structure.
  130. USBShort(0x110), // USB version 1.1 (if we say 2.0, hosts assume
  131. // high-speed - see USB 2.0 spec 9.2.6.6)
  132. USB_CLASS_VEND_SPECIFIC, // USB Device Class
  133. 0, // USB Device Sub-class
  134. 0, // USB Device protocol
  135. 64, // Maximum packet size for default pipe.
  136. USBShort(0), // Vendor ID (VID).
  137. USBShort(0), // Product ID (PID).
  138. USBShort(0x100), // Device Version BCD.
  139. 1, // Manufacturer string identifier.
  140. 2, // Product string identifier.
  141. 3, // Product serial number.
  142. 1 // Number of configurations.
  143. };
  144. //*****************************************************************************
  145. //
  146. // Bulk device configuration descriptor.
  147. //
  148. // It is vital that the configuration descriptor bConfigurationValue field
  149. // (byte 6) is 1 for the first configuration and increments by 1 for each
  150. // additional configuration defined here. This relationship is assumed in the
  151. // device stack for simplicity even though the USB 2.0 specification imposes
  152. // no such restriction on the bConfigurationValue values.
  153. //
  154. // Note that this structure is deliberately located in RAM since we need to
  155. // be able to patch some values in it based on client requirements.
  156. //
  157. //*****************************************************************************
  158. unsigned char g_pBulkDescriptor[] =
  159. {
  160. //
  161. // Configuration descriptor header.
  162. //
  163. 9, // Size of the configuration descriptor.
  164. USB_DTYPE_CONFIGURATION, // Type of this descriptor.
  165. USBShort(32), // The total size of this full structure.
  166. 1, // The number of interfaces in this
  167. // configuration.
  168. 1, // The unique value for this configuration.
  169. 5, // The string identifier that describes this
  170. // configuration.
  171. USB_CONF_ATTR_SELF_PWR, // Bus Powered, Self Powered, remote wake up.
  172. 250, // The maximum power in 2mA increments.
  173. };
  174. //*****************************************************************************
  175. //
  176. // The remainder of the configuration descriptor is stored in flash since we
  177. // don't need to modify anything in it at runtime.
  178. //
  179. //*****************************************************************************
  180. const unsigned char g_pBulkInterface[] =
  181. {
  182. //
  183. // Vendor-specific Interface Descriptor.
  184. //
  185. 9, // Size of the interface descriptor.
  186. USB_DTYPE_INTERFACE, // Type of this descriptor.
  187. 0, // The index for this interface.
  188. 0, // The alternate setting for this interface.
  189. 2, // The number of endpoints used by this
  190. // interface.
  191. USB_CLASS_VEND_SPECIFIC, // The interface class
  192. 0, // The interface sub-class.
  193. 0, // The interface protocol for the sub-class
  194. // specified above.
  195. 4, // The string index for this interface.
  196. //
  197. // Endpoint Descriptor
  198. //
  199. 7, // The size of the endpoint descriptor.
  200. USB_DTYPE_ENDPOINT, // Descriptor type is an endpoint.
  201. USB_EP_DESC_IN | USB_EP_TO_INDEX(DATA_IN_ENDPOINT),
  202. USB_EP_ATTR_BULK, // Endpoint is a bulk endpoint.
  203. USBShort(DATA_IN_EP_MAX_SIZE), // The maximum packet size.
  204. 0, // The polling interval for this endpoint.
  205. //
  206. // Endpoint Descriptor
  207. //
  208. 7, // The size of the endpoint descriptor.
  209. USB_DTYPE_ENDPOINT, // Descriptor type is an endpoint.
  210. USB_EP_DESC_OUT | USB_EP_TO_INDEX(DATA_OUT_ENDPOINT),
  211. USB_EP_ATTR_BULK, // Endpoint is a bulk endpoint.
  212. USBShort(DATA_OUT_EP_MAX_SIZE), // The maximum packet size.
  213. 0, // The polling interval for this endpoint.
  214. };
  215. //*****************************************************************************
  216. //
  217. // The serial config descriptor is defined as two sections, one containing
  218. // just the 9 byte USB configuration descriptor and the other containing
  219. // everything else that is sent to the host along with it.
  220. //
  221. //*****************************************************************************
  222. const tConfigSection g_sBulkConfigSection =
  223. {
  224. sizeof(g_pBulkDescriptor),
  225. g_pBulkDescriptor
  226. };
  227. const tConfigSection g_sBulkInterfaceSection =
  228. {
  229. sizeof(g_pBulkInterface),
  230. g_pBulkInterface
  231. };
  232. //*****************************************************************************
  233. //
  234. // This array lists all the sections that must be concatenated to make a
  235. // single, complete bulk device configuration descriptor.
  236. //
  237. //*****************************************************************************
  238. const tConfigSection *g_psBulkSections[] =
  239. {
  240. &g_sBulkConfigSection,
  241. &g_sBulkInterfaceSection
  242. };
  243. #define NUM_BULK_SECTIONS (sizeof(g_psBulkSections) / \
  244. sizeof(tConfigSection *))
  245. //*****************************************************************************
  246. //
  247. // The header for the single configuration we support. This is the root of
  248. // the data structure that defines all the bits and pieces that are pulled
  249. // together to generate the configuration descriptor.
  250. //
  251. //*****************************************************************************
  252. const tConfigHeader g_sBulkConfigHeader =
  253. {
  254. NUM_BULK_SECTIONS,
  255. g_psBulkSections
  256. };
  257. //*****************************************************************************
  258. //
  259. // Configuration Descriptor.
  260. //
  261. //*****************************************************************************
  262. const tConfigHeader * const g_pBulkConfigDescriptors[] =
  263. {
  264. &g_sBulkConfigHeader
  265. };
  266. //*****************************************************************************
  267. //
  268. // Forward references for device handler callbacks
  269. //
  270. //*****************************************************************************
  271. static void HandleConfigChange(void *pvInstance, unsigned int ulInfo,
  272. unsigned int ulIndex);
  273. static void HandleDisconnect(void *pvInstance);
  274. static void HandleEndpoints(void *pvInstance, unsigned int ulStatus,
  275. unsigned int ulIndex);
  276. static void HandleSuspend(void *pvInstance);
  277. static void HandleResume(void *pvInstance);
  278. static void HandleDevice(void *pvInstance, unsigned int ulRequest,
  279. void *pvRequestData);
  280. //*****************************************************************************
  281. //
  282. // The device information structure for the USB serial device.
  283. //
  284. //*****************************************************************************
  285. tDeviceInfo g_sBulkDeviceInfo =
  286. {
  287. //
  288. // Device event handler callbacks.
  289. //
  290. {
  291. 0, // GetDescriptor
  292. 0, // RequestHandler
  293. 0, // InterfaceChange
  294. HandleConfigChange, // ConfigChange
  295. 0, // DataReceived
  296. 0, // DataSentCallback
  297. 0, // ResetHandler
  298. HandleSuspend, // SuspendHandler
  299. HandleResume, // ResumeHandler
  300. HandleDisconnect, // DisconnectHandler
  301. HandleEndpoints, // EndpointHandler
  302. HandleDevice // Device handler.
  303. },
  304. g_pBulkDeviceDescriptor,
  305. g_pBulkConfigDescriptors,
  306. 0, // Will be completed during USBDBulkInit().
  307. 0, // Will be completed during USBDBulkInit().
  308. &g_sUSBDefaultFIFOConfig
  309. };
  310. //*****************************************************************************
  311. //
  312. // Set or clear deferred operation flags in an "atomic" manner.
  313. //
  314. // \param pusDeferredOp points to the flags variable which is to be modified.
  315. // \param usBit indicates which bit number is to be set or cleared.
  316. // \param bSet indicates the state that the flag must be set to. If \b true,
  317. // the flag is set, if \b false, the flag is cleared.
  318. //
  319. // This function safely sets or clears a bit in a flag variable. The operation
  320. // makes use of bitbanding to ensure that the operation is atomic (no read-
  321. // modify-write is required).
  322. //
  323. // \return None.
  324. //
  325. //*****************************************************************************
  326. static void
  327. SetDeferredOpFlag(volatile unsigned short *pusDeferredOp, unsigned short usBit,
  328. tBoolean bSet)
  329. {
  330. //
  331. // Set the flag bit to 1 or 0 using a bitband access.
  332. //
  333. HWREGBITH(pusDeferredOp, usBit) = bSet ? 1 : 0;
  334. }
  335. //*****************************************************************************
  336. //
  337. // Receives notifications related to data received from the host.
  338. //
  339. // \param psDevice is the device instance whose endpoint is to be processed.
  340. // \param ulStatus is the USB interrupt status that caused this function to
  341. // be called.
  342. //
  343. // This function is called from HandleEndpoints for all interrupts signaling
  344. // the arrival of data on the bulk OUT endpoint (in other words, whenever the
  345. // host has sent us a packet of data). We inform the client that a packet
  346. // is available and, on return, check to see if the packet has been read. If
  347. // not, we schedule another notification to the client for a later time.
  348. //
  349. // \return Returns \b true on success or \b false on failure.
  350. //
  351. //*****************************************************************************
  352. static tBoolean
  353. ProcessDataFromHost(const tUSBDBulkDevice *psDevice, unsigned int ulStatus,
  354. unsigned int ulIndex)
  355. {
  356. unsigned int ulEPStatus;
  357. unsigned int ulSize;
  358. tBulkInstance *psInst;
  359. //
  360. // Get a pointer to our instance data.
  361. //
  362. psInst = psDevice->psPrivateBulkData;
  363. //
  364. // Get the endpoint status to see why we were called.
  365. //
  366. ulEPStatus = USBEndpointStatus(g_USBInstance[ulIndex].uiBaseAddr, psInst->ucOUTEndpoint);
  367. //
  368. // Clear the status bits.
  369. //
  370. USBDevEndpointStatusClear(g_USBInstance[ulIndex].uiBaseAddr, psInst->ucOUTEndpoint, ulEPStatus);
  371. //
  372. // Has a packet been received?
  373. //
  374. if(ulEPStatus & USB_DEV_RX_PKT_RDY)
  375. {
  376. //
  377. // Set the flag we use to indicate that a packet read is pending. This
  378. // will be cleared if the packet is read. If the client doesn't read
  379. // the packet in the context of the USB_EVENT_RX_AVAILABLE callback,
  380. // the event will be signaled later during tick processing.
  381. //
  382. SetDeferredOpFlag(&psInst->usDeferredOpFlags, BULK_DO_PACKET_RX, true);
  383. //
  384. // How big is the packet we've just been sent?
  385. //
  386. ulSize = USBEndpointDataAvail(psInst->ulUSBBase, psInst->ucOUTEndpoint);
  387. //
  388. // The receive channel is not blocked so let the caller know
  389. // that a packet is waiting. The parameters are set to indicate
  390. // that the packet has not been read from the hardware FIFO yet.
  391. //
  392. psDevice->pfnRxCallback(psDevice->pvRxCBData,
  393. USB_EVENT_RX_AVAILABLE, ulSize,
  394. (void *)0);
  395. }
  396. else
  397. {
  398. //
  399. // No packet was received. Some error must have been reported. Check
  400. // and pass this on to the client if necessary.
  401. //
  402. if(ulEPStatus & USB_RX_ERROR_FLAGS)
  403. {
  404. //
  405. // This is an error we report to the client so...
  406. //
  407. psDevice->pfnRxCallback(psDevice->pvRxCBData,
  408. USB_EVENT_ERROR,
  409. (ulEPStatus & USB_RX_ERROR_FLAGS),
  410. (void *)0);
  411. }
  412. return (false);
  413. }
  414. return (true);
  415. }
  416. //*****************************************************************************
  417. //
  418. // Receives notifications related to data sent to the host.
  419. //
  420. // \param psDevice is the device instance whose endpoint is to be processed.
  421. // \param ulStatus is the USB interrupt status that caused this function to
  422. // be called.
  423. //
  424. // This function is called from HandleEndpoints for all interrupts originating
  425. // from the bulk IN endpoint (in other words, whenever data has been
  426. // transmitted to the USB host). We examine the cause of the interrupt and,
  427. // if due to completion of a transmission, notify the client.
  428. //
  429. // \return Returns \b true on success or \b false on failure.
  430. //
  431. //*****************************************************************************
  432. static tBoolean
  433. ProcessDataToHost(const tUSBDBulkDevice *psDevice, unsigned int ulStatus,
  434. unsigned int ulIndex)
  435. {
  436. tBulkInstance *psInst;
  437. unsigned int ulEPStatus;
  438. unsigned int ulSize;
  439. //
  440. // Get a pointer to our instance data.
  441. //
  442. psInst = psDevice->psPrivateBulkData;
  443. //
  444. // Get the endpoint status to see why we were called.
  445. //
  446. ulEPStatus = USBEndpointStatus(psInst->ulUSBBase, psInst->ucINEndpoint);
  447. //
  448. // Clear the status bits.
  449. //
  450. USBDevEndpointStatusClear(psInst->ulUSBBase, psInst->ucINEndpoint,
  451. ulEPStatus);
  452. //
  453. // Our last transmission completed. Clear our state back to idle and
  454. // see if we need to send any more data.
  455. //
  456. psInst->eBulkTxState = BULK_STATE_IDLE;
  457. //
  458. // Notify the client that the last transmission completed.
  459. //
  460. ulSize = psInst->usLastTxSize;
  461. psInst->usLastTxSize = 0;
  462. psDevice->pfnTxCallback(psDevice->pvTxCBData, USB_EVENT_TX_COMPLETE,
  463. ulSize, (void *)0);
  464. return (true);
  465. }
  466. //*****************************************************************************
  467. //
  468. // Called by the USB stack for any activity involving one of our endpoints
  469. // other than EP0. This function is a fan out that merely directs the call to
  470. // the correct handler depending upon the endpoint and transaction direction
  471. // signaled in ulStatus.
  472. //
  473. //*****************************************************************************
  474. static void
  475. HandleEndpoints(void *pvInstance, unsigned int ulStatus, unsigned int ulIndex)
  476. {
  477. const tUSBDBulkDevice *psBulkInst;
  478. tBulkInstance *psInst;
  479. ASSERT(pvInstance != 0);
  480. //
  481. // Determine if the serial device is in single or composite mode because
  482. // the meaning of ulIndex is different in both cases.
  483. //
  484. psBulkInst = (const tUSBDBulkDevice *)pvInstance;
  485. psInst = psBulkInst->psPrivateBulkData;
  486. //
  487. // Handler for the bulk OUT data endpoint.
  488. //
  489. if(ulStatus & (0x10000 << USB_EP_TO_INDEX(psInst->ucOUTEndpoint)))
  490. {
  491. //
  492. // Data is being sent to us from the host.
  493. //
  494. ProcessDataFromHost(pvInstance, ulStatus, ulIndex);
  495. }
  496. //
  497. // Handler for the bulk IN data endpoint.
  498. //
  499. if(ulStatus & (1 << USB_EP_TO_INDEX(psInst->ucINEndpoint)))
  500. {
  501. ProcessDataToHost(pvInstance, ulStatus, ulIndex);
  502. }
  503. }
  504. //*****************************************************************************
  505. //
  506. // Called by the USB stack whenever a configuration change occurs.
  507. //
  508. //*****************************************************************************
  509. static void
  510. HandleConfigChange(void *pvInstance, unsigned int ulInfo,
  511. unsigned int ulIndex)
  512. {
  513. tBulkInstance *psInst;
  514. const tUSBDBulkDevice *psDevice;
  515. ASSERT(pvInstance != 0);
  516. //
  517. // Create a device instance pointer.
  518. //
  519. psDevice = (const tUSBDBulkDevice *)pvInstance;
  520. //
  521. // Get a pointer to our instance data.
  522. //
  523. psInst = psDevice->psPrivateBulkData;
  524. //
  525. // Set all our endpoints to idle state.
  526. //
  527. psInst->eBulkRxState = BULK_STATE_IDLE;
  528. psInst->eBulkTxState = BULK_STATE_IDLE;
  529. //
  530. // If we have a control callback, let the client know we are open for
  531. // business.
  532. //
  533. if(psDevice->pfnRxCallback)
  534. {
  535. //
  536. // Pass the connected event to the client.
  537. //
  538. psDevice->pfnRxCallback(psDevice->pvRxCBData, USB_EVENT_CONNECTED, 0,
  539. (void *)0);
  540. }
  541. //
  542. // Remember that we are connected.
  543. //
  544. psInst->bConnected = true;
  545. }
  546. //*****************************************************************************
  547. //
  548. // Device instance specific handler.
  549. //
  550. //*****************************************************************************
  551. static void
  552. HandleDevice(void *pvInstance, unsigned int ulRequest, void *pvRequestData)
  553. {
  554. tBulkInstance *psInst;
  555. unsigned char *pucData;
  556. //
  557. // Create the serial instance data.
  558. //
  559. psInst = ((tUSBDBulkDevice *)pvInstance)->psPrivateBulkData;
  560. //
  561. // Create the char array used by the events supported by the USB CDC
  562. // serial class.
  563. //
  564. pucData = (unsigned char *)pvRequestData;
  565. switch(ulRequest)
  566. {
  567. //
  568. // This was an interface change event.
  569. //
  570. case USB_EVENT_COMP_IFACE_CHANGE:
  571. {
  572. psInst->ucInterface = pucData[1];
  573. break;
  574. }
  575. //
  576. // This was an endpoint change event.
  577. //
  578. case USB_EVENT_COMP_EP_CHANGE:
  579. {
  580. //
  581. // Determine if this is an IN or OUT endpoint that has changed.
  582. //
  583. if(pucData[0] & USB_EP_DESC_IN)
  584. {
  585. psInst->ucINEndpoint = INDEX_TO_USB_EP((pucData[1] & 0x7f));
  586. }
  587. else
  588. {
  589. //
  590. // Extract the new endpoint number.
  591. //
  592. psInst->ucOUTEndpoint = INDEX_TO_USB_EP(pucData[1] & 0x7f);
  593. }
  594. break;
  595. }
  596. default:
  597. {
  598. break;
  599. }
  600. }
  601. }
  602. //*****************************************************************************
  603. //
  604. // This function is called by the USB device stack whenever the device is
  605. // disconnected from the host.
  606. //
  607. //*****************************************************************************
  608. static void
  609. HandleDisconnect(void *pvInstance)
  610. {
  611. const tUSBDBulkDevice *psBulkDevice;
  612. tBulkInstance *psInst;
  613. ASSERT(pvInstance != 0);
  614. //
  615. // Create the instance pointer.
  616. //
  617. psBulkDevice = (const tUSBDBulkDevice *)pvInstance;
  618. //
  619. // Get a pointer to our instance data.
  620. //
  621. psInst = psBulkDevice->psPrivateBulkData;
  622. //
  623. // If we are not currently connected so let the client know we are open
  624. // for business.
  625. //
  626. if(psInst->bConnected)
  627. {
  628. //
  629. // Pass the disconnected event to the client.
  630. //
  631. psBulkDevice->pfnRxCallback(psBulkDevice->pvRxCBData,
  632. USB_EVENT_DISCONNECTED, 0, (void *)0);
  633. }
  634. //
  635. // Remember that we are no longer connected.
  636. //
  637. psInst->bConnected = false;
  638. }
  639. //*****************************************************************************
  640. //
  641. // This function is called by the USB device stack whenever the bus is put into
  642. // suspend state.
  643. //
  644. //*****************************************************************************
  645. static void
  646. HandleSuspend(void *pvInstance)
  647. {
  648. const tUSBDBulkDevice *psBulkDevice;
  649. ASSERT(pvInstance != 0);
  650. //
  651. // Create the instance pointer.
  652. //
  653. psBulkDevice = (const tUSBDBulkDevice *)pvInstance;
  654. //
  655. // Pass the event on to the client.
  656. //
  657. psBulkDevice->pfnRxCallback(psBulkDevice->pvRxCBData, USB_EVENT_SUSPEND, 0,
  658. (void *)0);
  659. }
  660. //*****************************************************************************
  661. //
  662. // This function is called by the USB device stack whenever the bus is taken
  663. // out of suspend state.
  664. //
  665. //*****************************************************************************
  666. static void
  667. HandleResume(void *pvInstance)
  668. {
  669. const tUSBDBulkDevice *psBulkDevice;
  670. ASSERT(pvInstance != 0);
  671. //
  672. // Create the instance pointer.
  673. //
  674. psBulkDevice = (const tUSBDBulkDevice *)pvInstance;
  675. //
  676. // Pass the event on to the client.
  677. //
  678. psBulkDevice->pfnRxCallback(psBulkDevice->pvRxCBData, USB_EVENT_RESUME, 0,
  679. (void *)0);
  680. }
  681. //*****************************************************************************
  682. //
  683. //! Initializes bulk device operation for a given USB controller.
  684. //!
  685. //! \param ulIndex is the index of the USB controller which is to be
  686. //! initialized for bulk device operation.
  687. //! \param psDevice points to a structure containing parameters customizing
  688. //! the operation of the bulk device.
  689. //!
  690. //! An application wishing to make use of a USB bulk communication channel
  691. //! must call this function to initialize the USB controller and attach the
  692. //! device to the USB bus. This function performs all required USB
  693. //! initialization.
  694. //!
  695. //! On successful completion, this function will return the \e psDevice pointer
  696. //! passed to it. This must be passed on all future calls to the device driver
  697. //! related to this device.
  698. //!
  699. //! The USBDBulk interface offers packet-based transmit and receive operation.
  700. //! If the application would rather use block based communication with
  701. //! transmit and receive buffers, USB buffers may be used above the bulk
  702. //! transmit and receive channels to offer this functionality.
  703. //!
  704. //! Transmit Operation:
  705. //!
  706. //! Calls to USBDBulkPacketWrite must send no more than 64 bytes of data at a
  707. //! time and may only be made when no other transmission is currently
  708. //! outstanding.
  709. //!
  710. //! Once a packet of data has been acknowledged by the USB host, a
  711. //! USB_EVENT_TX_COMPLETE event is sent to the application callback to inform
  712. //! it that another packet may be transmitted.
  713. //!
  714. //! Receive Operation:
  715. //!
  716. //! An incoming USB data packet will result in a call to the application
  717. //! callback with event USBD_EVENT_RX_AVAILABLE. The application must then
  718. //! call USBDBulkPacketRead(), passing a buffer capable of holding 64 bytes, to
  719. //! retrieve the data and acknowledge reception to the USB host.
  720. //!
  721. //! \note The application must not make any calls to the low level USB Device
  722. //! API if interacting with USB via the USB bulk device class API. Doing so
  723. //! will cause unpredictable (though almost certainly unpleasant) behavior.
  724. //!
  725. //! \return Returns NULL on failure or the psDevice pointer on success.
  726. //
  727. //*****************************************************************************
  728. void *
  729. USBDBulkInit(unsigned int ulIndex, const tUSBDBulkDevice *psDevice)
  730. {
  731. void *pvInstance;
  732. //
  733. // Check parameter validity.
  734. //
  735. ASSERT(ulIndex == 0);
  736. ASSERT(psDevice);
  737. pvInstance = USBDBulkCompositeInit(ulIndex, psDevice);
  738. if(pvInstance)
  739. {
  740. //
  741. // All is well so now pass the descriptors to the lower layer and put
  742. // the bulk device on the bus.
  743. //
  744. USBDCDInit(ulIndex, psDevice->psPrivateBulkData->psDevInfo);
  745. }
  746. //
  747. // Return the pointer to the instance indicating that everything went well.
  748. //
  749. return(pvInstance);
  750. }
  751. //*****************************************************************************
  752. //
  753. //! Initializes bulk device operation for a given USB controller.
  754. //!
  755. //! \param ulIndex is the index of the USB controller which is to be
  756. //! initialized for bulk device operation.
  757. //! \param psDevice points to a structure containing parameters customizing
  758. //! the operation of the bulk device.
  759. //!
  760. //! An application wishing to make use of a composite
  761. //! USB bulk communication channel needs to call this function.
  762. //! This function is used for initializing an instance related information of the
  763. //! bulk device.
  764. //!
  765. //! \return Returns zero on failure or a non-zero value that should be
  766. //! used with the remaining USB HID Bulk APIs.
  767. //
  768. //*****************************************************************************
  769. void *
  770. USBDBulkCompositeInit(unsigned int ulIndex, const tUSBDBulkDevice *psDevice)
  771. {
  772. tBulkInstance *psInst;
  773. tDeviceDescriptor *psDevDesc;
  774. //
  775. // Check parameter validity.
  776. //
  777. ASSERT(ulIndex == 0);
  778. ASSERT(psDevice);
  779. ASSERT(psDevice->ppStringDescriptors);
  780. ASSERT(psDevice->psPrivateBulkData);
  781. ASSERT(psDevice->pfnRxCallback);
  782. ASSERT(psDevice->pfnTxCallback);
  783. if(ulIndex == 0)
  784. {
  785. g_USBInstance[ulIndex].uiUSBInstance = ulIndex;
  786. g_USBInstance[ulIndex].uiBaseAddr = USB0_BASE;
  787. g_USBInstance[ulIndex].uiSubBaseAddr = USB_0_OTGBASE;
  788. g_USBInstance[ulIndex].uiInterruptNum = SYS_INT_USB0;
  789. g_USBInstance[ulIndex].uiSubInterruptNum = SYS_INT_USBSSINT;
  790. g_USBInstance[ulIndex].uiPHYConfigRegAddr = CFGCHIP2_USBPHYCTRL;
  791. }
  792. #if (USB_NUM_INSTANCE == 2)
  793. else if(ulIndex == 1)
  794. {
  795. g_USBInstance[ulIndex].uiUSBInstance = ulIndex;
  796. g_USBInstance[ulIndex].uiBaseAddr = USB1_BASE;
  797. g_USBInstance[ulIndex].uiSubBaseAddr = USB_1_OTGBASE;
  798. g_USBInstance[ulIndex].uiInterruptNum = SYS_INT_USB1;
  799. g_USBInstance[ulIndex].uiSubInterruptNum = SYS_INT_USBSSINT;
  800. g_USBInstance[ulIndex].uiPHYConfigRegAddr = CFGCHIP2_USB1PHYCTRL;
  801. }
  802. #endif
  803. //
  804. // Initialize the workspace in the passed instance structure.
  805. //
  806. psInst = psDevice->psPrivateBulkData;
  807. psInst->psConfDescriptor = (tConfigDescriptor *)g_pBulkDescriptor;
  808. psInst->psDevInfo = &g_sBulkDeviceInfo;
  809. psInst->ulUSBBase = g_USBInstance[ulIndex].uiBaseAddr;
  810. psInst->eBulkRxState = BULK_STATE_UNCONFIGURED;
  811. psInst->eBulkTxState = BULK_STATE_UNCONFIGURED;
  812. psInst->usDeferredOpFlags = 0;
  813. psInst->bConnected = false;
  814. //
  815. // Set the default endpoint and interface assignments.
  816. //
  817. psInst->ucINEndpoint = DATA_IN_ENDPOINT;
  818. psInst->ucOUTEndpoint = DATA_OUT_ENDPOINT;
  819. psInst->ucInterface = 0;
  820. //
  821. // Fix up the device descriptor with the client-supplied values.
  822. //
  823. psDevDesc = (tDeviceDescriptor *)psInst->psDevInfo->pDeviceDescriptor;
  824. psDevDesc->idVendor = psDevice->usVID;
  825. psDevDesc->idProduct = psDevice->usPID;
  826. //
  827. // Fix up the configuration descriptor with client-supplied values.
  828. //
  829. psInst->psConfDescriptor->bmAttributes = psDevice->ucPwrAttributes;
  830. psInst->psConfDescriptor->bMaxPower =
  831. (unsigned char)(psDevice->usMaxPowermA / 2);
  832. //
  833. // Plug in the client's string stable to the device information
  834. // structure.
  835. //
  836. psInst->psDevInfo->ppStringDescriptors = psDevice->ppStringDescriptors;
  837. psInst->psDevInfo->ulNumStringDescriptors
  838. = psDevice->ulNumStringDescriptors;
  839. //
  840. // Set the device instance.
  841. //
  842. psInst->psDevInfo->pvInstance = (void *)psDevice;
  843. //
  844. // Return the pointer to the instance indicating that everything went well.
  845. //
  846. return((void *)psDevice);
  847. }
  848. //*****************************************************************************
  849. //
  850. //! Shut down the bulk device.
  851. //!
  852. //! \param pvInstance is the pointer to the device instance structure as
  853. //! returned by USBDBulkInit().
  854. //!
  855. //! This function terminates device operation for the instance supplied and
  856. //! removes the device from the USB bus. This function should not be called
  857. //! if the bulk device is part of a composite device and instead the
  858. //! USBDCompositeTerm() function should be called for the full composite
  859. //! device.
  860. //!
  861. //! Following this call, the \e pvInstance instance should not me used in any
  862. //! other calls.
  863. //!
  864. //! \return None.
  865. //
  866. //*****************************************************************************
  867. void
  868. USBDBulkTerm(void *pvInstance)
  869. {
  870. tBulkInstance *psInst;
  871. int index;
  872. ASSERT(pvInstance);
  873. //
  874. // Get a pointer to our instance data.
  875. //
  876. psInst = ((tUSBDBulkDevice *)pvInstance)->psPrivateBulkData;
  877. //
  878. // Terminate the requested instance.
  879. //
  880. USB_BASE_TO_INDEX(psInst->ulUSBBase, index);
  881. USBDCDTerm(index);
  882. psInst->ulUSBBase = 0;
  883. psInst->psDevInfo = (tDeviceInfo *)0;
  884. psInst->psConfDescriptor = (tConfigDescriptor *)0;
  885. return;
  886. }
  887. //*****************************************************************************
  888. //
  889. //! Sets the client-specific pointer parameter for the receive channel
  890. //! callback.
  891. //!
  892. //! \param pvInstance is the pointer to the device instance structure as
  893. //! returned by USBDBulkInit().
  894. //! \param pvCBData is the pointer that client wishes to be provided on each
  895. //! event sent to the receive channel callback function.
  896. //!
  897. //! The client uses this function to change the callback pointer passed in
  898. //! the first parameter on all callbacks to the \e pfnRxCallback function
  899. //! passed on USBDBulkInit().
  900. //!
  901. //! If a client wants to make runtime changes in the callback pointer, it must
  902. //! ensure that the \e pvInstance structure passed to USBDBulkInit() resides in
  903. //! RAM. If this structure is in flash, callback pointer changes will not be
  904. //! possible.
  905. //!
  906. //! \return Returns the previous callback pointer that was being used for
  907. //! this instance's receive callback.
  908. //
  909. //*****************************************************************************
  910. void *
  911. USBDBulkSetRxCBData(void *pvInstance, void *pvCBData)
  912. {
  913. void *pvOldValue;
  914. ASSERT(pvInstance);
  915. //
  916. // Set the callback data for the receive channel after remembering the
  917. // previous value.
  918. //
  919. pvOldValue = ((tUSBDBulkDevice *)pvInstance)->pvRxCBData;
  920. ((tUSBDBulkDevice *)pvInstance)->pvRxCBData = pvCBData;
  921. //
  922. // Return the previous callback pointer.
  923. //
  924. return (pvOldValue);
  925. }
  926. //*****************************************************************************
  927. //
  928. //! Sets the client-specific pointer parameter for the transmit callback.
  929. //!
  930. //! \param pvInstance is the pointer to the device instance structure as
  931. //! returned by USBDBulkInit().
  932. //! \param pvCBData is the pointer that client wishes to be provided on each
  933. //! event sent to the transmit channel callback function.
  934. //!
  935. //! The client uses this function to change the callback pointer passed in
  936. //! the first parameter on all callbacks to the \e pfnTxCallback function
  937. //! passed on USBDBulkInit().
  938. //!
  939. //! If a client wants to make runtime changes in the callback pointer, it must
  940. //! ensure that the \e pvInstance structure passed to USBDBulkInit() resides in
  941. //! RAM. If this structure is in flash, callback pointer changes will not be
  942. //! possible.
  943. //!
  944. //! \return Returns the previous callback pointer that was being used for
  945. //! this instance's transmit callback.
  946. //
  947. //*****************************************************************************
  948. void *
  949. USBDBulkSetTxCBData(void *pvInstance, void *pvCBData)
  950. {
  951. void *pvOldValue;
  952. ASSERT(pvInstance);
  953. //
  954. // Set the callback pointer for the transmit channel after remembering the
  955. // previous value.
  956. //
  957. pvOldValue = ((tUSBDBulkDevice *)pvInstance)->pvTxCBData;
  958. ((tUSBDBulkDevice *)pvInstance)->pvTxCBData = pvCBData;
  959. //
  960. // Return the previous callback pointer value.
  961. //
  962. return (pvOldValue);
  963. }
  964. //*****************************************************************************
  965. //
  966. //! Transmits a packet of data to the USB host via the bulk data interface.
  967. //!
  968. //! \param pvInstance is the pointer to the device instance structure as
  969. //! returned by USBDBulkInit().
  970. //! \param pcData points to the first byte of data which is to be transmitted.
  971. //! \param ulLength is the number of bytes of data to transmit.
  972. //! \param bLast indicates whether more data is to be written before a packet
  973. //! should be scheduled for transmission. If \b true, the client will make
  974. //! a further call to this function. If \b false, no further call will be
  975. //! made and the driver should schedule transmission of a short packet.
  976. //!
  977. //! This function schedules the supplied data for transmission to the USB
  978. //! host in a single USB packet. If no transmission is currently ongoing,
  979. //! the data is immediately copied to the relevant USB endpoint FIFO for
  980. //! transmission. Whenever a USB packet is acknowledged by the host, a
  981. //! USB_EVENT_TX_COMPLETE event will be sent to the transmit channel callback
  982. //! indicating that more data can now be transmitted.
  983. //!
  984. //! The maximum value for \e ulLength is 64 bytes (the maximum USB packet size
  985. //! for the bulk endpoints in use by the device). Attempts to send more data
  986. //! than this will result in a return code of 0 indicating that the data cannot
  987. //! be sent.
  988. //!
  989. //! The \e bLast parameter allows a client to make multiple calls to this
  990. //! function before scheduling transmission of the packet to the host. This
  991. //! can be helpful if, for example, constructing a packet on the fly or
  992. //! writing a packet which spans the wrap point in a ring buffer.
  993. //!
  994. //! \return Returns the number of bytes actually sent. At this level, this
  995. //! will either be the number of bytes passed (if less than or equal to the
  996. //! maximum packet size for the USB endpoint in use and no outstanding
  997. //! transmission ongoing) or 0 to indicate a failure.
  998. //
  999. //*****************************************************************************
  1000. unsigned int
  1001. USBDBulkPacketWrite(void *pvInstance, unsigned char *pcData,
  1002. unsigned int ulLength, tBoolean bLast)
  1003. {
  1004. tBulkInstance *psInst;
  1005. int iRetcode;
  1006. ASSERT(pvInstance);
  1007. //
  1008. // Get our instance data pointer
  1009. //
  1010. psInst = ((tUSBDBulkDevice *)pvInstance)->psPrivateBulkData;
  1011. //
  1012. // Can we send the data provided?
  1013. //
  1014. if((ulLength > DATA_IN_EP_MAX_SIZE) ||
  1015. (psInst->eBulkTxState != BULK_STATE_IDLE))
  1016. {
  1017. //
  1018. // Either the packet was too big or we are in the middle of sending
  1019. // another packet. Return 0 to indicate that we can't send this data.
  1020. //
  1021. return (0);
  1022. }
  1023. //
  1024. // Copy the data into the USB endpoint FIFO.
  1025. //
  1026. iRetcode = USBEndpointDataPut(psInst->ulUSBBase, psInst->ucINEndpoint,
  1027. pcData, ulLength);
  1028. //
  1029. // Did we copy the data successfully?
  1030. //
  1031. if(iRetcode != -1)
  1032. {
  1033. //
  1034. // Remember how many bytes we sent.
  1035. //
  1036. psInst->usLastTxSize += (unsigned short)ulLength;
  1037. //
  1038. // If this is the last call for this packet, schedule transmission.
  1039. //
  1040. if(bLast)
  1041. {
  1042. //
  1043. // Send the packet to the host if we have received all the data we
  1044. // can expect for this packet.
  1045. //
  1046. psInst->eBulkTxState = BULK_STATE_WAIT_DATA;
  1047. iRetcode = USBEndpointDataSend(psInst->ulUSBBase,
  1048. psInst->ucINEndpoint, USB_TRANS_IN);
  1049. }
  1050. }
  1051. //
  1052. // Did an error occur while trying to send the data?
  1053. //
  1054. if(iRetcode != -1)
  1055. {
  1056. //
  1057. // No - tell the caller we sent all the bytes provided.
  1058. //
  1059. return (ulLength);
  1060. }
  1061. else
  1062. {
  1063. //
  1064. // Yes - tell the caller we couldn't send the data.
  1065. //
  1066. return (0);
  1067. }
  1068. }
  1069. //*****************************************************************************
  1070. //
  1071. //! Reads a packet of data received from the USB host via the bulk data
  1072. //! interface.
  1073. //!
  1074. //! \param pvInstance is the pointer to the device instance structure as
  1075. //! returned by USBDBulkInit().
  1076. //! \param pcData points to a buffer into which the received data will be
  1077. //! written.
  1078. //! \param ulLength is the size of the buffer pointed to by pcData.
  1079. //! \param bLast indicates whether the client will make a further call to
  1080. //! read additional data from the packet.
  1081. //!
  1082. //! This function reads up to \e ulLength bytes of data received from the USB
  1083. //! host into the supplied application buffer. If the driver detects that the
  1084. //! entire packet has been read, it is acknowledged to the host.
  1085. //!
  1086. //! The \e bLast parameter is ignored in this implementation since the end of
  1087. //! a packet can be determined without relying upon the client to provide
  1088. //! this information.
  1089. //!
  1090. //! \return Returns the number of bytes of data read.
  1091. //
  1092. //*****************************************************************************
  1093. unsigned int
  1094. USBDBulkPacketRead(void *pvInstance, unsigned char *pcData,
  1095. unsigned int ulLength, tBoolean bLast)
  1096. {
  1097. unsigned int ulEPStatus, ulPkt;
  1098. unsigned int ulCount;
  1099. tBulkInstance *psInst;
  1100. int iRetcode;
  1101. ASSERT(pvInstance);
  1102. //
  1103. // Get our instance data pointer
  1104. //
  1105. psInst = ((tUSBDBulkDevice *)pvInstance)->psPrivateBulkData;
  1106. //
  1107. // Does the relevant endpoint FIFO have a packet waiting for us?
  1108. //
  1109. ulEPStatus = USBEndpointStatus(psInst->ulUSBBase, psInst->ucOUTEndpoint);
  1110. if(ulEPStatus & USB_DEV_RX_PKT_RDY)
  1111. {
  1112. //
  1113. // How many bytes are available for us to receive?
  1114. //
  1115. ulPkt = USBEndpointDataAvail(psInst->ulUSBBase, psInst->ucOUTEndpoint);
  1116. //
  1117. // Get as much data as we can.
  1118. //
  1119. ulCount = ulLength;
  1120. iRetcode = USBEndpointDataGet(psInst->ulUSBBase, psInst->ucOUTEndpoint,
  1121. pcData, &ulCount);
  1122. //
  1123. // Did we read the last of the packet data?
  1124. //
  1125. if(ulCount == ulPkt)
  1126. {
  1127. //
  1128. // Clear the endpoint status so that we know no packet is
  1129. // waiting.
  1130. //
  1131. USBDevEndpointStatusClear(psInst->ulUSBBase, psInst->ucOUTEndpoint,
  1132. ulEPStatus);
  1133. //
  1134. // Acknowledge the data, thus freeing the host to send the
  1135. // next packet.
  1136. //
  1137. USBDevEndpointDataAck(psInst->ulUSBBase, psInst->ucOUTEndpoint,
  1138. true);
  1139. //
  1140. // Clear the flag we set to indicate that a packet read is
  1141. // pending.
  1142. //
  1143. SetDeferredOpFlag(&psInst->usDeferredOpFlags, BULK_DO_PACKET_RX,
  1144. false);
  1145. }
  1146. //
  1147. // If all went well, tell the caller how many bytes they got.
  1148. //
  1149. if(iRetcode != -1)
  1150. {
  1151. return (ulCount);
  1152. }
  1153. }
  1154. //
  1155. // No packet was available or an error occurred while reading so tell
  1156. // the caller no bytes were returned.
  1157. //
  1158. return (0);
  1159. }
  1160. //*****************************************************************************
  1161. //
  1162. //! Returns the number of free bytes in the transmit buffer.
  1163. //!
  1164. //! \param pvInstance is the pointer to the device instance structure as
  1165. //! returned by USBDBulkInit().
  1166. //!
  1167. //! This function returns the maximum number of bytes that can be passed on a
  1168. //! call to USBDBulkPacketWrite and accepted for transmission. The value
  1169. //! returned will be the maximum USB packet size (64) if no transmission is
  1170. //! currently outstanding or 0 if a transmission is in progress.
  1171. //!
  1172. //! \return Returns the number of bytes available in the transmit buffer.
  1173. //
  1174. //*****************************************************************************
  1175. unsigned int
  1176. USBDBulkTxPacketAvailable(void *pvInstance)
  1177. {
  1178. tBulkInstance *psInst;
  1179. ASSERT(pvInstance);
  1180. //
  1181. // Get our instance data pointer.
  1182. //
  1183. psInst = ((tUSBDBulkDevice *)pvInstance)->psPrivateBulkData;
  1184. //
  1185. // Do we have a packet transmission currently ongoing?
  1186. //
  1187. if(psInst->eBulkTxState != BULK_STATE_IDLE)
  1188. {
  1189. //
  1190. // We are not ready to receive a new packet so return 0.
  1191. //
  1192. return (0);
  1193. }
  1194. else
  1195. {
  1196. //
  1197. // We can receive a packet so return the max packet size for the
  1198. // relevant endpoint.
  1199. //
  1200. return (DATA_IN_EP_MAX_SIZE);
  1201. }
  1202. }
  1203. //*****************************************************************************
  1204. //
  1205. //! Determines whether a packet is available and, if so, the size of the
  1206. //! buffer required to read it.
  1207. //!
  1208. //! \param pvInstance is the pointer to the device instance structure as
  1209. //! returned by USBDBulkInit().
  1210. //!
  1211. //! This function may be used to determine if a received packet remains to be
  1212. //! read and allows the application to determine the buffer size needed to
  1213. //! read the data.
  1214. //!
  1215. //! \return Returns 0 if no received packet remains unprocessed or the
  1216. //! size of the packet if a packet is waiting to be read.
  1217. //
  1218. //*****************************************************************************
  1219. unsigned int
  1220. USBDBulkRxPacketAvailable(void *pvInstance)
  1221. {
  1222. unsigned int ulEPStatus;
  1223. unsigned int ulSize;
  1224. tBulkInstance *psInst;
  1225. ASSERT(pvInstance);
  1226. //
  1227. // Get our instance data pointer
  1228. //
  1229. psInst = ((tUSBDBulkDevice *)pvInstance)->psPrivateBulkData;
  1230. //
  1231. // Does the relevant endpoint FIFO have a packet waiting for us?
  1232. //
  1233. ulEPStatus = USBEndpointStatus(psInst->ulUSBBase, psInst->ucOUTEndpoint);
  1234. if(ulEPStatus & USB_DEV_RX_PKT_RDY)
  1235. {
  1236. //
  1237. // Yes - a packet is waiting. How big is it?
  1238. //
  1239. ulSize = USBEndpointDataAvail(psInst->ulUSBBase, psInst->ucOUTEndpoint);
  1240. return (ulSize);
  1241. }
  1242. else
  1243. {
  1244. //
  1245. // There is no packet waiting to be received.
  1246. //
  1247. return (0);
  1248. }
  1249. }
  1250. //*****************************************************************************
  1251. //
  1252. //! Reports the device power status (bus- or self-powered) to the USB library.
  1253. //!
  1254. //! \param pvInstance is the pointer to the bulk device instance structure.
  1255. //! \param ucPower indicates the current power status, either \b
  1256. //! USB_STATUS_SELF_PWR or \b USB_STATUS_BUS_PWR.
  1257. //!
  1258. //! Applications which support switching between bus- or self-powered
  1259. //! operation should call this function whenever the power source changes
  1260. //! to indicate the current power status to the USB library. This information
  1261. //! is required by the USB library to allow correct responses to be provided
  1262. //! when the host requests status from the device.
  1263. //!
  1264. //! \return None.
  1265. //
  1266. //*****************************************************************************
  1267. void
  1268. USBDBulkPowerStatusSet(void *pvInstance, unsigned char ucPower)
  1269. {
  1270. ASSERT(pvInstance);
  1271. //
  1272. // Pass the request through to the lower layer.
  1273. //
  1274. USBDCDPowerStatusSet(0, ucPower);
  1275. }
  1276. //*****************************************************************************
  1277. //
  1278. //! Requests a remote wake up to resume communication when in suspended state.
  1279. //!
  1280. //! \param pvInstance is the pointer to the bulk device instance structure.
  1281. //!
  1282. //! When the bus is suspended, an application which supports remote wake up
  1283. //! (advertised to the host via the configuration descriptor) may call this function
  1284. //! to initiate remote wake up signaling to the host. If the remote wake up
  1285. //! feature has not been disabled by the host, this will cause the bus to
  1286. //! resume operation within 20mS. If the host has disabled remote wake up,
  1287. //! \b false will be returned to indicate that the wake up request was not
  1288. //! successful.
  1289. //!
  1290. //! \return Returns \b true if the remote wake up is not disabled and the
  1291. //! signaling was started or \b false if remote wake up is disabled or if
  1292. //! signaling is currently ongoing following a previous call to this function.
  1293. //
  1294. //*****************************************************************************
  1295. tBoolean
  1296. USBDBulkRemoteWakeupRequest(void *pvInstance)
  1297. {
  1298. ASSERT(pvInstance);
  1299. //
  1300. // Pass the request through to the lower layer.
  1301. //
  1302. return(USBDCDRemoteWakeupRequest(0));
  1303. }
  1304. //*****************************************************************************
  1305. //
  1306. // Close the Doxygen group.
  1307. //! @}
  1308. //
  1309. //*****************************************************************************