cancel
Showing results for 
Search instead for 
Did you mean: 

USB Device Library. Beginner question

petrovchita
Associate II
Posted on May 12, 2016 at 12:44

Hello friends!

I'm working on implementing USBTMC class USB Device Library. The question was how to implement the transfer of bulk-out. According USBTMC specification: ''... If the last data payload

is wMaxPacketSize, the Host should not send a zero-length packet. ''How can I correctly receive a packet of arbitrary length multiple wMaxPacketSize? How do you know that the data transfer from the host is completed?

#usb #usb #hal #device #usbtmc #device #usb #usbtmc #usbtmc
4 REPLIES 4
tsuneo
Senior
Posted on May 13, 2016 at 11:35

Each USBTMC message has TransferSize field in the header.

Reading out the first Bulk-OUT transaction, your device knows this TransferSize. And then, it determines how many bytes expected in latter transactions of this transfer.

Tsuneo

petrovchita
Associate II
Posted on May 13, 2016 at 12:22

Thanks for your reply. I am interested in the question of whether to do this act which at the class level possible for STM USB Device Library Library (from STM32CubeMX). Using the call function HAL_PCD_EP_Receive (PCD_HandleTypeDef * hpcd, uint8_t ep_addr, uint8_t * pBuf, uint32_t len), EP I prepare to receive data less than or equal len. In this case, if the packet length will multiple wMaxPacketSize, HAL_PCD_DataOutStageCallback function (PCD_HandleTypeDef * hpcd, uint8_t epnum) is not called, as the transfer is not considered finished. If I understand you correctly, the only right way for me is to write low-level code to work with usb?

tsuneo
Senior
Posted on May 13, 2016 at 17:05

Your device may do this job with HAL_PCD_EP_Receive(), as it is.

Just after HAL_PCD_EP_Open() of the Bulk-OUT EP (in SetConfig request handling),

1) prime the Bulk-OUT endpoint using HAL_PCD_EP_Receive() for wMaxPacketSize of the Bulk-OUT EP.

When host sends a USBTMC message,

2) At the completion of the first transaction, HAL_PCD_DataOutStageCallback() is called by the stack.

3) In this callback, read out TransferSize field on the received packet, calculate the remaining size of the transfer. Call another HAL_PCD_EP_Receive() for the remaining transfer, if required.

5) At the completion of the second transfer, concatenate the first packet with the rest, to recover full message.

6) process the USBTMC message.

7) back to 1)

Tsuneo

petrovchita
Associate II
Posted on May 14, 2016 at 19:52

Thank you

.

It really works

.