I'm busy with a USB to Ethernet implementation (ECM) on an STM32 using the ST USB library (not HAL) and LwIP.

When I send a packet to the host (Data IN) that came from the ethernet interface, I know where is the end of the frame and therefore know when to send a zero length packet (ZLP) (when needed).

But how do I know when it is the end of a USB packet from the host (Data OUT)? So that I can send an entire frame to the ethernet interface?

I had a look at the endpoint parameters but none of them provide info on the the total packet (frame) length. Only per packet information is available.

I do not know the USB specification that well and not sure if this info in transferred with the initial/setup packet?

Maybe there is another method I can use to detect the end of a frame from the host?


I don't know about the ST USB library, but I have some register level bare-metal programming experience with the STM32F407 OTG_FS hardware.

First, some terminology to avoid confusion: A USB transfer consists of one or more transactions. A transaction consists of packets but we don't need to go that deep. The important thing is that a transaction can contain max-packet-size bytes or less. This is defined in the endpoint descriptor.

Actually, the logic is the same as the IN transfers. A transfer is considered to be complete if one of these two conditions are detected:

  1. A transaction with less than max-packet-size bytes
  2. A full length transaction followed by a ZLP

OTG_FS hardware detects the end of transfer itself and fires an interrupt if enabled. You can also obtain the actual number of bytes the transfer contains. However, before enabling the endpoint, you need to program the maximum number of bytes to expect. I don't remember what happens if this number is exceeded, but I guess the endpoint disables itself in that case.

For example, you set the OUT endpoint to receive 1000 bytes. After you get a transfer complete interrupt, you read back that register and see that it's now 900. Then you can conclude that the transfer was 100 bytes long. Look for OTG_FS_DOEPTSIZx register in the reference manual.

Of course, not all STM32 devices have OTG_FS hardware. For example, the famous STM32F103 uses a different USB hardware. I have no experience with that hardware. But if it's unable to detect end of transfers itself, you can check it yourself using the 2 rules I mentioned.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.