Upgrade to v0.92
[hackover2013-badge-firmware.git] / core / usbcdc / cdcuser.c
1 /*----------------------------------------------------------------------------
2 * U S B - K e r n e l
3 *----------------------------------------------------------------------------
4 * Name: cdcuser.c
5 * Purpose: USB Communication Device Class User module
6 * Version: V1.10
7 *----------------------------------------------------------------------------
8 * This software is supplied "AS IS" without any warranties, express,
9 * implied or statutory, including but not limited to the implied
10 * warranties of fitness for purpose, satisfactory quality and
11 * noninfringement. Keil extends you a royalty-free right to reproduce
12 * and distribute executable files created using this software for use
13 * on NXP Semiconductors LPC microcontroller devices only. Nothing else
14 * gives you the right to use this software.
15 *
16 * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17 *---------------------------------------------------------------------------*/
18
19 #include "projectconfig.h"
20
21 #include "usb.h"
22 #include "usbhw.h"
23 #include "usbcfg.h"
24 #include "usbcore.h"
25 #include "cdc.h"
26 #include "cdcuser.h"
27 #include "cdc_buf.h"
28
29 // unsigned char BulkBufIn [64]; // Buffer to store USB IN packet
30 unsigned char BulkBufOut [64]; // Buffer to store USB OUT packet
31 unsigned char NotificationBuf [10];
32
33 CDC_LINE_CODING CDC_LineCoding = {CFG_USBCDC_BAUDRATE, 0, 0, 8};
34 unsigned short CDC_SerialState = 0x0000;
35 unsigned short CDC_DepInEmpty = 1; // Data IN EP is empty
36
37 /*----------------------------------------------------------------------------
38 We need a buffer for incoming data on USB port because USB receives
39 much faster than UART transmits
40 *---------------------------------------------------------------------------*/
41 /* Buffer masks */
42 #define CDC_BUF_SIZE (64) // Output buffer in bytes (power 2)
43 // large enough for file transfer
44 #define CDC_BUF_MASK (CDC_BUF_SIZE-1ul)
45
46 /* Buffer read / write macros */
47 #define CDC_BUF_RESET(cdcBuf) (cdcBuf.rdIdx = cdcBuf.wrIdx = 0)
48 #define CDC_BUF_WR(cdcBuf, dataIn) (cdcBuf.data[CDC_BUF_MASK & cdcBuf.wrIdx++] = (dataIn))
49 #define CDC_BUF_RD(cdcBuf) (cdcBuf.data[CDC_BUF_MASK & cdcBuf.rdIdx++])
50 #define CDC_BUF_EMPTY(cdcBuf) (cdcBuf.rdIdx == cdcBuf.wrIdx)
51 #define CDC_BUF_FULL(cdcBuf) (cdcBuf.rdIdx == cdcBuf.wrIdx+1)
52 #define CDC_BUF_COUNT(cdcBuf) (CDC_BUF_MASK & (cdcBuf.wrIdx - cdcBuf.rdIdx))
53
54
55 // CDC output buffer
56 typedef struct __CDC_BUF_T
57 {
58 unsigned char data[CDC_BUF_SIZE];
59 volatile unsigned int wrIdx;
60 volatile unsigned int rdIdx;
61 } CDC_BUF_T;
62
63 CDC_BUF_T CDC_OutBuf; // buffer for all CDC Out data
64
65 /*----------------------------------------------------------------------------
66 read data from CDC_OutBuf
67 *---------------------------------------------------------------------------*/
68 int CDC_RdOutBuf (char *buffer, const int *length)
69 {
70 int bytesToRead, bytesRead;
71
72 /* Read *length bytes, block if *bytes are not avaialable */
73 bytesToRead = *length;
74 bytesToRead = (bytesToRead < (*length)) ? bytesToRead : (*length);
75 bytesRead = bytesToRead;
76
77
78 // ... add code to check for underrun
79
80 while (bytesToRead--) {
81 *buffer++ = CDC_BUF_RD(CDC_OutBuf);
82 }
83 return (bytesRead);
84 }
85
86 /*----------------------------------------------------------------------------
87 write data to CDC_OutBuf
88 *---------------------------------------------------------------------------*/
89 int CDC_WrOutBuf (const char *buffer, int *length)
90 {
91 int bytesToWrite, bytesWritten;
92
93 // Write *length bytes
94 bytesToWrite = *length;
95 bytesWritten = bytesToWrite;
96
97
98 // ... add code to check for overwrite
99
100 while (bytesToWrite) {
101 CDC_BUF_WR(CDC_OutBuf, *buffer++); // Copy Data to buffer
102 bytesToWrite--;
103 }
104
105 return (bytesWritten);
106 }
107
108 /*----------------------------------------------------------------------------
109 check if character(s) are available at CDC_OutBuf
110 *---------------------------------------------------------------------------*/
111 int CDC_OutBufAvailChar (int *availChar)
112 {
113 *availChar = CDC_BUF_COUNT(CDC_OutBuf);
114
115 return (0);
116 }
117 /* end Buffer handling */
118
119
120 /*----------------------------------------------------------------------------
121 CDC Initialisation
122 Initializes the data structures and serial port
123 Parameters: None
124 Return Value: None
125 *---------------------------------------------------------------------------*/
126 void CDC_Init (void)
127 {
128 CDC_DepInEmpty = 1;
129 CDC_SerialState = CDC_GetSerialState();
130
131 CDC_BUF_RESET(CDC_OutBuf);
132
133 // Initialise the CDC buffer. This is required to buffer outgoing
134 // data (MCU to PC) since data can only be sent 64 bytes per frame
135 // with at least 1ms between frames. To see how the buffer is used,
136 // see 'puts' in systeminit.c
137 cdcBufferInit();
138 }
139
140
141 /*----------------------------------------------------------------------------
142 CDC SendEncapsulatedCommand Request Callback
143 Called automatically on CDC SEND_ENCAPSULATED_COMMAND Request
144 Parameters: None (global SetupPacket and EP0Buf)
145 Return Value: TRUE - Success, FALSE - Error
146 *---------------------------------------------------------------------------*/
147 uint32_t CDC_SendEncapsulatedCommand (void)
148 {
149 return (TRUE);
150 }
151
152
153 /*----------------------------------------------------------------------------
154 CDC GetEncapsulatedResponse Request Callback
155 Called automatically on CDC Get_ENCAPSULATED_RESPONSE Request
156 Parameters: None (global SetupPacket and EP0Buf)
157 Return Value: TRUE - Success, FALSE - Error
158 *---------------------------------------------------------------------------*/
159 uint32_t CDC_GetEncapsulatedResponse (void)
160 {
161 /* ... add code to handle request */
162 return (TRUE);
163 }
164
165
166 /*----------------------------------------------------------------------------
167 CDC SetCommFeature Request Callback
168 Called automatically on CDC Set_COMM_FATURE Request
169 Parameters: FeatureSelector
170 Return Value: TRUE - Success, FALSE - Error
171 *---------------------------------------------------------------------------*/
172 uint32_t CDC_SetCommFeature (unsigned short wFeatureSelector)
173 {
174 /* ... add code to handle request */
175 return (TRUE);
176 }
177
178
179 /*----------------------------------------------------------------------------
180 CDC GetCommFeature Request Callback
181 Called automatically on CDC Get_COMM_FATURE Request
182 Parameters: FeatureSelector
183 Return Value: TRUE - Success, FALSE - Error
184 *---------------------------------------------------------------------------*/
185 uint32_t CDC_GetCommFeature (unsigned short wFeatureSelector)
186 {
187 /* ... add code to handle request */
188 return (TRUE);
189 }
190
191
192 /*----------------------------------------------------------------------------
193 CDC ClearCommFeature Request Callback
194 Called automatically on CDC CLEAR_COMM_FATURE Request
195 Parameters: FeatureSelector
196 Return Value: TRUE - Success, FALSE - Error
197 *---------------------------------------------------------------------------*/
198 uint32_t CDC_ClearCommFeature (unsigned short wFeatureSelector)
199 {
200 /* ... add code to handle request */
201 return (TRUE);
202 }
203
204
205 /*----------------------------------------------------------------------------
206 CDC SetLineCoding Request Callback
207 Called automatically on CDC SET_LINE_CODING Request
208 Parameters: none (global SetupPacket and EP0Buf)
209 Return Value: TRUE - Success, FALSE - Error
210 *---------------------------------------------------------------------------*/
211 uint32_t CDC_SetLineCoding (void)
212 {
213 CDC_LineCoding.dwDTERate = (EP0Buf[0] << 0)
214 | (EP0Buf[1] << 8)
215 | (EP0Buf[2] << 16)
216 | (EP0Buf[3] << 24);
217 CDC_LineCoding.bCharFormat = EP0Buf[4];
218 CDC_LineCoding.bParityType = EP0Buf[5];
219 CDC_LineCoding.bDataBits = EP0Buf[6];
220
221 return (TRUE);
222 }
223
224
225 /*----------------------------------------------------------------------------
226 CDC GetLineCoding Request Callback
227 Called automatically on CDC GET_LINE_CODING Request
228 Parameters: None (global SetupPacket and EP0Buf)
229 Return Value: TRUE - Success, FALSE - Error
230 *---------------------------------------------------------------------------*/
231 uint32_t CDC_GetLineCoding (void)
232 {
233 EP0Buf[0] = (CDC_LineCoding.dwDTERate >> 0) & 0xFF;
234 EP0Buf[1] = (CDC_LineCoding.dwDTERate >> 8) & 0xFF;
235 EP0Buf[2] = (CDC_LineCoding.dwDTERate >> 16) & 0xFF;
236 EP0Buf[3] = (CDC_LineCoding.dwDTERate >> 24) & 0xFF;
237 EP0Buf[4] = CDC_LineCoding.bCharFormat;
238 EP0Buf[5] = CDC_LineCoding.bParityType;
239 EP0Buf[6] = CDC_LineCoding.bDataBits;
240
241 return (TRUE);
242 }
243
244
245 /*----------------------------------------------------------------------------
246 CDC SetControlLineState Request Callback
247 Called automatically on CDC SET_CONTROL_LINE_STATE Request
248 Parameters: ControlSignalBitmap
249 Return Value: TRUE - Success, FALSE - Error
250 *---------------------------------------------------------------------------*/
251 uint32_t CDC_SetControlLineState (unsigned short wControlSignalBitmap) {
252
253 /* ... add code to handle request */
254 return (TRUE);
255 }
256
257
258 /*----------------------------------------------------------------------------
259 CDC SendBreak Request Callback
260 Called automatically on CDC Set_COMM_FATURE Request
261 Parameters: 0xFFFF start of Break
262 0x0000 stop of Break
263 0x#### Duration of Break
264 Return Value: TRUE - Success, FALSE - Error
265 *---------------------------------------------------------------------------*/
266 uint32_t CDC_SendBreak (unsigned short wDurationOfBreak) {
267
268 /* ... add code to handle request */
269 return (TRUE);
270 }
271
272
273 /*----------------------------------------------------------------------------
274 CDC_BulkIn call on DataIn Request
275 Parameters: none
276 Return Value: none
277 *---------------------------------------------------------------------------*/
278 void CDC_BulkIn(void)
279 {
280 // int numBytesRead, numBytesAvail;
281 //
282 // // ToDo: Modify BulkIn to send incoming data to USB
283 //
284 // ser_AvailChar (&numBytesAvail);
285 //
286 // // ... add code to check for overwrite
287 //
288 // numBytesRead = ser_Read ((char *)&BulkBufIn[0], &numBytesAvail);
289 //
290 // // send over USB
291 // if (numBytesRead > 0) {
292 // USB_WriteEP (CDC_DEP_IN, &BulkBufIn[0], numBytesRead);
293 // }
294 // else {
295 // CDC_DepInEmpty = 1;
296 // }
297 //
298 //
299 }
300
301
302 /*----------------------------------------------------------------------------
303 CDC_BulkOut call on DataOut Request
304 Parameters: none
305 Return Value: none
306 *---------------------------------------------------------------------------*/
307 void CDC_BulkOut(void)
308 {
309 int numBytesRead;
310
311 // get data from USB into intermediate buffer
312 numBytesRead = USB_ReadEP(CDC_DEP_OUT, &BulkBufOut[0]);
313
314 // ... add code to check for overwrite
315
316 // store data in a buffer to transmit it over serial interface
317 CDC_WrOutBuf ((char *)&BulkBufOut[0], &numBytesRead);
318 }
319
320
321 /*----------------------------------------------------------------------------
322 Get the SERIAL_STATE as defined in usbcdc11.pdf, 6.3.5, Table 69.
323 Parameters: none
324 Return Value: SerialState as defined in usbcdc11.pdf
325 *---------------------------------------------------------------------------*/
326 unsigned short CDC_GetSerialState (void)
327 {
328 CDC_SerialState = 0;
329
330 return (CDC_SerialState);
331 }
332
333
334 /*----------------------------------------------------------------------------
335 Send the SERIAL_STATE notification as defined in usbcdc11.pdf, 6.3.5.
336 *---------------------------------------------------------------------------*/
337 void CDC_NotificationIn (void)
338 {
339 NotificationBuf[0] = 0xA1; // bmRequestType
340 NotificationBuf[1] = CDC_NOTIFICATION_SERIAL_STATE; // bNotification (SERIAL_STATE)
341 NotificationBuf[2] = 0x00; // wValue
342 NotificationBuf[3] = 0x00;
343 NotificationBuf[4] = 0x00; // wIndex (Interface #, LSB first)
344 NotificationBuf[5] = 0x00;
345 NotificationBuf[6] = 0x02; // wLength (Data length = 2 bytes, LSB first)
346 NotificationBuf[7] = 0x00;
347 NotificationBuf[8] = (CDC_SerialState >> 0) & 0xFF; // UART State Bitmap (16bits, LSB first)
348 NotificationBuf[9] = (CDC_SerialState >> 8) & 0xFF;
349
350 USB_WriteEP (CDC_CEP_IN, &NotificationBuf[0], 10); // send notification
351 }
This page took 0.070152 seconds and 5 git commands to generate.