⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 upsd_usb.c

📁 DK3200 USB DEMO for KEIL C
💻 C
📖 第 1 页 / 共 2 页
字号:
 /* `=========================================================================`

                    ***************************************
                  ****   *                           *   ****
                                Title: UPSD_USB
                             File name: upsd_usb.c
                             Project name: USB Demo
                  ***                                    ****
                    ****************** * ******************
                  ****                                   ****
                              Author: Petr PFEIFER
                           MPG Prague, Czech Republic
                  ****   *                           *   ****
                    ***************************************

                 $Version:  0.107a  Build: April 30, 2004

                 $Version:  0.107   Build: 2004-04-29,17:55:46

 
 
 
                                  Description:
                                  ============

                      USB driver module for uPSD3200 chip.
                        Improved version of USB demo v7
                        Version 0.105 - GetStatus added


                                     Notes:
                                     ======
                      - do not change EPx service routines
                         and the order of commands ...


                           ..........................
                          .                          .
                          .      ******************  .
                          .     **PPPPPPPPPPPPPPPP   .
                          .     *PPPP*******PP****   .
                          .    **PPP********PP***    .
                          .    ***PPP******PP****    .
                          .   *****PPP****PP****     .
                          .   *****PPP****PP****     .
                          .  PPPPPPPP****PP****  (R) .
                          .                          .
                           ..........................


                                  =-=-=-=-=-=
                           =-=-=-=-=-=-=-=-=-=-=-=-=
                =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

                       Copyright 2004 ST Microelectronics

             This code/file is provided as is and has no warranty,
     implied or otherwise.  You are free to use/modify any of the provided
    code at your own risk in your applications with the expressed limitation
        of liability (see below) so long as your product using the code
                 contains at least one uPSD products (device).

                            LIMITATION OF LIABILITY:
                            ========================
              NEITHER STMicroelectronics NOR ITS VENDORS OR AGENTS
      SHALL BE LIABLE FOR ANY LOSS OF PROFITS, LOSS OF USE, LOSS OF DATA,
       INTERRUPTION OF BUSINESS, NOR FOR INDIRECT, SPECIAL, INCIDENTAL OR
       CONSEQUENTIAL DAMAGES OF ANY KIND WHETHER UNDER THIS AGREEMENT OR
         OTHERWISE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

                =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
                           =-=-=-=-=-=-=-=-=-=-=-=-=
                                  =-=-=-=-=-=

                   For current information on uPSD products,
                please consult our pages on the World Wide Web:

                                 www.st.com/psm

                            - - - - - - - - - - - -

                     STMicroelectronics GROUP OF COMPANIES
    Australia - Brazil - China - Czech Republic - Finland - France - Germany
   Hong Kong - India - Italy - Japan - Malaysia - Malta - Morocco - Singapore
             Spain - Sweden - Switzerland - United Kingdom - U.S.A.

                               http://www.st.com


 `========================================================================` */

#pragma NOAREGS

#include "upsd3200.h"
#include "upsd_usb.h"

//-- Variables ---------------------------------------------------------------

#define HID_DEVICE 1

// Constant Variables for USB configuration
extern const uchar code cUSCL_value;

// Constant Variables for USB Descriptors
extern const device_descriptor code deviceDesc;
extern const configuration_descriptor code configDesc;
extern const uchar code string0Desc[];
extern const uchar code string1Desc[];
extern const uchar code string2Desc[];
extern const uchar * const code stringDescTable[];

#if HID_DEVICE
extern const uchar code hidClassDesc[];
extern const uchar code hidClassDescSize;
extern const uchar code reportDesc[];
extern const uchar code reportDescSize;
extern const uchar code PhysicalReportDesc;
extern const uchar code PhysicalReportDescSize;
#endif

volatile uchar idata usbState;
volatile uchar idata ep1State;			//added April 30, 2004


volatile uchar idata SuspendCounter;

