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
35 #include "../../USB.h"
40 /** CDC Class specific request to get the current virtual serial port configuration settings. */
41 #define REQ_GetLineEncoding 0x21
43 /** CDC Class specific request to set the current virtual serial port configuration settings. */
44 #define REQ_SetLineEncoding 0x20
46 /** CDC Class specific request to set the current virtual serial port handshake line states. */
47 #define REQ_SetControlLineState 0x22
49 /** Notification type constant for a change in the virtual serial port handshake line states, for
50 * use with a USB_Notification_Header_t notification structure when sent to the host via the CDC
51 * notification endpoint.
53 #define NOTIF_SerialState 0x20
55 /** Mask for the DTR handshake line for use with the REQ_SetControlLineState class specific request
56 * from the host, to indicate that the DTR line state should be high.
58 #define CONTROL_LINE_OUT_DTR (1 << 0)
60 /** Mask for the RTS handshake line for use with the REQ_SetControlLineState class specific request
61 * from the host, to indicate that theRTS line state should be high.
63 #define CONTROL_LINE_OUT_RTS (1 << 1)
65 /** Mask for the DCD handshake line for use with the a NOTIF_SerialState class specific notification
66 * from the device to the host, to indicate that the DCD line state is currently high.
68 #define CONTROL_LINE_IN_DCD (1 << 0)
70 /** Mask for the DSR handshake line for use with the a NOTIF_SerialState class specific notification
71 * from the device to the host, to indicate that the DSR line state is currently high.
73 #define CONTROL_LINE_IN_DSR (1 << 1)
75 /** Mask for the BREAK handshake line for use with the a NOTIF_SerialState class specific notification
76 * from the device to the host, to indicate that the BREAK line state is currently high.
78 #define CONTROL_LINE_IN_BREAK (1 << 2)
80 /** Mask for the RING handshake line for use with the a NOTIF_SerialState class specific notification
81 * from the device to the host, to indicate that the RING line state is currently high.
83 #define CONTROL_LINE_IN_RING (1 << 3)
85 /** Mask for use with the a NOTIF_SerialState class specific notification from the device to the host,
86 * to indicate that a framing error has occurred on the virtual serial port.
88 #define CONTROL_LINE_IN_FRAMEERROR (1 << 4)
90 /** Mask for use with the a NOTIF_SerialState class specific notification from the device to the host,
91 * to indicate that a parity error has occurred on the virtual serial port.
93 #define CONTROL_LINE_IN_PARITYERROR (1 << 5)
95 /** Mask for use with the a NOTIF_SerialState class specific notification from the device to the host,
96 * to indicate that a data overrun error has occurred on the virtual serial port.
98 #define CONTROL_LINE_IN_OVERRUNERROR (1 << 6)
100 /** Macro to define a CDC class-specific functional descriptor. CDC functional descriptors have a
101 * uniform structure but variable sized data payloads, thus cannot be represented accurately by
102 * a single typedef struct. A macro is used instead so that functional descriptors can be created
103 * easily by specifying the size of the payload. This allows sizeof() to work correctly.
105 * \param DataSize Size in bytes of the CDC functional descriptor's data payload
107 #define CDC_FUNCTIONAL_DESCRIPTOR(DataSize) \
110 USB_Descriptor_Header_t Header; \
112 uint8_t Data[DataSize]; \
116 /** Enum for the possible line encoding formats of a virtual serial port. */
117 enum CDCDevice_CDC_LineCodingFormats_t
119 OneStopBit
= 0, /**< Each frame contains one stop bit */
120 OneAndAHalfStopBits
= 1, /**< Each frame contains one and a half stop bits */
121 TwoStopBits
= 2, /**< Each frame contains two stop bits */
124 /** Enum for the possible line encoding parity settings of a virtual serial port. */
125 enum CDCDevice_LineCodingParity_t
127 Parity_None
= 0, /**< No parity bit mode on each frame */
128 Parity_Odd
= 1, /**< Odd parity bit mode on each frame */
129 Parity_Even
= 2, /**< Even parity bit mode on each frame */
130 Parity_Mark
= 3, /**< Mark parity bit mode on each frame */
131 Parity_Space
= 4, /**< Space parity bit mode on each frame */
135 /** Type define for the virtual serial port line encoding settings, for storing the current USART configuration
136 * as set by the host via a class specific request.
140 uint8_t ControlInterfaceNumber
; /**< Interface number of the CDC control interface within the device */
142 uint8_t DataINEndpointNumber
; /**< Endpoint number of the CDC interface's IN data endpoint */
143 uint16_t DataINEndpointSize
; /**< Size in bytes of the CDC interface's IN data endpoint */
145 uint8_t DataOUTEndpointNumber
; /**< Endpoint number of the CDC interface's OUT data endpoint */
146 uint16_t DataOUTEndpointSize
; /**< Size in bytes of the CDC interface's OUT data endpoint */
148 uint8_t NotificationEndpointNumber
; /**< Endpoint number of the CDC interface's IN notification endpoint, if used */
149 uint16_t NotificationEndpointSize
; /**< Size in bytes of the CDC interface's IN notification endpoint, if used */
151 uint8_t ControlLineState
;
155 uint32_t BaudRateBPS
; /**< Baud rate of the virtual serial port, in bits per second */
156 uint8_t CharFormat
; /**< Character format of the virtual serial port, a value from the
157 * CDCDevice_CDC_LineCodingFormats_t enum
159 uint8_t ParityType
; /**< Parity setting of the virtual serial port, a value from the
160 * CDCDevice_LineCodingParity_t enum
162 uint8_t DataBits
; /**< Bits of data per character of the virtual serial port */
164 } USB_ClassInfo_CDC_t
;
166 /* Function Prototypes: */
167 #if defined(INCLUDE_FROM_CDC_CLASS_C)
168 void USB_CDC_Event_Stub(void);
169 void EVENT_USB_CDC_LineEncodingChanged(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
)
170 ATTR_WEAK
ATTR_ALIAS(USB_CDC_Event_Stub
);
171 void EVENT_USB_CDC_ControLineStateChanged(void) ATTR_WEAK
ATTR_ALIAS(USB_CDC_Event_Stub
);;
174 void USB_CDC_USBTask(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
);
175 bool USB_CDC_ConfigureEndpoints(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
);
176 void USB_CDC_ProcessControlPacket(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
);
177 void USB_CDC_USBTask(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
);
179 void EVENT_USB_CDC_LineEncodingChanged(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
);
180 void EVENT_USB_CDC_ControLineStateChanged(void);
182 void USB_CDC_SendString(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
, char* Data
, uint16_t Length
);
183 void USB_CDC_SendByte(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
, uint8_t Data
);
184 uint16_t USB_CDC_BytesReceived(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
);
185 uint8_t USB_CDC_ReceiveByte(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
);
186 void USB_CDC_SendSerialLineStateChanged(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
, uint16_t LineStateMask
);