1 /* ----------------------------------------------------------------------------
\r
2 * ATMEL Microcontroller Software Support
\r
3 * ----------------------------------------------------------------------------
\r
4 * Copyright (c) 2008, Atmel Corporation
\r
6 * All rights reserved.
\r
8 * Redistribution and use in source and binary forms, with or without
\r
9 * modification, are permitted provided that the following conditions are met:
\r
11 * - Redistributions of source code must retain the above copyright notice,
\r
12 * this list of conditions and the disclaimer below.
\r
14 * Atmel's name may not be used to endorse or promote products derived from
\r
15 * this software without specific prior written permission.
\r
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
\r
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
\r
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
\r
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
\r
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
\r
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
27 * ----------------------------------------------------------------------------
\r
31 * Implementation of a single CDC serial port function for USB device.
\r
34 /** \addtogroup usbd_cdc
\r
38 /*------------------------------------------------------------------------------
\r
40 *------------------------------------------------------------------------------*/
\r
42 #include "CDCDSerial.h"
\r
44 #include <USBLib_Trace.h>
\r
45 #include <USBDDriver.h>
\r
46 #include <USBD_HAL.h>
\r
48 /*------------------------------------------------------------------------------
\r
50 *------------------------------------------------------------------------------*/
\r
52 /*------------------------------------------------------------------------------
\r
53 * Internal variables
\r
54 *------------------------------------------------------------------------------*/
\r
56 /** Serial Port instance list */
\r
57 static CDCDSerialPort cdcdSerial;
\r
59 /*------------------------------------------------------------------------------
\r
60 * Internal functions
\r
61 *------------------------------------------------------------------------------*/
\r
64 * USB CDC Serial Port Event Handler.
\r
65 * \param event Event code.
\r
66 * \param param Event parameter.
\r
68 static uint32_t CDCDSerial_EventHandler(uint32_t event,
\r
72 case CDCDSerialPortEvent_SETCONTROLLINESTATE:
\r
74 if (CDCDSerial_ControlLineStateChanged != NULL) {
\r
75 CDCDSerial_ControlLineStateChanged(
\r
76 (param & CDCControlLineState_DTR) > 0,
\r
77 (param & CDCControlLineState_RTS) > 0);
\r
81 case CDCDSerialPortEvent_SETLINECODING:
\r
83 if (NULL != CDCDSerial_LineCodingIsToChange) {
\r
84 event = CDCDSerial_LineCodingIsToChange(
\r
85 (CDCLineCoding*)param);
\r
86 if (event != USBRC_SUCCESS)
\r
92 return USBRC_SUCCESS;
\r
95 return USBRC_SUCCESS;
\r
98 /*------------------------------------------------------------------------------
\r
99 * Exported functions
\r
100 *------------------------------------------------------------------------------*/
\r
103 * Initializes the USB Device CDC serial driver & USBD Driver.
\r
104 * \param pUsbd Pointer to USBDDriver instance.
\r
105 * \param bInterfaceNb Interface number for the function.
\r
107 void CDCDSerial_Initialize(
\r
108 USBDDriver *pUsbd, uint8_t bInterfaceNb)
\r
110 CDCDSerialPort *pCdcd = &cdcdSerial;
\r
112 TRACE_INFO("CDCDSerial_Initialize\n\r");
\r
114 /* Initialize serial port function */
\r
115 CDCDSerialPort_Initialize(
\r
117 (CDCDSerialPortEventHandler)CDCDSerial_EventHandler,
\r
123 * Invoked whenever the device is changed by the
\r
125 * \pDescriptors Pointer to the descriptors for function configure.
\r
126 * \wLength Length of descriptors in number of bytes.
\r
128 void CDCDSerial_ConfigureFunction(USBGenericDescriptor *pDescriptors,
\r
131 CDCDSerialPort *pCdcd = &cdcdSerial;
\r
132 CDCDSerialPort_ParseInterfaces(pCdcd,
\r
133 (USBGenericDescriptor*)pDescriptors,
\r
138 * Handles CDC-specific SETUP requests. Should be called from a
\r
139 * re-implementation of USBDCallbacks_RequestReceived() method.
\r
140 * \param request Pointer to a USBGenericRequest instance.
\r
142 uint32_t CDCDSerial_RequestHandler(const USBGenericRequest *request)
\r
144 CDCDSerialPort * pCdcd = &cdcdSerial;
\r
146 TRACE_INFO_WP("Cdcf ");
\r
147 return CDCDSerialPort_RequestHandler(pCdcd, request);
\r
151 * Receives data from the host through the virtual COM port created by
\r
152 * the CDC device serial driver. This function behaves like USBD_Read.
\r
153 * \param data Pointer to the data buffer to put received data.
\r
154 * \param size Size of the data buffer in bytes.
\r
155 * \param callback Optional callback function to invoke when the transfer
\r
157 * \param argument Optional argument to the callback function.
\r
158 * \return USBD_STATUS_SUCCESS if the read operation has been started normally;
\r
159 * otherwise, the corresponding error code.
\r
161 uint32_t CDCDSerial_Read(void *data,
\r
163 TransferCallback callback,
\r
166 CDCDSerialPort * pCdcd = &cdcdSerial;
\r
167 return CDCDSerialPort_Read(pCdcd, data, size, callback, argument);
\r
171 * Sends a data buffer through the virtual COM port created by the CDC
\r
172 * device serial driver. This function behaves exactly like USBD_Write.
\r
173 * \param data Pointer to the data buffer to send.
\r
174 * \param size Size of the data buffer in bytes.
\r
175 * \param callback Optional callback function to invoke when the transfer
\r
177 * \param argument Optional argument to the callback function.
\r
178 * \return USBD_STATUS_SUCCESS if the read operation has been started normally;
\r
179 * otherwise, the corresponding error code.
\r
181 uint32_t CDCDSerial_Write(void *data,
\r
183 TransferCallback callback,
\r
186 CDCDSerialPort * pCdcd = &cdcdSerial;
\r
187 return CDCDSerialPort_Write(pCdcd, data, size, callback, argument);
\r
191 * Returns the current control line state of the RS-232 line.
\r
193 uint8_t CDCDSerial_GetControlLineState(void)
\r
195 CDCDSerialPort * pCdcd = &cdcdSerial;
\r
196 return CDCDSerialPort_GetControlLineState(pCdcd);
\r
200 * Copy current line coding settings to pointered space.
\r
201 * \param pLineCoding Pointer to CDCLineCoding instance.
\r
203 void CDCDSerial_GetLineCoding(CDCLineCoding* pLineCoding)
\r
205 CDCDSerialPort * pCdcd = &cdcdSerial;
\r
206 CDCDSerialPort_GetLineCoding(pCdcd, pLineCoding);
\r
210 * Returns the current status of the RS-232 line.
\r
212 uint16_t CDCDSerial_GetSerialState(void)
\r
214 CDCDSerialPort * pCdcd = &cdcdSerial;
\r
215 return CDCDSerialPort_GetSerialState(pCdcd);
\r
219 * Sets the current serial state of the device to the given value.
\r
220 * \param serialState New device state.
\r
222 void CDCDSerial_SetSerialState(uint16_t serialState)
\r
224 CDCDSerialPort * pCdcd = &cdcdSerial;
\r
225 CDCDSerialPort_SetSerialState(pCdcd, serialState);
\r