00001 /********************************************************************* 00002 * 00003 * Microchip USB C18 Firmware - CDC Version 1.0 00004 * 00005 ********************************************************************* 00006 * FileName: cdc.h 00007 * Dependencies: See INCLUDES section below 00008 * Processor: PIC18 00009 * Compiler: C18 2.30.01+ 00010 * Company: Microchip Technology, Inc. 00011 * 00012 * Software License Agreement 00013 * 00014 * The software supplied herewith by Microchip Technology Incorporated 00015 * (the “Company”) for its PICmicro® Microcontroller is intended and 00016 * supplied to you, the Company’s customer, for use solely and 00017 * exclusively on Microchip PICmicro Microcontroller products. The 00018 * software is owned by the Company and/or its supplier, and is 00019 * protected under applicable copyright laws. All rights are reserved. 00020 * Any use in violation of the foregoing restrictions may subject the 00021 * user to criminal sanctions under applicable laws, as well as to 00022 * civil liability for the breach of the terms and conditions of this 00023 * license. 00024 * 00025 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES, 00026 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 00027 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00028 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, 00029 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR 00030 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. 00031 * 00032 * Author Date Comment 00033 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00034 * Rawin Rojvanit 7/21/04 Original. 00035 ********************************************************************/ 00036 #ifndef CDC_H 00037 #define CDC_H 00038 00039 /** I N C L U D E S **********************************************************/ 00040 #include "system\typedefs.h" 00041 00042 /** D E F I N I T I O N S ****************************************************/ 00043 00044 /* Class-Specific Requests */ 00045 #define SEND_ENCAPSULATED_COMMAND 0x00 00046 #define GET_ENCAPSULATED_RESPONSE 0x01 00047 #define SET_COMM_FEATURE 0x02 00048 #define GET_COMM_FEATURE 0x03 00049 #define CLEAR_COMM_FEATURE 0x04 00050 #define SET_LINE_CODING 0x20 00051 #define GET_LINE_CODING 0x21 00052 #define SET_CONTROL_LINE_STATE 0x22 00053 #define SEND_BREAK 0x23 00054 00055 /* Notifications * 00056 * Note: Notifications are polled over 00057 * Communication Interface (Interrupt Endpoint) 00058 */ 00059 #define NETWORK_CONNECTION 0x00 00060 #define RESPONSE_AVAILABLE 0x01 00061 #define SERIAL_STATE 0x20 00062 00063 00064 /* Device Class Code */ 00065 #define CDC_DEVICE 0x02 00066 00067 /* Communication Interface Class Code */ 00068 #define COMM_INTF 0x02 00069 00070 /* Communication Interface Class SubClass Codes */ 00071 #define ABSTRACT_CONTROL_MODEL 0x02 00072 00073 /* Communication Interface Class Control Protocol Codes */ 00074 #define V25TER 0x01 // Common AT commands ("Hayes(TM)") 00075 00076 00077 /* Data Interface Class Codes */ 00078 #define DATA_INTF 0x0A 00079 00080 /* Data Interface Class Protocol Codes */ 00081 #define NO_PROTOCOL 0x00 // No class specific protocol required 00082 00083 00084 /* Communication Feature Selector Codes */ 00085 #define ABSTRACT_STATE 0x01 00086 #define COUNTRY_SETTING 0x02 00087 00088 /* Functional Descriptors */ 00089 /* Type Values for the bDscType Field */ 00090 #define CS_INTERFACE 0x24 00091 #define CS_ENDPOINT 0x25 00092 00093 /* bDscSubType in Functional Descriptors */ 00094 #define DSC_FN_HEADER 0x00 00095 #define DSC_FN_CALL_MGT 0x01 00096 #define DSC_FN_ACM 0x02 // ACM - Abstract Control Management 00097 #define DSC_FN_DLM 0x03 // DLM - Direct Line Managment 00098 #define DSC_FN_TELEPHONE_RINGER 0x04 00099 #define DSC_FN_RPT_CAPABILITIES 0x05 00100 #define DSC_FN_UNION 0x06 00101 #define DSC_FN_COUNTRY_SELECTION 0x07 00102 #define DSC_FN_TEL_OP_MODES 0x08 00103 #define DSC_FN_USB_TERMINAL 0x09 00104 /* more.... see Table 25 in USB CDC Specification 1.1 */ 00105 00106 /* CDC Bulk IN transfer states */ 00107 #define CDC_TX_READY 0 00108 #define CDC_TX_BUSY 1 00109 #define CDC_TX_BUSY_ZLP 2 // ZLP: Zero Length Packet 00110 #define CDC_TX_COMPLETING 3 00111 00112 /****************************************************************************** 00113 * Macro: BOOL mUSBUSARTIsTxTrfReady(void) 00114 * 00115 * PreCondition: None 00116 * 00117 * Input: None 00118 * 00119 * Output: None 00120 * 00121 * Side Effects: None 00122 * 00123 * Overview: This macro is used to check if the CDC class is ready 00124 * to send more data. 00125 * Typical Usage: if(mUSBUSARTIsTxTrfReady()) 00126 * 00127 * Note: None 00128 *****************************************************************************/ 00129 #define mUSBUSARTIsTxTrfReady() (cdc_trf_state == CDC_TX_READY) 00130 00131 /****************************************************************************** 00132 * Macro: (bit) mCDCUsartRxIsBusy(void) 00133 * 00134 * PreCondition: None 00135 * 00136 * Input: None 00137 * 00138 * Output: None 00139 * 00140 * Side Effects: None 00141 * 00142 * Overview: This macro is used to check if CDC bulk OUT endpoint is 00143 * busy (owned by SIE) or not. 00144 * Typical Usage: if(mCDCUsartRxIsBusy()) 00145 * 00146 * Note: None 00147 *****************************************************************************/ 00148 #define mCDCUsartRxIsBusy() CDC_BULK_BD_OUT.Stat.UOWN 00149 00150 /****************************************************************************** 00151 * Macro: (bit) mCDCUsartTxIsBusy(void) 00152 * 00153 * PreCondition: None 00154 * 00155 * Input: None 00156 * 00157 * Output: None 00158 * 00159 * Side Effects: None 00160 * 00161 * Overview: This macro is used to check if CDC bulk IN endpoint is 00162 * busy (owned by SIE) or not. 00163 * Typical Usage: if(mCDCUsartTxIsBusy()) 00164 * 00165 * Note: None 00166 *****************************************************************************/ 00167 #define mCDCUsartTxIsBusy() CDC_BULK_BD_IN.Stat.UOWN 00168 00169 /****************************************************************************** 00170 * Macro: byte mCDCGetRxLength(void) 00171 * 00172 * PreCondition: None 00173 * 00174 * Input: None 00175 * 00176 * Output: mCDCGetRxLength returns cdc_rx_len 00177 * 00178 * Side Effects: None 00179 * 00180 * Overview: mCDCGetRxLength is used to retrieve the number of bytes 00181 * copied to user's buffer by the most recent call to 00182 * getsUSBUSART function. 00183 * 00184 * Note: None 00185 *****************************************************************************/ 00186 #define mCDCGetRxLength() cdc_rx_len 00187 00188 /****************************************************************************** 00189 * Macro: void mUSBUSARTTxRam(byte *pData, byte len) 00190 * 00191 * PreCondition: cdc_trf_state must be in the CDC_TX_READY state. 00192 * 00193 * Value of 'len' must be equal to or smaller than 255 bytes. 00194 * 00195 * Input: pDdata : Pointer to the starting location of data bytes 00196 * len : Number of bytes to be transferred 00197 * 00198 * Output: None 00199 * 00200 * Side Effects: None 00201 * 00202 * Overview: Use this macro to transfer data located in data memory. 00203 * Use this macro when: 00204 * 1. Data stream is not null-terminated 00205 * 2. Transfer length is known 00206 * 00207 * Remember: cdc_trf_state must == CDC_TX_READY 00208 * Unlike putsUSBUSART, there is not code double checking 00209 * the transfer state. Unexpected behavior will occur if 00210 * this function is called when cdc_trf_state != CDC_TX_READY 00211 * 00212 * Note: This macro only handles the setup of the transfer. The 00213 * actual transfer is handled by CDCTxService(). 00214 *****************************************************************************/ 00215 #define mUSBUSARTTxRam(pData,len) \ 00216 { \ 00217 pCDCSrc.bRam = pData; \ 00218 cdc_tx_len = len; \ 00219 cdc_mem_type = _RAM; \ 00220 cdc_trf_state = CDC_TX_BUSY; \ 00221 } 00222 00223 /****************************************************************************** 00224 * Macro: void mUSBUSARTTxRom(rom byte *pData, byte len) 00225 * 00226 * PreCondition: cdc_trf_state must be in the CDC_TX_READY state. 00227 * 00228 * Value of 'len' must be equal to or smaller than 255 bytes. 00229 * 00230 * Input: pDdata : Pointer to the starting location of data bytes 00231 * len : Number of bytes to be transferred 00232 * 00233 * Output: None 00234 * 00235 * Side Effects: None 00236 * 00237 * Overview: Use this macro to transfer data located in program memory. 00238 * Use this macro when: 00239 * 1. Data stream is not null-terminated 00240 * 2. Transfer length is known 00241 * 00242 * Remember: cdc_trf_state must == CDC_TX_READY 00243 * Unlike putrsUSBUSART, there is not code double checking 00244 * the transfer state. Unexpected behavior will occur if 00245 * this function is called when cdc_trf_state != CDC_TX_READY 00246 * 00247 * Note: This macro only handles the setup of the transfer. The 00248 * actual transfer is handled by CDCTxService(). 00249 *****************************************************************************/ 00250 #define mUSBUSARTTxRom(pData,len) \ 00251 { \ 00252 pCDCSrc.bRom = pData; \ 00253 cdc_tx_len = len; \ 00254 cdc_mem_type = _ROM; \ 00255 cdc_trf_state = CDC_TX_BUSY; \ 00256 } 00257 00258 /** S T R U C T U R E S ******************************************************/ 00259 00260 /* Line Coding Structure */ 00261 #define LINE_CODING_LENGTH 0x07 00262 00263 typedef union _LINE_CODING 00264 { 00265 struct 00266 { 00267 byte _byte[LINE_CODING_LENGTH]; 00268 }; 00269 struct 00270 { 00271 DWORD dwDTERate; // Complex data structure 00272 byte bCharFormat; 00273 byte bParityType; 00274 byte bDataBits; 00275 }; 00276 } LINE_CODING; 00277 00278 typedef union _CONTROL_SIGNAL_BITMAP 00279 { 00280 byte _byte; 00281 struct 00282 { 00283 unsigned DTE_PRESENT; // [0] Not Present [1] Present 00284 unsigned CARRIER_CONTROL; // [0] Deactivate [1] Activate 00285 }; 00286 } CONTROL_SIGNAL_BITMAP; 00287 00288 00289 /* Functional Descriptor Structure - See CDC Specification 1.1 for details */ 00290 00291 /* Header Functional Descriptor */ 00292 typedef struct _USB_CDC_HEADER_FN_DSC 00293 { 00294 byte bFNLength; 00295 byte bDscType; 00296 byte bDscSubType; 00297 word bcdCDC; 00298 } USB_CDC_HEADER_FN_DSC; 00299 00300 /* Abstract Control Management Functional Descriptor */ 00301 typedef struct _USB_CDC_ACM_FN_DSC 00302 { 00303 byte bFNLength; 00304 byte bDscType; 00305 byte bDscSubType; 00306 byte bmCapabilities; 00307 } USB_CDC_ACM_FN_DSC; 00308 00309 /* Union Functional Descriptor */ 00310 typedef struct _USB_CDC_UNION_FN_DSC 00311 { 00312 byte bFNLength; 00313 byte bDscType; 00314 byte bDscSubType; 00315 byte bMasterIntf; 00316 byte bSaveIntf0; 00317 } USB_CDC_UNION_FN_DSC; 00318 00319 /* Call Management Functional Descriptor */ 00320 typedef struct _USB_CDC_CALL_MGT_FN_DSC 00321 { 00322 byte bFNLength; 00323 byte bDscType; 00324 byte bDscSubType; 00325 byte bmCapabilities; 00326 byte bDataInterface; 00327 } USB_CDC_CALL_MGT_FN_DSC; 00328 00329 /** E X T E R N S ************************************************************/ 00330 extern byte cdc_rx_len; 00331 00332 extern byte cdc_trf_state; 00333 extern POINTER pCDCSrc; 00334 extern byte cdc_tx_len; 00335 extern byte cdc_mem_type; 00336 00337 /** P U B L I C P R O T O T Y P E S *****************************************/ 00338 void USBCheckCDCRequest(void); 00339 void CDCInitEP(void); 00340 byte getsUSBUSART(char *buffer, byte len); 00341 void putrsUSBUSART(const rom char *data); 00342 void putsUSBUSART(char *data); 00343 void CDCTxService(void); 00344 00345 #endif //CDC_H