These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!
cdcuser.c
00001 /*---------------------------------------------------------------------------- 00002 * U S B - K e r n e l 00003 *---------------------------------------------------------------------------- 00004 * Name: cdcuser.c 00005 * Purpose: USB Communication Device Class User module 00006 * Version: V1.10 00007 *---------------------------------------------------------------------------- 00008 * This software is supplied "AS IS" without any warranties, express, 00009 * implied or statutory, including but not limited to the implied 00010 * warranties of fitness for purpose, satisfactory quality and 00011 * noninfringement. Keil extends you a royalty-free right to reproduce 00012 * and distribute executable files created using this software for use 00013 * on NXP Semiconductors LPC microcontroller devices only. Nothing else 00014 * gives you the right to use this software. 00015 * 00016 * Copyright (c) 2009 Keil - An ARM Company. All rights reserved. 00017 *---------------------------------------------------------------------------*/ 00018 00019 #include "lpc_types.h" 00020 00021 #include "usb.h" 00022 #include "usbhw.h" 00023 #include "usbcfg.h" 00024 #include "usbcore.h" 00025 #include "cdc.h" 00026 #include "cdcuser.h" 00027 #include "serial.h" 00028 00029 00030 unsigned char BulkBufIn [USB_CDC_BUFSIZE]; // Buffer to store USB IN packet 00031 unsigned char BulkBufOut [USB_CDC_BUFSIZE]; // Buffer to store USB OUT packet 00032 unsigned char NotificationBuf [10]; 00033 00034 CDC_LINE_CODING CDC_LineCoding = {9600, 0, 0, 8}; 00035 unsigned short CDC_SerialState = 0x0000; 00036 unsigned short CDC_DepInEmpty = 1; // Data IN EP is empty 00037 00038 /*---------------------------------------------------------------------------- 00039 We need a buffer for incomming data on USB port because USB receives 00040 much faster than UART transmits 00041 *---------------------------------------------------------------------------*/ 00042 /* Buffer masks */ 00043 #define CDC_BUF_SIZE (64) // Output buffer in bytes (power 2) 00044 // large enough for file transfer 00045 #define CDC_BUF_MASK (CDC_BUF_SIZE-1ul) 00046 00047 /* Buffer read / write macros */ 00048 #define CDC_BUF_RESET(cdcBuf) (cdcBuf.rdIdx = cdcBuf.wrIdx = 0) 00049 #define CDC_BUF_WR(cdcBuf, dataIn) (cdcBuf.data[CDC_BUF_MASK & cdcBuf.wrIdx++] = (dataIn)) 00050 #define CDC_BUF_RD(cdcBuf) (cdcBuf.data[CDC_BUF_MASK & cdcBuf.rdIdx++]) 00051 #define CDC_BUF_EMPTY(cdcBuf) (cdcBuf.rdIdx == cdcBuf.wrIdx) 00052 #define CDC_BUF_FULL(cdcBuf) (cdcBuf.rdIdx == cdcBuf.wrIdx+1) 00053 #define CDC_BUF_COUNT(cdcBuf) (CDC_BUF_MASK & (cdcBuf.wrIdx - cdcBuf.rdIdx)) 00054 00055 00056 // CDC output buffer 00057 typedef struct __CDC_BUF_T { 00058 unsigned char data[CDC_BUF_SIZE]; 00059 unsigned int wrIdx; 00060 unsigned int rdIdx; 00061 } CDC_BUF_T; 00062 00063 CDC_BUF_T CDC_OutBuf; // buffer for all CDC Out data 00064 00065 /*---------------------------------------------------------------------------- 00066 read data from CDC_OutBuf 00067 *---------------------------------------------------------------------------*/ 00068 int CDC_RdOutBuf (char *buffer, const int *length) { 00069 int bytesToRead, bytesRead; 00070 00071 /* Read *length bytes, block if *bytes are not avaialable */ 00072 bytesToRead = *length; 00073 bytesToRead = (bytesToRead < (*length)) ? bytesToRead : (*length); 00074 bytesRead = bytesToRead; 00075 00076 00077 // ... add code to check for underrun 00078 00079 while (bytesToRead--) { 00080 *buffer++ = CDC_BUF_RD(CDC_OutBuf); 00081 } 00082 return (bytesRead); 00083 } 00084 00085 /*---------------------------------------------------------------------------- 00086 write data to CDC_OutBuf 00087 *---------------------------------------------------------------------------*/ 00088 int CDC_WrOutBuf (const char *buffer, int *length) { 00089 int bytesToWrite, bytesWritten; 00090 00091 // Write *length bytes 00092 bytesToWrite = *length; 00093 bytesWritten = bytesToWrite; 00094 00095 00096 // ... add code to check for overwrite 00097 00098 while (bytesToWrite) { 00099 CDC_BUF_WR(CDC_OutBuf, *buffer++); // Copy Data to buffer 00100 bytesToWrite--; 00101 } 00102 00103 return (bytesWritten); 00104 } 00105 00106 /*---------------------------------------------------------------------------- 00107 check if character(s) are available at CDC_OutBuf 00108 *---------------------------------------------------------------------------*/ 00109 int CDC_OutBufAvailChar (int *availChar) { 00110 00111 *availChar = CDC_BUF_COUNT(CDC_OutBuf); 00112 00113 return (0); 00114 } 00115 /* end Buffer handling */ 00116 00117 00118 /*---------------------------------------------------------------------------- 00119 CDC Initialisation 00120 Initializes the data structures and serial port 00121 Parameters: None 00122 Return Value: None 00123 *---------------------------------------------------------------------------*/ 00124 void CDC_Init (char portNum ) { 00125 00126 if ( portNum == 0 ) 00127 { 00128 ser_OpenPort (0); 00129 ser_InitPort0 (CDC_LineCoding.dwDTERate, 00130 CDC_LineCoding.bDataBits, 00131 CDC_LineCoding.bParityType, 00132 CDC_LineCoding.bCharFormat); 00133 } 00134 else 00135 { 00136 ser_OpenPort (1); 00137 ser_InitPort1 (CDC_LineCoding.dwDTERate, 00138 CDC_LineCoding.bDataBits, 00139 CDC_LineCoding.bParityType, 00140 CDC_LineCoding.bCharFormat); 00141 } 00142 CDC_DepInEmpty = 1; 00143 CDC_SerialState = CDC_GetSerialState(); 00144 00145 CDC_BUF_RESET(CDC_OutBuf); 00146 } 00147 00148 00149 /*---------------------------------------------------------------------------- 00150 CDC SendEncapsulatedCommand Request Callback 00151 Called automatically on CDC SEND_ENCAPSULATED_COMMAND Request 00152 Parameters: None (global SetupPacket and EP0Buf) 00153 Return Value: TRUE - Success, FALSE - Error 00154 *---------------------------------------------------------------------------*/ 00155 uint32_t CDC_SendEncapsulatedCommand (void) { 00156 00157 return (TRUE); 00158 } 00159 00160 00161 /*---------------------------------------------------------------------------- 00162 CDC GetEncapsulatedResponse Request Callback 00163 Called automatically on CDC Get_ENCAPSULATED_RESPONSE Request 00164 Parameters: None (global SetupPacket and EP0Buf) 00165 Return Value: TRUE - Success, FALSE - Error 00166 *---------------------------------------------------------------------------*/ 00167 uint32_t CDC_GetEncapsulatedResponse (void) { 00168 00169 /* ... add code to handle request */ 00170 return (TRUE); 00171 } 00172 00173 00174 /*---------------------------------------------------------------------------- 00175 CDC SetCommFeature Request Callback 00176 Called automatically on CDC Set_COMM_FATURE Request 00177 Parameters: FeatureSelector 00178 Return Value: TRUE - Success, FALSE - Error 00179 *---------------------------------------------------------------------------*/ 00180 uint32_t CDC_SetCommFeature (unsigned short wFeatureSelector) { 00181 00182 /* ... add code to handle request */ 00183 return (TRUE); 00184 } 00185 00186 00187 /*---------------------------------------------------------------------------- 00188 CDC GetCommFeature Request Callback 00189 Called automatically on CDC Get_COMM_FATURE Request 00190 Parameters: FeatureSelector 00191 Return Value: TRUE - Success, FALSE - Error 00192 *---------------------------------------------------------------------------*/ 00193 uint32_t CDC_GetCommFeature (unsigned short wFeatureSelector) { 00194 00195 /* ... add code to handle request */ 00196 return (TRUE); 00197 } 00198 00199 00200 /*---------------------------------------------------------------------------- 00201 CDC ClearCommFeature Request Callback 00202 Called automatically on CDC CLEAR_COMM_FATURE Request 00203 Parameters: FeatureSelector 00204 Return Value: TRUE - Success, FALSE - Error 00205 *---------------------------------------------------------------------------*/ 00206 uint32_t CDC_ClearCommFeature (unsigned short wFeatureSelector) { 00207 00208 /* ... add code to handle request */ 00209 return (TRUE); 00210 } 00211 00212 00213 /*---------------------------------------------------------------------------- 00214 CDC SetLineCoding Request Callback 00215 Called automatically on CDC SET_LINE_CODING Request 00216 Parameters: none (global SetupPacket and EP0Buf) 00217 Return Value: TRUE - Success, FALSE - Error 00218 *---------------------------------------------------------------------------*/ 00219 uint32_t CDC_SetLineCoding (void) { 00220 00221 CDC_LineCoding.dwDTERate = (EP0Buf[0] << 0) 00222 | (EP0Buf[1] << 8) 00223 | (EP0Buf[2] << 16) 00224 | (EP0Buf[3] << 24); 00225 CDC_LineCoding.bCharFormat = EP0Buf[4]; 00226 CDC_LineCoding.bParityType = EP0Buf[5]; 00227 CDC_LineCoding.bDataBits = EP0Buf[6]; 00228 00229 #if PORT_NUM 00230 ser_ClosePort(1); 00231 ser_OpenPort (1); 00232 ser_InitPort1 (CDC_LineCoding.dwDTERate, 00233 CDC_LineCoding.bDataBits, 00234 CDC_LineCoding.bParityType, 00235 CDC_LineCoding.bCharFormat); 00236 #else 00237 ser_ClosePort(0); 00238 ser_OpenPort (0); 00239 ser_InitPort0 (CDC_LineCoding.dwDTERate, 00240 CDC_LineCoding.bDataBits, 00241 CDC_LineCoding.bParityType, 00242 CDC_LineCoding.bCharFormat); 00243 #endif 00244 return (TRUE); 00245 } 00246 00247 00248 /*---------------------------------------------------------------------------- 00249 CDC GetLineCoding Request Callback 00250 Called automatically on CDC GET_LINE_CODING Request 00251 Parameters: None (global SetupPacket and EP0Buf) 00252 Return Value: TRUE - Success, FALSE - Error 00253 *---------------------------------------------------------------------------*/ 00254 uint32_t CDC_GetLineCoding (void) { 00255 00256 EP0Buf[0] = (CDC_LineCoding.dwDTERate >> 0) & 0xFF; 00257 EP0Buf[1] = (CDC_LineCoding.dwDTERate >> 8) & 0xFF; 00258 EP0Buf[2] = (CDC_LineCoding.dwDTERate >> 16) & 0xFF; 00259 EP0Buf[3] = (CDC_LineCoding.dwDTERate >> 24) & 0xFF; 00260 EP0Buf[4] = CDC_LineCoding.bCharFormat; 00261 EP0Buf[5] = CDC_LineCoding.bParityType; 00262 EP0Buf[6] = CDC_LineCoding.bDataBits; 00263 00264 return (TRUE); 00265 } 00266 00267 00268 /*---------------------------------------------------------------------------- 00269 CDC SetControlLineState Request Callback 00270 Called automatically on CDC SET_CONTROL_LINE_STATE Request 00271 Parameters: ControlSignalBitmap 00272 Return Value: TRUE - Success, FALSE - Error 00273 *---------------------------------------------------------------------------*/ 00274 uint32_t CDC_SetControlLineState (unsigned short wControlSignalBitmap) { 00275 00276 /* ... add code to handle request */ 00277 return (TRUE); 00278 } 00279 00280 00281 /*---------------------------------------------------------------------------- 00282 CDC SendBreak Request Callback 00283 Called automatically on CDC Set_COMM_FATURE Request 00284 Parameters: 0xFFFF start of Break 00285 0x0000 stop of Break 00286 0x#### Duration of Break 00287 Return Value: TRUE - Success, FALSE - Error 00288 *---------------------------------------------------------------------------*/ 00289 uint32_t CDC_SendBreak (unsigned short wDurationOfBreak) { 00290 00291 /* ... add code to handle request */ 00292 return (TRUE); 00293 } 00294 00295 00296 /*---------------------------------------------------------------------------- 00297 CDC_BulkIn call on DataIn Request 00298 Parameters: none 00299 Return Value: none 00300 *---------------------------------------------------------------------------*/ 00301 void CDC_BulkIn(void) { 00302 int numBytesRead, numBytesAvail; 00303 00304 ser_AvailChar (&numBytesAvail); 00305 00306 // ... add code to check for overwrite 00307 00308 numBytesRead = ser_Read ((char *)&BulkBufIn[0], &numBytesAvail); 00309 00310 // send over USB 00311 if (numBytesRead > 0) { 00312 USB_WriteEP (CDC_DEP_IN, &BulkBufIn[0], numBytesRead); 00313 } 00314 else { 00315 CDC_DepInEmpty = 1; 00316 } 00317 } 00318 00319 00320 /*---------------------------------------------------------------------------- 00321 CDC_BulkOut call on DataOut Request 00322 Parameters: none 00323 Return Value: none 00324 *---------------------------------------------------------------------------*/ 00325 void CDC_BulkOut(void) { 00326 int numBytesRead; 00327 00328 // get data from USB into intermediate buffer 00329 numBytesRead = USB_ReadEP(CDC_DEP_OUT, &BulkBufOut[0]); 00330 00331 // ... add code to check for overwrite 00332 00333 // store data in a buffer to transmit it over serial interface 00334 CDC_WrOutBuf ((char *)&BulkBufOut[0], &numBytesRead); 00335 00336 } 00337 00338 00339 /*---------------------------------------------------------------------------- 00340 Get the SERIAL_STATE as defined in usbcdc11.pdf, 6.3.5, Table 69. 00341 Parameters: none 00342 Return Value: SerialState as defined in usbcdc11.pdf 00343 *---------------------------------------------------------------------------*/ 00344 unsigned short CDC_GetSerialState (void) { 00345 unsigned short temp; 00346 00347 CDC_SerialState = 0; 00348 ser_LineState (&temp); 00349 00350 if (temp & 0x8000) CDC_SerialState |= CDC_SERIAL_STATE_RX_CARRIER; 00351 if (temp & 0x2000) CDC_SerialState |= CDC_SERIAL_STATE_TX_CARRIER; 00352 if (temp & 0x0010) CDC_SerialState |= CDC_SERIAL_STATE_BREAK; 00353 if (temp & 0x4000) CDC_SerialState |= CDC_SERIAL_STATE_RING; 00354 if (temp & 0x0008) CDC_SerialState |= CDC_SERIAL_STATE_FRAMING; 00355 if (temp & 0x0004) CDC_SerialState |= CDC_SERIAL_STATE_PARITY; 00356 if (temp & 0x0002) CDC_SerialState |= CDC_SERIAL_STATE_OVERRUN; 00357 00358 return (CDC_SerialState); 00359 } 00360 00361 00362 /*---------------------------------------------------------------------------- 00363 Send the SERIAL_STATE notification as defined in usbcdc11.pdf, 6.3.5. 00364 *---------------------------------------------------------------------------*/ 00365 void CDC_NotificationIn (void) { 00366 00367 NotificationBuf[0] = 0xA1; // bmRequestType 00368 NotificationBuf[1] = CDC_NOTIFICATION_SERIAL_STATE; // bNotification (SERIAL_STATE) 00369 NotificationBuf[2] = 0x00; // wValue 00370 NotificationBuf[3] = 0x00; 00371 NotificationBuf[4] = 0x00; // wIndex (Interface #, LSB first) 00372 NotificationBuf[5] = 0x00; 00373 NotificationBuf[6] = 0x02; // wLength (Data length = 2 bytes, LSB first) 00374 NotificationBuf[7] = 0x00; 00375 NotificationBuf[8] = (CDC_SerialState >> 0) & 0xFF; // UART State Bitmap (16bits, LSB first) 00376 NotificationBuf[9] = (CDC_SerialState >> 8) & 0xFF; 00377 00378 USB_WriteEP (CDC_CEP_IN, &NotificationBuf[0], 10); // send notification 00379 }
Generated on Tue Jul 12 2022 17:28:08 by 1.7.2