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

📄 usb_9602.bak

📁 USB9602 source code for C 8051
💻 BAK
📖 第 1 页 / 共 4 页
字号:
/************************************************************************/
/* PSI  1999							                          St醔l Robert */
/*                                                                      */
/*			               ADuC812 12MHz                                   */
/* USB_9602.c (Ovladace pro USBN9602 )                       19.10.1999 */
/************************************************************************/

/* !!! Pozor Predpoklada se adresace typu int Hi Lo pro promenne !!! */

#pragma CODE DEBUG SYMBOLS LISTINCLUDE
#pragma REGISTERBANK (0)

#include <stdio.h>
#include <absacc.h>
#include <reg52.h>
#include "defusb.h"                         /*include USB defines     */
#include "def9602.h"                        /*include 9602 defines    */
#include "bitops.h"
#include "strings.h"

#include "joystk.h"


#define Adr_9602_Adr  0xC001 /* Adresa, od niz se adresuji registery 9602 */
#define Adr_9602_Dat  0xC000 /* Adresa, od niz se adresuji registery 9602 */
#define VREG_ST  VGE
/* The system clock is the 48 MHz USB clock, divided by this value:   */
#define CLKDIV   5                          /*USB clock divisor       */
/**********************************************************************/
/* Message related values                                             */
/**********************************************************************/
/* Command message constants  */
#define SYNCBYT  0xAA                       /*sync code expected      */
#define MAJREV   'A'                        /*major revision          */
#define MINREV   40                         /*minor revision          */

/**********************************************************************/
/* These are system independent macros and defines                    */
/**********************************************************************/
#define HIBYT(x) (x)/256
#define LOBYT(x) (x)%256

#define NORMAL_ALTMSK SD3+RESET_A           /*ALTMSK if not in suspend*/
#define SUSPND_ALTMSK RESUME_A+RESET_A      /*ALTMSK if in suspend    */

#define OUTQLEN  8                          /*length of outq          */

/* Enforce a minimum time period active before suspending to make sure*/
/* that the board is detected at power on and restart.  The value here*/
/* corresponds to the minimum number of time periods between suspend  */
/* events.  At half-second intervals, the value here equals 2 seconds */
#define SUSPND_TO 4
#define INTR_TYPE INT_L_P                   /*act low pushpull ints   */
      /*these are the bit definitions for the status variable *********/
#define RPTBUSY   BIT1                      /*set when report  changes*/
#define RPTCHNG   BIT2                      /*set for HID data changes*/
#define DEBUG     BIT3                      /*set when host listening */
#define MLTIPKT   BIT4                      /*set for multi-packet ops*/
#define GETDESC   BIT5                      /*set for get_descr. req. */
#define GETRPRT   BIT6                      /*set for get_report req. */
#define USB_CMD   BIT7                      /*set when doing usb cmd  */
                                            /*sequence is underway    */

      /*these are the bit definitions for the dtapid variable *********/
#define TGL0PID   BIT0                      /*tracks NEXT data PID    */
#define TGL1PID   BIT1                      /*tracks NEXT data PID    */
#define TGL2PID   BIT2                      /*tracks NEXT data PID    */
#define TGL3PID   BIT3                      /*tracks NEXT data PID    */

#define DEV_DESC_SIZE sizeof(DEV_DESC)
#define CFG_DESC_SIZE sizeof(CFG_DESC)
#define RPT_DESC_SIZE sizeof(ReportDescriptor)

//#define PUT_TX(dta) SBAF=dta                /*transmit top of queue !!!  */
#define TXINTON  SETBIT(SCCR2,TIE)          /*turn on/off ser. tx int */
#define TXINTOFF CLRBIT(SCCR2,TIE)

/* Flush and disable the USB TX0 **************************************/
#define FLUSHTX0 {write_usb(TXC0,FLUSH);}

/* Flush and disable the USB TX1 **************************************/
#define FLUSHTX1 {write_usb(TXC1,FLUSH);}

/* Flush and disable the USB TX2 **************************************/
#define FLUSHTX2 {write_usb(TXC2,FLUSH);}

/* Flush and disable the USB TX3 **************************************/
#define FLUSHTX3 {write_usb(TXC3,FLUSH);}

/* Flush and disable the USB RX0 **************************************/
#define FLUSHRX0 {write_usb(RXC0,FLUSH);}

