|
|
Component
USB
USB device bean
Component Level: High
Category:
CPU Internal Peripherals-Communication
Typical Usage:
(Examples of a typical usage of the component in user code.
For more information please
see the page Component Code
Typical Usage.)
USB device is a slave device, so all transfers must be done with respect to this fact. Device is reset by the USB host controler (host) after connection to the USB bus. When reset signal stops, event OnReset is invoked. Data transfers are possible after a configuration other then configuration zero is set by the host. Event OnConfigurationChange informs you about host request to set new configuration. You can use two methods to send data to the host. SendChar and SendBlock. Method SendChar puts character to the output buffer and returns. Events OnTransmit is invoked after all buffer has been sent and ACK handshake packet from the host is received. Method SendBlock transfers a block of bytes to the host. When a transfer involves more data than can fit in one data payload of the currently established maximum size, all data payloads are maximum-sized except for the last data payload, which will contain the remaining data. You can use this method to send zero length packet. Output buffer must be empty to start transfer (method returns ERR_BUSY if there are characters in output buffer). Event OnTransmit is envoked after all data have been sent and a last ACK handshake packet received from the host. There are two typical usages of this method:
MAIN.C
void main(void)
{
byte buff[20];
/* Loop until all data are sent */
while(USB1_SendBlock(buff,sizeof(buff),1,TRUE));
}
or
Events.C
extern bool NotFinished;
.
.
void USB1_OnTransmitt(byte endpoint)
{
.
NotFinished=FALSE;
.
}
MAIN.C
bool NotFinished;
void main(void)
{
byte buff[]="Test packet";
NotFinished=TRUE;
/* Start to send data (don't wait for finish */
while(USB1_SendBlock(buff,sizeof(buff),1,FALSE));
/* Do some other work */
/* Loop until all data are sent */
while(NotFinished);
}
Ussages of method RecvChar and RecvBlock is similar
|