firmware/usb9.c
author cpresser@rika
Thu, 07 Feb 2008 00:18:02 +0000
changeset 10 9798f65f2f35
parent 2 2f55e5dd591d
permissions -rw-r--r--
thx @arghgra. defined variable != declared variable
slime@2
     1
/*********************************************************************
slime@2
     2
 *
slime@2
     3
 *                Microchip USB C18 Firmware Version 1.0
slime@2
     4
 *
slime@2
     5
 *********************************************************************
slime@2
     6
 * FileName:        usb9.c
slime@2
     7
 * Dependencies:    See INCLUDES section below
slime@2
     8
 * Processor:       PIC18
slime@2
     9
 * Compiler:        C18 2.30.01+
slime@2
    10
 * Company:         Microchip Technology, Inc.
slime@2
    11
 *
slime@2
    12
 * Software License Agreement
slime@2
    13
 *
slime@2
    14
 * The software supplied herewith by Microchip Technology Incorporated
slime@2
    15
 * (the “Company”) for its PICmicro® Microcontroller is intended and
slime@2
    16
 * supplied to you, the Company’s customer, for use solely and
slime@2
    17
 * exclusively on Microchip PICmicro Microcontroller products. The
slime@2
    18
 * software is owned by the Company and/or its supplier, and is
slime@2
    19
 * protected under applicable copyright laws. All rights are reserved.
slime@2
    20
 * Any use in violation of the foregoing restrictions may subject the
slime@2
    21
 * user to criminal sanctions under applicable laws, as well as to
slime@2
    22
 * civil liability for the breach of the terms and conditions of this
slime@2
    23
 * license.
slime@2
    24
 *
slime@2
    25
 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
slime@2
    26
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
slime@2
    27
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
slime@2
    28
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
slime@2
    29
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
slime@2
    30
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
slime@2
    31
 *
slime@2
    32
 * Author               Date        Comment
slime@2
    33
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
slime@2
    34
 * Rawin Rojvanit       11/19/04    Original.
slime@2
    35
 ********************************************************************/
slime@2
    36
slime@2
    37
/** I N C L U D E S **********************************************************/
slime@2
    38
#include <p18cxxx.h>
slime@2
    39
#include "typedefs.h"
slime@2
    40
#include "usb.h"
slime@2
    41
#include "io_cfg.h"                     // Required for self_power status
slime@2
    42
slime@2
    43
/** V A R I A B L E S ********************************************************/
slime@2
    44
#pragma udata
slime@2
    45
slime@2
    46
/** P R I V A T E  P R O T O T Y P E S ***************************************/
slime@2
    47
void USBStdGetDscHandler(void);
slime@2
    48
void USBStdSetCfgHandler(void);
slime@2
    49
void USBStdGetStatusHandler(void);
slime@2
    50
void USBStdFeatureReqHandler(void);
slime@2
    51
slime@2
    52
/** D E C L A R A T I O N S **************************************************/
slime@2
    53
#pragma code
slime@2
    54
/******************************************************************************
slime@2
    55
 * Function:        void USBCheckStdRequest(void)
slime@2
    56
 *
slime@2
    57
 * PreCondition:    None
slime@2
    58
 *
slime@2
    59
 * Input:           None
slime@2
    60
 *
slime@2
    61
 * Output:          None
slime@2
    62
 *
slime@2
    63
 * Side Effects:    None
slime@2
    64
 *
slime@2
    65
 * Overview:        This routine checks the setup data packet to see if it
slime@2
    66
 *                  knows how to handle it
slime@2
    67
 *
slime@2
    68
 * Note:            None
slime@2
    69
 *****************************************************************************/
slime@2
    70
