8/12/2019 An758 Usb Cdc
1/30
Rev. 0.1 3/13 Copyright 2013 by Silicon Laboratories AN758
A N758
IMPLEMENTINGUSB COMMUNICATIONDEVICECLASS
(CDC) ONS iM3U1XXMCUS
1. IntroductionUSB revolutionized the PC peripheral space by making a very simple plug-and-play interface for users. As a result,
many modern computers no longer support RS-232 serial COM ports, opting for the slimmer USB alternative. This
can be an issue for the developer who needs a COM port for communication between a peripheral and host PC. A
subset of the USB Communication Device Class (CDC) can be used to emulate a serial port providing a virtual
COM port UART interface. This allows developers to use legacy applications with new products using the same
COM port interface as before, with few hardware and software modifications.
Figure 1. USB CDC Virtual COM Port System
This application note describes the USB communications device class driver (or USB CDC) in detail and includes
an implementation example for the Silicon Labs SiM3U1xx MCU.
1.1. Assumptions
This document assumes the following: A working knowledge of the C programming language.
Familiarity with the USB 2.0 specification and terms and abbreviations defined by the USB specification.
Familiarity with Silicon Labs SiM3U1xx development environment.
1.2. Features and Limitations
The CDC firmware implemented with this application note includes the following features:
Emulates a serial COM port on PC that supports the CDC Abstract Control Model (ACM).
Provides an abstract communication interface for data transfers between the host and the device.
Handles standard Chapter 9 USB device requests.
Handles CDC-specific requests from USB host.
Notifies the USB host of status using an interrupt endpoint. Provides data communication with the USB host using a bulk endpoint.
The following baud rates are supported: 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400,
460800, and 921600 bps.
The example does not implement the following:
No CTS/RTS control is performed, so flow control must be set to nonein the terminal program.
RTS/DTR control is not implemented.
Serial Device
Host PC USB cable SiM3U1xxRS232
cableEnd device
8/12/2019 An758 Usb Cdc
2/30
A N758
2 Rev. 0.1
2. Relevant Documentation
Precision32 Application Notes are listed on the following website: www.silabs.com/32bit-appnotes.
AN667: Gett ing Started with the Si li con Labs Prec is ion32 IDEprovides a description of the IDE
features and environment.
AN670: Gett ing Started with the Sili con Labs Precision32 AppBui lderprovides a description of the
AppBuilder features.
3. USB CDC Class
The USB communications device class (CDC) is a composite USB device class, and the class may include more
than one interface. The CDC is used primarily for modems, but also for ISDN, fax machines, and telephony
applications for performing regular voice calls.
The Abstract Control Model subclass of CDC and bridges the gap between legacy modem devices and USB
devices, enabling the use of application programs designed for older modems.
3.1. Class Requests
The class requests and class notifications supported are listed in Table 1.
These class-specific requests are used for device and call management.
3.1.1. Set Line Coding
This request allows the host to specify typical asynchronous line-character formatting properties.
Table 2defines the line coding properties.
Table 1. Abst ract Cont rol Model Requests
Request Code Description
SET_LINE_CODING 20h Configures baud rate, stop-bits, parity, and number-
of-character bits.
GET_LINE_CODING 21h Requests current DTE rate, stop-bits, parity, and
number-of-character bits.
SET_CONTROL_LINE_STATE 22h RS232 signal used to tell the DCE device the DTE
device is now present.
bmRequestType bRequest wValue wIndex wLength Data
00100001b SET_LINE_CODING 0 interface size of
structure
line coding
structure
http://www.silabs.com/32bit-appnoteshttp://www.silabs.com/32bit-appnoteshttp://www.silabs.com/32bit-appnotes8/12/2019 An758 Usb Cdc
3/30
AN758
Rev. 0.1 3
3.1.2. Get L ine CodingThis request allows the host to find out the currently configured line coding. Table 2 defines the line coding
properties.
3.1.3. Set Control L ine State
This request generates RS-232/V.24 style control signals. Table 3defines control signal bitmap.
Table 2. Line Coding Format
Offset Field Size Value Description
0 dwDTERate 4 Number Data terminal rate, in bits per second.
4 bCharFormat 1 Number 0: 1 Stop bit1: 1.5 Stop bits
2: 2 Stop bits
5 bParityType 1 Number Parity:
0:None
1: Odd
2: Even
3: Mark
4: Space
6 bDataBits 1 Number Data bits (5, 6, 7, 8 or 16).
bmRequestType bRequest wValue wIndex wLength Data
10100001b GET_LINE_CODING 0 interface size of
structure
line coding
structure
bmRequestType bRequest wValue wIndex wLength Data
00100001b SET_LINE_CON-
TROL_STATE
control signal
bitmap
interface 0 none
Table 3. Contro l Signal B itmap
Bit Posit ion Descript ion
15:2 Reserved (Reset to zero).
1 Carrier control for half duplex modems. This signal corresponds to V.24 signal
105 and RS232 signal RTS.0: Deactivate carrier.
1: Activate carrier.
The device ignores the value of this bit when operating in full duplex mode.
0 Indicates to DCE if DTE is present or not. This signal corresponds to V.24 signal
108/2 and RS232 signal DTR.
0: DTE is not present.
1: DTE is present.
8/12/2019 An758 Usb Cdc
4/30
8/12/2019 An758 Usb Cdc
5/30
AN758
Rev. 0.1 5
3.3. Endpoint Configuration
Table 6illustrates the endpoint configuration for the Abstract Control Model.
Figure 2shows a standard CDC communication flow.
Figure 2. USB CDC Communication Flow
Table 6. UART State Bi tmap
Endpoint Direction Type Max Packet
Size
Description
EP0 In/Out Control 64 Standard requests, class requests.
EP1 In Interrupt 16 State notification from device to host.
EP2 In Bulk 64 Data transfer from device to host.
EP3 Out Bulk 64 Data transfer from host to device.
8/12/2019 An758 Usb Cdc
6/30
A N758
6 Rev. 0.1
4. Firmware Example Overview
The firmware example included with this application note contains the driver, USB stack for the SiM3U1xx, and PC-
side application layer code.
4.1. File Organization
The files included in the USB CDC example are as follows: generateddirectory containing SiM3U1xx hardware initialization files
VirtualSerialdirectory containing the virtual COM demo files
CDC_ACM.infUSB CDC INF file
Descriptors.cVirtual COM USB descriptors
LUFAConfig.hProject configuration
VirtualSerial.cprimary application file
LUFASiM3L1xx USB stack firmware
Commoncommon header files
DriversUSB driver files
Miscmiscellaneous files
USBUSB stack files
ClassUSB class files
DeviceUSB device class files
AudioClassDevice.*USB audio class implementation
CDCClassDevice.*USB CDC class implementation
HIDClassDevice.*USB HID class implementation
MassStorageClassDevice.*USB mass storage class implementation
MIDIClassDevice.*USB MIDI class implementation
RNDISClassDevice.*USB RNDIS class implementation
HostUSB host class files, not implemented by the SiM3U1xx
Commoncommon driver files
CoreUSB Chapter 9 handler files
SiM3USiM3U1xx USB hardware control firmware files Device_SIM3U.*USB device definitions
Endpoint_SIM3U.*USB endpoint transition control
EndpointStream_SIM3U.*USB endpoint stream transition control
USBController_SIM3U.*USB controller definitions
USBInterrupt_SIM3U.*USB interrupt handler
USB.hUSB header file
main.c
4.2. LUFA USB Stack
The USB CDC firmware example is based on the LUFA open-source project. LUFA is an open-source complete
USB stack released under the permissive MIT License. It includes support for many USB classes, both for USB
Hosts and USB Devices. For USB Devices, the LUFA stack includes support for Audio Class, CDC Class, HIDClass, Mass Storage Class, MIDI Class, and RNDIS Class.
More information about the LUFA project can be found on the official website: http://www.fourwalledcubicle.com/
LUFA.php.
http://www.fourwalledcubicle.com/LUFA.phphttp://www.fourwalledcubicle.com/LUFA.php8/12/2019 An758 Usb Cdc
7/30
AN758
Rev. 0.1 7
5. Detailed List of Functions
This section discusses the detailed function declarations in the CDC USB stack firmware example.
5.1. CDC Functions
This section focuses on the functions found in the CDCClassDevice.c and CDCClassDevice.h files
(AN758_USB_CDC\src\LUFA\Dri vers\USB\Class\Dev icedirectory). This file implements the USB CDC deviceclass.
5.1.1. CDC_Device_Process_ControlRequest
Handle USB CDC special class request.
Syntax
voi d CDC_Devi ce_ProcessCont r ol Request ( USB_Cl assI nf o_CDC_Devi ce_t * const
CDCI nt er f aceI nf o)
Parameters
CDCI nt er f aceI nf o: Pointer to CDC Class state structure
Return Value
None
DescriptionThis function parses the USB control request to handle different USB CDC special class request. Update
CDCInterfaceInfo with data from USB host, or send data in CDCInterfaceInfo to USB host.
Example
USB_Cl assI nf o_CDC_Devi ce_t Vi r t ual Ser i al _CDC_I nt er f ace =
{
. Conf i g =
{
. Cont r ol I nt er f aceNumber = 0,
. DataI NEndpoi nt =
{
. Addr ess = CDC_TX_EPADDR,
. Si ze = CDC_TXRX_EPSI ZE,
. Banks = 1,
},
. Dat aOUTEndpoi nt =
{
. Address = CDC_RX_EPADDR,
. Si ze = CDC_TXRX_EPSI ZE,
. Banks = 1,
},
. Not i f i cat i onEndpoi nt =
{
. Address = CDC_NOTI FI CATI ON_EPADDR,
. Si ze = CDC_NOTI FI CATI ON_EPSI ZE,
. Banks = 1,
},
},
};CDC_Devi ce_Pr ocessCont r ol Request ( &Vi r t ual Ser i al _CDC_I nt er f ace) ;
8/12/2019 An758 Usb Cdc
8/30
A N758
8 Rev. 0.1
5.1.2. CDC_Device_ConfigureEndpoints
USB CDC device endpoint configuration.
Syntax
bool CDC_Devi ce_Conf i gur eEndpoi nt s( USB_Cl assI nf o_CDC_Devi ce_t * const
CDCI nt er f aceI nf o)
ParametersCDCI nt er f aceI nf o: Poi nt er t o CDC Cl ass st at e st r uct ur e
Return Value
Tr ue: i f conf i gur at i on success
Fal se: i f conf i gurat i on f ai l ure
Description
This functions set data in/out endpoint as bulk type, and set notification endpoint as interrupt type.
Example
bool Conf i gSuccess = t r ue;Conf i gSuccess &= CDC_Devi ce_Conf i gur eEndpoi nts( &Vi r t ual Ser i al _CDC_I nt erf ace) ;
5.1.3. CDC_Device_SendData
USB CDC sends data to USB IN FIFO.
Syntax
ui nt 8_t CDC_Devi ce_SendDat a(USB_Cl assI nf o_CDC_Devi ce_t * const CDCI nter- f aceI nf o,const char * const Buf f er , const ui nt 16_t Lengt h)
Parameters
CDCInterfaceInfo: Pointer to CDC Class state structure
Buffer: Pointer to data buffer
Length: Transmit length
Return Value
Er r or code: def i ni t i on can be f ound i n Endpoi nt St r eam. h
ENDPOI NT_RWSTREAM_NoEr r or
ENDPOI NT_RWSTREAM_Endpoi nt St al l edENDPOI NT_RWSTREAM_Devi ceDi sconnect ed
ENDPOI NT_RWSTREAM_BusSuspended
ENDPOI NT_RWSTREAM_Ti meout
ENDPOI NT_RWSTREAM_I ncompl et eTr ansf er
Description
This function sends data to USB IN FIFO. Transmit data size specified in Length.
Note: Endpoi nt _Cl ear I N( ) must be called to send data to USB host, otherwise, the data will keep in USB device FIFO.
Example
ui nt 32_t r ecv_count ;r ecv_count = uar t _get _dat a( i n_buf f ) ;
i f ( r ecv_count )
{
CDC_Devi ce_SendData( &Vi r t ual Ser i al _CDC_I nt er f ace, ( char *) i n_buf f , r ecv_count ) ;
Endpoi nt _Cl ear I N( ) ;
}
8/12/2019 An758 Usb Cdc
9/30
AN758
Rev. 0.1 9
5.1.4. CDC_Device_SendByte
USB CDC sends one byte to USB IN FIFO.
Syntax
ui nt 8_t CDC_Devi ce_SendByt e(USB_Cl assI nf o_CDC_Devi ce_t * const CDCI nterf aceI nf o,
const ui nt 8_t Dat a)
Parameters
CDCI nt er f aceI nf o: Poi nt er t o CDC Cl ass st at e st r uct ur e
Data: byt e dat a to send
Return Value
Er r or code: def i ni t i on can be f ound i n Endpoi nt St r eam. h
ENDPOI NT_RWSTREAM_NoEr r or
ENDPOI NT_RWSTREAM_Endpoi nt St al l ed
ENDPOI NT_RWSTREAM_Devi ceDi sconnect ed
ENDPOI NT_RWSTREAM_BusSuspended
ENDPOI NT_RWSTREAM_Ti meout
ENDPOI NT_RWSTREAM_I ncompl et eTr ansf er
Description
This function sends one byte to USB IN FIFO.
Note: Endpoi nt _Cl ear I N( ) must be called to send data to USB host, otherwise, the data will keep in USB device FIFO.
Example
CDC_Devi ce_SendByte(&Vi r t ual Ser i al _CDC_I nt erf ace, 0x55) ;Endpoi nt _Cl ear I N( ) ;
5.1.5. CDC_Device_BytesReceived
USB CDC number of bytes received.
Syntax
ui nt 16_t CDC_Devi ce_Byt esRecei ved( USB_Cl assI nf o_CDC_Devi ce_t * const
CDCI nt er f aceI nf o)Parameters
CDCI nt er f aceI nf o: Poi nt er t o CDC Cl ass st at e st r uct ur e
Return Value
Byt es r ecei ved i n USB endpoi nt .
Description
This function check how many bytes received in USB data out endpoint
Example
ui nt 32_t r ecv_count ;r ecv_count = CDC_Devi ce_Byt esRecei ved( &Vi r t ual Ser i al _CDC_I nt erf ace) ;
8/12/2019 An758 Usb Cdc
10/30
A N758
10 Rev. 0.1
5.1.6. CDC_Device_ReceiveByte
USB CDC receive one byte from USB OUT FIFO.
Syntax
i nt 16_t CDC_Devi ce_Recei veByt e(USB_Cl assI nf o_CDC_Devi ce_t * const CDCI nt er f aceI nf o)
Parameters
CDCI nt er f aceI nf o: Poi nt er t o CDC Cl ass st at e st r uct ur e
Return Value
- 1: i f no dat a r ecei ved
Data: one byt e f r om USB data out FI FO.
Description
This function receives one byte from USB data out FIFO. It returns -1 if no data received.
Example
ui nt 32_t out _buf f ;out _buf f = CDC_Devi ce_Recei veByte( &Vi r t ual Seri al _CDC_I nt er f ace) ;
5.1.7. CDC_Device_SendControlL ineStateChange
USB CDC notification.
Syntax
voi d CDC_Devi ce_SendCont r ol Li neStat eChange(USB_Cl assI nf o_CDC_Devi ce_t * const
CDCI nt er f aceI nf o)
Parameters
CDCI nt er f aceI nf o: Poi nt er t o CDC Cl ass st at e st r uct ur e
Return Value
None
Description
This function sends state data to notification endpoint.
Example
CDC_Devi ce_SendCont r ol Li neSt at eChange(CDCI nt er f aceI nf o) ;
8/12/2019 An758 Usb Cdc
11/30
AN758
Rev. 0.1 11
5.2. UART Functions
This section details the functions found in the gUART0.c file (AN758_USB_CDC\src\generated directory). This
file contains the hardware configuration for the SiM3U1xx device.
5.2.1. UART0_enter_default_mode_from_reset
UART0 hardware default configuration.
Syntaxvoi d UART0_ent er_def aul t _mode_f r om_r eset ( voi d)
Parameters
None
Return Value
None
Description
This function set UART0 TX/RX default configuration as 115200 bps, 8-bit data width, 1 bit stop bit, no-parity
ExampleUART0_ent er_def aul t _mode_f r om_r eset ( ) ;
5.2.2. uart_configuration
UART0 configuration setting.
Syntax
voi d uar t _conf i gur at i on( ui nt 32_t baud_r at e, ui nt 8_t st op_bi t s, ui nt 8_t par i t y, ui nt 8_t
dat a_bi t s)
Parameters
baud_r ate: baud rate
st op_bi t s: 0 - 1 st op bi t ; 1 - 1. 5 st op bi t s; 2 - 2 st op bi t s
par i t y: 0 - none; 1 - odd; 2 - even; 3 - mar k; 4 - space
dat a_bi t s: dat a bi t s( 5, 6, 7, 8 or 16)
Return Value
None
DescriptionThis function set UART0 TX/RX baud rate, stop bits, parity and data bits.
Exampleuar t _conf i gur at i on( 115200, 0, 0, 8) ;
5.2.3. uart_get_data
UART0 get data.
Syntax
ui nt 32_t uar t _get _dat a( ui nt 8_t * dat a)
Parameters
dat a: poi nt er t o r ecei ve dat a buf f er
Return ValueThe number of byt es r ecei ved f r om UART
Description
This function gets data from UART FIFO to data buffer and return number of bytes received.
Example
ui nt 32_t r ecv_count ;
stati c ui nt 8_t i n_buf f [ 64] ;r ecv_count = uar t _get _dat a( i n_buf f ) ;
8/12/2019 An758 Usb Cdc
12/30
A N758
12 Rev. 0.1
5.2.4. uart_send_data
UART0 send data.
Syntax
voi d uart _send_dat a(unsi gned char *dat a, unsi gned i nt count )
Parameters
dat a: poi nt er t o send dat a buf f er
count : Number of byt es t o send
Return Value
None
Description
This function send data from data buffer to UART FIFO, transmit data bytes specified in count.
Example
ui nt32_t send_count = 4;
stati c ui nt 8_t out _buf f [ 64] ;uar t _send_dat a( out _buf f , sendcount ) ;
5.2.5. uart_send_byte
UART0 send one byte.
Syntax
voi d uar t _send_byte( ui nt 8_t dat a)
Parameters
data: Byt e to send
Return Value
None
Description
This function sends one byte data to UART FIFO
Example
ui nt 8_t out _byte = 0x55;
uar t _send_byte( out _byte) ;
5.2.6. uart_get_byte
UART0 get one byte.
Syntax
ui nt 8_t uar t _get _byt e( )
Parameters
None
Return Value
Byte recei ved
Description
This function gets one byte data from UART FIFOExample
ui nt 8_t i n_byte;I n_byte = uar t _get _byte( ) ;
8/12/2019 An758 Usb Cdc
13/30
AN758
Rev. 0.1 13
5.3. Application Layer
The application layer for the USB CDC firmware example can be found in the VirtualSerial.c and Descriptors.c
files in theAN758_USB_CDC\src\Virt ualSerialdirectory.
5.3.1. vcp_main
Virtual COM port entry function.
Syntaxvoi d vcp_mai n(voi d)
Parameters
None
Return Value
None
Description
This function initialize USB stack and execute Echo mode or Bridge mode in endless while loop.
Example
ent er _def aul t _mode_f r om_r eset ( ) ;vcp_mai n( ) ;
5.3.2. VCOM_echo
It echo input character to USB virtual COM
Syntax
voi d VCOM_echo( voi d)
Parameters
None
Return Value
None
Description
This function get byte from USB virtual COM port and send back to virtual COM. User will see echo character in
their serial com tool.
ExampleVCOM_echo( ) ;
5.3.3. VCOM_bridge
It transfer data between USB virtual COM port and UART hardware COM port.
Syntax
voi d VCOM_br i dge( voi d)
Parameters
None
Return Value
None
DescriptionThis function acts as USB to UART bridge function.
ExampleVCOM_br i dge( ) ;
8/12/2019 An758 Usb Cdc
14/30
A N758
14 Rev. 0.1
5.3.4. EVENT_USB_Device_ControlRequest
Event handler for control request.
Syntax
voi d EVENT_USB_Devi ce_Cont r ol Request ( voi d)
Parameters
None
Return Value
None
Description
This function is event handler for the library USB Control Request reception event. It handlers USB CDC special
class request from USB host.
Example
i f ( Endpoi nt _I sSETUPRecei ved( ) )
{
ui nt 8_t * Request Header = ( ui nt8_t *) &USB_Cont r ol Request ;
f or ( ui nt8_t Request Header Byt e = 0; Request Header Byt e St at e. Li neEncodi ng. BaudRat eBPS = Endpoi nt _Read_32_LE( ) ;
CDCI nt er f aceI nf o- >St at e. Li neEncodi ng. Char For mat = Endpoi nt _Read_8( ) ;
CDCI nt er f aceI nf o- >St ate. Li neEncodi ng. Par i t yType = Endpoi nt _Read_8( ) ;
CDCI nt er f aceI nf o- >St ate. Li neEncodi ng. DataBi t s = Endpoi nt _Read_8( ) ;
Endpoi nt _Cl ear OUT( ) ;
Endpoi nt _Cl earStatusSt age( ) ;
EVENT_CDC_Devi ce_Li neEncodi ngChanged( CDCI nter f aceI nf o) ;
8/12/2019 An758 Usb Cdc
15/30
AN758
Rev. 0.1 15
5.3.6. EVENT_USB_Device_ConfigurationChanged
Event handler for the library USB Configuration changed event.
Syntax
voi d EVENT_USB_Devi ce_Conf i gur at i onChanged( voi d)
Parameters
None
Return Value
None
Description
This function configures endpoint according USB CDC class interface configuration structure.
ExampleEVENT_USB_Devi ce_Conf i gur at i onChanged( ) ;
5.3.7. CALLBACK_USB_GetDescriptor
Send back descriptor per Get Descriptor request.
Syntax
ui nt 16_t CALLBACK_USB_GetDescr i pt or ( const ui nt 16_t wVal ue,
const ui nt 8_t wI ndex, const voi d** const Descr i pt orAddr ess)
Parameters
None
Return Value
Descr i pt or si ze
Description
When the device receives a Get Descriptor request on the control endpoint, this function is called so that the
descriptor details can be passed back and the appropriate descriptor sent back to the USB host.
Example
const voi d* Descri pt or Poi nt er ;
ui nt 16_t Descri pt or Si ze;
i f ( ( Descr i pt orSi ze = CALLBACK_USB_GetDescr i pt or ( USB_Cont r ol Request . wVal ue,
USB_Cont r ol Request . wI ndex, &Descr i ptorPoi nter ) ) == NO_DESCRI PTOR)
{
return;
}
Endpoi nt _Cl ear SETUP( ) ;
Endpoi nt _Wr i t e_Cont r ol _St r eam_LE( Descr i pt or Poi nt er , Descr i pt or Si ze) ;
8/12/2019 An758 Usb Cdc
16/30
A N758
16 Rev. 0.1
5.3.8. Macros
#def i ne ENDPOI NT_DI R_I N 0x80
#def i ne ENDPOI NT_DI R_OUT 0x00
/ ** Endpoi nt addr ess of t he CDC devi ce- t o- host not i f i cat i on I N endpoi nt . */
#def i ne CDC_NOTI FI CATI ON_EPADDR ( ENDPOI NT_DI R_I N | 1)
/ ** Endpoi nt addr ess of t he CDC devi ce- t o- host data I N endpoi nt . */
#def i ne CDC_TX_EPADDR ( ENDPOI NT_DI R_I N | 2)
/ ** Endpoi nt addr ess of t he CDC host - t o- devi ce dat a OUT endpoi nt . */
#def i ne CDC_RX_EPADDR ( ENDPOI NT_DI R_OUT | 3)
/ * Si ze i n bytes of t he CDC devi ce- t o- host not i f i cat i on I N endpoi nt */
#def i ne CDC_NOTI FI CATI ON_EPSI ZE 16
/ ** Si ze i n byt es of t he CDC data I N and OUT endpoi nt s. */#def i ne CDC_TXRX_EPSI ZE 64
5.3.9. USB CDC Class In terface Structu re
USB_Cl assI nf o_CDC_Devi ce_t Vi r t ual Ser i al _CDC_I nt er f ace =
{
. Conf i g =
{
. Cont r ol I nt er f aceNumber = 0,
. DataI NEndpoi nt =
{
. Addr ess = CDC_TX_EPADDR,
. Si ze = CDC_TXRX_EPSI ZE,
. Banks = 1,
},
. Dat aOUTEndpoi nt =
{. Address = CDC_RX_EPADDR,
. Si ze = CDC_TXRX_EPSI ZE,
. Banks = 1,
},
. Not i f i cat i onEndpoi nt =
{
. Address = CDC_NOTI FI CATI ON_EPADDR,
. Si ze = CDC_NOTI FI CATI ON_EPSI ZE,
. Banks = 1,
},
},};
8/12/2019 An758 Usb Cdc
17/30
AN758
Rev. 0.1 17
5.3.10. USB CDC Descripto rs
Device descriptor structure, this descriptor, located in flash memory, describes the overall device characteristics,
including the supported USB version, control endpoint size and the number of device configurations. The
descriptor is read out by the USB host when the enumeration process begins.
const USB_Descr i ptor_Devi ce_t PROGMEM Devi ceDescr i pt or =
{
. Header = {. Si ze = si zeof ( USB_Descr i pt or_Devi ce_t ) , . Type =
DTYPE_Devi ce},
. USBSpeci f i cat i on = VERSI ON_BCD( 01. 10) ,
. Cl ass = CDC_CSCP_CDCCl ass ,
. SubCl ass = CDC_CSCP_NoSpeci f i cSubcl ass,
. Protocol = CDC_CSCP_NoSpeci f i cProtocol ,
. Endpoi nt 0Si ze = ENDPOI NT_CONTROLEP_DEFAULT_ SI ZE,
. Vendor I D = 0x10C4,
. Product I D = 0xA003,
. Rel easeNumber = VERSI ON_BCD( 00. 01) ,
. Manuf act ur erSt r I ndex = 0x01,
. Product St r I ndex = 0x02,
. Ser i al NumSt r I ndex = USE_I NTERNAL_SERI AL,
. Number Of Conf i gur at i ons = 1
};
Type defines for the device configuration descriptor structure. As the configuration descriptor contains several sub-
descriptors which vary between devices, and which describe the device's usage to the host. Detail definition can be
found in Descriptors.c.
t ypedef st r uct
{
USB_Descr i pt or_Conf i gur at i on_Header_ t Conf i g;
/ / CDC Cont r ol I nt er f ace
USB_Descr i pt or_ I nt er f ace_t CDC_CCI _I nt er f ace;
USB_CDC_Descr i ptor _Funct i onal Header _t CDC_Funct i onal _Header ;
USB_CDC_Descr i ptor _Funct i onal ACM_t CDC_Funct i onal _ACM;
USB_CDC_Descr i pt or_Funct i onal Uni on_t CDC_Funct i onal _Uni on;
USB_Descr i pt or_Endpoi nt _t CDC_Not i f i cat i onEndpoi nt ;
/ / CDC Dat a I nt er f ace
USB_Descr i pt or_I nt er f ace_t CDC_DCI _I nt er f ace;
USB_Descr i ptor _Endpoi nt _t CDC_Dat aOutEndpoi nt ;
USB_Descr i pt or_Endpoi nt_t CDC_DataI nEndpoi nt ;
} USB_Descr i pt or _Conf i gur at i on_t ;
8/12/2019 An758 Usb Cdc
18/30
A N758
18 Rev. 0.1
Example:
const USB_Descr i pt or _Conf i gur at i on_t Conf i gur ati onDescr i pt or ;
String Descriptors
#def i ne STR0LEN 4
st at i c ui nt 8_t const St r i ng0Desc[ STR0LEN] =
{
STR0LEN, DTYPE_St r i ng, 0x09, 0x04
};
#def i ne STR1LEN si zeof ( "SI LI CON LABORATORI ES I NC. " ) *2
st at i c ui nt 8_t const St r i ng1Desc[ STR1LEN] ={. . . }
#def i ne STR2LEN si zeof ( "Si M3U1xx CDC Cl ass" ) *2
st at i c ui nt 8_t const St r i ng2Desc[ STR2LEN] ={. . . }
8/12/2019 An758 Usb Cdc
19/30
8/12/2019 An758 Usb Cdc
20/30
A N758
20 Rev. 0.1
5.4.3. USB_Device_SetFullSpeed
Set USB device as full speed.
Syntax
st at i c i nl i ne voi d USB_Devi ce_Set Ful l Speed( voi d)
Parameters
None
Return Value
None
Description
This function set USB device as Full speed.
Example
i f ( USB_Opt i ons & USB_DEVI CE_OPT_LOWSPEED)
{
USB_Devi ce_Set LowSpeed( ) ;
}
el se
{
USB_Devi ce_SetFul l Speed( ) ;}
5.4.4. USB_Device_SetDeviceAddress
Set USB device address.
Syntax
st at i c i nl i ne voi d USB_Devi ce_Set Devi ceAddr ess( const ui nt 8_t Addr ess)
Parameters
Addr ess: USB address
Return Value
None
DescriptionThis function set USB device address assigned from USB host .
Example
ui nt8_t Devi ceAddr ess = ( USB_Contr ol Request . wVal ue & 0x7F) ;USB_Devi ce_Set Devi ceAddr ess( Devi ceAddr ess) ;
5.4.5. USB_Device_IsAddressSet
Check USB device address assigned or not.
Syntax
st at i c i nl i ne bool USB_Devi ce_I sAddr essSet ( voi d)
Parameters
NoneReturn ValueUSB devi ce addr ess
Description
This function USB device address assigned or not.
ExampleUSB_Devi ceSt at e = ( USB_Devi ce_I sAddr essSet ( ) ) ? DEVI CE_STATE_Conf i gur ed :DEVI CE_STATE_Power ed;
8/12/2019 An758 Usb Cdc
21/30
AN758
Rev. 0.1 21
5.4.6. USB_Init
USB hardware initialization.
Syntax
voi d USB_I ni t ( const ui nt 8_t Opt i ons)
Parameters
Opt i ons: USB Devi ce Mode Opt i on
Return Value
None
Description
This function initializes USB hardware. Set full or low speed. Turn on USB 48 MHz oscillators. Enable USB
interrupt. Configure Endpoint to default state. Enable internal pull up resistor to attach USB device.
ExampleUSB_I ni t ( USB_DEVI CE_OPT_FULLSPEED) ;
5.4.7. USB0_IRQHandler
USB interrupt handler.
Syntax
voi d USB0_I RQHandl er( voi d)
Parameters
None
Return Value
None
Description
This function handles all enabled USB device interrupt. Clear interrupt flag. Execute handler function.
ExampleWhen USB interrupt happened, PC will jump to vector table and then jump into this function.
5.4.8. USB0_ep0_handler
Handle endpoint 0 request.Syntax
voi d USB0_ep0_handl er( voi d)
Parameters
None
Return Value
None
Description
This function handles all endpoint 0 request.
Example
i f ( usbEpI nter r upt Mask & SI 32_USB_A_I OI NT_EP0I _MASK)
{
USB0_ep0_handl er ( ) ;
return;}
8/12/2019 An758 Usb Cdc
22/30
A N758
22 Rev. 0.1
5.4.9. USB0_epn_handler
Handle endpoint n request.
Syntax
voi d USB0_epn_handl er( voi d)
Parameters
None
Return Value
None
Description
This function handles all endpoint n request.
Example
i f ( usbEpI nter r upt Mask & ( SI 32_USB_A_I OI NT_I N1I _MASK | SI 32_USB_A_I OI NT_OUT1I _MASK) )
{
Endpoi nt _Sel ect Endpoi nt ( 1) ;
USB0_epn_handl er ( ) ;
}
i f ( usbEpI nter r upt Mask & ( SI 32_USB_A_I OI NT_I N2I _MASK | SI 32_USB_A_I OI NT_OUT2I _MASK) )
{Endpoi nt _Sel ect Endpoi nt ( 2) ;
USB0_epn_handl er ( ) ;}
5.4.10. Endpoint_WaitUnt ilReady
Wait for endpoint ready.
Syntax
ui nt 8_t Endpoi nt _Wai t Unt i l Ready(voi d)
Parameters
None
Return ValueENDPOI NT_READYWAI T_NoEr r or ,
ENDPOI NT_READYWAI T_Endpoi ntSt al l ed,
ENDPOI NT_READYWAI T_Devi ceDi sconnect ed,
ENDPOI NT_READYWAI T_BusSuspended,
ENDPOI NT_READYWAI T_Ti meout
Description
This function will check endpoint ready status. It set 100ms timeout with counting frame number.
Example
i f ( ( Err orCode = Endpoi nt_Wai t Unt i l Ready( ) ) ! = ENDPOI NT_READYWAI T_NoEr r or ) r et ur n Er r orCode;
8/12/2019 An758 Usb Cdc
23/30
AN758
Rev. 0.1 23
5.4.11. Endpoint_ConfigureEndpointTable
Configure endpoint.
Syntax
bool Endpoi nt _Conf i gur eEndpoi nt Tabl e(
const USB_Endpoi nt _Tabl e_t * const Tabl e,
const ui nt 8_t Ent r i es)
Parameters
Tabl e: poi nt er t o endpoi nt t abl e
Ent r i es: no use i n t hi s sof t war e
Return Value
Tr ue: i f conf i gur e successf ul .
Fal se: i f conf i gure f ai l ure.
Description
This function configures endpoint.
Example
i f ( ! ( Endpoi nt _Conf i gur eEndpoi nt Tabl e( &CDCI nt er f aceI nf o- >Conf i g. Dat aI NEndpoi nt , 1) ) ) r et ur n f al se;
5.4.12. Endpoint_Write_Control_Stream_LE
Write stream data to control endpoint
Syntax
ui nt 8_t Endpoi nt _Wr i t e_Cont r ol _St r eam_LE( const voi d* const Buf f er ,
ui nt 16_t Lengt h)
Parameters
Buf f er : poi nt er t o dat a buf f er
Lengt h: Transmi t l engt h
Return Value
ENDPOI NT_RWCSTREAM_NoEr r or
DescriptionThis function writes stream data to control endpoint.
ExampleEndpoi nt _Wr i t e_Cont r ol _St r eam_LE( Descr i pt or Poi nt er , Descr i pt or Si ze) ;
8/12/2019 An758 Usb Cdc
24/30
A N758
24 Rev. 0.1
5.4.13. Endpoint_Write_Stream_LE
Write stream data to correspond endpoint.
Syntax
ui nt 8_t Endpoi nt _Wr i t e_St r eam_LE(const voi d* const Buf f er ,
ui nt 16_t Lengt h,
ui nt 16_t * const Byt esProcessed)
Parameters
Buf f er : poi nt er t o dat a buf f er
Lengt h: Transmi t l engt h
Return Value
ENDPOI NT_RWSTREAM_NoEr r or ,
ENDPOI NT_READYWAI T_Endpoi ntSt al l ed,
ENDPOI NT_READYWAI T_Devi ceDi sconnect ed,
ENDPOI NT_READYWAI T_BusSuspended,
ENDPOI NT_READYWAI T_Ti meout
Description
This function writes stream data to correspond endpoint according usb_ep_selected.
ExampleEndpoi nt _Sel ect Endpoi nt ( CDCI nt er f aceI nf o- >Conf i g. DataI NEndpoi nt . Addr ess) ;
r et ur n Endpoi nt _Wr i t e_St r eam_LE(Buf f er , Lengt h, NULL) ;
5.4.14. Endpoint_ClearIN
Sends an IN packet to the host on the currently selected endpoint.
Syntax
st at i c i nl i ne voi d Endpoi nt _Cl ear I N( voi d)
Parameters
None
Return Value
NoneDescription
This function Sends an IN packet to the host on the currently selected endpoint. It set IPRDYI = 1 to start USB IN
transmits.
Example
CDC_Devi ce_SendData( &Vi r t ual Ser i al _CDC_I nt er f ace, ( char *) i n_buf f , r ecv_count ) ;Endpoi nt _Cl ear I N( ) ;
8/12/2019 An758 Usb Cdc
25/30
AN758
Rev. 0.1 25
5.4.15. Endpoint_ClearOUT
Acknowledges an OUT packet to the host on the currently selected endpoint
Syntax
st at i c i nl i ne voi d Endpoi nt _Cl ear OUT( voi d)
Parameters
None
Return Value
None
Description
This function acknowledges an OUT packet to the host on the currently selected endpoint.
Example
CDCI nt er f aceI nf o- >St at e. Li neEncodi ng. BaudRat eBPS = Endpoi nt _Read_32_LE( ) ;
CDCI nt er f aceI nf o- >St at e. Li neEncodi ng. Char For mat = Endpoi nt _Read_8( ) ;
CDCI nt er f aceI nf o- >St ate. Li neEncodi ng. Par i t yType = Endpoi nt _Read_8( ) ;
CDCI nt er f aceI nf o- >St ate. Li neEncodi ng. DataBi t s = Endpoi nt _Read_8( ) ;
Endpoi nt _Cl ear OUT( ) ;
8/12/2019 An758 Usb Cdc
26/30
8/12/2019 An758 Usb Cdc
27/30
AN758
Rev. 0.1 27
5. Select Browse my computer for driver software.
8/12/2019 An758 Usb Cdc
28/30
A N758
28 Rev. 0.1
6. Enter the directory path of the CDC_ACM.inffile (AN758_USB_CDC\src\VirtualSerialdirectory). If the
Include subfoldersoption is checked, entering the main sim3u1xx_USB_CDCdirectory in the
workspace is sufficient.
7. Windows will display a warning. Select Install this dri ver software anyway.
8. When the driver finishes installing, Windows will report the installation results.
8/12/2019 An758 Usb Cdc
29/30
AN758
Rev. 0.1 29
9. Open Device Manager and observe the device. It will now appear under Ports (COM & LPT) with an
assigned COM port number.
8/12/2019 An758 Usb Cdc
30/30