SAMV71 Xplained Ultra Software Package 1.5

CDCDSerial.c

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