void USBCheckStdRequest(void)
slime@2
    71
{   
slime@2
    72
    if(SetupPkt.RequestType != STANDARD) return;
slime@2
    73
    
slime@2
    74
    switch(SetupPkt.bRequest)
slime@2
    75
    {
slime@2
    76
        case SET_ADR:
slime@2
    77
            ctrl_trf_session_owner = MUID_USB9;
slime@2
    78
            usb_device_state = ADR_PENDING_STATE;       // Update state only
slime@2
    79
            /* See USBCtrlTrfInHandler() in usbctrltrf.c for the next step */
slime@2
    80
            break;
slime@2
    81
        case GET_DSC:
slime@2
    82
            USBStdGetDscHandler();
slime@2
    83
            break;
slime@2
    84
        case SET_CFG:
slime@2
    85
            USBStdSetCfgHandler();
slime@2
    86
            break;
slime@2
    87
        case GET_CFG:
slime@2
    88
            ctrl_trf_session_owner = MUID_USB9;
slime@2
    89
            pSrc.bRam = (byte*)&usb_active_cfg;         // Set Source
slime@2
    90
            usb_stat.ctrl_trf_mem = _RAM;               // Set memory type
slime@2
    91
            LSB(wCount) = 1;                            // Set data count
slime@2
    92
            break;
slime@2
    93
        case GET_STATUS:
slime@2
    94
            USBStdGetStatusHandler();
slime@2
    95
            break;
slime@2
    96
        case CLR_FEATURE:
slime@2
    97
        case SET_FEATURE:
slime@2
    98
            USBStdFeatureReqHandler();
slime@2
    99
            break;
slime@2
   100
        case GET_INTF:
slime@2
   101
            ctrl_trf_session_owner = MUID_USB9;
slime@2
   102
            pSrc.bRam = (byte*)&usb_alt_intf+SetupPkt.bIntfID;  // Set source
slime@2
   103
            usb_stat.ctrl_trf_mem = _RAM;               // Set memory type
slime@2
   104
            LSB(wCount) = 1;                            // Set data count
slime@2
   105
            break;
slime@2
   106
        case SET_INTF:
slime@2
   107
            ctrl_trf_session_owner = MUID_USB9;
slime@2
   108
            usb_alt_intf[SetupPkt.bIntfID] = SetupPkt.bAltID;
slime@2
   109
            break;
slime@2
   110
        case SET_DSC:
slime@2
   111
        case SYNCH_FRAME:
slime@2
   112
        default:
slime@2
   113
            break;
slime@2
   114
    }//end switch
slime@2
   115
    
slime@2
   116
}//end USBCheckStdRequest
slime@2
   117
slime@2
   118
/******************************************************************************
slime@2
   119
 * Function:        void USBStdGetDscHandler(void)
slime@2
   120
 *
slime@2
   121
 * PreCondition:    None
slime@2
   122
 *
slime@2
   123
 * Input:           None
slime@2
   124
 *
slime@2
   125
 * Output:          None
slime@2
   126
 *
slime@2
   127
 * Side Effects:    None
slime@2
   128
 *
slime@2
   129
 * Overview:        This routine handles the standard GET_DESCRIPTOR request.
slime@2
   130
 *                  It utilizes tables dynamically looks up descriptor size.
slime@2
   131
 *                  This routine should never have to be modified if the tables
slime@2
   132
 *                  in usbdsc.c are declared correctly.
slime@2
   133
 *
slime@2
   134
 * Note:            None
slime@2
   135
 *****************************************************************************/
slime@2
   136
void USBStdGetDscHandler(void)
slime@2
   137
{
slime@2
   138
    if(SetupPkt.bmRequestType == 0x80)
slime@2
   139
    {
slime@2
   140
        switch(SetupPkt.bDscType)
slime@2
   141
        {
slime@2
   142
            case DSC_DEV:
slime@2
   143
                ctrl_trf_session_owner = MUID_USB9;
slime@2
   144
                pSrc.bRom = (rom byte*)&device_dsc;
slime@2
   145
                wCount._word = sizeof(device_dsc);          // Set data count
slime@2
   146
                break;
slime@2
   147
            case DSC_CFG:
slime@2
   148
                ctrl_trf_session_owner = MUID_USB9;
slime@2
   149
                pSrc.bRom = *(USB_CD_Ptr+SetupPkt.bDscIndex);
slime@2
   150
                wCount._word = *(pSrc.wRom+1);              // Set data count
slime@2
   151
                break;
slime@2
   152
            case DSC_STR:
slime@2
   153
                ctrl_trf_session_owner = MUID_USB9;
slime@2
   154
                pSrc.bRom = *(USB_SD_Ptr+SetupPkt.bDscIndex);
slime@2
   155
                wCount._word = *pSrc.bRom;                  // Set data count
slime@2
   156
                break;
slime@2
   157
        }//end switch
slime@2
   158
        
slime@2
   159
        usb_stat.ctrl_trf_mem = _ROM;                       // Set memory type
slime@2
   160
    }//end if
slime@2
   161
}//end USBStdGetDscHandler
slime@2
   162