setup_buffer setupPacket;

static uchar* pTransmitBufferEP0;
static int    bytesToTransmitEP0;
static BOOL   shortTransfer;

extern void OnDeviceConfigured();







void OnUSBReset()
/******************************************************************************
 Function   : void OnUSBReset()
 Parameters : none
 Description: USB driver module initialization routine, in USR ISR only !!!.
 ******************************************************************************/
 {
  pTransmitBufferEP0 = NULL;
  bytesToTransmitEP0 = 0;

  UADR  = 0;                                 // Disable USB hardware
  UCON0 = uRX0E;                             // Enable EP0 for receiving only
  UCON1 = 0;                                 // Disable Endpoint 1
  UCON2 = uEP1E;                             // Enable EP1
  UADR  = uUSBEN + 0;                        // Enable USB with default address 0
  UIEN  = ~(uMCUR+uEOPF+uRESUMIE);
   // Enable all USB interrupts except uMCUR (MCU reset on USB reset) and EOP, ...
  USTA  = 0;
  UISTA = 0;                                 // Clear USB interrupts
  IEA  |= bEUSB;
 }






void UsbInitialize()
/******************************************************************************
 Function   : void UsbInitialize()
 Parameters : none
 Description: USB driver module initialization routine after MCU startup.
 ******************************************************************************/
 {
  USCL = cUSCL_value;                        // Set USB clock prescaler
  UIEN  = 0;                                 // Disable all USB interrupts
  UADR  = 0;                                 // Disable USB hardware
  OnUSBReset();
 }








extern data char usr_cnt;



void STALL_EP0()
/******************************************************************************
 Function   : void STALL_EP0()
 Parameters : none
 Description: Stalls EP0.
     This endpoint is halted or a control pipe request is not supported.
     Endpoint can be unstalled by the next SETUP packet.
     STALL is returned by a function in response to an IN token
     or after the data phase of an OUT transaction.
     It indicates that a function is unable to transmit or receive data,
     or that a control pipe request is not supported.
 ******************************************************************************/
 {
  #pragma asm
              anl    UCON0,#uTSEQ0+uRX0E+uSTALL0+uTX0E                   ;mask
              orl    UCON0,#uSTALL0                 ;clear TSEQ bit, STALL EP0
  #pragma endasm
 }






static BOOL TransmitBufferEP0()
/******************************************************************************
 Function   : static BOOL TransmitBufferEP0()
 Parameters : none
 Description: Transmits next segment of descriptor buffer (pTransmitBufferEP0).
     This routine prepates 0..8 bytes  of data from TransmitBufferEP0
     at endpoint0. If no data is available in buffer, the endpoint is disabled.
     According to USB standard, zero length packet is trasmitted at then end.
 ******************************************************************************/
 {
  data int i;
  data int nBytes;

  if (pTransmitBufferEP0)                    // If there is data going out...
   {
    #pragma asm                              // send zero length packet
              xrl    UCON0,#uTSEQ0                            ;toggle TSEQ bit
              anl    UCON0,#uTSEQ0+uRX0E              ;mask
    #pragma endasm
    if (!bytesToTransmitEP0)                 // If data has already been sent...
     {
      #pragma asm                            // send zero length packet
              orl    UCON0,#uTX0E                             ;enable transmit
      #pragma endasm
      pTransmitBufferEP0 = NULL;             // If data has already been sent...
     }
    else
     {
      nBytes = bytesToTransmitEP0;           // Transmit next chunk of data
      if (nBytes > 8)
       {
        nBytes = 8;
       }
      UCON0 |= nBytes;                       // set FIFO size

      for (i = 0; i < nBytes; i++)           // Load data into FIFO
       {
        UDT0 = *pTransmitBufferEP0++;
       }

      if ((bytesToTransmitEP0 -= nBytes) == 0)
       {
                  // For short transfers, we need to send a zero length terminator
        if (!shortTransfer || (nBytes < 8))
         {
          pTransmitBufferEP0 = NULL;         // it will be sent the next cycle
         }
       }
      #pragma asm
              orl    UCON0,#uTX0E                            ; enable transmit
      #pragma endasm
     }
    return TRUE;
   }

  #pragma asm                            // No data going out, so disable endpoint
              anl    UCON0,#uRX0E                                        ;mask
              orl    UCON0,#uTSEQ0                               ;set TSEQ bit
  #pragma endasm

  return FALSE;
 }








