1 /*********************************************************************
3 * Microchip USB C18 Firmware - CDC Version 1.0
5 *********************************************************************
7 * Dependencies: See INCLUDES section below
9 * Compiler: C18 2.30.01+
10 * Company: Microchip Technology, Inc.
12 * Software License Agreement
14 * The software supplied herewith by Microchip Technology Incorporated
15 * (the “Company”) for its PICmicro® Microcontroller is intended and
16 * supplied to you, the Company’s customer, for use solely and
17 * exclusively on Microchip PICmicro Microcontroller products. The
18 * software is owned by the Company and/or its supplier, and is
19 * protected under applicable copyright laws. All rights are reserved.
20 * Any use in violation of the foregoing restrictions may subject the
21 * user to criminal sanctions under applicable laws, as well as to
22 * civil liability for the breach of the terms and conditions of this
25 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
26 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
27 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
28 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
29 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
30 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
33 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34 * Rawin Rojvanit 7/21/04 Original.
35 ********************************************************************/
39 /** I N C L U D E S **********************************************************/
42 /** D E F I N I T I O N S ****************************************************/
44 /* Class-Specific Requests */
45 #define SEND_ENCAPSULATED_COMMAND 0x00
46 #define GET_ENCAPSULATED_RESPONSE 0x01
47 #define SET_COMM_FEATURE 0x02
48 #define GET_COMM_FEATURE 0x03
49 #define CLEAR_COMM_FEATURE 0x04
50 #define SET_LINE_CODING 0x20
51 #define GET_LINE_CODING 0x21
52 #define SET_CONTROL_LINE_STATE 0x22
53 #define SEND_BREAK 0x23
56 * Note: Notifications are polled over
57 * Communication Interface (Interrupt Endpoint)
59 #define NETWORK_CONNECTION 0x00
60 #define RESPONSE_AVAILABLE 0x01
61 #define SERIAL_STATE 0x20
64 /* Device Class Code */
65 #define CDC_DEVICE 0x02
67 /* Communication Interface Class Code */
68 #define COMM_INTF 0x02
70 /* Communication Interface Class SubClass Codes */
71 #define ABSTRACT_CONTROL_MODEL 0x02
73 /* Communication Interface Class Control Protocol Codes */
74 #define V25TER 0x01 // Common AT commands ("Hayes(TM)")
77 /* Data Interface Class Codes */
78 #define DATA_INTF 0x0A
80 /* Data Interface Class Protocol Codes */
81 #define NO_PROTOCOL 0x00 // No class specific protocol required
84 /* Communication Feature Selector Codes */
85 #define ABSTRACT_STATE 0x01
86 #define COUNTRY_SETTING 0x02
88 /* Functional Descriptors */
89 /* Type Values for the bDscType Field */
90 #define CS_INTERFACE 0x24
91 #define CS_ENDPOINT 0x25
93 /* bDscSubType in Functional Descriptors */
94 #define DSC_FN_HEADER 0x00
95 #define DSC_FN_CALL_MGT 0x01
96 #define DSC_FN_ACM 0x02 // ACM - Abstract Control Management
97 #define DSC_FN_DLM 0x03 // DLM - Direct Line Managment
98 #define DSC_FN_TELEPHONE_RINGER 0x04
99 #define DSC_FN_RPT_CAPABILITIES 0x05
100 #define DSC_FN_UNION 0x06
101 #define DSC_FN_COUNTRY_SELECTION 0x07
102 #define DSC_FN_TEL_OP_MODES 0x08
103 #define DSC_FN_USB_TERMINAL 0x09
104 /* more.... see Table 25 in USB CDC Specification 1.1 */
106 /* CDC Bulk IN transfer states */
107 #define CDC_TX_READY 0
108 #define CDC_TX_BUSY 1
109 #define CDC_TX_BUSY_ZLP 2 // ZLP: Zero Length Packet
110 #define CDC_TX_COMPLETING 3
112 /******************************************************************************
113 * Macro: BOOL mUSBUSARTIsTxTrfReady(void)
123 * Overview: This macro is used to check if the CDC class is ready
125 * Typical Usage: if(mUSBUSARTIsTxTrfReady())
128 *****************************************************************************/
129 #define mUSBUSARTIsTxTrfReady() (cdc_trf_state == CDC_TX_READY)
131 /******************************************************************************
132 * Macro: (bit) mCDCUsartRxIsBusy(void)
142 * Overview: This macro is used to check if CDC bulk OUT endpoint is
143 * busy (owned by SIE) or not.
144 * Typical Usage: if(mCDCUsartRxIsBusy())
147 *****************************************************************************/
148 #define mCDCUsartRxIsBusy() CDC_BULK_BD_OUT.Stat.UOWN
150 /******************************************************************************
151 * Macro: (bit) mCDCUsartTxIsBusy(void)
161 * Overview: This macro is used to check if CDC bulk IN endpoint is
162 * busy (owned by SIE) or not.
163 * Typical Usage: if(mCDCUsartTxIsBusy())
166 *****************************************************************************/
167 #define mCDCUsartTxIsBusy() CDC_BULK_BD_IN.Stat.UOWN
169 /******************************************************************************
170 * Macro: byte mCDCGetRxLength(void)
176 * Output: mCDCGetRxLength returns cdc_rx_len
180 * Overview: mCDCGetRxLength is used to retrieve the number of bytes
181 * copied to user's buffer by the most recent call to
182 * getsUSBUSART function.
185 *****************************************************************************/
186 #define mCDCGetRxLength() cdc_rx_len
188 /******************************************************************************
189 * Macro: void mUSBUSARTTxRam(byte *pData, byte len)
191 * PreCondition: cdc_trf_state must be in the CDC_TX_READY state.
193 * Value of 'len' must be equal to or smaller than 255 bytes.
195 * Input: pDdata : Pointer to the starting location of data bytes
196 * len : Number of bytes to be transferred
202 * Overview: Use this macro to transfer data located in data memory.
203 * Use this macro when:
204 * 1. Data stream is not null-terminated
205 * 2. Transfer length is known
207 * Remember: cdc_trf_state must == CDC_TX_READY
208 * Unlike putsUSBUSART, there is not code double checking
209 * the transfer state. Unexpected behavior will occur if
210 * this function is called when cdc_trf_state != CDC_TX_READY
212 * Note: This macro only handles the setup of the transfer. The
213 * actual transfer is handled by CDCTxService().
214 *****************************************************************************/
215 #define mUSBUSARTTxRam(pData,len) \
217 pCDCSrc.bRam = pData; \
219 cdc_mem_type = _RAM; \
220 cdc_trf_state = CDC_TX_BUSY; \
223 /******************************************************************************
224 * Macro: void mUSBUSARTTxRom(rom byte *pData, byte len)
226 * PreCondition: cdc_trf_state must be in the CDC_TX_READY state.
228 * Value of 'len' must be equal to or smaller than 255 bytes.
230 * Input: pDdata : Pointer to the starting location of data bytes
231 * len : Number of bytes to be transferred
237 * Overview: Use this macro to transfer data located in program memory.
238 * Use this macro when:
239 * 1. Data stream is not null-terminated
240 * 2. Transfer length is known
242 * Remember: cdc_trf_state must == CDC_TX_READY
243 * Unlike putrsUSBUSART, there is not code double checking
244 * the transfer state. Unexpected behavior will occur if
245 * this function is called when cdc_trf_state != CDC_TX_READY
247 * Note: This macro only handles the setup of the transfer. The
248 * actual transfer is handled by CDCTxService().
249 *****************************************************************************/
250 #define mUSBUSARTTxRom(pData,len) \
252 pCDCSrc.bRom = pData; \
254 cdc_mem_type = _ROM; \
255 cdc_trf_state = CDC_TX_BUSY; \
258 /** S T R U C T U R E S ******************************************************/
260 /* Line Coding Structure */
261 #define LINE_CODING_LENGTH 0x07
263 typedef union _LINE_CODING
267 byte _byte[LINE_CODING_LENGTH];
271 DWORD dwDTERate; // Complex data structure
278 typedef union _CONTROL_SIGNAL_BITMAP
283 unsigned DTE_PRESENT; // [0] Not Present [1] Present
284 unsigned CARRIER_CONTROL; // [0] Deactivate [1] Activate
286 } CONTROL_SIGNAL_BITMAP;
289 /* Functional Descriptor Structure - See CDC Specification 1.1 for details */
291 /* Header Functional Descriptor */
292 typedef struct _USB_CDC_HEADER_FN_DSC
298 } USB_CDC_HEADER_FN_DSC;
300 /* Abstract Control Management Functional Descriptor */
301 typedef struct _USB_CDC_ACM_FN_DSC
307 } USB_CDC_ACM_FN_DSC;
309 /* Union Functional Descriptor */
310 typedef struct _USB_CDC_UNION_FN_DSC
317 } USB_CDC_UNION_FN_DSC;
319 /* Call Management Functional Descriptor */
320 typedef struct _USB_CDC_CALL_MGT_FN_DSC
327 } USB_CDC_CALL_MGT_FN_DSC;
329 /** E X T E R N S ************************************************************/
330 extern byte cdc_rx_len;
332 extern byte cdc_trf_state;
333 extern POINTER pCDCSrc;
334 extern byte cdc_tx_len;
335 extern byte cdc_mem_type;
337 /** P U B L I C P R O T O T Y P E S *****************************************/
338 void USBCheckCDCRequest(void);
339 void CDCInitEP(void);
340 byte getsUSBUSART(char *buffer, byte len);
341 void putrsUSBUSART(const rom char *data);
342 void putsUSBUSART(char *data);
343 void CDCTxService(void);