Zero Length Packets Technology Guide

This guide explains Zero Length Packets (ZLPs) and how to use them.

Purpose

Short packets and Zero Length Packets signify the end of a bulk data transfer.

Intended audience

This document is for Symbian licensees who are designing Function Drivers (FDs).

Reason for using Zero Length Packets

At its most basic level, a data transfer takes place on a pipe by breaking data up into multiple transactions. Each transaction allows a data packet to be transmitted. Packets have a limited size, known as the maximum packet size. The maximum packet size is defined by the USB peripheral in its endpoint descriptor and its value depends on the bus speed and the transfer type supported by the pipe in question.

Because each packet allows for the transmission of a limited amount of data, multiple packets are usually required to complete a transfer. For example a host that needs to send 2000 bytes of data, may send three packets of 512 bytes and one of 464 bytes. A host that needs to send a block of data of 1024 bytes, may send two packets of 512 bytes.

Most data transactions involve blocks of data that are not integral multiples of the maximum packet size. Therefore in most transfers, the final packet does not reach its maximum packet size. When the final packet is smaller than its maximum size, it signals to the host or peripheral receiving the data that the transfer has been completed. In the example of a data transfer of 2000 bytes broken up into three packets of 512 bytes and one of 464 bytes, the packet of 464 bytes, less than its maximum packet size, signals the end of the transaction.

Some data transfers, however, involve blocks of data that are integral multiples of the maximum packet size. In these instances the final packet will be the maximum packet size. In the example of a data transfer of a block of data that is 1024 bytes broken up into two packets of 512 bytes, there is no incomplete packet to signal to the receiving host or peripheral that the transaction has ended.

In these cases you need to use a Zero Length Packet to indicate the end of the transfer. To enable Zero Length Packets, set a flag in the transfer descriptor.

TUsbTransferRequestDetails details;
...
details.iFlags        = 0x04;    // Short transfer okay

For an OUT transfer, the host stack will intelligently decide whether a ZLP is necessary to indicate the end of the transfer and insert one if necessary.

Related concepts