firmware/usb9.c
changeset 2 2f55e5dd591d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/firmware/usb9.c	Tue Jan 29 22:31:52 2008 +0100
     1.3 @@ -0,0 +1,307 @@
     1.4 +/*********************************************************************
     1.5 + *
     1.6 + *                Microchip USB C18 Firmware Version 1.0
     1.7 + *
     1.8 + *********************************************************************
     1.9 + * FileName:        usb9.c
    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       11/19/04    Original.
    1.38 + ********************************************************************/
    1.39 +
    1.40 +/** I N C L U D E S **********************************************************/
    1.41 +#include <p18cxxx.h>
    1.42 +#include "typedefs.h"
    1.43 +#include "usb.h"
    1.44 +#include "io_cfg.h"                     // Required for self_power status
    1.45 +
    1.46 +/** V A R I A B L E S ********************************************************/
    1.47 +#pragma udata
    1.48 +
    1.49 +/** P R I V A T E  P R O T O T Y P E S ***************************************/
    1.50 +void USBStdGetDscHandler(void);
    1.51 +void USBStdSetCfgHandler(void);
    1.52 +void USBStdGetStatusHandler(void);
    1.53 +void USBStdFeatureReqHandler(void);
    1.54 +
    1.55 +/** D E C L A R A T I O N S **************************************************/
    1.56 +#pragma code
    1.57 +/******************************************************************************
    1.58 + * Function:        void USBCheckStdRequest(void)
    1.59 + *
    1.60 + * PreCondition:    None
    1.61 + *
    1.62 + * Input:           None
    1.63 + *
    1.64 + * Output:          None
    1.65 + *
    1.66 + * Side Effects:    None
    1.67 + *
    1.68 + * Overview:        This routine checks the setup data packet to see if it
    1.69 + *                  knows how to handle it
    1.70 + *
    1.71 + * Note:            None
    1.72 + *****************************************************************************/
    1.73 +void USBCheckStdRequest(void)
    1.74 +{   
    1.75 +    if(SetupPkt.RequestType != STANDARD) return;
    1.76 +    
    1.77 +    switch(SetupPkt.bRequest)
    1.78 +    {
    1.79 +        case SET_ADR:
    1.80 +            ctrl_trf_session_owner = MUID_USB9;
    1.81 +            usb_device_state = ADR_PENDING_STATE;       // Update state only
    1.82 +            /* See USBCtrlTrfInHandler() in usbctrltrf.c for the next step */
    1.83 +            break;
    1.84 +        case GET_DSC:
    1.85 +            USBStdGetDscHandler();
    1.86 +            break;
    1.87 +        case SET_CFG:
    1.88 +            USBStdSetCfgHandler();
    1.89 +            break;
    1.90 +        case GET_CFG:
    1.91 +            ctrl_trf_session_owner = MUID_USB9;
    1.92 +            pSrc.bRam = (byte*)&usb_active_cfg;         // Set Source
    1.93 +            usb_stat.ctrl_trf_mem = _RAM;               // Set memory type
    1.94 +            LSB(wCount) = 1;                            // Set data count
    1.95 +            break;
    1.96 +        case GET_STATUS:
    1.97 +            USBStdGetStatusHandler();
    1.98 +            break;
    1.99 +        case CLR_FEATURE:
   1.100 +        case SET_FEATURE:
   1.101 +            USBStdFeatureReqHandler();
   1.102 +            break;
   1.103 +        case GET_INTF:
   1.104 +            ctrl_trf_session_owner = MUID_USB9;
   1.105 +            pSrc.bRam = (byte*)&usb_alt_intf+SetupPkt.bIntfID;  // Set source
   1.106 +            usb_stat.ctrl_trf_mem = _RAM;               // Set memory type
   1.107 +            LSB(wCount) = 1;                            // Set data count
   1.108 +            break;
   1.109 +        case SET_INTF:
   1.110 +            ctrl_trf_session_owner = MUID_USB9;
   1.111 +            usb_alt_intf[SetupPkt.bIntfID] = SetupPkt.bAltID;
   1.112 +            break;
   1.113 +        case SET_DSC:
   1.114 +        case SYNCH_FRAME:
   1.115 +        default:
   1.116 +            break;
   1.117 +    }//end switch
   1.118 +    
   1.119 +}//end USBCheckStdRequest
   1.120 +
   1.121 +/******************************************************************************
   1.122 + * Function:        void USBStdGetDscHandler(void)
   1.123 + *
   1.124 + * PreCondition:    None
   1.125 + *
   1.126 + * Input:           None
   1.127 + *
   1.128 + * Output:          None
   1.129 + *
   1.130 + * Side Effects:    None
   1.131 + *
   1.132 + * Overview:        This routine handles the standard GET_DESCRIPTOR request.
   1.133 + *                  It utilizes tables dynamically looks up descriptor size.
   1.134 + *                  This routine should never have to be modified if the tables
   1.135 + *                  in usbdsc.c are declared correctly.
   1.136 + *
   1.137 + * Note:            None
   1.138 + *****************************************************************************/
   1.139 +void USBStdGetDscHandler(void)
   1.140 +{
   1.141 +    if(SetupPkt.bmRequestType == 0x80)
   1.142 +    {
   1.143 +        switch(SetupPkt.bDscType)
   1.144 +        {
   1.145 +            case DSC_DEV:
   1.146 +                ctrl_trf_session_owner = MUID_USB9;
   1.147 +                pSrc.bRom = (rom byte*)&device_dsc;
   1.148 +                wCount._word = sizeof(device_dsc);          // Set data count
   1.149 +                break;
   1.150 +            case DSC_CFG:
   1.151 +                ctrl_trf_session_owner = MUID_USB9;
   1.152 +                pSrc.bRom = *(USB_CD_Ptr+SetupPkt.bDscIndex);
   1.153 +                wCount._word = *(pSrc.wRom+1);              // Set data count
   1.154 +                break;
   1.155 +            case DSC_STR:
   1.156 +                ctrl_trf_session_owner = MUID_USB9;
   1.157 +                pSrc.bRom = *(USB_SD_Ptr+SetupPkt.bDscIndex);
   1.158 +                wCount._word = *pSrc.bRom;                  // Set data count
   1.159 +                break;
   1.160 +        }//end switch
   1.161 +        
   1.162 +        usb_stat.ctrl_trf_mem = _ROM;                       // Set memory type
   1.163 +    }//end if
   1.164 +}//end USBStdGetDscHandler
   1.165 +
   1.166 +/******************************************************************************
   1.167 + * Function:        void USBStdSetCfgHandler(void)
   1.168 + *
   1.169 + * PreCondition:    None
   1.170 + *
   1.171 + * Input:           None
   1.172 + *
   1.173 + * Output:          None
   1.174 + *
   1.175 + * Side Effects:    None
   1.176 + *
   1.177 + * Overview:        This routine first disables all endpoints by clearing
   1.178 + *                  UEP registers. It then configures (initializes) endpoints
   1.179 + *                  specified in the modifiable section.
   1.180 + *
   1.181 + * Note:            None
   1.182 + *****************************************************************************/
   1.183 +void USBStdSetCfgHandler(void)
   1.184 +{
   1.185 +    ctrl_trf_session_owner = MUID_USB9;
   1.186 +    mDisableEP1to15();                          // See usbdrv.h
   1.187 +    ClearArray((byte*)&usb_alt_intf,MAX_NUM_INT);
   1.188 +    usb_active_cfg = SetupPkt.bCfgValue;
   1.189 +    if(SetupPkt.bCfgValue == 0)
   1.190 +        usb_device_state = ADDRESS_STATE;
   1.191 +    else
   1.192 +    {
   1.193 +        usb_device_state = CONFIGURED_STATE;
   1.194 +
   1.195 +        /* Modifiable Section */
   1.196 +
   1.197 +        #if defined(USB_USE_CDC)                // See autofiles\usbcfg.h
   1.198 +        CDCInitEP();
   1.199 +        #endif
   1.200 +
   1.201 +        /* End modifiable section */
   1.202 +
   1.203 +    }//end if(SetupPkt.bcfgValue == 0)
   1.204 +}//end USBStdSetCfgHandler
   1.205 +
   1.206 +/******************************************************************************
   1.207 + * Function:        void USBStdGetStatusHandler(void)
   1.208 + *
   1.209 + * PreCondition:    None
   1.210 + *
   1.211 + * Input:           None
   1.212 + *
   1.213 + * Output:          None
   1.214 + *
   1.215 + * Side Effects:    None
   1.216 + *
   1.217 + * Overview:        This routine handles the standard GET_STATUS request
   1.218 + *
   1.219 + * Note:            None
   1.220 + *****************************************************************************/
   1.221 +void USBStdGetStatusHandler(void)
   1.222 +{
   1.223 +    CtrlTrfData._byte0 = 0;                         // Initialize content
   1.224 +    CtrlTrfData._byte1 = 0;
   1.225 +        
   1.226 +    switch(SetupPkt.Recipient)
   1.227 +    {
   1.228 +        case RCPT_DEV:
   1.229 +            ctrl_trf_session_owner = MUID_USB9;
   1.230 +            /*
   1.231 +             * _byte0: bit0: Self-Powered Status [0] Bus-Powered [1] Self-Powered
   1.232 +             *         bit1: RemoteWakeup        [0] Disabled    [1] Enabled
   1.233 +             */
   1.234 +            if(self_power == 1)                     // self_power defined in io_cfg.h
   1.235 +                CtrlTrfData._byte0|=0b000000001;    // Set bit0
   1.236 +            
   1.237 +            if(usb_stat.RemoteWakeup == 1)          // usb_stat defined in usbmmap.c
   1.238 +                CtrlTrfData._byte0|=0b00000010;     // Set bit1
   1.239 +            break;
   1.240 +        case RCPT_INTF:
   1.241 +            ctrl_trf_session_owner = MUID_USB9;     // No data to update
   1.242 +            break;
   1.243 +        case RCPT_EP:
   1.244 +            ctrl_trf_session_owner = MUID_USB9;
   1.245 +            /*
   1.246 +             * _byte0: bit0: Halt Status [0] Not Halted [1] Halted
   1.247 +             */
   1.248 +            pDst.bRam = (byte*)&ep0Bo+(SetupPkt.EPNum*8)+(SetupPkt.EPDir*4);
   1.249 +            if(*pDst.bRam & _BSTALL)    // Use _BSTALL as a bit mask
   1.250 +                CtrlTrfData._byte0=0x01;// Set bit0
   1.251 +            break;
   1.252 +    }//end switch
   1.253 +    
   1.254 +    if(ctrl_trf_session_owner == MUID_USB9)
   1.255 +    {
   1.256 +        pSrc.bRam = (byte*)&CtrlTrfData;            // Set Source
   1.257 +        usb_stat.ctrl_trf_mem = _RAM;               // Set memory type
   1.258 +        LSB(wCount) = 2;                            // Set data count
   1.259 +    }//end if(...)
   1.260 +}//end USBStdGetStatusHandler
   1.261 +
   1.262 +/******************************************************************************
   1.263 + * Function:        void USBStdFeatureReqHandler(void)
   1.264 + *
   1.265 + * PreCondition:    None
   1.266 + *
   1.267 + * Input:           None
   1.268 + *
   1.269 + * Output:          None
   1.270 + *
   1.271 + * Side Effects:    None
   1.272 + *
   1.273 + * Overview:        This routine handles the standard SET & CLEAR FEATURES
   1.274 + *                  requests
   1.275 + *
   1.276 + * Note:            None
   1.277 + *****************************************************************************/
   1.278 +void USBStdFeatureReqHandler(void)
   1.279 +{
   1.280 +    if((SetupPkt.bFeature == DEVICE_REMOTE_WAKEUP)&&
   1.281 +       (SetupPkt.Recipient == RCPT_DEV))
   1.282 +    {
   1.283 +        ctrl_trf_session_owner = MUID_USB9;
   1.284 +        if(SetupPkt.bRequest == SET_FEATURE)
   1.285 +            usb_stat.RemoteWakeup = 1;
   1.286 +        else
   1.287 +            usb_stat.RemoteWakeup = 0;
   1.288 +    }//end if
   1.289 +    
   1.290 +    if((SetupPkt.bFeature == ENDPOINT_HALT)&&
   1.291 +       (SetupPkt.Recipient == RCPT_EP)&&
   1.292 +       (SetupPkt.EPNum != 0))
   1.293 +    {
   1.294 +        ctrl_trf_session_owner = MUID_USB9;
   1.295 +        /* Must do address calculation here */
   1.296 +        pDst.bRam = (byte*)&ep0Bo+(SetupPkt.EPNum*8)+(SetupPkt.EPDir*4);
   1.297 +        
   1.298 +        if(SetupPkt.bRequest == SET_FEATURE)
   1.299 +            *pDst.bRam = _USIE|_BSTALL;
   1.300 +        else
   1.301 +        {
   1.302 +            if(SetupPkt.EPDir == 1) // IN
   1.303 +                *pDst.bRam = _UCPU;
   1.304 +            else
   1.305 +                *pDst.bRam = _USIE|_DAT0|_DTSEN;
   1.306 +        }//end if
   1.307 +    }//end if
   1.308 +}//end USBStdFeatureReqHandler
   1.309 +
   1.310 +/** EOF usb9.c ***************************************************************/