00001 /********************************************************************* 00002 * 00003 * Microchip USB C18 Firmware Version 1.0 00004 * 00005 ********************************************************************* 00006 * FileName: usb9.c 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 11/19/04 Original. 00035 ********************************************************************/ 00036 00037 /** I N C L U D E S **********************************************************/ 00038 #include <p18cxxx.h> 00039 #include "system\typedefs.h" 00040 #include "system\usb\usb.h" 00041 #include "io_cfg.h" // Required for self_power status 00042 00043 /** V A R I A B L E S ********************************************************/ 00044 #pragma udata 00045 00046 /** P R I V A T E P R O T O T Y P E S ***************************************/ 00047 void USBStdGetDscHandler(void); 00048 void USBStdSetCfgHandler(void); 00049 void USBStdGetStatusHandler(void); 00050 void USBStdFeatureReqHandler(void); 00051 00052 /** D E C L A R A T I O N S **************************************************/ 00053 #pragma code 00054 /****************************************************************************** 00055 * Function: void USBCheckStdRequest(void) 00056 * 00057 * PreCondition: None 00058 * 00059 * Input: None 00060 * 00061 * Output: None 00062 * 00063 * Side Effects: None 00064 * 00065 * Overview: This routine checks the setup data packet to see if it 00066 * knows how to handle it 00067 * 00068 * Note: None 00069 *****************************************************************************/ 00070 void USBCheckStdRequest(void) 00071 { 00072 if(SetupPkt.RequestType != STANDARD) return; 00073 00074 switch(SetupPkt.bRequest) 00075 { 00076 case SET_ADR: 00077 ctrl_trf_session_owner = MUID_USB9; 00078 usb_device_state = ADR_PENDING_STATE; // Update state only 00079 /* See USBCtrlTrfInHandler() in usbctrltrf.c for the next step */ 00080 break; 00081 case GET_DSC: 00082 USBStdGetDscHandler(); 00083 break; 00084 case SET_CFG: 00085 USBStdSetCfgHandler(); 00086 break; 00087 case GET_CFG: 00088 ctrl_trf_session_owner = MUID_USB9; 00089 pSrc.bRam = (byte*)&usb_active_cfg; // Set Source 00090 usb_stat.ctrl_trf_mem = _RAM; // Set memory type 00091 LSB(wCount) = 1; // Set data count 00092 break; 00093 case GET_STATUS: 00094 USBStdGetStatusHandler(); 00095 break; 00096 case CLR_FEATURE: 00097 case SET_FEATURE: 00098 USBStdFeatureReqHandler(); 00099 break; 00100 case GET_INTF: 00101 ctrl_trf_session_owner = MUID_USB9; 00102 pSrc.bRam = (byte*)&usb_alt_intf+SetupPkt.bIntfID; // Set source 00103 usb_stat.ctrl_trf_mem = _RAM; // Set memory type 00104 LSB(wCount) = 1; // Set data count 00105 break; 00106 case SET_INTF: 00107 ctrl_trf_session_owner = MUID_USB9; 00108 usb_alt_intf[SetupPkt.bIntfID] = SetupPkt.bAltID; 00109 break; 00110 case SET_DSC: 00111 case SYNCH_FRAME: 00112 default: 00113 break; 00114 }//end switch 00115 00116 }//end USBCheckStdRequest 00117 00118 /****************************************************************************** 00119 * Function: void USBStdGetDscHandler(void) 00120 * 00121 * PreCondition: None 00122 * 00123 * Input: None 00124 * 00125 * Output: None 00126 * 00127 * Side Effects: None 00128 * 00129 * Overview: This routine handles the standard GET_DESCRIPTOR request. 00130 * It utilizes tables dynamically looks up descriptor size. 00131 * This routine should never have to be modified if the tables 00132 * in usbdsc.c are declared correctly. 00133 * 00134 * Note: None 00135 *****************************************************************************/ 00136 void USBStdGetDscHandler(void) 00137 { 00138 if(SetupPkt.bmRequestType == 0x80) 00139 { 00140 switch(SetupPkt.bDscType) 00141 { 00142 case DSC_DEV: 00143 ctrl_trf_session_owner = MUID_USB9; 00144 pSrc.bRom = (rom byte*)&device_dsc; 00145 wCount._word = sizeof(device_dsc); // Set data count 00146 break; 00147 case DSC_CFG: 00148 ctrl_trf_session_owner = MUID_USB9; 00149 pSrc.bRom = *(USB_CD_Ptr+SetupPkt.bDscIndex); 00150 wCount._word = *(pSrc.wRom+1); // Set data count 00151 break; 00152 case DSC_STR: 00153 ctrl_trf_session_owner = MUID_USB9; 00154 pSrc.bRom = *(USB_SD_Ptr+SetupPkt.bDscIndex); 00155 wCount._word = *pSrc.bRom; // Set data count 00156 break; 00157 }//end switch 00158 00159 usb_stat.ctrl_trf_mem = _ROM; // Set memory type 00160 }//end if 00161 }//end USBStdGetDscHandler 00162 00163 /****************************************************************************** 00164 * Function: void USBStdSetCfgHandler(void) 00165 * 00166 * PreCondition: None 00167 * 00168 * Input: None 00169 * 00170 * Output: None 00171 * 00172 * Side Effects: None 00173 * 00174 * Overview: This routine first disables all endpoints by clearing 00175 * UEP registers. It then configures (initializes) endpoints 00176 * specified in the modifiable section. 00177 * 00178 * Note: None 00179 *****************************************************************************/ 00180 void USBStdSetCfgHandler(void) 00181 { 00182 ctrl_trf_session_owner = MUID_USB9; 00183 mDisableEP1to15(); // See usbdrv.h 00184 ClearArray((byte*)&usb_alt_intf,MAX_NUM_INT); 00185 usb_active_cfg = SetupPkt.bCfgValue; 00186 if(SetupPkt.bCfgValue == 0) 00187 usb_device_state = ADDRESS_STATE; 00188 else 00189 { 00190 usb_device_state = CONFIGURED_STATE; 00191 00192 /* Modifiable Section */ 00193 00194 #if defined(USB_USE_CDC) // See autofiles\usbcfg.h 00195 CDCInitEP(); 00196 #endif 00197 00198 /* End modifiable section */ 00199 00200 }//end if(SetupPkt.bcfgValue == 0) 00201 }//end USBStdSetCfgHandler 00202 00203 /****************************************************************************** 00204 * Function: void USBStdGetStatusHandler(void) 00205 * 00206 * PreCondition: None 00207 * 00208 * Input: None 00209 * 00210 * Output: None 00211 * 00212 * Side Effects: None 00213 * 00214 * Overview: This routine handles the standard GET_STATUS request 00215 * 00216 * Note: None 00217 *****************************************************************************/ 00218 void USBStdGetStatusHandler(void) 00219 { 00220 CtrlTrfData._byte0 = 0; // Initialize content 00221 CtrlTrfData._byte1 = 0; 00222 00223 switch(SetupPkt.Recipient) 00224 { 00225 case RCPT_DEV: 00226 ctrl_trf_session_owner = MUID_USB9; 00227 /* 00228 * _byte0: bit0: Self-Powered Status [0] Bus-Powered [1] Self-Powered 00229 * bit1: RemoteWakeup [0] Disabled [1] Enabled 00230 */ 00231 if(self_power == 1) // self_power defined in io_cfg.h 00232 CtrlTrfData._byte0|=0b000000001; // Set bit0 00233 00234 if(usb_stat.RemoteWakeup == 1) // usb_stat defined in usbmmap.c 00235 CtrlTrfData._byte0|=0b00000010; // Set bit1 00236 break; 00237 case RCPT_INTF: 00238 ctrl_trf_session_owner = MUID_USB9; // No data to update 00239 break; 00240 case RCPT_EP: 00241 ctrl_trf_session_owner = MUID_USB9; 00242 /* 00243 * _byte0: bit0: Halt Status [0] Not Halted [1] Halted 00244 */ 00245 pDst.bRam = (byte*)&ep0Bo+(SetupPkt.EPNum*8)+(SetupPkt.EPDir*4); 00246 if(*pDst.bRam & _BSTALL) // Use _BSTALL as a bit mask 00247 CtrlTrfData._byte0=0x01;// Set bit0 00248 break; 00249 }//end switch 00250 00251 if(ctrl_trf_session_owner == MUID_USB9) 00252 { 00253 pSrc.bRam = (byte*)&CtrlTrfData; // Set Source 00254 usb_stat.ctrl_trf_mem = _RAM; // Set memory type 00255 LSB(wCount) = 2; // Set data count 00256 }//end if(...) 00257 }//end USBStdGetStatusHandler 00258 00259 /****************************************************************************** 00260 * Function: void USBStdFeatureReqHandler(void) 00261 * 00262 * PreCondition: None 00263 * 00264 * Input: None 00265 * 00266 * Output: None 00267 * 00268 * Side Effects: None 00269 * 00270 * Overview: This routine handles the standard SET & CLEAR FEATURES 00271 * requests 00272 * 00273 * Note: None 00274 *****************************************************************************/ 00275 void USBStdFeatureReqHandler(void) 00276 { 00277 if((SetupPkt.bFeature == DEVICE_REMOTE_WAKEUP)&& 00278 (SetupPkt.Recipient == RCPT_DEV)) 00279 { 00280 ctrl_trf_session_owner = MUID_USB9; 00281 if(SetupPkt.bRequest == SET_FEATURE) 00282 usb_stat.RemoteWakeup = 1; 00283 else 00284 usb_stat.RemoteWakeup = 0; 00285 }//end if 00286 00287 if((SetupPkt.bFeature == ENDPOINT_HALT)&& 00288 (SetupPkt.Recipient == RCPT_EP)&& 00289 (SetupPkt.EPNum != 0)) 00290 { 00291 ctrl_trf_session_owner = MUID_USB9; 00292 /* Must do address calculation here */ 00293 pDst.bRam = (byte*)&ep0Bo+(SetupPkt.EPNum*8)+(SetupPkt.EPDir*4); 00294 00295 if(SetupPkt.bRequest == SET_FEATURE) 00296 *pDst.bRam = _USIE|_BSTALL; 00297 else 00298 { 00299 if(SetupPkt.EPDir == 1) // IN 00300 *pDst.bRam = _UCPU; 00301 else 00302 *pDst.bRam = _USIE|_DAT0|_DTSEN; 00303 }//end if 00304 }//end if 00305 }//end USBStdFeatureReqHandler 00306 00307 /** EOF usb9.c ***************************************************************/