3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
31 /** \ingroup Group_USBDeviceClassDrivers
32 * @defgroup Group_USBClassCDCDevice CDC Device Class Driver - LUFA/Drivers/Class/Device/CDC.h
34 * \section Sec_Dependencies Module Source Dependencies
35 * The following files must be built with any user project that uses this module:
36 * - LUFA/Drivers/USB/Class/Device/CDC.c
38 * \section Module Description
39 * Functions, macros, variables, enums and types related to the management of USB CDC Class interfaces
40 * within a USB device, for the implementation of CDC-ACM virtual serial ports.
49 #include "../../USB.h"
53 /* Enable C linkage for C++ Compilers: */
54 #if defined(__cplusplus)
59 /** CDC Class specific request to get the current virtual serial port configuration settings. */
60 #define REQ_GetLineEncoding 0x21
62 /** CDC Class specific request to set the current virtual serial port configuration settings. */
63 #define REQ_SetLineEncoding 0x20
65 /** CDC Class specific request to set the current virtual serial port handshake line states. */
66 #define REQ_SetControlLineState 0x22
68 /** Notification type constant for a change in the virtual serial port handshake line states, for
69 * use with a USB_Notification_Header_t notification structure when sent to the host via the CDC
70 * notification endpoint.
72 #define NOTIF_SerialState 0x20
74 /** Mask for the DTR handshake line for use with the REQ_SetControlLineState class specific request
75 * from the host, to indicate that the DTR line state should be high.
77 #define CDC_CONTROL_LINE_OUT_DTR (1 << 0)
79 /** Mask for the RTS handshake line for use with the REQ_SetControlLineState class specific request
80 * from the host, to indicate that theRTS line state should be high.
82 #define CDC_CONTROL_LINE_OUT_RTS (1 << 1)
84 /** Mask for the DCD handshake line for use with the a NOTIF_SerialState class specific notification
85 * from the device to the host, to indicate that the DCD line state is currently high.
87 #define CDC_CONTROL_LINE_IN_DCD (1 << 0)
89 /** Mask for the DSR handshake line for use with the a NOTIF_SerialState class specific notification
90 * from the device to the host, to indicate that the DSR line state is currently high.
92 #define CDC_CONTROL_LINE_IN_DSR (1 << 1)
94 /** Mask for the BREAK handshake line for use with the a NOTIF_SerialState class specific notification
95 * from the device to the host, to indicate that the BREAK line state is currently high.
97 #define CDC_CONTROL_LINE_IN_BREAK (1 << 2)
99 /** Mask for the RING handshake line for use with the a NOTIF_SerialState class specific notification
100 * from the device to the host, to indicate that the RING line state is currently high.
102 #define CDC_CONTROL_LINE_IN_RING (1 << 3)
104 /** Mask for use with the a NOTIF_SerialState class specific notification from the device to the host,
105 * to indicate that a framing error has occurred on the virtual serial port.
107 #define CDC_CONTROL_LINE_IN_FRAMEERROR (1 << 4)
109 /** Mask for use with the a NOTIF_SerialState class specific notification from the device to the host,
110 * to indicate that a parity error has occurred on the virtual serial port.
112 #define CDC_CONTROL_LINE_IN_PARITYERROR (1 << 5)
114 /** Mask for use with the a NOTIF_SerialState class specific notification from the device to the host,
115 * to indicate that a data overrun error has occurred on the virtual serial port.
117 #define CDC_CONTROL_LINE_IN_OVERRUNERROR (1 << 6)
119 /** Macro to define a CDC class-specific functional descriptor. CDC functional descriptors have a
120 * uniform structure but variable sized data payloads, thus cannot be represented accurately by
121 * a single typedef struct. A macro is used instead so that functional descriptors can be created
122 * easily by specifying the size of the payload. This allows sizeof() to work correctly.
124 * \param DataSize Size in bytes of the CDC functional descriptor's data payload
126 #define CDC_FUNCTIONAL_DESCRIPTOR(DataSize) \
129 USB_Descriptor_Header_t Header; \
131 uint8_t Data[DataSize]; \
135 /** Enum for the possible line encoding formats of a virtual serial port. */
136 enum CDCDevice_CDC_LineCodingFormats_t
138 CDC_LINEENCODING_OneStopBit
= 0, /**< Each frame contains one stop bit */
139 CDC_LINEENCODING_OneAndAHalfStopBits
= 1, /**< Each frame contains one and a half stop bits */
140 CDC_LINEENCODING_TwoStopBits
= 2, /**< Each frame contains two stop bits */
143 /** Enum for the possible line encoding parity settings of a virtual serial port. */
144 enum CDCDevice_LineCodingParity_t
146 CDC_PARITY_None
= 0, /**< No parity bit mode on each frame */
147 CDC_PARITY_Odd
= 1, /**< Odd parity bit mode on each frame */
148 CDC_PARITY_Even
= 2, /**< Even parity bit mode on each frame */
149 CDC_PARITY_Mark
= 3, /**< Mark parity bit mode on each frame */
150 CDC_PARITY_Space
= 4, /**< Space parity bit mode on each frame */
154 /** Class state structure. An instance of this structure should be made for each CDC interface
155 * within the user application, and passed to each of the CDC class driver functions as the
156 * CDCInterfaceInfo parameter. The contents of this structure should be set to their correct
157 * values when used, or ommitted to force the library to use default values.
161 uint8_t ControlInterfaceNumber
; /**< Interface number of the CDC control interface within the device */
163 uint8_t DataINEndpointNumber
; /**< Endpoint number of the CDC interface's IN data endpoint */
164 uint16_t DataINEndpointSize
; /**< Size in bytes of the CDC interface's IN data endpoint */
166 uint8_t DataOUTEndpointNumber
; /**< Endpoint number of the CDC interface's OUT data endpoint */
167 uint16_t DataOUTEndpointSize
; /**< Size in bytes of the CDC interface's OUT data endpoint */
169 uint8_t NotificationEndpointNumber
; /**< Endpoint number of the CDC interface's IN notification endpoint, if used */
170 uint16_t NotificationEndpointSize
; /**< Size in bytes of the CDC interface's IN notification endpoint, if used */
172 uint8_t ControlLineState
; /**< Current control line states, as set by the host */
176 uint32_t BaudRateBPS
; /**< Baud rate of the virtual serial port, in bits per second */
177 uint8_t CharFormat
; /**< Character format of the virtual serial port, a value from the
178 * CDCDevice_CDC_LineCodingFormats_t enum
180 uint8_t ParityType
; /**< Parity setting of the virtual serial port, a value from the
181 * CDCDevice_LineCodingParity_t enum
183 uint8_t DataBits
; /**< Bits of data per character of the virtual serial port */
185 } USB_ClassInfo_CDC_t
;
187 /* Function Prototypes: */
188 #if defined(INCLUDE_FROM_CDC_CLASS_C)
189 void USB_CDC_Event_Stub(void);
190 void EVENT_USB_CDC_LineEncodingChanged(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
)
191 ATTR_WEAK
ATTR_ALIAS(USB_CDC_Event_Stub
);
192 void EVENT_USB_CDC_ControLineStateChanged(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
)
193 ATTR_WEAK
ATTR_ALIAS(USB_CDC_Event_Stub
);
196 /** Configures the endpoints of a given CDC interface, ready for use. This should be linked to the library
197 * \ref EVENT_USB_ConfigurationChanged() event so that the endpoints are configured when the configuration containing the
198 * given CDC interface is selected.
200 * \param CDCInterfaceInfo Pointer to a structure containing a CDC Class configuration and state.
202 * \return Boolean true if the endpoints were sucessfully configured, false otherwise
204 bool USB_CDC_ConfigureEndpoints(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
);
206 /** Processes incomming control requests from the host, that are directed to the given CDC class interface. This should be
207 * linked to the library \ref EVENT_USB_UnhandledControlPacket() event.
209 * \param CDCInterfaceInfo Pointer to a structure containing a CDC Class configuration and state.
211 void USB_CDC_ProcessControlPacket(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
);
213 /** General management task for a given CDC class interface, required for the correct operation of the interface. This should
214 * be called frequently in the main program loop, before the master USB management task \ref USB_USBTask().
216 * \param CDCInterfaceInfo Pointer to a structure containing a CDC Class configuration and state.
218 void USB_CDC_USBTask(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
);
220 /** CDC class driver event for a line encoding change on a CDC interface. This event fires each time the host requests a
221 * line encoding change (containing the serial parity, baud and other configuration information) and may be hooked in the
222 * user program by declaring a handler function with the same name and parameters listed here. The new line encoding
223 * settings are available in the LineEncoding structure inside the CDC interface structure passed as a parameter.
225 * \param CDCInterfaceInfo Pointer to a structure containing a CDC Class configuration and state.
227 void EVENT_USB_CDC_LineEncodingChanged(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
);
229 /** CDC class driver event for a control line state change on a CDC interface. This event fires each time the host requests a
230 * control line state change (containing the virtual serial control line states, such as DTR) and may be hooked in the
231 * user program by declaring a handler function with the same name and parameters listed here. The new control line states
232 * are available in the ControlLineState value inside the CDC interface structure passed as a parameter, set as a mask of
233 * CDC_CONTROL_LINE_OUT_* masks.
235 * \param CDCInterfaceInfo Pointer to a structure containing a CDC Class configuration and state.
237 void EVENT_USB_CDC_ControLineStateChanged(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
);
239 /** Sends a given string to the attached USB host, if connected. If a host is not connected when the function is called, the
240 * string is discarded.
242 * \param CDCInterfaceInfo Pointer to a structure containing a CDC Class configuration and state.
243 * \param Data Pointer to the string to send to the host
244 * \param Length Size in bytes of the string to send to the host
246 void USB_CDC_SendString(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
, char* Data
, uint16_t Length
);
248 /** Sends a given byte to the attached USB host, if connected. If a host is not connected when the function is called, the
251 * \param CDCInterfaceInfo Pointer to a structure containing a CDC Class configuration and state.
252 * \param Data Byte of data to send to the host
254 void USB_CDC_SendByte(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
, uint8_t Data
);
256 /** Determines the number of bytes received by the CDC interface from the host, waiting to be read.
258 * \param CDCInterfaceInfo Pointer to a structure containing a CDC Class configuration and state.
260 * \return Total number of buffered bytes received from the host
262 uint16_t USB_CDC_BytesReceived(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
);
264 /** Reads a byte of data from the host. If no data is waiting to be read of if a USB host is not connected, the function
265 * returns 0. The USB_CDC_BytesReceived() function should be queried before data is recieved to ensure that no data
268 * \param CDCInterfaceInfo Pointer to a structure containing a CDC Class configuration and state.
270 * \return Next received byte from the host, or 0 if no data received
272 uint8_t USB_CDC_ReceiveByte(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
);
274 /** Sends a Serial Control Line State Change notification to the host. This should be called when the virtual serial control
275 * lines (DCD, DSR, etc.) have changed states, or to give BREAK notfications to the host. Line states persist until they are
276 * cleared via a second notification.
278 * \param CDCInterfaceInfo Pointer to a structure containing a CDC Class configuration and state.
279 * \param LineStateMask Mask of CDC_CONTROL_LINE_IN_* masks giving the current control line states
281 void USB_CDC_SendSerialLineStateChange(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
, uint16_t LineStateMask
);
283 /* Disable C linkage for C++ Compilers: */
284 #if defined(__cplusplus)