在USB 資料Packet的傳輸中,

有兩個函數會被使用到, 分別是USBGenWrite()與USBGenRead(),

我們從help檔中分別來看這兩個函數,

  1. 首先是USBGenWrite(),

裡面要注意兩個變數 : data與len,

data 是只要傳送的資料內容, 由Byte陣列構成 , 而len是指要傳送的陣列長度,

C

1
#define USBGenWrite(ep,data,len) USBTxOnePacket(ep,data,len)

Description

This function sends the specified data out the specified endpoint and returns a handle to the transfer information.

Typical Usage:

Copy Code

1
2
3
4
5
6
7
8
9
10
11
12
13
//make sure that the last transfer isn't busy by checking the handle

if(!USBHandleBusy(USBGenericInHandle)) --> input handle是否忙碌中?

{

//Send the data contained in the INPacket[] array out on

// endpoint USBGEN_EP_NUM

USBGenericInHandle = USBGenWrite(USBGEN_EP_NUM,(BYTE*)&INPacket[0],sizeof(INPacket));

}

Preconditions

None

Parameters

Parameters Description
ep the endpoint you want to send the data out of
data pointer to the data that you wish to send
len the length of the data that you wish to send

Return Values

Return Values Description
USB_HANDLE a handle for the transfer. This information should be kept to track the status of the transfer
  1. 再來是 USBGenRead() , 變數與USBGenWrite()大同小異,

注意的是:

USBGenWrite()的data使用INPacket, 而USBGenRead()使用是OUTPacket, 資料長度為64 byte.

C

1
#define USBGenRead(ep,data,len) USBRxOnePacket(ep,data,len)

Description

Receives the specified data out the specified endpoint.

Typical Usage:

Copy Code

1
2
3
4
5
6
7
8
9
10
11
12
13
//Read 64-bytes from endpoint USBGEN_EP_NUM, into the OUTPacket array.

// Make sure to save the return handle so that we can check it later

// to determine when the transfer is complete.

if(!USBHandleBusy(USBOutHandle))

{

USBOutHandle = USBGenRead(USBGEN_EP_NUM,(BYTE*)&OUTPacket,64);

}

Preconditions

None

Parameters

Parameters Description
ep the endpoint you want to receive the data into
data pointer to where the data will go when it arrives
len the length of the data that you wish to receive

Return Values

Return Values Description
USB_HANDLE a handle for the transfer. This information should be kept to track the status of the transfer

USBGenWrite()與USBGenRead()的使用方式套用上述說明中範例程式中的code即可.