- Main Page
- Data Structures
- Files

- File List
- Globals

# USBCDC_constructs.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.
    00052 //
    00053 //This software has been submitted to export control regulations
    00054 //The ECCN is EAR99
    00066 #include "MSP430.h"
    00067 #include "Common\types.h"          // Basic Type declarations
    00068
    00069 #include "USB_Common\descriptors.h"
    00070 #include "USB_Common\usb.h"        // USB-specific functions
    00071
    00072 #include "main.h"
    00073 #ifdef _CDC_
    00074     #include "USB_CDC_API\UsbCdc.h"
    00075 #endif
    00076
    00077 #include <intrinsics.h>
    00078 #include "USBCDC_constructs.h"
    00079
    00080
    00081
    00082
    00083 //*********************************************************************************************
    00084 // Please see the MSP430 USB CDC API Programmer's Guide Sec. 9 for a full description of these
    00085 // functions, how they work, and how to use them.
    00086 //*********************************************************************************************
    00087
    00088
    00089
    00090
    00091 // This call assumes no previous send operation is underway; also assumes size is non-zero.
    00092 // Returns zero if send completed; non-zero if it failed, with 1 = timeout and 2 = bus is gone.
    00093 BYTE sendData_waitTilDone(BYTE* dataBuf, WORD size, BYTE intfNum, ULONG ulTimeout)
    00094 {
    00095   ULONG sendCounter = 0;
    00096   WORD bytesSent, bytesReceived;
    00097   BYTE ret;
    00098
    00099   ret = USBCDC_sendData(dataBuf,size,intfNum);  // ret is either sendStarted or busNotAvailable
    00100   if(ret == kUSBCDC_busNotAvailable)
    00101     return 2;
    00102
    00103   while(1)                                      // ret was sendStarted
    00104   {
    00105     ret = USBCDC_intfStatus(intfNum,&bytesSent,&bytesReceived);
    00106     if(ret & kUSBCDC_busNotAvailable)           // This may happen at any time
    00107       return 2;
    00108     else if(ret & kUSBCDC_waitingForSend)
    00109     {
    00110       if(ulTimeout && (sendCounter++ >= ulTimeout))  // Incr counter & try again
    00111         return(1);                                   // Timed out
    00112     }
    00113     else return 0;                              // If neither busNotAvailable nor waitingForSend, it succeeded
    00114   }
    00115 }
    00116
    00117
    00118
    00119 // This call assumes a previous send operation might be underway; also assumes size is non-zero.
    00120 // Returns zero if send completed; non-zero if it failed, with 1 = timeout and 2 = bus is gone.
    00121 BYTE sendData_inBackground(BYTE* dataBuf, WORD size, BYTE intfNum, ULONG ulTimeout)
    00122 {
    00123   ULONG sendCounter = 0;
    00124   WORD bytesSent, bytesReceived;
    00125
    00126   while(USBCDC_intfStatus(intfNum,&bytesSent,&bytesReceived) & kUSBCDC_waitingForSend)
    00127   {
    00128     if(ulTimeout && ((sendCounter++)>ulTimeout))  // A send operation is underway; incr counter & try again
    00129       return 1;                                   // Timed out
    00130   }
    00131
    00132   // At this point, a return from sendData() has to be either busNotAvailable or sendStarted
    00133   if(USBCDC_sendData(dataBuf,size,intfNum) == kUSBCDC_busNotAvailable)    // This may happen at any time
    00134     return 2;
    00135   else return 0;                                  // Indicate success
    00136 }
    00137
    00138
    00139
    00140 // This call assumes a previous receive operation is NOT underway; also assumes size is non-zero.
    00141 // Returns zero if receive completed; non-zero if it failed, with 1 = timeout and 2 = bus is gone
    00142 BYTE receiveData_waitTilDone(BYTE* dataBuf, WORD size, BYTE intfNum, ULONG ulTimeout)
    00143 {
    00144   ULONG rcvCounter = 0;
    00145   BYTE ret;
    00146   WORD bytesSent, bytesReceived;
    00147
    00148   ret = USBCDC_receiveData(dataBuf,size,intfNum);
    00149   if(ret == kUSBCDC_busNotAvailable)
    00150     return 2;       // Indicate bus is gone
    00151   if(ret == kUSBCDC_receiveCompleted)
    00152     return 0;       // Indicate success
    00153
    00154   while(1)
    00155   {
    00156     ret = USBCDC_intfStatus(intfNum,&bytesSent,&bytesReceived);
    00157     if(ret & kUSBCDC_busNotAvailable)
    00158       return 2;
    00159     else if(ret & kUSBCDC_waitingForReceive)
    00160     {
    00161       if(ulTimeout && (rcvCounter++ >= ulTimeout))
    00162          return 1;   // Indicate timed out
    00163     }
    00164     else return 0;   // Indicate success
    00165   }
    00166 }
    00167
    00168
    00169 // This call assumes a prevoius receive operation is NOT underway.  It only retrieves what data is waiting in the buffer
    00170 // It doesn't check for kUSBCDC_busNotAvailable, b/c it doesn't matter if it's not.  size is the maximum that
    00171 // is allowed to be received before exiting; i.e., it is the size allotted to dataBuf.
    00172 // Returns the number of bytes received.
    00173 WORD receiveDataInBuffer(BYTE* dataBuf, WORD size, BYTE intfNum)
    00174 {
    00175   WORD bytesInBuf;
    00176   BYTE* currentPos=dataBuf;
    00177
    00178   while(bytesInBuf = USBCDC_bytesInUSBBuffer(intfNum))
    00179   {
    00180     if((WORD)(currentPos-dataBuf+bytesInBuf) > size)
    00181       break;
    00182
    00183     USBCDC_receiveData(currentPos,bytesInBuf,intfNum);
    00184     currentPos += bytesInBuf;
    00185   }
    00186   return (currentPos-dataBuf);
    00187 }
    00188
    00189