Finished CDC device class driver documentation.
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / CDC.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 #define INCLUDE_FROM_CDC_CLASS_C
32 #include "CDC.h"
33
34 void USB_CDC_Event_Stub(void)
35 {
36
37 }
38
39 void USB_CDC_ProcessControlPacket(USB_ClassInfo_CDC_t* CDCInterfaceInfo)
40 {
41 if (!(Endpoint_IsSETUPReceived()))
42 return;
43
44 if (USB_ControlRequest.wIndex != CDCInterfaceInfo->ControlInterfaceNumber)
45 return;
46
47 switch (USB_ControlRequest.bRequest)
48 {
49 case REQ_GetLineEncoding:
50 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
51 {
52 Endpoint_ClearSETUP();
53 Endpoint_Write_Control_Stream_LE(&CDCInterfaceInfo->LineEncoding, sizeof(CDCInterfaceInfo->LineEncoding));
54 Endpoint_ClearOUT();
55 }
56
57 break;
58 case REQ_SetLineEncoding:
59 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
60 {
61 Endpoint_ClearSETUP();
62 Endpoint_Read_Control_Stream_LE(&CDCInterfaceInfo->LineEncoding, sizeof(CDCInterfaceInfo->LineEncoding));
63 Endpoint_ClearIN();
64
65 EVENT_USB_CDC_LineEncodingChanged(CDCInterfaceInfo);
66 }
67
68 break;
69 case REQ_SetControlLineState:
70 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
71 {
72 Endpoint_ClearSETUP();
73
74 CDCInterfaceInfo->ControlLineState = USB_ControlRequest.wValue;
75
76 EVENT_USB_CDC_ControLineStateChanged(CDCInterfaceInfo);
77
78 while (!(Endpoint_IsINReady()));
79 Endpoint_ClearIN();
80 }
81
82 break;
83 }
84 }
85
86 bool USB_CDC_ConfigureEndpoints(USB_ClassInfo_CDC_t* CDCInterfaceInfo)
87 {
88 if (!(Endpoint_ConfigureEndpoint(CDCInterfaceInfo->DataINEndpointNumber, EP_TYPE_BULK,
89 ENDPOINT_DIR_IN, CDCInterfaceInfo->DataINEndpointSize,
90 ENDPOINT_BANK_SINGLE)))
91 {
92 return false;
93 }
94
95 if (!(Endpoint_ConfigureEndpoint(CDCInterfaceInfo->DataOUTEndpointNumber, EP_TYPE_BULK,
96 ENDPOINT_DIR_OUT, CDCInterfaceInfo->DataOUTEndpointSize,
97 ENDPOINT_BANK_SINGLE)))
98 {
99 return false;
100 }
101
102 if (!(Endpoint_ConfigureEndpoint(CDCInterfaceInfo->NotificationEndpointNumber, EP_TYPE_INTERRUPT,
103 ENDPOINT_DIR_IN, CDCInterfaceInfo->NotificationEndpointSize,
104 ENDPOINT_BANK_SINGLE)))
105 {
106 return false;
107 }
108
109 return true;
110 }
111
112 void USB_CDC_USBTask(USB_ClassInfo_CDC_t* CDCInterfaceInfo)
113 {
114 if (!(USB_IsConnected))
115 return;
116
117 Endpoint_SelectEndpoint(CDCInterfaceInfo->DataINEndpointNumber);
118
119 if (!(Endpoint_BytesInEndpoint()))
120 return;
121
122 if (!(Endpoint_IsReadWriteAllowed()))
123 {
124 Endpoint_ClearIN();
125 while (!(Endpoint_IsReadWriteAllowed()));
126 }
127
128 Endpoint_ClearIN();
129 }
130
131 void USB_CDC_SendString(USB_ClassInfo_CDC_t* CDCInterfaceInfo, char* Data, uint16_t Length)
132 {
133 if (!(USB_IsConnected))
134 return;
135
136 Endpoint_SelectEndpoint(CDCInterfaceInfo->DataINEndpointNumber);
137 Endpoint_Write_Stream_LE(Data, Length, NO_STREAM_CALLBACK);
138 }
139
140 void USB_CDC_SendByte(USB_ClassInfo_CDC_t* CDCInterfaceInfo, uint8_t Data)
141 {
142 if (!(USB_IsConnected))
143 return;
144
145 Endpoint_SelectEndpoint(CDCInterfaceInfo->DataINEndpointNumber);
146
147 if (!(Endpoint_IsReadWriteAllowed()))
148 {
149 Endpoint_ClearIN();
150 while (!(Endpoint_IsReadWriteAllowed()));
151 }
152
153 Endpoint_Write_Byte(Data);
154 }
155
156 uint16_t USB_CDC_BytesReceived(USB_ClassInfo_CDC_t* CDCInterfaceInfo)
157 {
158 Endpoint_SelectEndpoint(CDCInterfaceInfo->DataOUTEndpointNumber);
159
160 return Endpoint_BytesInEndpoint();
161 }
162
163 uint8_t USB_CDC_ReceiveByte(USB_ClassInfo_CDC_t* CDCInterfaceInfo)
164 {
165 if (!(USB_IsConnected))
166 return 0;
167
168 Endpoint_SelectEndpoint(CDCInterfaceInfo->DataOUTEndpointNumber);
169
170 uint8_t DataByte = Endpoint_Read_Byte();
171
172 if (!(Endpoint_BytesInEndpoint()))
173 Endpoint_ClearOUT();
174
175 return DataByte;
176 }
177
178 void USB_CDC_SendSerialLineStateChange(USB_ClassInfo_CDC_t* CDCInterfaceInfo, uint16_t LineStateMask)
179 {
180 if (!(USB_IsConnected))
181 return;
182
183 Endpoint_SelectEndpoint(CDCInterfaceInfo->NotificationEndpointNumber);
184
185 USB_Request_Header_t Notification = (USB_Request_Header_t)
186 {
187 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
188 .bRequest = NOTIF_SerialState,
189 .wValue = 0,
190 .wIndex = 0,
191 .wLength = sizeof(uint16_t),
192 };
193
194 Endpoint_Write_Stream_LE(&Notification, sizeof(Notification), NO_STREAM_CALLBACK);
195 Endpoint_Write_Stream_LE(&LineStateMask, sizeof(LineStateMask), NO_STREAM_CALLBACK);
196 Endpoint_ClearIN();
197 }