/* Flush and disable the USB RX1 **************************************/
#define FLUSHRX1 {write_usb(RXC1,FLUSH);}

/* Flush and disable the USB RX2 **************************************/
#define FLUSHRX2 {write_usb(RXC2,FLUSH);}

/* Flush and disable the USB RX3 **************************************/
#define FLUSHRX3 {write_usb(RXC3,FLUSH);}

/* sets status.GETDESC and status.MLTIPKT *****************************/
#define SET_GETDESC {SETBIT(status,GETDESC); SETBIT(status,MLTIPKT);}

/* clears the multi-packet mode bits **********************************/
#define CLR_MLTIPKT {CLRBIT(status,GETDESC); CLRBIT(status,GETRPRT);   \
            CLRBIT(status,MLTIPKT);}

/* enable TX1, using the appropriate DATA PID, but not toggling it ****/
#define TXEN1_PID_NO_TGL {if(TSTBIT(dtapid,TGL1PID))                   \
            write_usb(TXC1,TX_TOGL+TX_LAST+TX_EN); else                \
            write_usb(TXC1,TX_LAST+TX_EN);}

/* enable TX0, using the appropriate DATA PID *************************/
#define TXEN0_PID {if(TSTBIT(dtapid,TGL0PID)) write_usb(TXC0,TX_TOGL+TX_EN);\
            else write_usb(TXC0,TX_EN); TGLBIT(dtapid,TGL0PID);}

/* enable TX1, using the appropriate DATA PID, but not toggling it ****/
#define TXEN1_PID_NO_TGL {if(TSTBIT(dtapid,TGL1PID))                   \
            write_usb(TXC1,TX_TOGL+TX_LAST+TX_EN); else                \
            write_usb(TXC1,TX_LAST+TX_EN);}

/* enable TX3, using the appropriate DATA PID, but not toggling it ****/
#define TXEN3_PID_NO_TGL {SETBIT(status,RPTBUSY);CLRBIT(status,RPTCHNG);\
            if(TSTBIT(dtapid,TGL3PID)) write_usb(TXC3,TX_TOGL+TX_LAST+TX_EN);\
            else write_usb(TXC3,TX_LAST+TX_EN);}

/* store the status byte in FIFO0 for the chosen endpoint *************/
#define EPSTATUS(epbit) if(TSTBIT(stalld,epbit)) write_usb(TXD0,1);    \
            else write_usb(TXD0,0); break;


#include "descrpt.h"


xdata unsigned char status;
xdata unsigned char usb_cfg;
xdata unsigned char evnt, rxstat, txstat;          /*USB status temp storage */
xdata unsigned char desc_typ, desc_idx;
xdata unsigned char setaddr;                       /*SET_ADDRESS state var.  */
xdata unsigned char dtapid;                        /*PID related status      */
xdata unsigned char usb_buf[8];                    /*buffer used for USB     */
      /*This is the cmd buffer for cmds received via the cmd channel **/
      /*the order here is critical, and should not change *************/
xdata unsigned char  rsnc;                          /*[+0]sync code (AAh exp) */
xdata unsigned char  rcmd;                          /*[+1]command op code     */
xdata unsigned char  rdta;                          /*[+2]data for command    */
xdata unsigned char  radh;                          /*[+3]address (msb)       */
xdata unsigned char  radl;                          /*[+4]        (lsb)       */
xdata unsigned char  rcks;                          /*[+5]checksum            */

xdata unsigned char  stalld;                        /*bits 0-6 correspond to  */
xdata unsigned char  rcount3;                       /*cnt of bytes in RX FIFO3*/
xdata unsigned char  usb_idl;                       /*usb idle    setting     */
xdata unsigned char  outq_idx;                      /*outq index              */
xdata unsigned char  brd_id;                        /*our unique ID           */
xdata unsigned char  suscntr;                       /*suspend timeout counter */

xdata char outq[8];                       /*serial transmit queue   */

xdata long desc_sze;

/************ Zasobnik pro seriak ****************/
extern xdata unsigned char PoleCh[200];
extern xdata unsigned char PointCh;
xdata unsigned char CountInt;

/******************************************************************************/
/* Ovladani obvodu ( non-multiplexed parallel interface mode ) */
void write_usb(unsigned char adr, Dat)
{
// XBYTE[Adr_9602|adr]=Dat;
 XBYTE[Adr_9602_Adr]=adr;       /* set Adr 01 */
 XBYTE[Adr_9602_Dat]=Dat;       /* sed Data */
}

