2 * @brief Device mode driver for the library USB CDC Class driver
\r
5 * Copyright(C) NXP Semiconductors, 2012
\r
6 * Copyright(C) Dean Camera, 2011, 2012
\r
7 * All rights reserved.
\r
10 * Software that is described herein is for illustrative purposes only
\r
11 * which provides customers with programming information regarding the
\r
12 * LPC products. This software is supplied "AS IS" without any warranties of
\r
13 * any kind, and NXP Semiconductors and its licensor disclaim any and
\r
14 * all warranties, express or implied, including all implied warranties of
\r
15 * merchantability, fitness for a particular purpose and non-infringement of
\r
16 * intellectual property rights. NXP Semiconductors assumes no responsibility
\r
17 * or liability for the use of the software, conveys no license or rights under any
\r
18 * patent, copyright, mask work right, or any other intellectual property rights in
\r
19 * or to any products. NXP Semiconductors reserves the right to make changes
\r
20 * in the software without notification. NXP Semiconductors also makes no
\r
21 * representation or warranty that such application will be suitable for the
\r
22 * specified use without further testing or modification.
\r
25 * Permission to use, copy, modify, and distribute this software and its
\r
26 * documentation is hereby granted, under NXP Semiconductors' and its
\r
27 * licensor's relevant copyrights in the software, without fee, provided that it
\r
28 * is used in conjunction with NXP Semiconductors microcontrollers. This
\r
29 * copyright, permission, and disclaimer notice must appear in all copies of
\r
33 /** @ingroup Group_USBClassCDC
\r
34 * @defgroup Group_USBClassCDCDevice CDC Class Device Mode Driver
\r
36 * @section Sec_Dependencies Module Source Dependencies
\r
37 * The following files must be built with any user project that uses this module:
\r
38 * - nxpUSBlib/Drivers/USB/Class/Device/CDCClassDevice.c <i>(Makefile source module name: NXPUSBLIB_SRC_USBCLASS)</i>
\r
40 * @section Sec_ModDescription Module Description
\r
41 * Device Mode USB Class driver framework interface, for the CDC USB Class driver.
\r
43 * @note There are several major drawbacks to the CDC-ACM standard USB class, however
\r
44 * it is very standardized and thus usually available as a built-in driver on
\r
45 * most platforms, and so is a better choice than a proprietary serial class.
\r
47 * One major issue with CDC-ACM is that it requires two Interface descriptors,
\r
48 * which will upset most hosts when part of a multi-function "Composite" USB
\r
49 * device, as each interface will be loaded into a separate driver instance. To
\r
50 * combat this, you should use the "Interface Association Descriptor" addendum to
\r
51 * the USB standard which is available on most OSes when creating Composite devices.
\r
53 * Another major oversight is that there is no mechanism for the host to notify the
\r
54 * device that there is a data sink on the host side ready to accept data. This
\r
55 * means that the device may try to send data while the host isn't listening, causing
\r
56 * lengthy blocking timeouts in the transmission routines. To combat this, it is
\r
57 * recommended that the virtual serial line DTR (Data Terminal Ready) be used where
\r
58 * possible to determine if a host application is ready for data.
\r
63 #ifndef _CDC_CLASS_DEVICE_H_
\r
64 #define _CDC_CLASS_DEVICE_H_
\r
67 #include "../../USB.h"
\r
68 #include "../Common/CDCClassCommon.h"
\r
72 /* Enable C linkage for C++ Compilers: */
\r
73 #if defined(__cplusplus)
\r
77 /* Preprocessor Checks: */
\r
78 #if !defined(__INCLUDE_FROM_CDC_DRIVER)
\r
79 #error Do not include this file directly. Include LPCUSBlib/Drivers/USB.h instead.
\r
82 /* Public Interface - May be used in end-application: */
\r
84 /** @brief CDC Class Device Mode Configuration and State Structure.
\r
86 * Class state structure. An instance of this structure should be made for each CDC interface
\r
87 * within the user application, and passed to each of the CDC class driver functions as the
\r
88 * CDCInterfaceInfo parameter. This stores each CDC interface's configuration and state information.
\r
94 uint8_t ControlInterfaceNumber; /**< Interface number of the CDC control interface within the device. */
\r
96 uint8_t DataINEndpointNumber; /**< Endpoint number of the CDC interface's IN data endpoint. */
\r
97 uint16_t DataINEndpointSize; /**< Size in bytes of the CDC interface's IN data endpoint. */
\r
98 bool DataINEndpointDoubleBank; /**< Indicates if the CDC interface's IN data endpoint should use double banking. */
\r
100 uint8_t DataOUTEndpointNumber; /**< Endpoint number of the CDC interface's OUT data endpoint. */
\r
101 uint16_t DataOUTEndpointSize; /**< Size in bytes of the CDC interface's OUT data endpoint. */
\r
102 bool DataOUTEndpointDoubleBank; /**< Indicates if the CDC interface's OUT data endpoint should use double banking. */
\r
104 uint8_t NotificationEndpointNumber; /**< Endpoint number of the CDC interface's IN notification endpoint, if used. */
\r
105 uint16_t NotificationEndpointSize; /**< Size in bytes of the CDC interface's IN notification endpoint, if used. */
\r
106 bool NotificationEndpointDoubleBank; /**< Indicates if the CDC interface's notification endpoint should use double banking. */
\r
107 uint8_t PortNumber; /**< Port number that this interface is running.*/
\r
108 } Config; /**< Config data for the USB class interface within the device. All elements in this section
\r
109 * <b>must</b> be set or the interface will fail to enumerate and operate correctly.
\r
115 uint16_t HostToDevice; /**< Control line states from the host to device, as a set of \c CDC_CONTROL_LINE_OUT_*
\r
116 * masks. This value is updated each time @ref CDC_Device_USBTask() is called.
\r
118 uint16_t DeviceToHost; /**< Control line states from the device to host, as a set of \c CDC_CONTROL_LINE_IN_*
\r
119 * masks - to notify the host of changes to these values, call the
\r
120 * @ref CDC_Device_SendControlLineStateChange() function.
\r
122 } ControlLineStates; /**< Current states of the virtual serial port's control lines between the device and host. */
\r
124 CDC_LineEncoding_t LineEncoding; /** Line encoding used in the virtual serial port, for the device's information.
\r
125 * This is generally only used if the virtual serial port data is to be
\r
126 * reconstructed on a physical UART.
\r
128 } State; /**< State data for the USB class interface within the device. All elements in this section
\r
129 * are reset to their defaults when the interface is enumerated.
\r
131 } USB_ClassInfo_CDC_Device_t;
\r
133 /* Function Prototypes: */
\r
136 * @brief Configures the endpoints of a given CDC interface, ready for use. This should be linked to the library
\r
137 * @ref EVENT_USB_Device_ConfigurationChanged() event so that the endpoints are configured when the configuration containing
\r
138 * the given CDC interface is selected.
\r
140 * @param CDCInterfaceInfo : Pointer to a structure containing a CDC Class configuration and state.
\r
141 * @return Boolean \c true if the endpoints were successfully configured, \c false otherwise.
\r
143 bool CDC_Device_ConfigureEndpoints(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
\r
146 * @brief Processes incoming control requests from the host, that are directed to the given CDC class interface. This should be
\r
147 * linked to the library @ref EVENT_USB_Device_ControlRequest() event.
\r
149 * @param CDCInterfaceInfo : Pointer to a structure containing a CDC Class configuration and state.
\r
152 void CDC_Device_ProcessControlRequest(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
\r
155 * @brief General management task for a given CDC class interface, required for the correct operation of the interface. This should
\r
156 * be called frequently in the main program loop, before the master USB management task @ref USB_USBTask().
\r
158 * @param CDCInterfaceInfo : Pointer to a structure containing a CDC Class configuration and state.
\r
161 void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
\r
164 * @brief CDC class driver event for a line encoding change on a CDC interface. This event fires each time the host requests a
\r
165 * line encoding change (containing the serial parity, baud and other configuration information) and may be hooked in the
\r
166 * user program by declaring a handler function with the same name and parameters listed here. The new line encoding
\r
167 * settings are available in the LineEncoding structure inside the CDC interface structure passed as a parameter.
\r
169 * @param CDCInterfaceInfo : Pointer to a structure containing a CDC Class configuration and state.
\r
172 void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
\r
175 * @brief CDC class driver event for a control line state change on a CDC interface. This event fires each time the host requests a
\r
176 * control line state change (containing the virtual serial control line states, such as DTR) and may be hooked in the
\r
177 * user program by declaring a handler function with the same name and parameters listed here. The new control line states
\r
178 * are available in the \c ControlLineStates.HostToDevice value inside the CDC interface structure passed as a parameter, set as
\r
179 * a mask of \c CDC_CONTROL_LINE_OUT_* masks.
\r
181 * @param CDCInterfaceInfo : Pointer to a structure containing a CDC Class configuration and state.
\r
185 void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
\r
188 * @brief CDC class driver event for a send break request sent to the device from the host. This is generally used to separate
\r
189 * data or to indicate a special condition to the receiving device.
\r
191 * @param CDCInterfaceInfo : Pointer to a structure containing a CDC Class configuration and state.
\r
192 * @param Duration : Duration of the break that has been sent by the host, in milliseconds.
\r
195 void EVENT_CDC_Device_BreakSent(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo,
\r
196 const uint8_t Duration) ATTR_NON_NULL_PTR_ARG(1);
\r
199 * @brief Sends a given data buffer to the attached USB host, if connected. If a host is not connected when the function is
\r
200 * called, the string is discarded. Bytes will be queued for transmission to the host until either the endpoint bank
\r
201 * becomes full, or the @ref CDC_Device_Flush() function is called to flush the pending data to the host. This allows
\r
202 * for multiple bytes to be packed into a single endpoint packet, increasing data throughput.
\r
204 * \pre This function must only be called when the Device state machine is in the @ref DEVICE_STATE_Configured state or
\r
205 * the call will fail.
\r
207 * @param CDCInterfaceInfo : Pointer to a structure containing a CDC Class configuration and state.
\r
208 * @param Buffer : Pointer to a buffer containing the data to send to the device.
\r
209 * @param Length : Length of the data to send to the host.
\r
210 * @return A value from the @ref Endpoint_Stream_RW_ErrorCodes_t enum.
\r
212 uint8_t CDC_Device_SendData(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo,
\r
213 const char* const Buffer,
\r
214 const uint16_t Length) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
\r
217 * @brief Sends a given null terminated string to the attached USB host, if connected. If a host is not connected when
\r
218 * the function is called, the string is discarded. Bytes will be queued for transmission to the host until either
\r
219 * the endpoint bank becomes full, or the @ref CDC_Device_Flush() function is called to flush the pending data to
\r
220 * the host. This allows for multiple bytes to be packed into a single endpoint packet, increasing data throughput.
\r
222 * \pre This function must only be called when the Device state machine is in the @ref DEVICE_STATE_Configured state or
\r
223 * the call will fail.
\r
225 * @param CDCInterfaceInfo : Pointer to a structure containing a CDC Class configuration and state.
\r
226 * @param String : Pointer to the null terminated string to send to the host.
\r
227 * @return A value from the @ref Endpoint_Stream_RW_ErrorCodes_t enum.
\r
229 uint8_t CDC_Device_SendString(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo,
\r
230 const char* const String) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
\r
233 * @brief Sends a given byte to the attached USB host, if connected. If a host is not connected when the function is called, the
\r
234 * byte is discarded. Bytes will be queued for transmission to the host until either the endpoint bank becomes full, or the
\r
235 * @ref CDC_Device_Flush() function is called to flush the pending data to the host. This allows for multiple bytes to be
\r
236 * packed into a single endpoint packet, increasing data throughput.
\r
238 * \pre This function must only be called when the Device state machine is in the @ref DEVICE_STATE_Configured state or
\r
239 * the call will fail.
\r
241 * @param CDCInterfaceInfo : Pointer to a structure containing a CDC Class configuration and state.
\r
242 * @param Data : Byte of data to send to the host.
\r
243 * @return A value from the @ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
\r
245 uint8_t CDC_Device_SendByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo,
\r
246 const uint8_t Data) ATTR_NON_NULL_PTR_ARG(1);
\r
249 * @brief Determines the number of bytes received by the CDC interface from the host, waiting to be read. This indicates the number
\r
250 * of bytes in the OUT endpoint bank only, and thus the number of calls to @ref CDC_Device_ReceiveByte() which are guaranteed to
\r
251 * succeed immediately. If multiple bytes are to be received, they should be buffered by the user application, as the endpoint
\r
252 * bank will not be released back to the USB controller until all bytes are read.
\r
254 * \pre This function must only be called when the Device state machine is in the @ref DEVICE_STATE_Configured state or
\r
255 * the call will fail.
\r
257 * @param CDCInterfaceInfo : Pointer to a structure containing a CDC Class configuration and state.
\r
258 * @return Total number of buffered bytes received from the host.
\r
260 uint16_t CDC_Device_BytesReceived(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
\r
263 * @brief 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
\r
264 * returns a negative value. The @ref CDC_Device_BytesReceived() function may be queried in advance to determine how many
\r
265 * bytes are currently buffered in the CDC interface's data receive endpoint bank, and thus how many repeated calls to this
\r
266 * function which are guaranteed to succeed.
\r
268 * \pre This function must only be called when the Device state machine is in the @ref DEVICE_STATE_Configured state or
\r
269 * the call will fail.
\r
271 * @param CDCInterfaceInfo : Pointer to a structure containing a CDC Class configuration and state.
\r
272 * @return Next received byte from the host, or a negative value if no data received.
\r
274 int16_t CDC_Device_ReceiveByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
\r
277 * @brief Flushes any data waiting to be sent, ensuring that the send buffer is cleared.
\r
279 * \pre This function must only be called when the Device state machine is in the @ref DEVICE_STATE_Configured state or
\r
280 * the call will fail.
\r
282 * @param CDCInterfaceInfo : Pointer to a structure containing a CDC Class configuration and state.
\r
283 * @return A value from the @ref Endpoint_WaitUntilReady_ErrorCodes_t enum.
\r
285 uint8_t CDC_Device_Flush(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
\r
288 * @brief Sends a Serial Control Line State Change notification to the host. This should be called when the virtual serial
\r
289 * control lines (DCD, DSR, etc.) have changed states, or to give BREAK notifications to the host. Line states persist
\r
290 * until they are cleared via a second notification. This should be called each time the CDC class driver's
\r
291 * \c ControlLineStates.DeviceToHost value is updated to push the new states to the USB host.
\r
293 * \pre This function must only be called when the Device state machine is in the @ref DEVICE_STATE_Configured state or
\r
294 * the call will fail.
\r
296 * @param CDCInterfaceInfo : Pointer to a structure containing a CDC Class configuration and state.
\r
299 void CDC_Device_SendControlLineStateChange(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
\r
300 #if (!defined(__IAR_SYSTEMS_ICC__) || (_DLIB_FILE_DESCRIPTOR == 1))
\r
302 * @brief Creates a standard character stream for the given CDC Device instance so that it can be used with all the regular
\r
303 * functions in the standard <stdio.h> library that accept a \c FILE stream as a destination (e.g. \c fprintf()). The created
\r
304 * stream is bidirectional and can be used for both input and output functions.
\r
306 * Reading data from this stream is non-blocking, i.e. in most instances, complete strings cannot be read in by a single
\r
307 * fetch, as the endpoint will not be ready at some point in the transmission, aborting the transfer. However, this may
\r
308 * be used when the read data is processed byte-per-bye (via \c getc()) or when the user application will implement its own
\r
311 * @note The created stream can be given as stdout if desired to direct the standard output from all <stdio.h> functions
\r
312 * to the given CDC interface.
\r
315 * @note This function is not available on all microcontroller architectures.
\r
317 * @param CDCInterfaceInfo : Pointer to a structure containing a CDC Class configuration and state.
\r
318 * @param Stream : Pointer to a FILE structure where the created stream should be placed.
\r
321 void CDC_Device_CreateStream(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo,
\r
322 FILE* const Stream) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
\r
325 * @brief Identical to @ref CDC_Device_CreateStream(), except that reads are blocking until the calling stream function terminates
\r
326 * the transfer. While blocking, the USB and CDC service tasks are called repeatedly to maintain USB communications.
\r
328 * @note This function is not available on all microcontroller architectures.
\r
330 * @param CDCInterfaceInfo : Pointer to a structure containing a CDC Class configuration and state.
\r
331 * @param Stream : Pointer to a FILE structure where the created stream should be placed.
\r
334 void CDC_Device_CreateBlockingStream(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo,
\r
335 FILE* const Stream) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
\r
336 #endif /* IAR DLIB check */
\r
338 /* Private Interface - For use in library only: */
\r
339 #if !defined(__DOXYGEN__)
\r
340 /* Function Prototypes: */
\r
341 #if defined(__INCLUDE_FROM_CDC_DEVICE_C)
\r
342 #if defined(FDEV_SETUP_STREAM)
\r
343 static int CDC_Device_putchar(char c,
\r
344 FILE* Stream) ATTR_NON_NULL_PTR_ARG(2);
\r
345 static int CDC_Device_getchar(FILE* Stream) ATTR_NON_NULL_PTR_ARG(1);
\r
346 static int CDC_Device_getchar_Blocking(FILE* Stream) ATTR_NON_NULL_PTR_ARG(1);
\r
349 void CDC_Device_Event_Stub(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) ATTR_CONST;
\r
350 void CDC_Device_Event_Stub2(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, const uint8_t Duration) ATTR_CONST;
\r
352 PRAGMA_WEAK(EVENT_CDC_Device_LineEncodingChanged,CDC_Device_Event_Stub)
\r
353 void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
\r
354 ATTR_WEAK ATTR_NON_NULL_PTR_ARG(1) ATTR_ALIAS(CDC_Device_Event_Stub);
\r
355 PRAGMA_WEAK(EVENT_CDC_Device_ControLineStateChanged,CDC_Device_Event_Stub)
\r
356 void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
\r
357 ATTR_WEAK ATTR_NON_NULL_PTR_ARG(1) ATTR_ALIAS(CDC_Device_Event_Stub);
\r
358 PRAGMA_WEAK(EVENT_CDC_Device_BreakSent,CDC_Device_Event_Stub2)
\r
359 void EVENT_CDC_Device_BreakSent(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo,
\r
360 const uint8_t Duration) ATTR_WEAK ATTR_NON_NULL_PTR_ARG(1)
\r
361 ATTR_ALIAS(CDC_Device_Event_Stub2);
\r
366 /* Disable C linkage for C++ Compilers: */
\r
367 #if defined(__cplusplus)
\r