slime@2
   163
/******************************************************************************
slime@2
   164
 * Function:        void USBStdSetCfgHandler(void)
slime@2
   165
 *
slime@2
   166
 * PreCondition:    None
slime@2
   167
 *
slime@2
   168
 * Input:           None
slime@2
   169
 *
slime@2
   170
 * Output:          None
slime@2
   171
 *
slime@2
   172
 * Side Effects:    None
slime@2
   173
 *
slime@2
   174
 * Overview:        This routine first disables all endpoints by clearing
slime@2
   175
 *                  UEP registers. It then configures (initializes) endpoints
slime@2
   176
 *                  specified in the modifiable section.
slime@2
   177
 *
slime@2
   178
 * Note:            None
slime@2
   179
 *****************************************************************************/
slime@2
   180
void USBStdSetCfgHandler(void)
slime@2
   181
{
slime@2
   182
    ctrl_trf_session_owner = MUID_USB9;
slime@2
   183
    mDisableEP1to15();                          // See usbdrv.h
slime@2
   184
    ClearArray((byte*)&usb_alt_intf,MAX_NUM_INT);
slime@2
   185
    usb_active_cfg = SetupPkt.bCfgValue;
slime@2
   186
    if(SetupPkt.bCfgValue == 0)
slime@2
   187
        usb_device_state = ADDRESS_STATE;
slime@2
   188
    else
slime@2
   189
    {
slime@2
   190
        usb_device_state = CONFIGURED_STATE;
slime@2
   191
slime@2
   192
        /* Modifiable Section */
slime@2
   193
slime@2
   194
        #if defined(USB_USE_CDC)                // See autofiles\usbcfg.h
slime@2
   195
        CDCInitEP();
slime@2
   196
        #endif
slime@2
   197
slime@2
   198
        /* End modifiable section */
slime@2
   199
slime@2
   200
    }//end if(SetupPkt.bcfgValue == 0)
slime@2
   201
}//end USBStdSetCfgHandler
slime@2
   202
slime@2
   203
/******************************************************************************
slime@2
   204
 * Function:        void USBStdGetStatusHandler(void)
slime@2
   205
 *
slime@2
   206
 * PreCondition:    None
slime@2
   207
 *
slime@2
   208
 * Input:           None
slime@2
   209
 *
slime@2
   210
 * Output:          None
slime@2
   211
 *
slime@2
   212
 * Side Effects:    None
slime@2
   213
 *
slime@2
   214
 * Overview:        This routine handles the standard GET_STATUS request
slime@2
   215
 *
slime@2
   216
 * Note:            None
slime@2
   217
 *****************************************************************************/
slime@2
   218