unsigned char read_usb(unsigned char adr)
{
 XBYTE[Adr_9602_Adr]=adr;       /* set Adr 01 */
 return (XBYTE[Adr_9602_Dat]);
}

/**********************************************************************/
/* This sets a bit in the specified 9602 register                     */
/**********************************************************************/
void bitset(unsigned char adr, mask)
{
  write_usb(adr,(read_usb(adr)|(mask)));   /*set the bit (RMW)  */
}

/**********************************************************************/
/* This clears a bit in the specified 9602 register                   */
/**********************************************************************/
void bitclr(unsigned char adr, mask)
{
  write_usb(adr,(read_usb(adr)&(~mask)));  /*clr the bit (RMW)  */
}

/**********************************************************************/
/* This subroutine loads a byte from a descriptor or report into the  */
/* specified fifo.                                                    */
/**********************************************************************/
void queue_rpt(unsigned char txd)
{
  write_usb(txd,0);                   /*no throttle data yet    */
//  write_usb(txd,report[JOYX]); !!!
//  write_usb(txd,report[JOYY]);   !!!
//  write_usb(txd,report[SWITCHES]); !!!
}

/**********************************************************************/
/* The HID class specific GET_REPORT request is done here             */
/**********************************************************************/
void getreport(void)
{
  desc_typ = usb_buf[3];              /*store the type requested*/
  queue_rpt(TXD0);                    /*queue up the report     */
}

/**********************************************************************/
/* The HID class specific SET_REPORT request is done here             */
/**********************************************************************/
void setreport(void)
{
  desc_typ = usb_buf[3];              /*store the report req'd  */
}

/**********************************************************************/
/* The HID class specific GET_IDLE request is done here               */
/**********************************************************************/
void getidle(void)
{
  write_usb(TXD0,usb_idl);            /*send back idle value    */
}

/**********************************************************************/
/* The HID class specific SET_IDLE request is done here               */
/**********************************************************************/
void setidle(void)
{
  usb_idl = usb_buf[3];               /*store the idle value    */
}

/**********************************************************************/
/* This is the handler for the RS232 port.                            */
/**********************************************************************/
//void xmitih(void)
//{
//unsigned char i;
//  if(outq_idx>0)
//   {
//    PUT_TX(outq[0]);                /*transmit top of queue   */
//    for(i=0; i<outq_idx; i++) outq[i]=outq[i+1];  /*move queue*/
//    outq_idx--;                     /*decriment index         */
//   }
//
//  if(outq_idx==0) TXINTOFF;           /*turn off int if done    */
//}


/**********************************************************************/
/* This subroutine sends a character through the UART transmitter or  */
/* The usb interface, depending on the mode.                          */
/**********************************************************************/
void xmit(char c)
{
/*USB mode: move character to endpoint 1 FIFO *******************/
  if(TSTBIT(status,USB_CMD))
   {
    write_usb(TXD1,c);              /*send data to the FIFO   */
   }

/*UART mode: move character to UART queue ***********************/
  else
   {
    while (outq_idx>=OUTQLEN)       /*wait for room in queue  */
//      if (TX_EMPTY) xmitih();       /*or make it if we can   !!! */
    outq[outq_idx++]=c;             /*post character          */
//    TXINTON;                        /*turn on interrupt       */
//    if (TX_EMPTY) xmitih();         /*start xmission if ready !!! */
   }
}

/**********************************************************************/
/* This subroutine puts the board to sleep, to reduce the overall     */
/* current consumption in the suspend mode.                           */
/**********************************************************************/
void deep_sleep(void)
  {
  }

/**********************************************************************/
/* The SET_FEATURE request is done here                               */
/**********************************************************************/
void setfeature(void)
{
  switch (usb_buf[0]&0x03)            /*find request target     */
   {
    case 0:                         /*DEVICE                  */
      break;

    case 1:                         /*INTERFACE               */
      break;

    case 2:                         /*ENDPOINT                */
      switch (usb_buf[4]&0x0F)      /*find specific endpoint  */
       {                           /*  (strip off dir. bit)  */
        case 0:
         /*control pipes must accept setup requests even if  */
         /*stalled, so we log the stall here, but don't      */
         /*actually do it                                    */
         /*bitset(EPC0,STALL);*/
          SETBIT(stalld,BIT0);
          break;
        case 1:

⌨️ 快捷键说明

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