class_common.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. #include "drivers/usb/class/common/class_common.h"
  2. #include "app/usb/usb_transfer_flags.h" // USB_TRANSFER_FLAG_SHORTPACKET
  3. #include "usbd_desc.h"
  4. #include "usbd_ctlreq.h"
  5. #if DEBUG_USB > 0 // SCPI debug
  6. volatile uint32_t gDebugUSBEPTxBytes = 0;
  7. volatile uint32_t gDebugUSBEPTxLastPacket = 0;
  8. #endif
  9. // USBD_COMMON_EP0_RxReady
  10. // This function is called when a packet via CONTROL protocol had been received from host
  11. // This function continues the transmitting
  12. uint8_t USBD_COMMON_EP0_RxReady (USBD_HandleTypeDef *pdev, sUSBTransfer_t * rx, size_t * pnBytesRollback )
  13. {
  14. USBD_EndpointTypeDef * pep = &pdev->ep_out[0];
  15. USBD_StatusTypeDef ret = USBD_FAIL;
  16. if( pdev->pUserData != NULL && usb_validate_transfer(rx) )
  17. {
  18. USBD_COMMON_ItfTypeDef * pUserIface = ((USBD_COMMON_ItfTypeDef *)pdev->pUserData);
  19. if( NULL != pUserIface->fUsbCtlEpRx )
  20. {
  21. size_t nBytesReceived = ((pep->rem_length > pep->maxpacket)?pep->maxpacket:pep->rem_length);
  22. size_t nBytesRemaining = (pep->rem_length - nBytesReceived);
  23. // Since the core uses raw @ep0_rx_transfer->pWriteData pointer, it is required to update the transfer information.
  24. // Use 'virutal write' to shift the pointer:
  25. usb_transfer_virtual_write( rx, nBytesReceived );
  26. // Check if the trasfer have enough free space for receiving another packet?
  27. // Or maybe it is a last packet?
  28. if( (usb_space_transfer(rx) < MIN(nBytesRemaining, pep->maxpacket)) || (0 == nBytesRemaining) )
  29. {
  30. // No, there no enough free space
  31. // Need to call user handler.
  32. // Retirieve actual amount of data in transfer
  33. size_t nBytesInTransfer = usb_count_transfer(rx);
  34. // Calculate current data offset
  35. size_t nOffset = (pep->total_length - nBytesRemaining - nBytesInTransfer);
  36. // pass the data to user
  37. size_t nBytesProcessed = pUserIface->fUsbCtlEpRx( &pdev->request,
  38. rx,
  39. nOffset,
  40. nBytesRemaining + nBytesInTransfer );
  41. // user can perform move semantic on transfer @rx.
  42. // It is valid condition if no remaining bytes left only.
  43. // Let's check it:
  44. bool bValid = usb_validate_transfer( rx );
  45. if( 0 == nBytesRemaining || bValid )
  46. {
  47. // Actually, it does not matter, what exactly the function @fUsbCtlEpRx returns.
  48. // The only that matters is the amount of data in the transfer.
  49. // Just check the return value for zero:
  50. if( nBytesProcessed > 0 )
  51. {
  52. // By default ( usb_count_transfer() is ZERO ) ==> @nCompressed equals to the number of bytes in the transfer before user handler call.
  53. size_t nCompressed = nBytesInTransfer;
  54. // It is required that the transfer be valid only if @nBytesRemaining greater than zero
  55. if( bValid )
  56. {
  57. // If the transfer still contains data, it means user didn't perform full read.
  58. if( usb_count_transfer(rx) > 0 )
  59. {
  60. // it is required to compress the data in the transfer
  61. // optimize the transfer to compress stored data
  62. nCompressed = usb_transfer_compress( rx );
  63. // @bNeedPacketSpace - true if the core deadly needs at least MAX_PACKET free bytes in transfer
  64. bool bNeedPacketSpace = false;
  65. // After compressing: check if the transfer have enough space for next packet?
  66. if( usb_space_transfer(rx) >= MIN(nBytesRemaining, pep->maxpacket) )
  67. {
  68. // yes, ok
  69. ret = USBD_OK;
  70. }
  71. else
  72. {
  73. // nope, user didn't read enough data from the transfer to free enough space for next packet
  74. // Need to free at least MAX_PACKET free bytes in transfer
  75. bNeedPacketSpace = true;
  76. }
  77. // try to call user handler again:
  78. if( bNeedPacketSpace || (0 == nBytesRemaining) )
  79. {
  80. // Here: two reasons:
  81. // 1) if it is required to free MAX_PACKET bytes in transfer for next packet
  82. // 2) or this is a last packet: last chance to retrieve data. It is required to feed all data to user... Even if he resists!!!! >:D
  83. // For USB-core it does not matter if user processed all bytes or not...
  84. // There no incoming packets expected.
  85. // The only problem is to send a confirmation for this transaction.
  86. // As soon user process all bytes from the transfer @rx, this function returns control to the
  87. // core to send a confirmation.
  88. // Ok, lets feed user..
  89. while( nBytesProcessed > 0 )
  90. {
  91. nBytesInTransfer = usb_count_transfer(rx);
  92. size_t nLeftBytes = nBytesInTransfer + ((bNeedPacketSpace)?nBytesRemaining:0);
  93. size_t nOffset = (pep->total_length - nLeftBytes);
  94. // Call user handler, again...
  95. nBytesProcessed = pUserIface->fUsbCtlEpRx( &pdev->request,
  96. rx,
  97. nOffset,
  98. nLeftBytes );
  99. // user can perform move semantic on transfer @rx.
  100. // It is valid condition if no remaining bytes left only.
  101. // Let's check it:
  102. if( 0 == nBytesRemaining || usb_validate_transfer( rx ) )
  103. {
  104. // ~~~ do not do this here, extra compressing possible (reduces performance) ~~ /* usb_transfer_compress( rx ); */
  105. // ~~~ do not do this here, extra compressing possible (reduces performance) ~~ /* nCompressed += (nBytesInTransfer - usb_count_transfer(rx)); */
  106. // Reason 1: (only if @bNeedPacketSpace is true)
  107. // check if the transfer has enough data for next packet
  108. if( bNeedPacketSpace
  109. && (usb_space_transfer(rx) >= MIN(nBytesRemaining, pep->maxpacket)) )
  110. {
  111. // Reason 1: exit, because transfer has enough free space for next packet
  112. // User have read data portion from the transfer, need to compress it again
  113. // Need to update @nCompressed variable to take in account new pointer to the free memory.
  114. usb_transfer_compress( rx );
  115. nCompressed += (nBytesInTransfer - usb_count_transfer(rx));
  116. ret = USBD_OK;
  117. break;
  118. }
  119. else
  120. {
  121. // User have read data portion from the transfer, need to compress it again
  122. // Need to update @nCompressed variable to take in account new pointer to the free memory.
  123. usb_transfer_compress( rx );
  124. nCompressed += (nBytesInTransfer - usb_count_transfer(rx));
  125. }
  126. // check if the transfer is still not empty
  127. if( 0 == usb_count_transfer(rx) )
  128. {
  129. // Reason 2: exit if transfer is empty
  130. // Empty! => Success (nBytesProcessed>0)
  131. break; // yee, at last!
  132. }
  133. }
  134. else
  135. {
  136. // Failed!
  137. ret = USBD_FAIL;
  138. }
  139. }
  140. // Check the last user call status
  141. if( 0 == nBytesProcessed )
  142. {
  143. // Failed!
  144. ret = USBD_FAIL;
  145. }
  146. }
  147. }
  148. else
  149. {
  150. // ~~~~~~~~~~~~ ADDED 18/04/19
  151. // Check if the trasfer have enough free space for receiving another packet?
  152. // Or maybe it is a last packet?
  153. if( (usb_space_transfer(rx) < MIN(nBytesRemaining, pep->maxpacket)) )
  154. {
  155. // it is required to compress the data in the transfer
  156. // optimize the transfer to compress stored data
  157. nCompressed = usb_transfer_compress( rx );
  158. }
  159. // ~~~~~~~~~~~~
  160. ret = USBD_OK;
  161. }
  162. }
  163. else
  164. {
  165. // If there no bytes left to receive
  166. // it is normal that the transfer is invalid (user may use move semantic)
  167. if( 0 == nBytesRemaining )
  168. {
  169. ret = USBD_OK;
  170. }
  171. }
  172. // let the core know how many excaclty bytes is required to shift the pointer back.
  173. if( (USBD_OK == ret) && (NULL != pnBytesRollback) )
  174. {
  175. // pass the exact number of bytes for rollback to the core
  176. *pnBytesRollback = nCompressed;
  177. }
  178. }
  179. else
  180. {
  181. // user must not return zero to proceed
  182. usb_reset_transfer( rx );
  183. ret = USBD_FAIL;
  184. }
  185. }
  186. else
  187. {
  188. ret = USBD_FAIL;
  189. }
  190. }
  191. else
  192. {
  193. // yep, there is free space for the next packet, let's receive it
  194. ret = USBD_CONTINUE;
  195. }
  196. }
  197. }
  198. return ret;
  199. }
  200. // USBD_COMMON_EP0_TxSent()
  201. // This function is called when at least one packet via CONTROL protocol had been sent to the host
  202. // This function continues the transmitting
  203. uint8_t USBD_COMMON_EP0_TxSent (USBD_HandleTypeDef *pdev, sUSBTransfer_t * tx, size_t * pnBytesRollback )
  204. {
  205. USBD_EndpointTypeDef * pep = &pdev->ep_in[0];
  206. uint8_t ret = USBD_FAIL;
  207. if( pdev->pUserData != NULL && usb_validate_transfer(tx) )
  208. {
  209. USBD_COMMON_ItfTypeDef * pUserIface = ((USBD_COMMON_ItfTypeDef *)pdev->pUserData);
  210. if( NULL != pUserIface->fUsbCtlEpTx )
  211. {
  212. // Due to the low-level driver uses dry transfer->@pReadData pointer, the
  213. // tranfer object must be modified.
  214. size_t nBytesRequested = 0;
  215. if( 0 == pep->rem_length )
  216. {
  217. // Update transfer information:
  218. // If @pep->rem_length is zero, the last packet sent was a last packet
  219. // The size of the short packet is a remainder of the division the
  220. // full transaction size and the endpoint size.
  221. // If the result of operation is non zero, the last packet was a short packet.
  222. // If the result of operation is zero, the last packet was full packet
  223. size_t last_packet_size = (pep->total_length % pep->maxpacket);
  224. if( last_packet_size == 0 )
  225. {
  226. last_packet_size = pep->maxpacket;
  227. }
  228. usb_transfer_virtual_read( tx, last_packet_size );
  229. }
  230. else
  231. {
  232. nBytesRequested = MIN( pep->maxpacket, pep->rem_length );
  233. // Update transfer information:
  234. // If @pep->rem_length is non zero, the last packet send was a full packet.
  235. // The size of full packet is pep->maxpacket
  236. usb_transfer_virtual_read( tx, pep->maxpacket );
  237. }
  238. // // Update transfer information:
  239. // usb_transfer_virtual_read( tx,
  240. // // The variable @nBytesRequested have quite confusing name, but this argument
  241. // // requires the length of already sent data. Actually the number of bytes already sent
  242. // // is numerially equals to @nBytesRequested
  243. // nBytesRequested // for pep->rem_length >= pep->maxpacket: the pep->maxpacket bytes just have been sent
  244. // ); // for pep->rem_length < pep->maxpacket: the pep->rem_length bytes just have been sent
  245. //
  246. // If the transaction is still not completed:
  247. if( pep->rem_length == 0 )
  248. {
  249. // transfer full reset
  250. usb_reset_transfer( tx );
  251. ret = USBD_OK;
  252. }
  253. else
  254. {
  255. size_t nBytesLeft = usb_count_transfer( tx );
  256. // If the transfer has enougth data to send another packet:
  257. if( nBytesLeft >= nBytesRequested )
  258. {
  259. ret = USBD_CONTINUE; // ask the core to continue sending the transfer (without rollback)
  260. }
  261. else
  262. {
  263. if( nBytesLeft > 0 )
  264. {
  265. // optimize the transfer to compress stored data
  266. size_t nCompressed = usb_transfer_compress( tx );
  267. if( NULL != pnBytesRollback )
  268. {
  269. // pass the exact number of bytes for rollback to the core
  270. *pnBytesRollback = nCompressed;
  271. }
  272. }
  273. else
  274. {
  275. // // transfer full reset
  276. // usb_reset_transfer( tx );
  277. size_t nCompressed = usb_transfer_compress( tx );
  278. if( NULL != pnBytesRollback )
  279. {
  280. // pass the exact number of bytes for rollback to the core
  281. *pnBytesRollback = nCompressed;
  282. }
  283. }
  284. size_t nOffset = (pep->total_length - pep->rem_length + nBytesLeft);
  285. size_t nBytesProcessed = pUserIface->fUsbCtlEpTx( &pdev->request,
  286. tx,
  287. nOffset,
  288. pep->rem_length );
  289. // User can perform move semantic on the @tx transfer.
  290. // This is an error condition, let's check it:
  291. if( usb_validate_transfer( tx ) )
  292. {
  293. // Actually, does not matter what exactly the function @fUsbCtlEpTx returns,
  294. // because only transfer size really matters ( usb_count_transfer() ).
  295. // Just check the return value for zero:
  296. if( nBytesProcessed > 0 )
  297. {
  298. // If more than one FULL packet is required to send:
  299. if( pep->rem_length >= pep->maxpacket )
  300. {
  301. // This packet must be full: transfer must contain at least @pep->maxpacket bytes
  302. if( usb_count_transfer( tx ) >= pep->maxpacket )
  303. {
  304. ret = USBD_OK;
  305. }
  306. }
  307. else
  308. // No, it is required to send "short packet"
  309. {
  310. // This packet must be short: transfer must contain exactly @pep->rem_length bytes
  311. if( usb_count_transfer( tx ) == pep->rem_length )
  312. {
  313. ret = USBD_OK;
  314. }
  315. }
  316. }
  317. else
  318. {
  319. ret = USBD_FAIL;
  320. }
  321. }
  322. else
  323. {
  324. ret = USBD_FAIL;
  325. }
  326. }
  327. }
  328. }
  329. }
  330. return ret;
  331. }
  332. // USBD_COMMON_Setup_DataProcess()
  333. // Handler for specific vendor or class requests
  334. // @pdev, @pUserIface, @req and @transf MUST BE NON-NULL!
  335. // Do not call if @req->wLength is zero
  336. uint8_t USBD_COMMON_Setup_DataProcess( USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req, sUSBTransfer_t * transf )
  337. {
  338. uint8_t ret = USBD_OK;
  339. USBD_COMMON_ItfTypeDef * pUserIface = ((USBD_COMMON_ItfTypeDef *)pdev->pUserData);
  340. if (req->wLength > 0)
  341. {
  342. if (req->bmRequest & 0x80)
  343. {
  344. // DataIN stage preparing
  345. if( usb_validate_transfer( transf ) ) // TX-transfer
  346. {
  347. // First packet in the stage: reset the transfer
  348. usb_reset_transfer( transf );
  349. // Single-transfer mode only:
  350. // Whole transfer is limited by the @ep0_tx_transfer size
  351. size_t nBytesProcessed = pUserIface->fUsbCtlEpTx( (const tUSBSetupPacket_t*)req,
  352. transf,
  353. 0,
  354. req->wLength );
  355. // User can perform move semantic on @ep0_tx_transfer transfer
  356. // This is an error condition, let's check it:
  357. if( usb_validate_transfer( transf ) )
  358. {
  359. USBD_EndpointTypeDef * pep = &pdev->ep_in[0];
  360. // If SETUP data stage assumes trasmitting more than one MAX_PACKET bytes,
  361. // this packet must include MAX_PACKET bytes (not to be "short packet")
  362. size_t min_limit = ((req->wLength > pep->maxpacket)?(pep->maxpacket-1):(0));
  363. // Actually, @ep0_tx_transfer can store more data than @nBytesProcessed.
  364. // @nBytesProcessed: bytes processed by last @fUsbCtlEpTx call.
  365. // Just check it for zero:
  366. size_t nBytesCount = usb_count_transfer( transf );
  367. if( nBytesProcessed > 0 )
  368. {
  369. // Check the transfer for minimal bytes limit:
  370. if( min_limit < nBytesCount )
  371. {
  372. // truncate the transfer in case if the transfer size is
  373. // greater than requested length.
  374. usb_truncate_transfer( transf, req->wLength );
  375. USBD_CtlSendData (pdev,
  376. (const uint8_t *)transf->pReadData,
  377. req->wLength );
  378. }
  379. else
  380. {
  381. USBD_CtlSendData (pdev,
  382. (const uint8_t *)transf->pReadData,
  383. nBytesCount );
  384. //ret = USBD_FAIL;
  385. }
  386. }
  387. else
  388. {
  389. ret = USBD_FAIL;
  390. }
  391. }
  392. else
  393. {
  394. ret = USBD_FAIL;
  395. }
  396. }
  397. else
  398. {
  399. ret = USBD_FAIL;
  400. }
  401. }
  402. else
  403. {
  404. // DataOUT stage preparing
  405. if( usb_validate_transfer( transf ) ) // RX-transfer
  406. {
  407. // Reset the RX-transfer
  408. usb_reset_transfer( transf );
  409. // Begin receiving first packet
  410. USBD_CtlPrepareRx (pdev,
  411. (uint8_t *)transf->pWriteData,
  412. req->wLength );
  413. }
  414. else
  415. {
  416. ret = USBD_FAIL;
  417. }
  418. }
  419. }
  420. else
  421. {
  422. ret = USBD_FAIL; // must be processed by caller
  423. }
  424. return ret;
  425. }
  426. #if DATAIN_INITIATESEND_REQUIRED
  427. // USBD_COMMON_DataIn_BeginSend
  428. // Initiates the first packet sending using virtual DataIN event
  429. // @bTxAlreadyPrepared - if the TX-transfer is already prepared to transmission, do not reset transfer, do not call TX-handler
  430. //
  431. // Note: e.g., is called from @USBD_USBTMC_DataIn_BeginSend
  432. //
  433. uint8_t USBD_COMMON_DataIn_BeginSend( USBD_HandleTypeDef *pdev, uint8_t epnum, sUSBTransfer_t * transf, bool bTxAlreadyPrepared )
  434. {
  435. USBD_EndpointTypeDef * pep = &pdev->ep_in[ epnum & 0x7F ];
  436. USBD_COMMON_ItfTypeDef * pUserIface = ((USBD_COMMON_ItfTypeDef *)pdev->pUserData);
  437. if( !bTxAlreadyPrepared )
  438. {
  439. if( NULL == pUserIface->fDataTxHandler )
  440. {
  441. // error: no user handler specified
  442. return USBD_FAIL;
  443. }
  444. // restore transfer
  445. usb_reset_transfer( transf );
  446. #warning SCPI: note, that setting 'short-packet' indicator is useless here
  447. // forward set short packet indicator:
  448. // (just to be compatible to @USBD_COMMON_DataInProcess function)
  449. usb_transfer_set_shortpacket( transf );
  450. // call user handler: generate virtual DataIN event
  451. if( ! pUserIface->fDataTxHandler( epnum, transf ) )
  452. {
  453. // error: user handler error
  454. return USBD_FAIL;
  455. }
  456. }
  457. uint16_t bytes_to_send = usb_count_transfer( transf );
  458. if( bytes_to_send > pep->maxpacket )
  459. {
  460. bytes_to_send = pep->maxpacket;
  461. }
  462. if( bytes_to_send == pep->maxpacket )
  463. {
  464. // this is the last packet with size equal EP size: the next packet must be zero packet
  465. usb_transfer_set_shortpacket( transf );
  466. }
  467. // initiate the data sending
  468. // Note: @total_length and @rem_length will be modified: @bytes_to_send will be stored to these fields.
  469. if( USBD_OK != USBD_EpSendData( pdev, (uint8_t*)transf->pReadData, bytes_to_send, epnum ) )
  470. {
  471. #if DEBUG_USB
  472. gDebugUSBEPTxLastPacket = bytes_to_send;
  473. #endif
  474. // error: can not send data
  475. return USBD_FAIL;
  476. }
  477. // perform virtual reading due to the USB Low Level have sent the data from the transfer
  478. usb_transfer_virtual_read( transf, bytes_to_send );
  479. #if DEBUG_USB > 0 // SCPI debug
  480. gDebugUSBEPTxBytes += bytes_to_send;
  481. #endif
  482. return USBD_OK;
  483. }
  484. #endif
  485. // USBD_USBTMC_DataIn_ZeroSend
  486. // Queue zero packet in EP IN
  487. //
  488. uint8_t USBD_COMMON_DataIn_ZeroSend( USBD_HandleTypeDef *pdev, uint8_t epnum )
  489. {
  490. USBD_EndpointTypeDef * pep = &pdev->ep_in[ epnum & 0x7F ];
  491. USBD_COMMON_ItfTypeDef * pUserIface = ((USBD_COMMON_ItfTypeDef *)pdev->pUserData);
  492. uint8_t dummy = 0;
  493. // initiate the data sending
  494. // Note: @total_length and @rem_length will be modified: @bytes_to_send will be stored to these fields.
  495. if( USBD_OK != USBD_EpSendData( pdev, &dummy, 0, epnum ) )
  496. {
  497. // error: can not send data
  498. return USBD_FAIL;
  499. }
  500. (void)pep, (void)pUserIface;
  501. return USBD_OK;
  502. }
  503. // USBD_COMMON_DataInProcess
  504. // Process DataIN events for Bulk and Interrupt endpoints
  505. //
  506. // Note: e.g., is called from @USBD_USBTMC_DataIn()
  507. //
  508. uint8_t USBD_COMMON_DataInProcess( USBD_HandleTypeDef *pdev, uint8_t epnum, sUSBTransfer_t * transf )
  509. {
  510. #if DEBUG_USBTMC_REQUESTS > 0
  511. void debug_log( uint8_t value, uint16_t value2 );
  512. #define DEBUG_LOG_STATE_DATAIN 0x04
  513. debug_log( DEBUG_LOG_STATE_DATAIN, usb_count_transfer( transf ) );
  514. #endif
  515. USBD_EndpointTypeDef * pep = &pdev->ep_in[ epnum & 0x7F ];
  516. USBD_COMMON_ItfTypeDef * pUserIface = ((USBD_COMMON_ItfTypeDef *)pdev->pUserData);
  517. #if !DATAIN_INITIATESEND_REQUIRED
  518. // Initiating the first packet sending is disabled,
  519. // the flow is being controlled by IRQ only,
  520. // so the fDataTxHandler is required.
  521. if( NULL == pUserIface->fDataTxHandler )
  522. {
  523. return USBD_FAIL;
  524. }
  525. #endif
  526. // Note: @total_length and @rem_length had been modified: these fields stores the length of the FIRST packet sent (USBD_EpSendData)
  527. // Check if is there enough data for next packet
  528. if( usb_count_transfer( transf ) < pep->maxpacket )
  529. {
  530. // No, the transfer does not have enough data for the full next packet
  531. // Check if the transfer have enough free space to store next packet?
  532. if( usb_space_transfer( transf ) < pep->maxpacket )
  533. {
  534. // No, the transfer must be compressed
  535. usb_transfer_compress( transf );
  536. }
  537. // Call user handler:
  538. // User shall reset Short-Packet indicator using @usb_transfer_modify_flags
  539. // ... routine to avoid sending short packet in case no data queued into
  540. // ... the transfer after successful call.
  541. if( ! pUserIface->fDataTxHandler( epnum, transf ) )
  542. {
  543. // restore transfer
  544. usb_reset_transfer( transf );
  545. // error
  546. return USBD_FAIL;
  547. }
  548. }
  549. // retrieve amount of bytes to send
  550. uint16_t bytes_to_send = usb_count_transfer( transf );
  551. if( bytes_to_send > pep->maxpacket )
  552. {
  553. // limit the packet size to EP buffer size
  554. bytes_to_send = pep->maxpacket;
  555. // this is the last packet with size equal EP size: the next packet must be zero packet
  556. usb_transfer_set_shortpacket( transf );
  557. #if DEBUG_USB > 0
  558. gDebugUSBEPTxLastPacket = bytes_to_send;
  559. #endif
  560. }
  561. else
  562. {
  563. // detect if a zero packet must be send
  564. if( bytes_to_send == pep->maxpacket )
  565. {
  566. // this is the last packet with size equal EP size: the next packet must be zero packet
  567. usb_transfer_set_shortpacket( transf );
  568. }
  569. else if( bytes_to_send > 0 ) // bytes_to_send < pep->maxpacket
  570. {
  571. usb_transfer_clear_shortpacket( transf );
  572. }
  573. else
  574. // Check if there is no data to send:
  575. if( 0 == bytes_to_send )
  576. {
  577. // probably, the short packet shall be send, check it:
  578. if( ! usb_transfer_check_shortpacket( transf ) )
  579. {
  580. // no, there no Short Packet to be send
  581. // Thus, there no more data, exiting...
  582. return USBD_OK;
  583. }
  584. // yes, the short packet will be send now,
  585. // but it is requied to clear the indicator:
  586. usb_transfer_clear_shortpacket( transf );
  587. }
  588. }
  589. // send next data part:
  590. if( USBD_OK == USBD_EpContinueSendData( pdev, (uint8_t*)transf->pReadData, bytes_to_send, epnum ) )
  591. {
  592. #if DEBUG_USB > 0 // SCPI debug
  593. gDebugUSBEPTxBytes += bytes_to_send;
  594. #endif
  595. #if DEBUG_USB
  596. gDebugUSBEPTxLastPacket = bytes_to_send;
  597. #endif
  598. // perform virtual reading due to the USB Low Level have sent the data from the transfer
  599. usb_transfer_virtual_read( transf, bytes_to_send );
  600. return USBD_OK;
  601. }
  602. return USBD_FAIL;
  603. }
  604. // USBD_COMMON_DataOutProcess
  605. // Process DataOUT events for Bulk and Interrupt endpoints
  606. uint8_t USBD_COMMON_DataOutProcess( USBD_HandleTypeDef *pdev, uint8_t epnum, sUSBTransfer_t * transf, bool ShortPacketReceived )
  607. {
  608. USBD_EndpointTypeDef * pep = &pdev->ep_out[ epnum & 0x7F ];
  609. USBD_COMMON_ItfTypeDef * pUserIface = ((USBD_COMMON_ItfTypeDef *)pdev->pUserData);
  610. // Retrieve amount of received bytes:
  611. uint32_t nBytesReceived = pep->total_length - pep->rem_length;
  612. if( ShortPacketReceived )
  613. usb_transfer_set_shortpacket( transf ); // Data-out: short packet received
  614. else
  615. usb_transfer_clear_shortpacket( transf ); // Data-out: Not-short packet received
  616. if( NULL != pUserIface->fDataRxHandler )
  617. {
  618. // If zero packet received:
  619. if( 0 == nBytesReceived )
  620. {
  621. // restore transfer
  622. usb_reset_transfer( transf );
  623. if( pUserIface->fDataRxHandler( epnum, transf ) )
  624. {
  625. return USBD_OK;
  626. }
  627. goto DataOut_failed;
  628. }
  629. // Perform virtual writing to the transfer: the transfer had been modified in unusual way
  630. // check if the transfer able to receive such packet with length = @nBytesReceived
  631. if( ! usb_transfer_virtual_write( transf, nBytesReceived ) )
  632. {
  633. // No, error
  634. goto DataOut_failed;
  635. }
  636. #if 0
  637. // Check if transfer is not empty
  638. while( 0 < usb_count_transfer( transf ) )
  639. {
  640. #error User-defined handler @fDataRxHandler can defer the data processing til enough data is received for correct processing
  641. // Call user handler
  642. if( ! pUserIface->fDataRxHandler( epnum, transf ) )
  643. {
  644. // error: user handler call failed
  645. break;
  646. }
  647. }
  648. #else
  649. // Call user handler
  650. if( ! pUserIface->fDataRxHandler( epnum, transf ) )
  651. {
  652. goto DataOut_failed;
  653. }
  654. #endif
  655. #if 1
  656. // If a short packet received - it is a last transfer transaction
  657. if( ShortPacketReceived )
  658. {
  659. // this case no bytes can be deferred in the transfer due to no user handler
  660. // call is expected for this transfer after this transaction.
  661. if( 0 < usb_count_transfer( transf ) )
  662. {
  663. // if the transfer is not empty - this is error condition
  664. goto DataOut_failed;
  665. }
  666. // it is a last transfer transaction - prepare transfer for the first transaction (next transfer)
  667. usb_reset_transfer( transf );
  668. }
  669. #endif
  670. // retrieve amount of free bytes in the transfer
  671. uint16_t free_space = usb_space_transfer( transf );
  672. // Check if the transfer have enough free space for next transaction?
  673. if( usb_space_transfer( transf ) < pep->maxpacket )
  674. {
  675. // No, not enough free space
  676. goto DataOut_failed;
  677. }
  678. else
  679. {
  680. // yes, let's receive
  681. // Optimize the transfer buffer and compress the data
  682. usb_transfer_compress( transf );
  683. // To prepare the receiving next transfer, the transfer size must be a multiply of max_packet of endpoint
  684. free_space -= free_space % pep->maxpacket;
  685. }
  686. // prepare the transfer for the next OUT transaction
  687. return USBD_EpContinueRx( pdev, (uint8_t*)transf->pWriteData, free_space, epnum );
  688. }
  689. DataOut_failed:
  690. // restore transfer
  691. usb_reset_transfer( transf );
  692. return USBD_FAIL;
  693. }
  694. // USBD_COMMON_DataOutBegin
  695. // Prepare the EP for next OUT (receive) transaction
  696. uint8_t USBD_COMMON_DataOutBegin( USBD_HandleTypeDef *pdev, uint8_t epnum, sUSBTransfer_t * transf )
  697. {
  698. USBD_COMMON_ItfTypeDef * pUserIface = ((USBD_COMMON_ItfTypeDef *)pdev->pUserData);
  699. // if( NULL != pUserIface->fDataRxHandler )
  700. // {
  701. // // If the transfer contains a data
  702. // // Notify user about the data stored in the transfer
  703. // while( (0 < usb_count_transfer( transf )) && pUserIface->fDataRxHandler( epnum, transf ) )
  704. // {
  705. // // Optimize the transfer buffer and compress the data
  706. // usb_transfer_compress( transf );
  707. // }
  708. //
  709. // // Notify user about starting new transfer: transf=NULL
  710. // pUserIface->fDataRxHandler( epnum, NULL );
  711. // }
  712. // recover transfer
  713. usb_reset_transfer( transf );
  714. // retrieve amount of free bytes in the transfer
  715. uint16_t free_space = usb_space_transfer( transf );
  716. USBD_EndpointTypeDef * pep = &pdev->ep_out[ epnum & 0x7F ];
  717. // the transfer must be able to contain at least one full packet
  718. if( free_space >= pep->maxpacket )
  719. {
  720. // To prepare the receiving next transfer, the transfer size must be a multiply of max_packet of endpoint
  721. free_space -= free_space % pep->maxpacket;
  722. // prepare the transfer for the next OUT transaction
  723. return USBD_EpPrepareRx( pdev, (uint8_t *)transf->pWriteData, free_space, epnum );
  724. }
  725. (void)pUserIface;
  726. return USBD_FAIL;
  727. }
  728. /*
  729. // USBD_COMMON_DataOutBegin
  730. // Prepare the EP for next OUT (receive) transaction
  731. uint8_t USBD_COMMON_DataOutBegin( USBD_HandleTypeDef *pdev, uint8_t epnum, sUSBTransfer_t * transf )
  732. {
  733. // Optimize the transfer buffer and compress the data
  734. usb_transfer_compress( transf );
  735. // retrieve amount of free bytes in the transfer
  736. uint16_t free_space = usb_space_transfer( transf );
  737. // prepare the transfer for the next OUT transaction
  738. return USBD_EpPrepareRx( pdev, (uint8_t *)transf->pWriteData, free_space, epnum );
  739. }
  740. */
  741. /**
  742. * @brief USBD_COMMON_RegisterInterface
  743. * @param pdev: device instance
  744. * @param fops: CD Interface callback
  745. * @retval status
  746. */
  747. #if USBD_CONSTSUBCLASS_IFACE
  748. uint8_t USBD_COMMON_RegisterInterface (USBD_HandleTypeDef *pdev,
  749. const USBD_COMMON_ItfTypeDef *fops)
  750. #else
  751. uint8_t USBD_COMMON_RegisterInterface (USBD_HandleTypeDef *pdev,
  752. USBD_COMMON_ItfTypeDef *fops)
  753. #endif
  754. {
  755. uint8_t ret = USBD_FAIL;
  756. if(fops != NULL)
  757. {
  758. pdev->pUserData= fops;
  759. ret = USBD_OK;
  760. }
  761. return ret;
  762. }