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

📄 upsdusb.c

📁 用keil开发的.单片机税控器程序.单片机用的是AT公司的.upsd3245
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
* Copyright (c) 2004,成都港顺科技发展有限公司
* All rights reserved.
*
* 编 译 器:Keil:C Compiler:7.20;Assembler:7.10
* 工程名称:POS-Test.UV2
* 文件名称:UPSD_USB.C
* 摘    要:USB与PC通讯
* 
* 单 片 机:uPSD3254 
* 当前版本:0.4
* 作    者:林云
* 完成日期:2004-12-7 14:45
*/
#pragma NOAREGS
#include "Main.h"
#include "Upsdusb.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;   // pocket packet

setup_buffer setupPacket;

static uchar* pTransmitBufferEP0;
static int    bytesToTransmitEP0;
//static BOOL   shortTransfer;	 //2004-08-02

extern void OnDeviceConfigured();

static BOOL   shortTransfer;//2004-08-02


BOOL ReadSetupPacket()
/******************************************************************************
 Function   : BOOL ReadSetupPacket()
 Parameters : none
 Description: Reads a setup packet from EP0.
              Returns TRUE if successful; stalls the endpoint and returns
              FALSE on an invalid packet size.
 ******************************************************************************/
 {
  data int i;
  uchar* p = (uchar*) &setupPacket;

  if ((USTA & 0x0F) != 8)     // Stall EP0 if SETUP packet is not the correct size
   {
    STALL_EP0();
    return FALSE;
   }

  for (i = 0; i < 8; i++)                    // Read the setup packet
   {
    *p++ = UDR0;
   }
  USTA |= uRSEQ;                          // Set data toggle bit (expecting DATA1)

  return TRUE;
 }




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*///2004-08-02
	UCON0 &= uTSEQ0+uRX0E+uSTALL0+uTX0E;
  UCON0 |= uSTALL0;
 }






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*///08-02-2004
		UCON0 ^= uTSEQ0;
		UCON0 &= uTSEQ0+uRX0E;
    if (!bytesToTransmitEP0)                 // If data has already been sent...
     {
      /*#pragma asm                            // send zero length packet
              orl    UCON0,#uTX0E                             ;enable transmit
      #pragma endasm*///08-02-2004
			UCON0 |= uTX0E;
      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*///08-02-2004
			UCON0 ^= uTX0E;
     }
    return TRUE;
   }

  /*#pragma asm                            // No data going out, so disable endpoint
              anl    UCON0,#uRX0E                                        ;mask
              orl    UCON0,#uTSEQ0                               ;set TSEQ bit
  #pragma endasm*///08-02-2004
	UCON0 &= uRX0E;
	UCON0 |= uTSEQ0;

  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*///08-02-2004
	UCON0 ^= uTSEQ0;
	UCON0 &= (uTSEQ0+uRX0E);
  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*///08-02-2004
	UCON0 |= uTSEQ0;
	UCON0 &= uTSEQ0+uRX0E;
	UCON0 |= 2;
  
  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
  	}
  /*#pragma asm
              orl    UCON0,#uTX0E                             ;enable transmit
  #pragma endasm*///08-02-2004
	UCON0 |= uTX0E;
 }






//the following function changed on April 30, 2004

⌨️ 快捷键说明

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