usbbuffer.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. //*****************************************************************************
  2. //
  3. // usbbuffer.c - USB buffer object.
  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 Sitaraware USB Library and reused from revision 6288
  22. // of the Stellaris USB Library.
  23. //
  24. //*****************************************************************************
  25. #include "hw_types.h"
  26. #include "debug.h"
  27. #include "usblib.h"
  28. #include "usblibpriv.h"
  29. //*****************************************************************************
  30. //
  31. //! \addtogroup usblib_buffer_api
  32. //! @{
  33. //
  34. //*****************************************************************************
  35. //*****************************************************************************
  36. //
  37. // USB instance Object
  38. //
  39. //*****************************************************************************
  40. extern tUSBInstanceObject g_USBInstance[];
  41. //*****************************************************************************
  42. //
  43. // Workspace variables required by each buffer instance. This structure is
  44. // overlaid on the pvWorkspace memory provided in the tUSBBuffer structure
  45. // passed to USBBufferInit().
  46. //
  47. //*****************************************************************************
  48. typedef struct
  49. {
  50. tUSBRingBufObject sRingBuf;
  51. }
  52. tUSBBufferVars;
  53. //*****************************************************************************
  54. //
  55. // Schedule the next packet transmission to the host if data remains to be
  56. // sent.
  57. //
  58. // \param psBuffer points to the buffer from which a packet transmission is
  59. // to be scheduled.
  60. //
  61. // This function checks to determine whether the lower layer is capable of
  62. // accepting a new packet for transmission and, if so, schedules the next
  63. // packet transmission if data remains in the buffer.
  64. //
  65. // \return None.
  66. //
  67. //*****************************************************************************
  68. static void
  69. ScheduleNextTransmission(const tUSBBuffer *psBuffer)
  70. {
  71. tUSBBufferVars *psVars;
  72. unsigned int ulPacket, ulSpace, ulTotal;
  73. //
  74. // Get a pointer to our workspace variables.
  75. //
  76. psVars = psBuffer->pvWorkspace;
  77. //
  78. // Ask the lower layer if it has space to accept another packet of data.
  79. //
  80. ulPacket = psBuffer->pfnAvailable(psBuffer->pvHandle);
  81. //
  82. // If we were returned something other than zero, we can write that number
  83. // of bytes to the lower layer.
  84. //
  85. if(ulPacket)
  86. {
  87. //
  88. // How much contiguous data do we have in the buffer?
  89. //
  90. ulSpace = USBRingBufContigUsed(&psVars->sRingBuf);
  91. //
  92. // How much total data do we have in the buffer?
  93. //
  94. ulTotal = USBRingBufUsed(&psVars->sRingBuf);
  95. //
  96. // Write the contiguous bytes to the lower layer assuming there is
  97. // something to send.
  98. //
  99. if(ulSpace)
  100. {
  101. //
  102. // Determine the maximum sized block we can send in this transfer.
  103. //
  104. ulSpace = (ulSpace < ulPacket) ? ulSpace : ulPacket;
  105. //
  106. // Call the lower layer to send the new packet. If the current
  107. // data spans the buffer wrap, tell the lower layer that it can
  108. // expect a second call to fill the whole packet before it
  109. // transmits it.
  110. //
  111. psBuffer->pfnTransfer(psBuffer->pvHandle,
  112. (psVars->sRingBuf.pucBuf +
  113. psVars->sRingBuf.ulReadIndex), ulSpace,
  114. (((ulSpace < ulPacket) &&
  115. (ulSpace < ulTotal)) ? false : true));
  116. //
  117. // Do we need to send a second part to fill out the packet? This
  118. // will occur if the current packet spans the buffer wrap.
  119. //
  120. if((ulSpace < ulPacket) && (ulSpace < ulTotal))
  121. {
  122. //
  123. // The packet straddled the wrap. How much space remains in
  124. // the packet?
  125. //
  126. ulPacket -= ulSpace;
  127. //
  128. // How much data can we actually send?
  129. //
  130. ulSpace = ulTotal - ulSpace;
  131. ulSpace = (ulSpace > ulPacket) ? ulPacket : ulSpace;
  132. psBuffer->pfnTransfer(psBuffer->pvHandle,
  133. psVars->sRingBuf.pucBuf, ulSpace, true);
  134. }
  135. }
  136. //
  137. // Don't update the ring buffer read index yet. We do this once we are
  138. // sure the packet was correctly transmitted.
  139. //
  140. }
  141. }
  142. //*****************************************************************************
  143. //
  144. // Handles USB_EVENT_RX_AVAILABLE for a receive buffer.
  145. //
  146. // \param psBuffer points to the buffer which is receiving the event.
  147. // \param ulSize is the size reported in the event.
  148. // \param pucData is the pointer provided in the event.
  149. //
  150. // This function is responsible for reading data from the lower layer into
  151. // the buffer or, if we had previously passed a section of the buffer to the
  152. // lower layer for it to write into directly, updating the buffer write pointer
  153. // to add the new data to the buffer.
  154. //
  155. // If the pointer provided is NULL, we call the low level pfnTransfer function
  156. // to get the new data. If the pointer is not NULL and not within the existing
  157. // ring buffer, we copy the data directly from the pointer to the buffer and
  158. // return the number of bytes read.
  159. //
  160. // \return Returns the number of bytes read from the lower layer.
  161. //
  162. //*****************************************************************************
  163. static unsigned int
  164. HandleRxAvailable(tUSBBuffer *psBuffer, unsigned int ulSize,
  165. unsigned char *pucData)
  166. {
  167. tUSBBufferVars *psVars;
  168. unsigned int ulAvail, ulRead, ulPacket, ulRetCount;
  169. //
  170. // Get a pointer to our workspace variables.
  171. //
  172. psVars = psBuffer->pvWorkspace;
  173. //
  174. // Has the data already been read into memory?
  175. //
  176. if(pucData)
  177. {
  178. //
  179. // Yes - is it already in our ring buffer?
  180. //
  181. if((pucData >= psBuffer->pcBuffer) &&
  182. (pucData < psBuffer->pcBuffer + psBuffer->ulBufferSize))
  183. {
  184. //
  185. // The data is already in our ring buffer so merely update the
  186. // write pointer to add the new data.
  187. //
  188. USBRingBufAdvanceWrite(&psVars->sRingBuf, ulSize);
  189. //
  190. // In this case, we pass back 0 to indicate that the lower layer
  191. // doesn't need to make any buffer pointer updates.
  192. //
  193. ulRetCount = 0;
  194. }
  195. else
  196. {
  197. //
  198. // The data is not within our buffer so we need to copy it into
  199. // the buffer.
  200. //
  201. // How much space does the buffer have available?
  202. //
  203. ulAvail = USBRingBufFree(&psVars->sRingBuf);
  204. //
  205. // How much should we copy?
  206. //
  207. ulRead = (ulAvail < ulSize) ? ulAvail : ulSize;
  208. //
  209. // Copy the data into the buffer.
  210. //
  211. USBRingBufWrite(&psVars->sRingBuf, pucData, ulRead);
  212. //
  213. // We need to return the number of bytes we read in this case
  214. // since the buffer supplied to us was owned by the lower layer and
  215. // it may need to update its read pointer.
  216. //
  217. ulRetCount = ulRead;
  218. }
  219. }
  220. else
  221. {
  222. //
  223. // We were passed a NULL pointer so the low level driver has not read
  224. // the data into memory yet. We need to call the transfer function to
  225. // get the packet.
  226. //
  227. // How big is the packet that we need to receive?
  228. //
  229. ulPacket = psBuffer->pfnAvailable(psBuffer->pvHandle);
  230. //
  231. // How much contiguous space do we have in the buffer?
  232. //
  233. ulAvail = USBRingBufContigFree(&psVars->sRingBuf);
  234. //
  235. // Get as much of the packet as we can in the available space.
  236. //
  237. ulRead = psBuffer->pfnTransfer(psBuffer->pvHandle,
  238. (psVars->sRingBuf.pucBuf +
  239. psVars->sRingBuf.ulWriteIndex),
  240. ulAvail, true);
  241. //
  242. // Advance the ring buffer write pointer to add our new data.
  243. //
  244. if(ulRead)
  245. {
  246. USBRingBufAdvanceWrite(&psVars->sRingBuf, ulRead);
  247. }
  248. //
  249. // Did we get the whole packet?
  250. //
  251. if(ulRead < ulPacket)
  252. {
  253. //
  254. // No - how much space do we have in the buffer?
  255. //
  256. ulAvail = USBRingBufContigFree(&psVars->sRingBuf);
  257. //
  258. // If there is any space left, read as much of the remainder of
  259. // the packet as we can.
  260. //
  261. if(ulAvail)
  262. {
  263. ulPacket =
  264. psBuffer->pfnTransfer(psBuffer->pvHandle,
  265. (psVars->sRingBuf.pucBuf +
  266. psVars->sRingBuf.ulWriteIndex),
  267. ulAvail, true);
  268. //
  269. // Update the write pointer after we read more data into the
  270. // buffer.
  271. //
  272. if(ulPacket)
  273. {
  274. USBRingBufAdvanceWrite(&psVars->sRingBuf, ulPacket);
  275. }
  276. }
  277. }
  278. //
  279. // We need to return 0 in this case to indicate that the lower layer
  280. // need not perform any buffer maintenance as a result of the callback.
  281. //
  282. ulRetCount = 0;
  283. }
  284. //
  285. // How much data do we have in the buffer?
  286. //
  287. ulAvail = USBRingBufUsed(&psVars->sRingBuf);
  288. //
  289. // Pass the event on to the client with the current read pointer and
  290. // available data size. The client is expected to understand the ring
  291. // structure and be able to deal with wrap if it wants to read the data
  292. // directly from the buffer.
  293. //
  294. ulRead = psBuffer->pfnCallback(psBuffer->pvCBData,
  295. USB_EVENT_RX_AVAILABLE,
  296. ulAvail,
  297. (psVars->sRingBuf.pucBuf +
  298. psVars->sRingBuf.ulReadIndex));
  299. //
  300. // If the client read anything from the buffer, update the read pointer.
  301. //
  302. USBRingBufAdvanceRead(&psVars->sRingBuf, ulRead);
  303. //
  304. // Return the correct value to the low level driver.
  305. //
  306. return(ulRetCount);
  307. }
  308. //*****************************************************************************
  309. //
  310. // Handles USB_EVENT_DATA_REMAINING for a receive buffer.
  311. //
  312. // \param psBuffer points to the buffer which is receiving the event.
  313. //
  314. // This function determines the total number of bytes of data that remain
  315. // unprocessed in the client and buffer and reports this back to the caller.
  316. //
  317. // \return Returns the number of bytes remaining to be processed.
  318. //
  319. //*****************************************************************************
  320. static unsigned int
  321. HandleDataRemaining(tUSBBuffer *psBuffer)
  322. {
  323. unsigned int ulBufData, ulClientData;
  324. tUSBBufferVars *psVars;
  325. //
  326. // Get a pointer to our workspace variables.
  327. //
  328. psVars = psBuffer->pvWorkspace;
  329. //
  330. // How much data does the client currently have buffered?
  331. //
  332. ulClientData = psBuffer->pfnCallback(psBuffer->pvCBData,
  333. USB_EVENT_DATA_REMAINING, 0,
  334. (void *)0);
  335. //
  336. // How much data do we have in the buffer?
  337. //
  338. ulBufData = USBRingBufUsed(&psVars->sRingBuf);
  339. //
  340. // Return the total number of bytes of unprocessed data to the lower layer.
  341. //
  342. return(ulBufData + ulClientData);
  343. }
  344. //*****************************************************************************
  345. //
  346. // Handles USB_EVENT_TX_COMPLETE for a transmit buffer.
  347. //
  348. // \param psBuffer points to the buffer which is receiving the event.
  349. // \param ulSize is the number of bytes that have been transmitted and
  350. // acknowledged.
  351. //
  352. // This function informs us that data written to the lower layer from a
  353. // transmit buffer has been successfully transmitted. We use this to update
  354. // the buffer read pointer and attempt to schedule the next transmission if
  355. // data remains in the buffer.
  356. //
  357. // \return Returns the number of bytes remaining to be processed.
  358. //
  359. //*****************************************************************************
  360. static unsigned int
  361. HandleTxComplete(tUSBBuffer *psBuffer, unsigned int ulSize)
  362. {
  363. tUSBBufferVars *psVars;
  364. //
  365. // Get a pointer to our workspace variables.
  366. //
  367. psVars = psBuffer->pvWorkspace;
  368. //
  369. // Update the transmit buffer read pointer to remove the data that has
  370. // now been transmitted.
  371. //
  372. USBRingBufAdvanceRead(&psVars->sRingBuf, ulSize);
  373. //
  374. // Try to schedule the next packet transmission if data remains to be
  375. // sent.
  376. //
  377. ScheduleNextTransmission(psBuffer);
  378. //
  379. // The return code from this event is ignored.
  380. //
  381. return(0);
  382. }
  383. //*****************************************************************************
  384. //
  385. // Handles USB_EVENT_REQUEST_BUFFER for a receive buffer.
  386. //
  387. // \param psBuffer points to the buffer which is receiving the event.
  388. // \param ulSize is the size of the buffer requested.
  389. // \param ppucBuffer is a pointer which is to be written with a pointer to
  390. // the returned buffer.
  391. //
  392. // This function is called by a low level driver that wishes to receive data
  393. // automatically and write it directly to a memory buffer, either using
  394. // software or DMA prior to issuing USB_EVENT_RX_AVAILABLE. The event is sent
  395. // in advance of receiving data to provide storage for whatever is received
  396. // next.
  397. //
  398. // If we have a contiguous block of space in the buffer of at least ulSize
  399. // bytes immediately in front of the current write pointer, we pass this back
  400. // otherwise we send NULL indicating that the next packet should be notified
  401. // using a standard USB_EVENT_RX_AVAILABLE event without being received
  402. // automatically. Note that the USB_EVENT_REQUEST_BUFFER protocol allows us to
  403. // return less than \e ulSize bytes if we know how much data is expected next
  404. // but this is not possible here since the USBBuffer knows nothing about the
  405. // protocol whose data it is handling.
  406. //
  407. // \return Returns the number of bytes remaining to be processed.
  408. //
  409. //*****************************************************************************
  410. static unsigned int
  411. HandleRequestBuffer(tUSBBuffer *psBuffer, unsigned int ulSize,
  412. unsigned char **ppucBuffer)
  413. {
  414. tUSBBufferVars *psVars;
  415. unsigned int ulSpace;
  416. //
  417. // Get a pointer to our workspace variables.
  418. //
  419. psVars = psBuffer->pvWorkspace;
  420. //
  421. // How much contiguous space do we have available?
  422. //
  423. ulSpace = USBRingBufContigFree(&psVars->sRingBuf);
  424. //
  425. // Is there enough space available to satisfy the request?
  426. //
  427. if(ulSpace >= ulSize)
  428. {
  429. //
  430. // Yes - return the current write pointer
  431. //
  432. *ppucBuffer = psVars->sRingBuf.pucBuf + psVars->sRingBuf.ulWriteIndex;
  433. return(ulSize);
  434. }
  435. else
  436. {
  437. //
  438. // We do not have enough contiguous space following the current write
  439. // pointer to satisfy the request so do not provide a buffer.
  440. //
  441. *ppucBuffer = (unsigned char *)0;
  442. return(0);
  443. }
  444. }
  445. //*****************************************************************************
  446. //
  447. //! Initializes a USB buffer object to be used with a given USB controller and
  448. //! device or host class driver.
  449. //!
  450. //! \param psBuffer points to a structure containing information on the buffer
  451. //! memory to be used and the underlying device or host class driver whose data
  452. //! is to be buffered. This structure must remain accessible for as int as
  453. //! the buffer is in use.
  454. //!
  455. //! This function is used to initialize a USB buffer object and insert it
  456. //! into the function and callback interfaces between an underlying driver
  457. //! and the application. The caller supplies information on both the RAM
  458. //! to be used to buffer data, the type of buffer to be created (transmit or
  459. //! receive) and the functions to be called in the lower layer to transfer
  460. //! data to or from the USB controller.
  461. //!
  462. //! \return Returns the original buffer structure pointer if successful or
  463. //! NULL if an error is detected.
  464. //
  465. //*****************************************************************************
  466. const tUSBBuffer *
  467. USBBufferInit(const tUSBBuffer *psBuffer)
  468. {
  469. tUSBBufferVars *psVars;
  470. //
  471. // Check parameter validity.
  472. //
  473. ASSERT(psBuffer && psBuffer->pvWorkspace && psBuffer->pcBuffer &&
  474. psBuffer->ulBufferSize && psBuffer->pfnAvailable &&
  475. psBuffer->pfnTransfer && psBuffer->pfnCallback);
  476. //
  477. // Get a pointer to the buffer workspace and inttialize the variables it
  478. // contains.
  479. //
  480. psVars = psBuffer->pvWorkspace;
  481. USBRingBufInit(&psVars->sRingBuf, psBuffer->pcBuffer,
  482. psBuffer->ulBufferSize);
  483. //
  484. // If all is well, return the same pointer we were originally passed.
  485. //
  486. return(psBuffer);
  487. }
  488. //*****************************************************************************
  489. //
  490. //! Returns the current ring buffer indices for this USB buffer.
  491. //!
  492. //! \param psBuffer is the pointer to the buffer instance whose information
  493. //! is being queried.
  494. //! \param psRingBuf is a pointer to storage that will be written with the
  495. //! current ring buffer control structure for this USB buffer.
  496. //!
  497. //! This function is provided to aid a client wishing to write data directly
  498. //! into the USB buffer rather than using the USBBufferWrite() function. This
  499. //! may be necessary to control when the USBBuffer starts transmission of a
  500. //! large block of data, for example.
  501. //!
  502. //! A transmit buffer will immediately send a new packet on any call to
  503. //! USBBufferWrite() if the underlying layer indicates that a transmission can
  504. //! be started. In some cases this is not desirable and a client may wish to
  505. //! wishes to write more data to the buffer in advance of starting transmission
  506. //! to the lower layer. In such cases, this function may be called to retrieve
  507. //! the current ring buffer indices and the buffer accessed directly. Once the
  508. //! client has written all data it wishes to send, it should call function
  509. //! USBBufferDataWritten() to indicate that transmission may begin.
  510. //!
  511. //! \return None.
  512. //
  513. //*****************************************************************************
  514. void
  515. USBBufferInfoGet(const tUSBBuffer *psBuffer, tUSBRingBufObject *psRingBuf)
  516. {
  517. tUSBBufferVars *psVars;
  518. //
  519. // Check parameter validity.
  520. //
  521. ASSERT(psBuffer && psRingBuf);
  522. //
  523. // Get our workspace variables.
  524. //
  525. psVars = psBuffer->pvWorkspace;
  526. //
  527. // Copy the current ring buffer settings to the clients storage.
  528. //
  529. *psRingBuf = psVars->sRingBuf;
  530. }
  531. //*****************************************************************************
  532. //
  533. //! Indicates that a client has written data directly into the buffer and
  534. //! wishes to start transmission.
  535. //!
  536. //! \param psBuffer is the pointer to the buffer instance into which data has
  537. //! been written.
  538. //! \param ulLength is the number of bytes of data that the client has written.
  539. //!
  540. //! This function updates the USB buffer write pointer and starts transmission
  541. //! of the data in the buffer assuming the lower layer is ready to receive a
  542. //! new packet. The function is provided to aid a client wishing to write
  543. //! data directly into the USB buffer rather than using the USBBufferWrite()
  544. //! function. This may be necessary to control when the USB buffer starts
  545. //! transmission of a large block of data, for example.
  546. //!
  547. //! A transmit buffer will immediately send a new packet on any call to
  548. //! USBBufferWrite() if the underlying layer indicates that a transmission can
  549. //! be started. In some cases this is not desirable and a client may wish to
  550. //! write more data to the buffer in advance of starting transmission
  551. //! to the lower layer. In such cases, USBBufferInfoGet() may be called to
  552. //! retrieve the current ring buffer indices and the buffer accessed directly.
  553. //! Once the client has written all data it wishes to send (taking care to
  554. //! handle the ring buffer wrap), it should call this function to indicate that
  555. //! transmission may begin.
  556. //!
  557. //! \return None.
  558. //
  559. //*****************************************************************************
  560. void
  561. USBBufferDataWritten(const tUSBBuffer *psBuffer, unsigned int ulLength)
  562. {
  563. tUSBBufferVars *psVars;
  564. //
  565. // Check parameter validity.
  566. //
  567. ASSERT(psBuffer);
  568. //
  569. // Get our workspace variables.
  570. //
  571. psVars = psBuffer->pvWorkspace;
  572. //
  573. // Advance the ring buffer write pointer to include the newly written
  574. // data.
  575. //
  576. if(ulLength)
  577. {
  578. USBRingBufAdvanceWrite(&psVars->sRingBuf, ulLength);
  579. }
  580. //
  581. // Try to schedule a new packet transmission.
  582. //
  583. ScheduleNextTransmission(psBuffer);
  584. }
  585. //*****************************************************************************
  586. //
  587. //! Indicates that a client has read data directly out of the buffer.
  588. //!
  589. //! \param psBuffer is the pointer to the buffer instance from which data has
  590. //! been read.
  591. //! \param ulLength is the number of bytes of data that the client has read.
  592. //!
  593. //! This function updates the USB buffer read pointer to remove data that
  594. //! the client has read directly rather than via a call to USBBufferRead().
  595. //! The function is provided to aid a client wishing to minimize data copying.
  596. //! To read directly from the buffer, a client must call USBBufferInfoGet() to
  597. //! retrieve the current buffer indices. With this information, the data
  598. //! following the current read index can be read. Once the client has
  599. //! processed much data as it needs, USBBufferDataRemoved() must be called to
  600. //! advance the read pointer past the data that has been read and free up that
  601. //! section of the buffer. The client must take care to correctly handle the
  602. //! wrap point if accessing the buffer directly.
  603. //!
  604. //! \return None.
  605. //
  606. //*****************************************************************************
  607. void
  608. USBBufferDataRemoved(const tUSBBuffer *psBuffer, unsigned int ulLength)
  609. {
  610. tUSBBufferVars *psVars;
  611. //
  612. // Check parameter validity.
  613. //
  614. ASSERT(psBuffer);
  615. //
  616. // Get our workspace variables.
  617. //
  618. psVars = psBuffer->pvWorkspace;
  619. //
  620. // Advance the ring buffer write pointer to include the newly written
  621. // data.
  622. //
  623. if(ulLength)
  624. {
  625. USBRingBufAdvanceRead(&psVars->sRingBuf, ulLength);
  626. }
  627. }
  628. //*****************************************************************************
  629. //
  630. //! Sets the callback pointer supplied to clients of this buffer.
  631. //!
  632. //! \param psBuffer is the pointer to the buffer instance whose callback data
  633. //! is to be changed.
  634. //! \param pvCBData is the pointer the client wishes to receive on all future
  635. //! callbacks from this buffer.
  636. //!
  637. //! This function sets the callback pointer which this buffer will supply
  638. //! to clients as the \e pvCBData parameter in all future calls to the
  639. //! event callback.
  640. //!
  641. //! \note If this function is to be used, the application must ensure that the
  642. //! tUSBBuffer structure used to describe this buffer is held in RAM rather
  643. //! than flash. The \e pvCBData value passed is written directly into this
  644. //! structure.
  645. //!
  646. //! \return Returns the previous callback pointer set for the buffer.
  647. //
  648. //*****************************************************************************
  649. void *
  650. USBBufferCallbackDataSet(tUSBBuffer *psBuffer, void *pvCBData)
  651. {
  652. void *pvOldData;
  653. //
  654. // Keep a copy of the old callback data.
  655. //
  656. pvOldData = psBuffer->pvCBData;
  657. //
  658. // Replace the callback data with the new value.
  659. //
  660. psBuffer->pvCBData = pvCBData;
  661. //
  662. // Give the caller the old value back.
  663. //
  664. return(pvOldData);
  665. }
  666. //*****************************************************************************
  667. //
  668. //! Writes a block of data to the transmit buffer and queues it for
  669. //! transmission to the USB controller.
  670. //!
  671. //! \param psBuffer points to the pointer instance into which data is to be
  672. //! written.
  673. //! \param pucData points to the first byte of data which is to be written.
  674. //! \param ulLength is the number of bytes of data to write to the buffer.
  675. //!
  676. //! This function copies the supplied data into the transmit buffer. The
  677. //! transmit buffer data will be packetized according to the constraints
  678. //! imposed by the lower layer in use and sent to the USB controller as soon as
  679. //! possible. Once a packet is transmitted and acknowledged, a
  680. //! \b USB_EVENT_TX_COMPLETE event will be sent to the application callback
  681. //! indicating the number of bytes that have been sent from the buffer.
  682. //!
  683. //! Attempts to send more data than there is space for in the transmit buffer
  684. //! will result in fewer bytes than expected being written. The value returned
  685. //! by the function indicates the actual number of bytes copied to the buffer.
  686. //!
  687. //! \return Returns the number of bytes actually written.
  688. //
  689. //*****************************************************************************
  690. unsigned int
  691. USBBufferWrite(const tUSBBuffer *psBuffer, const unsigned char *pucData,
  692. unsigned int ulLength)
  693. {
  694. unsigned int ulSpace;
  695. tUSBBufferVars *psVars;
  696. //
  697. // Check parameter validity.
  698. //
  699. ASSERT(psBuffer && pucData);
  700. ASSERT(psBuffer->bTransmitBuffer == true);
  701. //
  702. // Get our workspace variables.
  703. //
  704. psVars = psBuffer->pvWorkspace;
  705. //
  706. // How much space is left in the buffer?
  707. //
  708. ulSpace = USBRingBufFree(&psVars->sRingBuf);
  709. //
  710. // How many bytes will we write?
  711. //
  712. ulLength = (ulLength > ulSpace) ? ulSpace : ulLength;
  713. //
  714. // Write the data to the buffer.
  715. //
  716. if(ulLength)
  717. {
  718. USBRingBufWrite(&psVars->sRingBuf, pucData, ulLength);
  719. }
  720. //
  721. // Try to transmit the next packet to the host.
  722. //
  723. ScheduleNextTransmission(psBuffer);
  724. //
  725. // Tell the caller how many bytes we wrote to the buffer.
  726. //
  727. return(ulLength);
  728. }
  729. //*****************************************************************************
  730. //
  731. //! Flushes a USB buffer, discarding any data that it contains.
  732. //!
  733. //! \param psBuffer is the pointer to the buffer instance which is to be
  734. //! flushed.
  735. //!
  736. //! This function discards all data currently in the supplied buffer without
  737. //! processing (transmitting it via the USB controller or passing it to the
  738. //! client depending upon the buffer mode).
  739. //!
  740. //! \return None.
  741. //
  742. //*****************************************************************************
  743. void
  744. USBBufferFlush(const tUSBBuffer *psBuffer)
  745. {
  746. tUSBBufferVars *psVars;
  747. //
  748. // Check parameter validity.
  749. //
  750. ASSERT(psBuffer);
  751. //
  752. // Get our workspace variables.
  753. //
  754. psVars = psBuffer->pvWorkspace;
  755. //
  756. // Flush the ring buffer.
  757. //
  758. USBRingBufFlush(&psVars->sRingBuf);
  759. }
  760. //*****************************************************************************
  761. //
  762. //! Reads a block of data from a USB receive buffer into storage supplied by
  763. //! the caller.
  764. //!
  765. //! \param psBuffer is the pointer to the buffer instance from which data is
  766. //! to be read.
  767. //! \param pucData points to a buffer into which the received data will be
  768. //! written.
  769. //! \param ulLength is the size of the buffer pointed to by pucData.
  770. //!
  771. //! This function reads up to ulLength bytes of data received from the USB
  772. //! host into the supplied application buffer. If the receive buffer
  773. //! contains fewer than \e ulLength bytes of data, the data that is present
  774. //! will be copied and the return code will indicate the actual number of bytes
  775. //! copied to \e pucData.
  776. //!
  777. //! \return Returns the number of bytes of data read.
  778. //
  779. //*****************************************************************************
  780. unsigned int
  781. USBBufferRead(const tUSBBuffer *psBuffer, unsigned char *pucData,
  782. unsigned int ulLength)
  783. {
  784. tUSBBufferVars *psVars;
  785. unsigned int ulAvail, ulRead;
  786. //
  787. // Check parameter validity.
  788. //
  789. ASSERT(psBuffer && pucData && ulLength);
  790. //
  791. // Get our workspace variables.
  792. //
  793. psVars = psBuffer->pvWorkspace;
  794. //
  795. // How much data is in the buffer?
  796. //
  797. ulAvail = USBRingBufUsed(&psVars->sRingBuf);
  798. //
  799. // Determine how many bytes we can actually read.
  800. //
  801. ulRead = (ulAvail < ulLength) ? ulAvail : ulLength;
  802. //
  803. // Read the data from the buffer assuming there is some to read.
  804. //
  805. if(ulRead)
  806. {
  807. USBRingBufRead(&psVars->sRingBuf, pucData, ulRead);
  808. }
  809. //
  810. // Tell the caller how many bytes we wrote to their buffer.
  811. //
  812. return(ulRead);
  813. }
  814. //*****************************************************************************
  815. //
  816. //! Returns the number of bytes of data available in the buffer.
  817. //!
  818. //! \param psBuffer is the pointer to the buffer instance which is to be
  819. //! queried.
  820. //!
  821. //! This function may be used to determine the number of bytes of data in a
  822. //! buffer. For a receive buffer, this indicates the number of bytes that the
  823. //! client can read from the buffer using USBBufferRead(). For a transmit
  824. //! buffer, this indicates the amount of data that remains to be sent to the
  825. //! USB controller.
  826. //!
  827. //! \return Returns the number of bytes of data in the buffer.
  828. //
  829. //*****************************************************************************
  830. unsigned int
  831. USBBufferDataAvailable(const tUSBBuffer *psBuffer)
  832. {
  833. tUSBBufferVars *psVars;
  834. //
  835. // Check parameter validity.
  836. //
  837. ASSERT(psBuffer);
  838. //
  839. // Get our workspace variables.
  840. //
  841. psVars = psBuffer->pvWorkspace;
  842. //
  843. // Return the amount of data in the buffer.
  844. //
  845. return(USBRingBufUsed(&psVars->sRingBuf));
  846. }
  847. //*****************************************************************************
  848. //
  849. //! Returns the number of free bytes in the buffer.
  850. //!
  851. //! \param psBuffer is the pointer to the buffer instance which is to be
  852. //! queried.
  853. //!
  854. //! This function returns the number of free bytes in the buffer. For a
  855. //! transmit buffer, this indicates the maximum number of bytes that can be
  856. //! passed on a call to USBBufferWrite() and accepted for transmission. For a
  857. //! receive buffer, it indicates the number of bytes that can be read from the
  858. //! USB controller before the buffer will be full.
  859. //!
  860. //! \return Returns the number of free bytes in the buffer.
  861. //
  862. //*****************************************************************************
  863. unsigned int
  864. USBBufferSpaceAvailable(const tUSBBuffer *psBuffer)
  865. {
  866. tUSBBufferVars *psVars;
  867. //
  868. // Check parameter validity.
  869. //
  870. ASSERT(psBuffer);
  871. //
  872. // Get our workspace variables.
  873. //
  874. psVars = psBuffer->pvWorkspace;
  875. //
  876. // Return the amount of space available in the buffer.
  877. //
  878. return(USBRingBufFree(&psVars->sRingBuf));
  879. }
  880. //*****************************************************************************
  881. //
  882. //! Called by the USB buffer to notify the client of asynchronous events.
  883. //!
  884. //! \param pvCBData is the client-supplied callback pointer associated with
  885. //! this buffer instance.
  886. //! \param ulEvent is the identifier of the event being sent. This will be
  887. //! a general event identifier of the form USBD_EVENT_xxxx or a device
  888. //! class-dependent event of the form USBD_CDC_EVENT_xxx or
  889. //! USBD_HID_EVENT_xxx.
  890. //! \param ulMsgValue is an event-specific parameter value.
  891. //! \param pvMsgData is an event-specific data pointer.
  892. //!
  893. //! This function is the USB buffer event handler that applications should
  894. //! register with the USB device class driver as the callback for the channel
  895. //! which is to be buffered using this buffer.
  896. //!
  897. //! \note This function will never be called by an application. It is the
  898. //! handler that allows the USB buffer to be inserted above the device class
  899. //! driver or host pipe driver and below the application to offer buffering
  900. //! support.
  901. //!
  902. //! \return The return value is dependent upon the event being processed.
  903. //
  904. //*****************************************************************************
  905. unsigned int
  906. USBBufferEventCallback(void *pvCBData, unsigned int ulEvent,
  907. unsigned int ulMsgValue, void *pvMsgData)
  908. {
  909. tUSBBuffer *psBuffer;
  910. //
  911. // Get our instance data pointers from the callback data.
  912. //
  913. psBuffer = (tUSBBuffer *)pvCBData;
  914. ASSERT(psBuffer);
  915. //
  916. // Which event have we been sent?
  917. //
  918. switch(ulEvent)
  919. {
  920. //
  921. // Data is available from the lower layer.
  922. //
  923. case USB_EVENT_RX_AVAILABLE:
  924. {
  925. //
  926. // This event is only relevant to us if we are a receive buffer.
  927. //
  928. if(!psBuffer->bTransmitBuffer)
  929. {
  930. return(HandleRxAvailable(psBuffer, ulMsgValue, pvMsgData));
  931. }
  932. break;
  933. }
  934. //
  935. // We are being asked how much data remains to be processed.
  936. //
  937. case USB_EVENT_DATA_REMAINING:
  938. {
  939. return(HandleDataRemaining(psBuffer));
  940. }
  941. //
  942. // A previous transmission has completed.
  943. //
  944. case USB_EVENT_TX_COMPLETE:
  945. {
  946. //
  947. // This event is only relevant to us if we are a transmit buffer.
  948. //
  949. if(psBuffer->bTransmitBuffer)
  950. {
  951. //
  952. // Handle the message then drop out of the switch so that the
  953. // event is echoed to the layer above.
  954. //
  955. HandleTxComplete(psBuffer, ulMsgValue);
  956. }
  957. break;
  958. }
  959. //
  960. // We are being asked to provide a buffer into which the next packet
  961. // can be received.
  962. //
  963. case USB_EVENT_REQUEST_BUFFER:
  964. {
  965. //
  966. // This event is only relevant to us if we are a receive buffer.
  967. //
  968. if(!psBuffer->bTransmitBuffer)
  969. {
  970. return(HandleRequestBuffer(psBuffer, ulMsgValue, pvMsgData));
  971. }
  972. break;
  973. }
  974. //
  975. // All other events are merely passed through to the client.
  976. //
  977. default:
  978. {
  979. break;
  980. }
  981. }
  982. //
  983. // If we drop out of the switch, we need to pass the event on to the client
  984. // unmodified and return the relevant return code back to the lower layer.
  985. //
  986. return(psBuffer->pfnCallback(psBuffer->pvCBData, ulEvent, ulMsgValue,
  987. pvMsgData));
  988. }
  989. //*****************************************************************************
  990. //
  991. // Close the Doxygen group.
  992. //! @}
  993. //
  994. //*****************************************************************************