1 /*********************************************************************
3 * Microchip USB C18 Firmware 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 11/19/04 Original.
35 ********************************************************************/
40 /** I N C L U D E S **********************************************************/
44 /** D E F I N I T I O N S ****************************************************/
46 /* UCFG Initialization Parameters */
47 #define _PPBM0 0x00 // Pingpong Buffer Mode 0
48 #define _PPBM1 0x01 // Pingpong Buffer Mode 1
49 #define _PPBM2 0x02 // Pingpong Buffer Mode 2
50 #define _LS 0x00 // Use Low-Speed USB Mode
51 #define _FS 0x04 // Use Full-Speed USB Mode
52 #define _TRINT 0x00 // Use internal transceiver
53 #define _TREXT 0x08 // Use external transceiver
54 #define _PUEN 0x10 // Use internal pull-up resistor
55 #define _OEMON 0x40 // Use SIE output indicator
56 #define _UTEYE 0x80 // Use Eye-Pattern test
58 /* UEPn Initialization Parameters */
59 #define EP_CTRL 0x06 // Cfg Control pipe for this ep
60 #define EP_OUT 0x0C // Cfg OUT only pipe for this ep
61 #define EP_IN 0x0A // Cfg IN only pipe for this ep
62 #define EP_OUT_IN 0x0E // Cfg both OUT & IN pipes for this ep
63 #define HSHK_EN 0x10 // Enable handshake packet
64 // Handshake should be disable for isoch
66 /******************************************************************************
67 * USB - PICmicro Endpoint Definitions
68 * PICmicro EP Address Format: X:EP3:EP2:EP1:EP0:DIR:PPBI:X
69 * This is used when checking the value read from USTAT
71 * NOTE: These definitions are not used in the descriptors.
72 * EP addresses used in the descriptors have different format and
73 * are defined in: "system\usb\usbdefs\usbdefs_std_dsc.h"
74 *****************************************************************************/
78 #define PIC_EP_NUM_MASK 0b01111000
79 #define PIC_EP_DIR_MASK 0b00000100
81 #define EP00_OUT (0x00<<3)|(OUT<<2)
82 #define EP00_IN (0x00<<3)|(IN<<2)
83 #define EP01_OUT (0x01<<3)|(OUT<<2)
84 #define EP01_IN (0x01<<3)|(IN<<2)
85 #define EP02_OUT (0x02<<3)|(OUT<<2)
86 #define EP02_IN (0x02<<3)|(IN<<2)
87 #define EP03_OUT (0x03<<3)|(OUT<<2)
88 #define EP03_IN (0x03<<3)|(IN<<2)
89 #define EP04_OUT (0x04<<3)|(OUT<<2)
90 #define EP04_IN (0x04<<3)|(IN<<2)
91 #define EP05_OUT (0x05<<3)|(OUT<<2)
92 #define EP05_IN (0x05<<3)|(IN<<2)
93 #define EP06_OUT (0x06<<3)|(OUT<<2)
94 #define EP06_IN (0x06<<3)|(IN<<2)
95 #define EP07_OUT (0x07<<3)|(OUT<<2)
96 #define EP07_IN (0x07<<3)|(IN<<2)
97 #define EP08_OUT (0x08<<3)|(OUT<<2)
98 #define EP08_IN (0x08<<3)|(IN<<2)
99 #define EP09_OUT (0x09<<3)|(OUT<<2)
100 #define EP09_IN (0x09<<3)|(IN<<2)
101 #define EP10_OUT (0x0A<<3)|(OUT<<2)
102 #define EP10_IN (0x0A<<3)|(IN<<2)
103 #define EP11_OUT (0x0B<<3)|(OUT<<2)
104 #define EP11_IN (0x0B<<3)|(IN<<2)
105 #define EP12_OUT (0x0C<<3)|(OUT<<2)
106 #define EP12_IN (0x0C<<3)|(IN<<2)
107 #define EP13_OUT (0x0D<<3)|(OUT<<2)
108 #define EP13_IN (0x0D<<3)|(IN<<2)
109 #define EP14_OUT (0x0E<<3)|(OUT<<2)
110 #define EP14_IN (0x0E<<3)|(IN<<2)
111 #define EP15_OUT (0x0F<<3)|(OUT<<2)
112 #define EP15_IN (0x0F<<3)|(IN<<2)
114 /******************************************************************************
115 * Macro: void mInitializeUSBDriver(void)
125 * Overview: Configures the USB module, definition of UCFG_VAL can be
126 * found in autofiles\usbcfg.h
128 * This register determines: USB Speed, On-chip pull-up
129 * resistor selection, On-chip tranceiver selection, bus
130 * eye pattern generation mode, Ping-pong buffering mode
134 *****************************************************************************/
135 #define mInitializeUSBDriver() {UCFG = UCFG_VAL; \
136 usb_device_state = DETACHED_STATE; \
137 usb_stat._byte = 0x00; \
138 usb_active_cfg = 0x00;}
140 /******************************************************************************
141 * Macro: void mDisableEP1to15(void)
151 * Overview: This macro disables all endpoints except EP0.
152 * This macro should be called when the host sends a RESET
153 * signal or a SET_CONFIGURATION request.
156 *****************************************************************************/
157 #define mDisableEP1to15() ClearArray((byte*)&UEP1,15);
159 #define mDisableEP1to15() UEP1=0x00;UEP2=0x00;UEP3=0x00;\
160 UEP4=0x00;UEP5=0x00;UEP6=0x00;UEP7=0x00;\
161 UEP8=0x00;UEP9=0x00;UEP10=0x00;UEP11=0x00;\
162 UEP12=0x00;UEP13=0x00;UEP14=0x00;UEP15=0x00;
165 /******************************************************************************
166 * Macro: void mUSBBufferReady(buffer_dsc)
168 * PreCondition: IN Endpoint: Buffer is loaded and ready to be sent.
169 * OUT Endpoint: Buffer is free to be written to by SIE.
171 * Input: byte buffer_dsc: Root name of the buffer descriptor group.
172 * i.e. ep0Bo, ep1Bi, ... Declared in usbmmap.c
173 * Names can be remapped for readability, see examples in
174 * usbcfg.h (#define HID_BD_OUT ep1Bo)
180 * Overview: This macro should be called each time after:
181 * 1. A non-EP0 IN endpoint buffer is populated with data.
182 * 2. A non-EP0 OUT endpoint buffer is read.
183 * This macro turns the buffer ownership to SIE for servicing.
184 * It also toggles the DTS bit for synchronization.
187 *****************************************************************************/
188 #define mUSBBufferReady(buffer_dsc) \
190 buffer_dsc.Stat._byte &= _DTSMASK; /* Save only DTS bit */ \
191 buffer_dsc.Stat.DTS = !buffer_dsc.Stat.DTS; /* Toggle DTS bit */ \
192 buffer_dsc.Stat._byte |= _USIE|_DTSEN; /* Turn ownership to SIE */ \
195 /** T Y P E S ****************************************************************/
197 /** E X T E R N S ************************************************************/
199 /** P U B L I C P R O T O T Y P E S *****************************************/
200 void USBCheckBusStatus(void);
201 void USBDriverService(void);
202 void USBRemoteWakeup(void);
203 void USBSoftDetach(void);
205 void ClearArray(byte* startAdr,byte count);