firmware/cdc.h
changeset 2 2f55e5dd591d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/firmware/cdc.h	Tue Jan 29 22:31:52 2008 +0100
     1.3 @@ -0,0 +1,345 @@
     1.4 +/*********************************************************************
     1.5 + *
     1.6 + *             Microchip USB C18 Firmware -  CDC Version 1.0
     1.7 + *
     1.8 + *********************************************************************
     1.9 + * FileName:        cdc.h
    1.10 + * Dependencies:    See INCLUDES section below
    1.11 + * Processor:       PIC18
    1.12 + * Compiler:        C18 2.30.01+
    1.13 + * Company:         Microchip Technology, Inc.
    1.14 + *
    1.15 + * Software License Agreement
    1.16 + *
    1.17 + * The software supplied herewith by Microchip Technology Incorporated
    1.18 + * (the “Company”) for its PICmicro® Microcontroller is intended and
    1.19 + * supplied to you, the Company’s customer, for use solely and
    1.20 + * exclusively on Microchip PICmicro Microcontroller products. The
    1.21 + * software is owned by the Company and/or its supplier, and is
    1.22 + * protected under applicable copyright laws. All rights are reserved.
    1.23 + * Any use in violation of the foregoing restrictions may subject the
    1.24 + * user to criminal sanctions under applicable laws, as well as to
    1.25 + * civil liability for the breach of the terms and conditions of this
    1.26 + * license.
    1.27 + *
    1.28 + * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
    1.29 + * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
    1.30 + * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    1.31 + * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
    1.32 + * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
    1.33 + * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
    1.34 + *
    1.35 + * Author               Date        Comment
    1.36 + *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1.37 + * Rawin Rojvanit       7/21/04     Original.
    1.38 + ********************************************************************/
    1.39 +#ifndef CDC_H
    1.40 +#define CDC_H
    1.41 +
    1.42 +/** I N C L U D E S **********************************************************/
    1.43 +#include "typedefs.h"
    1.44 +
    1.45 +/** D E F I N I T I O N S ****************************************************/
    1.46 +
    1.47 +/* Class-Specific Requests */
    1.48 +#define SEND_ENCAPSULATED_COMMAND   0x00
    1.49 +#define GET_ENCAPSULATED_RESPONSE   0x01
    1.50 +#define SET_COMM_FEATURE            0x02
    1.51 +#define GET_COMM_FEATURE            0x03
    1.52 +#define CLEAR_COMM_FEATURE          0x04
    1.53 +#define SET_LINE_CODING             0x20
    1.54 +#define GET_LINE_CODING             0x21
    1.55 +#define SET_CONTROL_LINE_STATE      0x22
    1.56 +#define SEND_BREAK                  0x23
    1.57 +
    1.58 +/* Notifications *
    1.59 + * Note: Notifications are polled over
    1.60 + * Communication Interface (Interrupt Endpoint)
    1.61 + */
    1.62 +#define NETWORK_CONNECTION          0x00
    1.63 +#define RESPONSE_AVAILABLE          0x01
    1.64 +#define SERIAL_STATE                0x20
    1.65 +
    1.66 +
    1.67 +/* Device Class Code */
    1.68 +#define CDC_DEVICE                  0x02
    1.69 +
    1.70 +/* Communication Interface Class Code */
    1.71 +#define COMM_INTF                   0x02
    1.72 +
    1.73 +/* Communication Interface Class SubClass Codes */
    1.74 +#define ABSTRACT_CONTROL_MODEL      0x02
    1.75 +
    1.76 +/* Communication Interface Class Control Protocol Codes */
    1.77 +#define V25TER                      0x01    // Common AT commands ("Hayes(TM)")
    1.78 +
    1.79 +
    1.80 +/* Data Interface Class Codes */
    1.81 +#define DATA_INTF                   0x0A
    1.82 +
    1.83 +/* Data Interface Class Protocol Codes */
    1.84 +#define NO_PROTOCOL                 0x00    // No class specific protocol required
    1.85 +
    1.86 +
    1.87 +/* Communication Feature Selector Codes */
    1.88 +#define ABSTRACT_STATE              0x01
    1.89 +#define COUNTRY_SETTING             0x02
    1.90 +
    1.91 +/* Functional Descriptors */
    1.92 +/* Type Values for the bDscType Field */
    1.93 +#define CS_INTERFACE                0x24
    1.94 +#define CS_ENDPOINT                 0x25
    1.95 +
    1.96 +/* bDscSubType in Functional Descriptors */
    1.97 +#define DSC_FN_HEADER               0x00
    1.98 +#define DSC_FN_CALL_MGT             0x01
    1.99 +#define DSC_FN_ACM                  0x02    // ACM - Abstract Control Management
   1.100 +#define DSC_FN_DLM                  0x03    // DLM - Direct Line Managment
   1.101 +#define DSC_FN_TELEPHONE_RINGER     0x04
   1.102 +#define DSC_FN_RPT_CAPABILITIES     0x05
   1.103 +#define DSC_FN_UNION                0x06
   1.104 +#define DSC_FN_COUNTRY_SELECTION    0x07
   1.105 +#define DSC_FN_TEL_OP_MODES         0x08
   1.106 +#define DSC_FN_USB_TERMINAL         0x09
   1.107 +/* more.... see Table 25 in USB CDC Specification 1.1 */
   1.108 +
   1.109 +/* CDC Bulk IN transfer states */
   1.110 +#define CDC_TX_READY                0
   1.111 +#define CDC_TX_BUSY                 1
   1.112 +#define CDC_TX_BUSY_ZLP             2       // ZLP: Zero Length Packet
   1.113 +#define CDC_TX_COMPLETING           3
   1.114 +
   1.115 +/******************************************************************************
   1.116 + * Macro:           BOOL mUSBUSARTIsTxTrfReady(void)
   1.117 + *
   1.118 + * PreCondition:    None
   1.119 + *
   1.120 + * Input:           None
   1.121 + *
   1.122 + * Output:          None
   1.123 + *
   1.124 + * Side Effects:    None
   1.125 + *
   1.126 + * Overview:        This macro is used to check if the CDC class is ready
   1.127 + *                  to send more data.
   1.128 + *                  Typical Usage: if(mUSBUSARTIsTxTrfReady())
   1.129 + *
   1.130 + * Note:            None
   1.131 + *****************************************************************************/
   1.132 +#define mUSBUSARTIsTxTrfReady()     (cdc_trf_state == CDC_TX_READY)
   1.133 +
   1.134 +/******************************************************************************
   1.135 + * Macro:           (bit) mCDCUsartRxIsBusy(void)
   1.136 + *
   1.137 + * PreCondition:    None
   1.138 + *
   1.139 + * Input:           None
   1.140 + *
   1.141 + * Output:          None
   1.142 + *
   1.143 + * Side Effects:    None
   1.144 + *
   1.145 + * Overview:        This macro is used to check if CDC bulk OUT endpoint is
   1.146 + *                  busy (owned by SIE) or not.
   1.147 + *                  Typical Usage: if(mCDCUsartRxIsBusy())
   1.148 + *
   1.149 + * Note:            None
   1.150 + *****************************************************************************/
   1.151 +#define mCDCUsartRxIsBusy()         CDC_BULK_BD_OUT.Stat.UOWN
   1.152 +
   1.153 +/******************************************************************************
   1.154 + * Macro:           (bit) mCDCUsartTxIsBusy(void)
   1.155 + *
   1.156 + * PreCondition:    None
   1.157 + *
   1.158 + * Input:           None
   1.159 + *
   1.160 + * Output:          None
   1.161 + *
   1.162 + * Side Effects:    None
   1.163 + *
   1.164 + * Overview:        This macro is used to check if CDC bulk IN endpoint is
   1.165 + *                  busy (owned by SIE) or not.
   1.166 + *                  Typical Usage: if(mCDCUsartTxIsBusy())
   1.167 + *
   1.168 + * Note:            None
   1.169 + *****************************************************************************/
   1.170 +#define mCDCUsartTxIsBusy()         CDC_BULK_BD_IN.Stat.UOWN
   1.171 +
   1.172 +/******************************************************************************
   1.173 + * Macro:           byte mCDCGetRxLength(void)
   1.174 + *
   1.175 + * PreCondition:    None
   1.176 + *
   1.177 + * Input:           None
   1.178 + *
   1.179 + * Output:          mCDCGetRxLength returns cdc_rx_len
   1.180 + *
   1.181 + * Side Effects:    None
   1.182 + *
   1.183 + * Overview:        mCDCGetRxLength is used to retrieve the number of bytes
   1.184 + *                  copied to user's buffer by the most recent call to
   1.185 + *                  getsUSBUSART function.
   1.186 + *
   1.187 + * Note:            None
   1.188 + *****************************************************************************/
   1.189 +#define mCDCGetRxLength()           cdc_rx_len
   1.190 +
   1.191 +/******************************************************************************
   1.192 + * Macro:           void mUSBUSARTTxRam(byte *pData, byte len)
   1.193 + *
   1.194 + * PreCondition:    cdc_trf_state must be in the CDC_TX_READY state.
   1.195 + *                  
   1.196 + *                  Value of 'len' must be equal to or smaller than 255 bytes.
   1.197 + *
   1.198 + * Input:           pDdata  : Pointer to the starting location of data bytes
   1.199 + *                  len     : Number of bytes to be transferred
   1.200 + *
   1.201 + * Output:          None
   1.202 + *
   1.203 + * Side Effects:    None
   1.204 + *
   1.205 + * Overview:        Use this macro to transfer data located in data memory.
   1.206 + *                  Use this macro when:
   1.207 + *                  1. Data stream is not null-terminated
   1.208 + *                  2. Transfer length is known
   1.209 + *
   1.210 + *                  Remember: cdc_trf_state must == CDC_TX_READY
   1.211 + *                  Unlike putsUSBUSART, there is not code double checking
   1.212 + *                  the transfer state. Unexpected behavior will occur if
   1.213 + *                  this function is called when cdc_trf_state != CDC_TX_READY
   1.214 + *
   1.215 + * Note:            This macro only handles the setup of the transfer. The
   1.216 + *                  actual transfer is handled by CDCTxService().
   1.217 + *****************************************************************************/
   1.218 +#define mUSBUSARTTxRam(pData,len)   \
   1.219 +{                                   \
   1.220 +    pCDCSrc.bRam = pData;           \
   1.221 +    cdc_tx_len = len;               \
   1.222 +    cdc_mem_type = _RAM;            \
   1.223 +    cdc_trf_state = CDC_TX_BUSY;    \
   1.224 +}
   1.225 +
   1.226 +/******************************************************************************
   1.227 + * Macro:           void mUSBUSARTTxRom(rom byte *pData, byte len)
   1.228 + *
   1.229 + * PreCondition:    cdc_trf_state must be in the CDC_TX_READY state.
   1.230 + *                  
   1.231 + *                  Value of 'len' must be equal to or smaller than 255 bytes.
   1.232 + *
   1.233 + * Input:           pDdata  : Pointer to the starting location of data bytes
   1.234 + *                  len     : Number of bytes to be transferred
   1.235 + *
   1.236 + * Output:          None
   1.237 + *
   1.238 + * Side Effects:    None
   1.239 + *
   1.240 + * Overview:        Use this macro to transfer data located in program memory.
   1.241 + *                  Use this macro when:
   1.242 + *                  1. Data stream is not null-terminated
   1.243 + *                  2. Transfer length is known
   1.244 + *
   1.245 + *                  Remember: cdc_trf_state must == CDC_TX_READY
   1.246 + *                  Unlike putrsUSBUSART, there is not code double checking
   1.247 + *                  the transfer state. Unexpected behavior will occur if
   1.248 + *                  this function is called when cdc_trf_state != CDC_TX_READY
   1.249 + *
   1.250 + * Note:            This macro only handles the setup of the transfer. The
   1.251 + *                  actual transfer is handled by CDCTxService().
   1.252 + *****************************************************************************/
   1.253 +#define mUSBUSARTTxRom(pData,len)   \
   1.254 +{                                   \
   1.255 +    pCDCSrc.bRom = pData;           \
   1.256 +    cdc_tx_len = len;               \
   1.257 +    cdc_mem_type = _ROM;            \
   1.258 +    cdc_trf_state = CDC_TX_BUSY;    \
   1.259 +}
   1.260 +
   1.261 +/** S T R U C T U R E S ******************************************************/
   1.262 +
   1.263 +/* Line Coding Structure */
   1.264 +#define LINE_CODING_LENGTH          0x07
   1.265 +
   1.266 +typedef union _LINE_CODING
   1.267 +{
   1.268 +    struct
   1.269 +    {
   1.270 +        byte _byte[LINE_CODING_LENGTH];
   1.271 +    };
   1.272 +    struct
   1.273 +    {
   1.274 +        DWORD   dwDTERate;          // Complex data structure
   1.275 +        byte    bCharFormat;
   1.276 +        byte    bParityType;
   1.277 +        byte    bDataBits;
   1.278 +    };
   1.279 +} LINE_CODING;
   1.280 +
   1.281 +typedef union _CONTROL_SIGNAL_BITMAP
   1.282 +{
   1.283 +    byte _byte;
   1.284 +    struct
   1.285 +    {
   1.286 +        unsigned DTE_PRESENT;       // [0] Not Present  [1] Present
   1.287 +        unsigned CARRIER_CONTROL;   // [0] Deactivate   [1] Activate
   1.288 +    };
   1.289 +} CONTROL_SIGNAL_BITMAP;
   1.290 +
   1.291 +
   1.292 +/* Functional Descriptor Structure - See CDC Specification 1.1 for details */
   1.293 +
   1.294 +/* Header Functional Descriptor */
   1.295 +typedef struct _USB_CDC_HEADER_FN_DSC
   1.296 +{
   1.297 +    byte bFNLength;
   1.298 +    byte bDscType;
   1.299 +    byte bDscSubType;
   1.300 +    word bcdCDC;
   1.301 +} USB_CDC_HEADER_FN_DSC;
   1.302 +
   1.303 +/* Abstract Control Management Functional Descriptor */
   1.304 +typedef struct _USB_CDC_ACM_FN_DSC
   1.305 +{
   1.306 +    byte bFNLength;
   1.307 +    byte bDscType;
   1.308 +    byte bDscSubType;
   1.309 +    byte bmCapabilities;
   1.310 +} USB_CDC_ACM_FN_DSC;
   1.311 +
   1.312 +/* Union Functional Descriptor */
   1.313 +typedef struct _USB_CDC_UNION_FN_DSC
   1.314 +{
   1.315 +    byte bFNLength;
   1.316 +    byte bDscType;
   1.317 +    byte bDscSubType;
   1.318 +    byte bMasterIntf;
   1.319 +    byte bSaveIntf0;
   1.320 +} USB_CDC_UNION_FN_DSC;
   1.321 +
   1.322 +/* Call Management Functional Descriptor */
   1.323 +typedef struct _USB_CDC_CALL_MGT_FN_DSC
   1.324 +{
   1.325 +    byte bFNLength;
   1.326 +    byte bDscType;
   1.327 +    byte bDscSubType;
   1.328 +    byte bmCapabilities;
   1.329 +    byte bDataInterface;
   1.330 +} USB_CDC_CALL_MGT_FN_DSC;
   1.331 +
   1.332 +/** E X T E R N S ************************************************************/
   1.333 +extern byte cdc_rx_len;
   1.334 +
   1.335 +extern byte cdc_trf_state;
   1.336 +extern POINTER pCDCSrc;
   1.337 +extern byte cdc_tx_len;
   1.338 +extern byte cdc_mem_type;
   1.339 +
   1.340 +/** P U B L I C  P R O T O T Y P E S *****************************************/
   1.341 +void USBCheckCDCRequest(void);
   1.342 +void CDCInitEP(void);
   1.343 +byte getsUSBUSART(char *buffer, byte len);
   1.344 +void putrsUSBUSART(const rom char *data);
   1.345 +void putsUSBUSART(char *data);
   1.346 +void CDCTxService(void);
   1.347 +
   1.348 +#endif //CDC_H