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