• Main Page
  • Data Structures
  • Files
  • File List
  • Globals

UsbIsr.c

Go to the documentation of this file.
00001 //******************************************************************************
00002 //THIS PROGRAM IS PROVIDED "AS IS". TI MAKES NO WARRANTIES OR
00003 //REPRESENTATIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
00004 //INCLUDING ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
00005 //FOR A PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR
00006 //COMPLETENESS OF RESPONSES, RESULTS AND LACK OF NEGLIGENCE.
00007 //TI DISCLAIMS ANY WARRANTY OF TITLE, QUIET ENJOYMENT, QUIET
00008 //POSSESSION, AND NON-INFRINGEMENT OF ANY THIRD PARTY
00009 //INTELLECTUAL PROPERTY RIGHTS WITH REGARD TO THE PROGRAM OR
00010 //YOUR USE OF THE PROGRAM.
00011 //
00012 //IN NO EVENT SHALL TI BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
00013 //CONSEQUENTIAL OR INDIRECT DAMAGES, HOWEVER CAUSED, ON ANY
00014 //THEORY OF LIABILITY AND WHETHER OR NOT TI HAS BEEN ADVISED
00015 //OF THE POSSIBILITY OF SUCH DAMAGES, ARISING IN ANY WAY OUT
00016 //OF THIS AGREEMENT, THE PROGRAM, OR YOUR USE OF THE PROGRAM.
00017 //EXCLUDED DAMAGES INCLUDE, BUT ARE NOT LIMITED TO, COST OF
00018 //REMOVAL OR REINSTALLATION, COMPUTER TIME, LABOR COSTS, LOSS
00019 //OF GOODWILL, LOSS OF PROFITS, LOSS OF SAVINGS, OR LOSS OF
00020 //USE OR INTERRUPTION OF BUSINESS. IN NO EVENT WILL TI'S
00021 //AGGREGATE LIABILITY UNDER THIS AGREEMENT OR ARISING OUT OF
00022 //YOUR USE OF THE PROGRAM EXCEED FIVE HUNDRED DOLLARS
00023 //(U.S.$500).
00024 //
00025 //Unless otherwise stated, the Program written and copyrighted
00026 //by Texas Instruments is distributed as "freeware".  You may,
00027 //only under TI's copyright in the Program, use and modify the
00028 //Program without any charge or restriction.  You may
00029 //distribute to third parties, provided that you transfer a
00030 //copy of this license to the third party and the third party
00031 //agrees to these terms by its first use of the Program. You
00032 //must reproduce the copyright notice and any other legend of
00033 //ownership on each copy or partial copy, of the Program.
00034 //
00035 //You acknowledge and agree that the Program contains
00036 //copyrighted material, trade secrets and other TI proprietary
00037 //information and is protected by copyright laws,
00038 //international copyright treaties, and trade secret laws, as
00039 //well as other intellectual property laws.  To protect TI's
00040 //rights in the Program, you agree not to decompile, reverse
00041 //engineer, disassemble or otherwise translate any object code
00042 //versions of the Program to a human-readable form.  You agree
00043 //that in no event will you alter, remove or destroy any
00044 //copyright notice included in the Program.  TI reserves all
00045 //rights not specifically granted under this license. Except
00046 //as specifically provided herein, nothing in this agreement
00047 //shall be construed as conferring by implication, estoppel,
00048 //or otherwise, upon you, any license or other right under any
00049 //TI patents, copyrights or trade secrets.
00050 // 
00051 //You may not use the Program in non-TI devices.
00063 // (c)2009 by Texas Instruments Incorporated, All Rights Reserved.
00064 /*----------------------------------------------------------------------------+
00065 |                                                                             |
00066 |                              Texas Instruments                              |
00067 |                                                                             |
00068 |                          MSP430 USB-Example (CDC/HID Driver)                |
00069 |                                                                             |
00070 +-----------------------------------------------------------------------------+
00071 |  Source: UsbIsr.c, v1.18 2009/06/11                                         |
00072 |  Author: RSTO                                                               |
00073 |                                                                             |
00074 |  WHO          WHEN         WHAT                                             |
00075 |  ---          ----------   ------------------------------------------------ |
00076 |  RSTO         2008/09/03   born                                             |
00077 |  RSTO         2008/12/23   enhancements of CDC API                          |
00078 +----------------------------------------------------------------------------*/
00079 /*----------------------------------------------------------------------------+
00080 | Include files                                                               |
00081 +----------------------------------------------------------------------------*/
00082 
00083 #include "MSP430.h"
00084 #include "..\Common\types.h"              // Basic Type declarations
00085 #include "..\USB_Common\defMSP430USB.h"
00086 #include "..\USB_Common\descriptors.h"
00087 #include "..\USB_Common\usb.h"            // USB-specific Data Structures
00088 # ifdef _CDC_
00089     #include "..\USB_CDC_API\UsbCdc.h"
00090 #endif
00091 
00092 #include "UsbIsr.h"
00093 #include <string.h>
00094 
00095 /*----------------------------------------------------------------------------+
00096 | External Variables                                                          |
00097 +----------------------------------------------------------------------------*/
00098 extern BYTE  bFunctionSuspended;
00099 
00100 extern __no_init tEDB0 __data16 tEndPoint0DescriptorBlock;
00101 extern __no_init tEDB __data16 tInputEndPointDescriptorBlock[];
00102 extern __no_init tEDB __data16 tOutputEndPointDescriptorBlock[];
00103 
00104 extern volatile BYTE bHostAsksUSBData;
00105 extern volatile BYTE bTransferInProgress;
00106 extern volatile BYTE bSecondUartTxDataCounter[];
00107 extern volatile PBYTE pbSecondUartTxData;
00108 
00109 extern BYTE bStatusAction;
00110 extern WORD wUsbEventMask;
00111 
00112 BOOL CdcToHostFromBuffer();
00113 BOOL CdcToBufferFromHost();
00114 BOOL CdcIsReceiveInProgress();
00115 
00116 BOOL HidToHostFromBuffer();
00117 BOOL HidToBufferFromHost();
00118 BOOL HidIsReceiveInProgress();
00119 
00120 extern unsigned char TEST;
00121 /*----------------------------------------------------------------------------+
00122 | General Subroutines                                                         |
00123 +----------------------------------------------------------------------------*/
00124 
00125 #pragma vector=USB_UBM_VECTOR
00126 __interrupt VOID iUsbInterruptHandler(VOID)
00127 {
00128     BYTE bWakeUp = FALSE;
00129   
00130     //Check if the setup interrupt is pending. 
00131     //We need to check it before other interrupts, 
00132     //to work around that the Setup Int has lower priority then Input Endpoint 0
00133     if (USBIFG & SETUPIFG)
00134     {
00135         bWakeUp = SetupPacketInterruptHandler();  
00136         USBIFG &= ~SETUPIFG;    // clear the interrupt bit
00137     }   
00138     
00139     switch (__even_in_range(USBVECINT & 0x3f, USBVECINT_OUTPUT_ENDPOINT7))
00140     {
00141       case USBVECINT_NONE:
00142         break;
00143       case USBVECINT_PWR_DROP:
00144         __no_operation();
00145         break;
00146       case USBVECINT_PLL_LOCK:
00147         break;
00148       case USBVECINT_PLL_SIGNAL:
00149         break;
00150       case USBVECINT_PLL_RANGE:
00151         if (wUsbEventMask & kUSB_clockFaultEvent)
00152         {
00153             bWakeUp = USB_handleClockEvent();
00154         }
00155         break;
00156       case USBVECINT_PWR_VBUSOn:
00157         PWRVBUSonHandler();
00158         if (wUsbEventMask & kUSB_VbusOnEvent)
00159         {
00160             bWakeUp = USB_handleVbusOnEvent();
00161         }
00162         break;
00163       case USBVECINT_PWR_VBUSOff:
00164         PWRVBUSoffHandler();
00165         if (wUsbEventMask & kUSB_VbusOffEvent)
00166         {
00167             bWakeUp = USB_handleVbusOffEvent();
00168         }
00169         break;
00170       case USBVECINT_USB_TIMESTAMP:
00171         break;
00172       case USBVECINT_INPUT_ENDPOINT0:
00173         IEP0InterruptHandler();
00174         break;
00175       case USBVECINT_OUTPUT_ENDPOINT0:
00176         OEP0InterruptHandler();
00177         break;
00178       case USBVECINT_RSTR:
00179         USB_reset();
00180         if (wUsbEventMask & kUSB_UsbResetEvent)
00181         {
00182             bWakeUp = USB_handleResetEvent();
00183         }
00184         break;
00185       case USBVECINT_SUSR:
00186         USB_suspend();
00187         if (wUsbEventMask & kUSB_UsbSuspendEvent)
00188         {
00189             bWakeUp = USB_handleSuspendEvent();
00190         }
00191         break;
00192       case USBVECINT_RESR:
00193         USB_resume();
00194         if (wUsbEventMask & kUSB_UsbResumeEvent)
00195         {
00196             bWakeUp = USB_handleResumeEvent();
00197         }
00198         //-- after resume we will wake up! Independ what event handler says.
00199         bWakeUp = TRUE;
00200   
00201         break;
00202       case USBVECINT_SETUP_PACKET_RECEIVED: 
00203         bWakeUp = SetupPacketInterruptHandler();
00204         break;
00205   
00206       case USBVECINT_STPOW_PACKET_RECEIVED:
00207         break;
00208   
00209       case USBVECINT_INPUT_ENDPOINT1:
00210 
00211           break;
00212   
00213       case USBVECINT_INPUT_ENDPOINT2:
00214         break;
00215   
00216       case USBVECINT_INPUT_ENDPOINT3:
00217         #ifdef _CDC_
00218             //send saved bytes from buffer...
00219             bWakeUp = CdcToHostFromBuffer();
00220         #endif
00221         break;
00222   
00223       case USBVECINT_INPUT_ENDPOINT4:
00224         break;
00225       case USBVECINT_INPUT_ENDPOINT5:
00226         break;
00227       case USBVECINT_INPUT_ENDPOINT6:
00228         break;
00229       case USBVECINT_INPUT_ENDPOINT7:
00230         break;
00231   
00232       case USBVECINT_OUTPUT_ENDPOINT1:
00233         
00234         break;
00235   
00236       case USBVECINT_OUTPUT_ENDPOINT2:
00237         break;
00238   
00239       case USBVECINT_OUTPUT_ENDPOINT3:
00240         #ifdef _CDC_
00241             //call callback function if no receive operation is underway
00242             if (!CdcIsReceiveInProgress())
00243             {
00244                 if (wUsbEventMask & kUSB_dataReceivedEvent)
00245                 {
00246                     bWakeUp = USBCDC_handleDataReceived(1);
00247                 }
00248             }
00249             else
00250             {
00251                 //complete receive opereation - copy data to user buffer
00252                 bWakeUp = CdcToBufferFromHost();
00253             }
00254             TEST++;
00255         #endif
00256             break;
00257   
00258       case USBVECINT_OUTPUT_ENDPOINT4:
00259             break;
00260       case USBVECINT_OUTPUT_ENDPOINT5:
00261         break;
00262       case USBVECINT_OUTPUT_ENDPOINT6:
00263         break;
00264       case USBVECINT_OUTPUT_ENDPOINT7:
00265         break;
00266       default:
00267         break;
00268     }
00269    
00270     if (bWakeUp)
00271     {
00272         __bic_SR_register_on_exit(LPM3_bits); // Exit LPM0-3
00273     }
00274 }
00275 
00276 /*----------------------------------------------------------------------------+
00277 | Interrupt Sub-routines                                                      |
00278 +----------------------------------------------------------------------------*/
00279 
00280 BYTE SetupPacketInterruptHandler(VOID)
00281 {
00282     BYTE bTemp;
00283     BYTE bWakeUp = FALSE;
00284 
00285     USBCTL |= FRSTE;      // Function Reset Connection Enable - set enable after first setup packet was received
00286 
00287     usbProcessNewSetupPacket:
00288 
00289     // copy the MSB of bmRequestType to DIR bit of USBCTL
00290     if((tSetupPacket.bmRequestType & USB_REQ_TYPE_INPUT) == USB_REQ_TYPE_INPUT)
00291     {
00292         USBCTL |= DIR;
00293     }
00294     else
00295     {
00296         USBCTL &= ~DIR;
00297     }
00298 
00299     bStatusAction = STATUS_ACTION_NOTHING;
00300 
00301     // clear out return data buffer
00302     for(bTemp=0; bTemp<USB_RETURN_DATA_LENGTH; bTemp++)
00303     {
00304         abUsbRequestReturnData[bTemp] = 0x00;
00305     }
00306 
00307     // decode and process the request
00308     bWakeUp = usbDecodeAndProcessUsbRequest();
00309 
00310     // check if there is another setup packet pending
00311     // if it is, abandon current one by NAKing both data endpoint 0
00312     if((USBIFG & STPOWIFG) != 0x00)
00313     {
00314         USBIFG &= ~(STPOWIFG | SETUPIFG);
00315         goto usbProcessNewSetupPacket;
00316     }
00317     return bWakeUp;
00318 }
00319 
00320 //----------------------------------------------------------------------------
00321 VOID PWRVBUSoffHandler(VOID)
00322 {
00323     volatile unsigned int i;
00324     for (i =0; i < 1000; i++);
00325     if (!(USBPWRCTL & USBBGVBV))
00326     {
00327         USBKEYPID   =    0x9628;        // set KEY and PID to 0x9628 -> access to configuration registers enabled
00328         bEnumerationStatus = 0x00;      // device is not enumerated
00329         bFunctionSuspended = FALSE;     // device is not suspended
00330         USBCNF     =    0;              // disable USB module
00331         USBPLLCTL  &=  ~UPLLEN;         // disable PLL
00332         USBPWRCTL &= ~(VBOFFIE + VBOFFIFG);          // disable interrupt VBUSoff
00333         USBKEYPID   =    0x9600;        // access to configuration registers disabled
00334     }
00335 }
00336 
00337 //----------------------------------------------------------------------------
00338 
00339 VOID PWRVBUSonHandler(VOID)
00340 {
00341     volatile unsigned int i;
00342     for (i =0; i < 1000; i++);          // waiting till voltage will be stable
00343 
00344     USBKEYPID =  0x9628;                // set KEY and PID to 0x9628 -> access to configuration registers enabled
00345     USBPWRCTL |= VBOFFIE;               // enable interrupt VBUSoff
00346     USBPWRCTL &= ~ (VBONIFG + VBOFFIFG);             // clean int flag (bouncing)
00347     USBKEYPID =  0x9600;                // access to configuration registers disabled
00348 }
00349 
00350 //----------------------------------------------------------------------------
00351 VOID IEP0InterruptHandler(VOID)
00352 {
00353     USBCTL |= FRSTE;                              // Function Reset Connection Enable
00354     tEndPoint0DescriptorBlock.bOEPBCNT = 0x00;     
00355 
00356     if(bStatusAction == STATUS_ACTION_DATA_IN)
00357     {
00358         usbSendNextPacketOnIEP0();
00359     }
00360     else
00361     {
00362         tEndPoint0DescriptorBlock.bIEPCNFG |= EPCNF_STALL; // no more data
00363     }
00364 }
00365 
00366 //----------------------------------------------------------------------------
00367 VOID OEP0InterruptHandler(VOID)
00368 {
00369     USBCTL |= FRSTE;                              // Function Reset Connection Enable
00370     tEndPoint0DescriptorBlock.bIEPBCNT = 0x00;    
00371 
00372     if(bStatusAction == STATUS_ACTION_DATA_OUT)
00373     {
00374         usbReceiveNextPacketOnOEP0();
00375         if(bStatusAction == STATUS_ACTION_NOTHING)
00376         {
00377             switch(tSetupPacket.bRequest)
00378             {
00379             #ifdef _CDC_
00380                 case USB_CDC_SET_LINE_CODING:
00381                     Handler_SetLineCoding();
00382                     break;
00383             #endif // _CDC_
00384             default:;
00385             }
00386         }
00387     }
00388     else
00389     {
00390         tEndPoint0DescriptorBlock.bOEPCNFG |= EPCNF_STALL; // no more data
00391     }
00392 }
00393 
00394 /*----------------------------------------------------------------------------+
00395 | End of source file                                                          |
00396 +----------------------------------------------------------------------------*/
00397 /*------------------------ Nothing Below This Line --------------------------*/

Generated on Fri Dec 17 2010 12:09:22 for Multi-cell Lithium-Ion Battery Manager System using MSP430 and BQ76PL536 by  doxygen 1.7.1