]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D3x_Xplained_IAR/AtmelFiles/usb/device/cdc-serial/CDCDSerial.c
SAMA5D3 demo: Add CDC driver code and use CDC to create a simple command console.
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D3x_Xplained_IAR / AtmelFiles / usb / device / cdc-serial / CDCDSerial.c
1 /* ----------------------------------------------------------------------------\r
2  *         ATMEL Microcontroller Software Support\r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2008, Atmel Corporation\r
5  *\r
6  * All rights reserved.\r
7  *\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
10  *\r
11  * - Redistributions of source code must retain the above copyright notice,\r
12  * this list of conditions and the disclaimer below.\r
13  *\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
16  *\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
28  */\r
29 \r
30 /**\file\r
31  * Implementation of a single CDC serial port function for USB device.\r
32  */\r
33 \r
34 /** \addtogroup usbd_cdc\r
35  *@{\r
36  */\r
37 \r
38 /*------------------------------------------------------------------------------\r
39  *         Headers\r
40  *------------------------------------------------------------------------------*/\r
41 \r
42 #include "CDCDSerial.h"\r
43 \r
44 #include <USBLib_Trace.h>\r
45 #include <USBDDriver.h>\r
46 #include <USBD_HAL.h>\r
47 \r
48 /*------------------------------------------------------------------------------\r
49  *         Types\r
50  *------------------------------------------------------------------------------*/\r
51 \r
52 /*------------------------------------------------------------------------------\r
53  *         Internal variables\r
54  *------------------------------------------------------------------------------*/\r
55 \r
56 /** Serial Port instance list */\r
57 static CDCDSerialPort cdcdSerial;\r
58 \r
59 /*------------------------------------------------------------------------------\r
60  *         Internal functions\r
61  *------------------------------------------------------------------------------*/\r
62 \r
63 /**\r
64  * USB CDC Serial Port Event Handler.\r
65  * \param event Event code.\r
66  * \param param Event parameter.\r
67  */\r
68 static uint32_t CDCDSerial_EventHandler(uint32_t event,\r
69                                         uint32_t param)\r
70 {\r
71     switch (event) {\r
72     case CDCDSerialPortEvent_SETCONTROLLINESTATE:\r
73         {\r
74             if (CDCDSerial_ControlLineStateChanged != NULL) {\r
75                 CDCDSerial_ControlLineStateChanged(\r
76                     (param & CDCControlLineState_DTR) > 0,\r
77                     (param & CDCControlLineState_RTS) > 0);\r
78             }\r
79         }\r
80         break;\r
81     case CDCDSerialPortEvent_SETLINECODING:\r
82         {\r
83             if (NULL != CDCDSerial_LineCodingIsToChange) {\r
84                 event = CDCDSerial_LineCodingIsToChange(\r
85                                         (CDCLineCoding*)param);\r
86                 if (event != USBRC_SUCCESS)\r
87                     return event;\r
88             }\r
89         }\r
90         break;\r
91     default:\r
92         return USBRC_SUCCESS;\r
93     }\r
94 \r
95     return USBRC_SUCCESS;\r
96 }\r
97 \r
98 /*------------------------------------------------------------------------------\r
99  *         Exported functions\r
100  *------------------------------------------------------------------------------*/\r
101 \r
102 /**\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
106  */\r
107 void CDCDSerial_Initialize(\r
108     USBDDriver *pUsbd, uint8_t bInterfaceNb)\r
109 {\r
110     CDCDSerialPort *pCdcd = &cdcdSerial;\r
111 \r
112     TRACE_INFO("CDCDSerial_Initialize\n\r");\r
113 \r
114     /* Initialize serial port function */\r
115     CDCDSerialPort_Initialize(\r
116                       pCdcd, pUsbd,\r
117                       (CDCDSerialPortEventHandler)CDCDSerial_EventHandler,\r
118                       0,\r
119                       bInterfaceNb, 2);\r
120 }\r
121 \r
122 /**\r
123  * Invoked whenever the device is changed by the\r
124  * host.\r
125  * \pDescriptors Pointer to the descriptors for function configure.\r
126  * \wLength      Length of descriptors in number of bytes.\r
127  */\r
128 void CDCDSerial_ConfigureFunction(USBGenericDescriptor *pDescriptors,\r
129                                   uint16_t wLength)\r
130 {\r
131     CDCDSerialPort *pCdcd = &cdcdSerial;\r
132     CDCDSerialPort_ParseInterfaces(pCdcd,\r
133                                    (USBGenericDescriptor*)pDescriptors,\r
134                                    wLength);\r
135 }\r
136 \r
137 /**\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
141  */\r
142 uint32_t CDCDSerial_RequestHandler(const USBGenericRequest *request)\r
143 {\r
144     CDCDSerialPort * pCdcd = &cdcdSerial;\r
145 \r
146     TRACE_INFO_WP("Cdcf ");\r
147     return CDCDSerialPort_RequestHandler(pCdcd, request);\r
148 }\r
149 \r
150 /**\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
156  *                 finishes.\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
160  */\r
161 uint32_t CDCDSerial_Read(void *data,\r
162                          uint32_t size,\r
163                          TransferCallback callback,\r
164                          void *argument)\r
165 {\r
166     CDCDSerialPort * pCdcd = &cdcdSerial;\r
167     return CDCDSerialPort_Read(pCdcd, data, size, callback, argument);\r
168 }\r
169 \r
170 /**\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
176  *                 finishes.\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
180  */\r
181 uint32_t CDCDSerial_Write(void *data,\r
182                           uint32_t size,\r
183                           TransferCallback callback,\r
184                           void *argument)\r
185 {\r
186     CDCDSerialPort * pCdcd = &cdcdSerial;\r
187     return CDCDSerialPort_Write(pCdcd, data, size, callback, argument);\r
188 }\r
189 \r
190 /**\r
191  * Returns the current control line state of the RS-232 line.\r
192  */\r
193 uint8_t CDCDSerial_GetControlLineState(void)\r
194 {\r
195     CDCDSerialPort * pCdcd = &cdcdSerial;\r
196     return CDCDSerialPort_GetControlLineState(pCdcd);\r
197 }\r
198 \r
199 /**\r
200  * Copy current line coding settings to pointered space.\r
201  * \param pLineCoding Pointer to CDCLineCoding instance.\r
202  */\r
203 void CDCDSerial_GetLineCoding(CDCLineCoding* pLineCoding)\r
204 {\r
205     CDCDSerialPort * pCdcd = &cdcdSerial;\r
206     CDCDSerialPort_GetLineCoding(pCdcd, pLineCoding);\r
207 }\r
208 \r
209 /**\r
210  * Returns the current status of the RS-232 line.\r
211  */\r
212 uint16_t CDCDSerial_GetSerialState(void)\r
213 {\r
214     CDCDSerialPort * pCdcd = &cdcdSerial;\r
215     return CDCDSerialPort_GetSerialState(pCdcd);\r
216 }\r
217 \r
218 /**\r
219  * Sets the current serial state of the device to the given value.\r
220  * \param serialState  New device state.\r
221  */\r
222 void CDCDSerial_SetSerialState(uint16_t serialState)\r
223 {\r
224     CDCDSerialPort * pCdcd = &cdcdSerial;\r
225     CDCDSerialPort_SetSerialState(pCdcd, serialState);\r
226 }\r
227 \r
228 /**@}*/\r