void TransmitDataEP0(uchar* pData, int nBytes)
/******************************************************************************
 Function   : void TransmitDataEP0()
 Parameters : (uchar* pData, int nBytes)
 Description: Load and arm EP0 to transmit data.
     This routine prepates 0..8 bytes of data at endpoint0 to be trasmitted.
     Caller must then clear FIFO status flag (uTXD0F) to arm the endpoint.
 ******************************************************************************/
 {
  data unsigned char xBytes;

  if (nBytes > EP0_SIZE) xBytes = EP0_SIZE; else xBytes = nBytes;

                       // Disable endpoint, set DATA0/1 sequence bit and FIFO size
  #pragma asm
              xrl    UCON0,#uTSEQ0                            ;toggle TSEQ bit
              anl    UCON0,#(uTSEQ0+uRX0E)
  #pragma endasm
  UCON0 |=xBytes;                            // set FIFO size

  while (xBytes--)
   {
    UDT0 = *pData++;                         // Load data into FIFO
   }
  UCON0 |= uTX0E;                            // Enable endpoint
 }







void TransmitDataEPx(int x, uchar* pData, int nBytes)
/******************************************************************************
 Function   : void TransmitDataEPx()
 Parameters : (int x, uchar* pData, int nBytes)
 Description: Loads EP1 or EP2 transmit FIFO.
              Caller must then clear FIFO status flag (uTXD1F) to arm endpoint.
 ******************************************************************************/
 {

  data unsigned char xBytes;

  if (nBytes > 8)  xBytes = 8; else xBytes = nBytes;
  // Disable endpoint, set DATA0/1 sequence bit, endpoint selection, and FIFO size
  UCON1 &= ~uEP12SEL;                        //mask
  UCON1 |= ((x == 2) ? uEP12SEL : 0);        //set endpoint
  UCON1 &= uTSEQ1+uEP12SEL;                  //mask
  UCON1 ^= uTSEQ1;                           //toggle DATAx bit
  UCON1 |= xBytes;                           //set FIFO size
  while (nBytes--)
   {
    UDT1 = *pData++;                         // Load data into FIFO
   }
  UCON1 |= uTX1E;                            // Enable endpoint
  UIEN  |= uTXD1IE;                          // enable INT
 }




















//the following function changed on April 30, 2004

static void OnGetStatus()
/******************************************************************************
 Function   : static void OnGetStatus()
 Parameters : none
 Description: Handler for GET_STATUS control requests.
 ******************************************************************************/
 {
  #pragma asm                                // send zero length packet
              orl    UCON0,#uTSEQ0                               ;set TSEQ bit
              anl    UCON0,#uTSEQ0+uRX0E                   ;mask, keep RX flag
              orl    UCON0,#2                                   ;set FIFO size
  #pragma endasm
  
  if((setupPacket.wIndex.lo == 0x00) & (setupPacket.wIndex.hi == 0x00)){//device
	UDT0 = 1;
	UDT0 = 0;
	}
  else if((setupPacket.wIndex.lo == 0x81) & (setupPacket.wIndex.hi == 0x00)){//endpoint1
	if(ep1State == US_EP1STALL)
		UDT0 = 1;
	else
		UDT0 = 0;

	UDT0 = 0;
	}
  else{ 
  	STALL_EP0();                  // No features currently implemented, so stall EP0

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -