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 #define INCLUDE_FROM_CDC_CLASS_C
34 void USB_CDC_Event_Stub(void)
39 void USB_CDC_ProcessControlPacket(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
)
41 if (!(Endpoint_IsSETUPReceived()))
44 if (USB_ControlRequest
.wIndex
!= CDCInterfaceInfo
->ControlInterfaceNumber
)
47 switch (USB_ControlRequest
.bRequest
)
49 case REQ_GetLineEncoding
:
50 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
52 Endpoint_ClearSETUP();
53 Endpoint_Write_Control_Stream_LE(&CDCInterfaceInfo
->LineEncoding
, sizeof(CDCInterfaceInfo
->LineEncoding
));
58 case REQ_SetLineEncoding
:
59 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
61 Endpoint_ClearSETUP();
62 Endpoint_Read_Control_Stream_LE(&CDCInterfaceInfo
->LineEncoding
, sizeof(CDCInterfaceInfo
->LineEncoding
));
65 EVENT_USB_CDC_LineEncodingChanged(CDCInterfaceInfo
);
69 case REQ_SetControlLineState
:
70 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
72 Endpoint_ClearSETUP();
74 CDCInterfaceInfo
->ControlLineState
= USB_ControlRequest
.wValue
;
76 EVENT_USB_CDC_ControLineStateChanged(CDCInterfaceInfo
);
78 while (!(Endpoint_IsINReady()));
86 bool USB_CDC_ConfigureEndpoints(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
)
88 if (!(Endpoint_ConfigureEndpoint(CDCInterfaceInfo
->DataINEndpointNumber
, EP_TYPE_BULK
,
89 ENDPOINT_DIR_IN
, CDCInterfaceInfo
->DataINEndpointSize
,
90 ENDPOINT_BANK_SINGLE
)))
95 if (!(Endpoint_ConfigureEndpoint(CDCInterfaceInfo
->DataOUTEndpointNumber
, EP_TYPE_BULK
,
96 ENDPOINT_DIR_OUT
, CDCInterfaceInfo
->DataOUTEndpointSize
,
97 ENDPOINT_BANK_SINGLE
)))
102 if (!(Endpoint_ConfigureEndpoint(CDCInterfaceInfo
->NotificationEndpointNumber
, EP_TYPE_INTERRUPT
,
103 ENDPOINT_DIR_IN
, CDCInterfaceInfo
->NotificationEndpointSize
,
104 ENDPOINT_BANK_SINGLE
)))
112 void USB_CDC_USBTask(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
)
114 if (!(USB_IsConnected
))
117 Endpoint_SelectEndpoint(CDCInterfaceInfo
->DataINEndpointNumber
);
119 if (!(Endpoint_BytesInEndpoint()))
122 if (!(Endpoint_IsReadWriteAllowed()))
125 while (!(Endpoint_IsReadWriteAllowed()));
131 void USB_CDC_SendString(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
, char* Data
, uint16_t Length
)
133 Endpoint_SelectEndpoint(CDCInterfaceInfo
->DataINEndpointNumber
);
134 Endpoint_Write_Stream_LE(Data
, Length
, NO_STREAM_CALLBACK
);
137 void USB_CDC_SendByte(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
, uint8_t Data
)
139 if (!(USB_IsConnected
))
142 Endpoint_SelectEndpoint(CDCInterfaceInfo
->DataINEndpointNumber
);
144 if (!(Endpoint_IsReadWriteAllowed()))
147 while (!(Endpoint_IsReadWriteAllowed()));
150 Endpoint_Write_Byte(Data
);
153 uint16_t USB_CDC_BytesReceived(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
)
155 Endpoint_SelectEndpoint(CDCInterfaceInfo
->DataOUTEndpointNumber
);
157 return Endpoint_BytesInEndpoint();
160 uint8_t USB_CDC_ReceiveByte(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
)
162 Endpoint_SelectEndpoint(CDCInterfaceInfo
->DataOUTEndpointNumber
);
164 uint8_t DataByte
= Endpoint_Read_Byte();
166 if (!(Endpoint_BytesInEndpoint()))
172 void USB_CDC_SendSerialLineStateChange(USB_ClassInfo_CDC_t
* CDCInterfaceInfo
, uint16_t LineStateMask
)
174 Endpoint_SelectEndpoint(CDCInterfaceInfo
->NotificationEndpointNumber
);
176 USB_Request_Header_t Notification
= (USB_Request_Header_t
)
178 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
179 .bRequest
= NOTIF_SerialState
,
182 .wLength
= sizeof(uint16_t),
185 Endpoint_Write_Stream_LE(&Notification
, sizeof(Notification
), NO_STREAM_CALLBACK
);
186 Endpoint_Write_Stream_LE(&LineStateMask
, sizeof(LineStateMask
), NO_STREAM_CALLBACK
);