00001 /* ---------------------------------------------------------------------------- 00002 * ATMEL Microcontroller Software Support 00003 * ---------------------------------------------------------------------------- 00004 * Copyright (c) 2008, Atmel Corporation 00005 * 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions are met: 00010 * 00011 * - Redistributions of source code must retain the above copyright notice, 00012 * this list of conditions and the disclaimer below. 00013 * 00014 * Atmel's name may not be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR 00018 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00019 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 00020 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00022 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 00023 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00024 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00025 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00026 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 * ---------------------------------------------------------------------------- 00028 */ 00029 00030 /**\file 00031 * Implementation of a single CDC serial port function for USB device. 00032 */ 00033 00034 /** \addtogroup usbd_cdc 00035 *@{ 00036 */ 00037 00038 /*------------------------------------------------------------------------------ 00039 * Headers 00040 *------------------------------------------------------------------------------*/ 00041 00042 #include "CDCDSerial.h" 00043 00044 #include <USBLib_Trace.h> 00045 #include <USBDDriver.h> 00046 #include <USBD_HAL.h> 00047 00048 /*------------------------------------------------------------------------------ 00049 * Types 00050 *------------------------------------------------------------------------------*/ 00051 00052 /*------------------------------------------------------------------------------ 00053 * Internal variables 00054 *------------------------------------------------------------------------------*/ 00055 00056 /** Serial Port instance list */ 00057 static CDCDSerialPort cdcdSerial; 00058 00059 /*------------------------------------------------------------------------------ 00060 * Internal functions 00061 *------------------------------------------------------------------------------*/ 00062 00063 /** 00064 * USB CDC Serial Port Event Handler. 00065 * \param event Event code. 00066 * \param param Event parameter. 00067 */ 00068 static uint32_t CDCDSerial_EventHandler(uint32_t event, 00069 uint32_t param) 00070 { 00071 switch (event) { 00072 case CDCDSerialPortEvent_SETCONTROLLINESTATE: 00073 { 00074 if (CDCDSerial_ControlLineStateChanged != NULL) { 00075 CDCDSerial_ControlLineStateChanged( 00076 (param & CDCControlLineState_DTR) > 0, 00077 (param & CDCControlLineState_RTS) > 0); 00078 } 00079 } 00080 break; 00081 case CDCDSerialPortEvent_SETLINECODING: 00082 { 00083 if (NULL != CDCDSerial_LineCodingIsToChange) { 00084 event = CDCDSerial_LineCodingIsToChange( 00085 (CDCLineCoding*)param); 00086 if (event != USBRC_SUCCESS) 00087 return event; 00088 } 00089 } 00090 break; 00091 default: 00092 return USBRC_SUCCESS; 00093 } 00094 00095 return USBRC_SUCCESS; 00096 } 00097 00098 /*------------------------------------------------------------------------------ 00099 * Exported functions 00100 *------------------------------------------------------------------------------*/ 00101 00102 /** 00103 * Initializes the USB Device CDC serial driver & USBD Driver. 00104 * \param pUsbd Pointer to USBDDriver instance. 00105 * \param bInterfaceNb Interface number for the function. 00106 */ 00107 void CDCDSerial_Initialize( 00108 USBDDriver *pUsbd, uint8_t bInterfaceNb) 00109 { 00110 CDCDSerialPort *pCdcd = &cdcdSerial; 00111 00112 TRACE_INFO("CDCDSerial_Initialize\n\r"); 00113 00114 /* Initialize serial port function */ 00115 CDCDSerialPort_Initialize( 00116 pCdcd, pUsbd, 00117 (CDCDSerialPortEventHandler)CDCDSerial_EventHandler, 00118 0, 00119 bInterfaceNb, 2); 00120 } 00121 00122 /** 00123 * Invoked whenever the device is changed by the 00124 * host. 00125 * \pDescriptors Pointer to the descriptors for function configure. 00126 * \wLength Length of descriptors in number of bytes. 00127 */ 00128 void CDCDSerial_ConfigureFunction(USBGenericDescriptor *pDescriptors, 00129 uint16_t wLength) 00130 { 00131 CDCDSerialPort *pCdcd = &cdcdSerial; 00132 CDCDSerialPort_ParseInterfaces(pCdcd, 00133 (USBGenericDescriptor*)pDescriptors, 00134 wLength); 00135 } 00136 00137 /** 00138 * Handles CDC-specific SETUP requests. Should be called from a 00139 * re-implementation of USBDCallbacks_RequestReceived() method. 00140 * \param request Pointer to a USBGenericRequest instance. 00141 */ 00142 uint32_t CDCDSerial_RequestHandler(const USBGenericRequest *request) 00143 { 00144 CDCDSerialPort * pCdcd = &cdcdSerial; 00145 00146 TRACE_INFO_WP("Cdcf "); 00147 return CDCDSerialPort_RequestHandler(pCdcd, request); 00148 } 00149 00150 /** 00151 * Receives data from the host through the virtual COM port created by 00152 * the CDC device serial driver. This function behaves like USBD_Read. 00153 * \param data Pointer to the data buffer to put received data. 00154 * \param size Size of the data buffer in bytes. 00155 * \param callback Optional callback function to invoke when the transfer 00156 * finishes. 00157 * \param argument Optional argument to the callback function. 00158 * \return USBD_STATUS_SUCCESS if the read operation has been started normally; 00159 * otherwise, the corresponding error code. 00160 */ 00161 uint32_t CDCDSerial_Read(void *data, 00162 uint32_t size, 00163 TransferCallback callback, 00164 void *argument) 00165 { 00166 CDCDSerialPort * pCdcd = &cdcdSerial; 00167 return CDCDSerialPort_Read(pCdcd, data, size, callback, argument); 00168 } 00169 00170 /** 00171 * Sends a data buffer through the virtual COM port created by the CDC 00172 * device serial driver. This function behaves exactly like USBD_Write. 00173 * \param data Pointer to the data buffer to send. 00174 * \param size Size of the data buffer in bytes. 00175 * \param callback Optional callback function to invoke when the transfer 00176 * finishes. 00177 * \param argument Optional argument to the callback function. 00178 * \return USBD_STATUS_SUCCESS if the read operation has been started normally; 00179 * otherwise, the corresponding error code. 00180 */ 00181 uint32_t CDCDSerial_Write(void *data, 00182 uint32_t size, 00183 TransferCallback callback, 00184 void *argument) 00185 { 00186 CDCDSerialPort * pCdcd = &cdcdSerial; 00187 return CDCDSerialPort_Write(pCdcd, data, size, callback, argument); 00188 } 00189 00190 /** 00191 * Returns the current control line state of the RS-232 line. 00192 */ 00193 uint8_t CDCDSerial_GetControlLineState(void) 00194 { 00195 CDCDSerialPort * pCdcd = &cdcdSerial; 00196 return CDCDSerialPort_GetControlLineState(pCdcd); 00197 } 00198 00199 /** 00200 * Copy current line coding settings to pointered space. 00201 * \param pLineCoding Pointer to CDCLineCoding instance. 00202 */ 00203 void CDCDSerial_GetLineCoding(CDCLineCoding* pLineCoding) 00204 { 00205 CDCDSerialPort * pCdcd = &cdcdSerial; 00206 CDCDSerialPort_GetLineCoding(pCdcd, pLineCoding); 00207 } 00208 00209 /** 00210 * Returns the current status of the RS-232 line. 00211 */ 00212 uint16_t CDCDSerial_GetSerialState(void) 00213 { 00214 CDCDSerialPort * pCdcd = &cdcdSerial; 00215 return CDCDSerialPort_GetSerialState(pCdcd); 00216 } 00217 00218 /** 00219 * Sets the current serial state of the device to the given value. 00220 * \param serialState New device state. 00221 */ 00222 void CDCDSerial_SetSerialState(uint16_t serialState) 00223 { 00224 CDCDSerialPort * pCdcd = &cdcdSerial; 00225 CDCDSerialPort_SetSerialState(pCdcd, serialState); 00226 } 00227 00228 /**@}*/