void USBStdGetStatusHandler(void)
slime@2
   219
{
slime@2
   220
    CtrlTrfData._byte0 = 0;                         // Initialize content
slime@2
   221
    CtrlTrfData._byte1 = 0;
slime@2
   222
        
slime@2
   223
    switch(SetupPkt.Recipient)
slime@2
   224
    {
slime@2
   225
        case RCPT_DEV:
slime@2
   226
            ctrl_trf_session_owner = MUID_USB9;
slime@2
   227
            /*
slime@2
   228
             * _byte0: bit0: Self-Powered Status [0] Bus-Powered [1] Self-Powered
slime@2
   229
             *         bit1: RemoteWakeup        [0] Disabled    [1] Enabled
slime@2
   230
             */
slime@2
   231
            if(self_power == 1)                     // self_power defined in io_cfg.h
slime@2
   232
                CtrlTrfData._byte0|=0b000000001;    // Set bit0
slime@2
   233
            
slime@2
   234
            if(usb_stat.RemoteWakeup == 1)          // usb_stat defined in usbmmap.c
slime@2
   235
                CtrlTrfData._byte0|=0b00000010;     // Set bit1
slime@2
   236
            break;
slime@2
   237
        case RCPT_INTF:
slime@2
   238
            ctrl_trf_session_owner = MUID_USB9;     // No data to update
slime@2
   239
            break;
slime@2
   240
        case RCPT_EP:
slime@2
   241
            ctrl_trf_session_owner = MUID_USB9;
slime@2
   242
            /*
slime@2
   243
             * _byte0: bit0: Halt Status [0] Not Halted [1] Halted
slime@2
   244
             */
slime@2
   245
            pDst.bRam = (byte*)&ep0Bo+(SetupPkt.EPNum*8)+(SetupPkt.EPDir*4);
slime@2
   246
            if(*pDst.bRam & _BSTALL)    // Use _BSTALL as a bit mask
slime@2
   247
                CtrlTrfData._byte0=0x01;// Set bit0
slime@2
   248
            break;
slime@2
   249
    }//end switch
slime@2
   250
    
slime@2
   251
    if(ctrl_trf_session_owner == MUID_USB9)
slime@2
   252
    {
slime@2
   253
        pSrc.bRam = (byte*)&CtrlTrfData;            // Set Source
slime@2
   254
        usb_stat.ctrl_trf_mem = _RAM;               // Set memory type
slime@2
   255
        LSB(wCount) = 2;                            // Set data count
slime@2
   256
    }//end if(...)
slime@2
   257
}//end USBStdGetStatusHandler
slime@2
   258
slime@2
   259
/******************************************************************************
slime@2
   260
 * Function:        void USBStdFeatureReqHandler(void)
slime@2
   261
 *
slime@2
   262
 * PreCondition:    None
slime@2
   263
 *
slime@2
   264
 * Input:           None
slime@2
   265
 *
slime@2
   266
 * Output:          None
slime@2
   267
 *
slime@2
   268
 * Side Effects:    None
slime@2
   269
 *
slime@2
   270
 * Overview:        This routine handles the standard SET & CLEAR FEATURES
slime@2
   271
 *                  requests
slime@2
   272
 *
slime@2
   273
 * Note:            None
slime@2
   274
 *****************************************************************************/
slime@2
   275
void USBStdFeatureReqHandler(void)
slime@2
   276
{
slime@2
   277
    if((SetupPkt.bFeature == DEVICE_REMOTE_WAKEUP)&&
slime@2
   278
       (SetupPkt.Recipient == RCPT_DEV))
slime@2
   279
    {
slime@2
   280
        ctrl_trf_session_owner = MUID_USB9;
slime@2
   281
        if(SetupPkt.bRequest == SET_FEATURE)
slime@2
   282
            usb_stat.RemoteWakeup = 1;
slime@2
   283
        else
slime@2
   284
            usb_stat.RemoteWakeup = 0;
slime@2
   285
    }//end if
slime@2
   286
    
slime@2
   287
    if((SetupPkt.bFeature == ENDPOINT_HALT)&&
slime@2
   288
       (SetupPkt.Recipient == RCPT_EP)&&
slime@2
   289
       (SetupPkt.EPNum != 0))
slime@2
   290
    {
slime@2
   291
        ctrl_trf_session_owner = MUID_USB9;
slime@2
   292
        /* Must do address calculation here */
slime@2
   293
        pDst.bRam = (byte*)&ep0Bo+(SetupPkt.EPNum*8)+(SetupPkt.EPDir*4);
slime@2
   294
        
slime@2
   295
        if(SetupPkt.bRequest == SET_FEATURE)
slime@2
   296
            *pDst.bRam = _USIE|_BSTALL;
slime@2
   297
        else
slime@2
   298
        {
slime@2
   299
            if(SetupPkt.EPDir == 1) // IN
slime@2
   300
                *pDst.bRam = _UCPU;
slime@2
   301
            else
slime@2
   302
                *pDst.bRam = _USIE|_DAT0|_DTSEN;
slime@2
   303
        }//end if
slime@2
   304
    }//end if
slime@2
   305
}//end USBStdFeatureReqHandler
slime@2
   306
slime@2
   307
/** EOF usb9.c ***************************************************************/