Commit of new class abstraction APIs for all device demos other than the MIDI demo...
[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();
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 Endpoint_SelectEndpoint(CDCInterfaceInfo->DataINEndpointNumber);
134 Endpoint_Write_Stream_LE(Data, Length, NO_STREAM_CALLBACK);
135 }
136
137 void USB_CDC_SendByte(USB_ClassInfo_CDC_t* CDCInterfaceInfo, uint8_t Data)
138 {
139 Endpoint_SelectEndpoint(CDCInterfaceInfo->DataINEndpointNumber);
140
141 if (!(Endpoint_IsReadWriteAllowed()))
142 {
143 Endpoint_ClearIN();
144 while (!(Endpoint_IsReadWriteAllowed()));
145 }
146
147 Endpoint_Write_Byte(Data);
148 }
149
150 uint16_t USB_CDC_BytesReceived(USB_ClassInfo_CDC_t* CDCInterfaceInfo)
151 {
152 Endpoint_SelectEndpoint(CDCInterfaceInfo->DataOUTEndpointNumber);
153
154 return Endpoint_BytesInEndpoint();
155 }
156
157 uint8_t USB_CDC_ReceiveByte(USB_ClassInfo_CDC_t* CDCInterfaceInfo)
158 {
159 Endpoint_SelectEndpoint(CDCInterfaceInfo->DataOUTEndpointNumber);
160
161 uint8_t DataByte = Endpoint_Read_Byte();
162
163 if (!(Endpoint_BytesInEndpoint()))
164 Endpoint_ClearOUT();
165
166 return DataByte;
167 }
168
169 void USB_CDC_SendSerialLineStateChanged(USB_ClassInfo_CDC_t* CDCInterfaceInfo, uint16_t LineStateMask)
170 {
171 Endpoint_SelectEndpoint(CDCInterfaceInfo->NotificationEndpointNumber);
172
173 USB_Request_Header_t Notification = (USB_Request_Header_t)
174 {
175 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
176 .bRequest = NOTIF_SerialState,
177 .wValue = 0,
178 .wIndex = 0,
179 .wLength = sizeof(uint16_t),
180 };
181
182 Endpoint_Write_Stream_LE(&Notification, sizeof(Notification), NO_STREAM_CALLBACK);
183 Endpoint_Write_Stream_LE(&LineStateMask, sizeof(LineStateMask), NO_STREAM_CALLBACK);
184 Endpoint_ClearIN();
185 }