1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2012, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 /*
33  * ======== UsbCdc.h ========
34  */
35 #ifndef _UsbCdc_H_
36 #define _UsbCdc_H_
37 
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #endif
42 
43 
44 #define kUSBCDC_sendStarted         0x01
45 #define kUSBCDC_sendComplete        0x02
46 #define kUSBCDC_intfBusyError       0x03
47 #define kUSBCDC_receiveStarted      0x04
48 #define kUSBCDC_receiveCompleted    0x05
49 #define kUSBCDC_receiveInProgress   0x06
50 #define kUSBCDC_generalError        0x07
51 #define kUSBCDC_busNotAvailable     0x08
52 
53 struct _CdcWrite {
54     WORD nCdcBytesToSend;                       //holds counter of bytes to be sent
55     WORD nCdcBytesToSendLeft;                   //holds counter how many bytes is still to be sent
56     const BYTE* pUsbBufferToSend;               //holds the buffer with data to be sent
57     BYTE bCurrentBufferXY;                      //is 0 if current buffer to write data is X, or 1 if current buffer is Y
58     BYTE bZeroPacketSent;                       //= FALSE;
59     BYTE last_ByteSend;
60 };
61 
62 struct _CdcRead {
63     BYTE *pUserBuffer;                          //holds the current position of user's receiving buffer. If NULL- no receiving
64                                                 //operation started
65     BYTE *pCurrentEpPos;                        //current positon to read of received data from curent EP
66     WORD nBytesToReceive;                       //holds how many bytes was requested by receiveData() to receive
67     WORD nBytesToReceiveLeft;                   //holds how many bytes is still requested by receiveData() to receive
68     BYTE * pCT1;                                //holds current EPBCTxx register
69     BYTE * pCT2;                                //holds next EPBCTxx register
70     BYTE * pEP2;                                //holds addr of the next EP buffer
71     BYTE nBytesInEp;                            //how many received bytes still available in current EP
72     BYTE bCurrentBufferXY;                      //indicates which buffer is used by host to transmit data via OUT endpoint3
73 };
74 
75 
76 /*----------------------------------------------------------------------------
77  * These functions can be used in application
78  +----------------------------------------------------------------------------*/
79 
80 /*
81  * Sends data over interface intfNum, of size size and starting at address data.
82  * Returns:  kUSBCDC_sendStarted
83  *          kUSBCDC_sendComplete
84  *          kUSBCDC_intfBusyError
85  */
86 BYTE USBCDC_sendData (const BYTE* data, WORD size, BYTE intfNum);
87 
88 /*
89  * Receives data over interface intfNum, of size size, into memory starting at address data.
90  */
91 BYTE USBCDC_receiveData (BYTE* data, WORD size, BYTE intfNum);
92 
93 /*
94  * Aborts an active receive operation on interface intfNum.
95  * size: the number of bytes that were received and transferred
96  * to the data location established for this receive operation.
97  */
98 BYTE USBCDC_abortReceive (WORD* size, BYTE intfNum);
99 
100 
101 #define kUSBCDC_noDataWaiting 1 //returned by USBCDC_rejectData() if no data pending
102 
103 /*
104  * This function rejects payload data that has been received from the host.
105  */
106 BYTE USBCDC_rejectData (BYTE intfNum);
107 
108 /*
109  * Aborts an active send operation on interface intfNum.  Returns the number of bytes that were sent prior to the abort, in size.
110  */
111 BYTE USBCDC_abortSend (WORD* size, BYTE intfNum);
112 
113 
114 #define kUSBCDC_waitingForSend      0x01
115 #define kUSBCDC_waitingForReceive   0x02
116 #define kUSBCDC_dataWaiting         0x04
117 #define kUSBCDC_busNotAvailable     0x08
118 #define kUSB_allCdcEvents           0xFF
119 
120 /*
121  * This function indicates the status of the interface intfNum.
122  * If a send operation is active for this interface,
123  * the function also returns the number of bytes that have been transmitted to the host.
124  * If a receiver operation is active for this interface, the function also returns
125  * the number of bytes that have been received from the host and are waiting at the assigned address.
126  *
127  * returns kUSBCDC_waitingForSend (indicates that a call to USBCDC_SendData()
128  * has been made, for which data transfer has not been completed)
129  *
130  * returns kUSBCDC_waitingForReceive (indicates that a receive operation
131  * has been initiated, but not all data has yet been received)
132  *
133  * returns kUSBCDC_dataWaiting (indicates that data has been received
134  * from the host, waiting in the USB receive buffers)
135  */
136 BYTE USBCDC_intfStatus (BYTE intfNum, WORD* bytesSent, WORD* bytesReceived);
137 
138 /*
139  * Returns how many bytes are in the buffer are received and ready to be read.
140  */
141 BYTE USBCDC_bytesInUSBBuffer (BYTE intfNum);
142 
143 /*----------------------------------------------------------------------------
144  * Event-Handling routines
145  +----------------------------------------------------------------------------*/
146 
147 /*
148  * This event indicates that data has been received for interface intfNum, but no data receive operation is underway.
149  * returns TRUE to keep CPU awake
150  */
151 BYTE USBCDC_handleDataReceived (BYTE intfNum);
152 
153 /*
154  * This event indicates that a send operation on interface intfNum has just been completed.
155  * returns TRUE to keep CPU awake
156  */
157 BYTE USBCDC_handleSendCompleted (BYTE intfNum);
158 
159 /*
160  * This event indicates that a receive operation on interface intfNum has just been completed.
161  * returns TRUE to keep CPU awake
162  */
163 BYTE USBCDC_handleReceiveCompleted (BYTE intfNum);
164 
165 /*
166  * Toggle state variable for CTS in USB Stack
167  */
168 void USBCDC_setCTS(BYTE state);
169 
170 /*
171  * This event indicates that a SetLineCoding request was received from the host and new values
172  * for line coding paramters are available.
173  *
174  */
175 BYTE USBCDC_handleSetLineCoding (BYTE intfNum, ULONG lBaudrate);
176 
177 /*
178  * This event indicates that a SetControlLineState request was received from the host.
179  * Basically new RTS and DTR states have been sent. Bit 0 of lineState is DTR and Bit 1 is RTS.
180  *
181  */
182 BYTE USBCDC_handleSetControlLineState (BYTE intfNum, BYTE lineState);
183 
184 /*----------------------------------------------------------------------------
185  * These functions is to be used ONLY by USB stack, and not by application
186  +----------------------------------------------------------------------------*/
187 
188 /**
189  * Send a packet with the settings of the second uart back to the usb host
190  */
191 BYTE usbGetLineCoding(VOID);
192 
193 /**
194  * Prepare EP0 to receive a packet with the settings for the second uart
195  */
196 BYTE usbSetLineCoding(VOID);
197 
198 /**
199  * Function set or reset RTS
200  */
201 BYTE usbSetControlLineState(VOID);
202 
203 /**
204  * Readout the settings (send from usb host) for the second uart
205  */
206 BYTE Handler_SetLineCoding(VOID);
207 
208 /**
209  * sets up dma for CDC interface used as usb to uart bridge
210  */
211 BYTE USBCDC_setupDMA_Bridge();
212 /**
213  * sends data alread present in endpoint buffer
214  */
215 BYTE USBCDC_Bridge_sendData (WORD size, BYTE intfNum);
216 /**
217  * gets address of input endpoint X
218  */
219 BYTE *USBCDC_Bridge_getInEndpointBufferXAddr(BYTE intfNum);
220 /**
221  * gets address of input endpoint Y
222  */
223 BYTE *USBCDC_Bridge_getInEndpointBufferYAddr(BYTE intfNum);
224 /**
225  * gets address of output endpoint X
226  */
227 BYTE *USBCDC_Bridge_getOutEndpointBufferXAddr(BYTE intfNum);
228 /**
229  * gets address of output endpoint Y
230  */
231 BYTE *USBCDC_Bridge_getOutEndpointBufferYAddr(BYTE intfNum);
232 /**
233  * completes receive operation once data from endpoint has been transmitted thru uart
234  */
235 BYTE USBCDC_Bridge_completeReceiveData(BYTE intfNum);
236 
237 
238 #ifdef __cplusplus
239 }
240 #endif
241 #endif  //_UsbCdc_H_
242