📄 usb.c
字号:
#define THROTTLE 0 /*throttle value */
#define JOYX 1 /*joystick x value */
#define JOYY 2 /*joystick y value */
#define SWITCHES 3 /*hat switch and buttons */
/*RS-232 transmitter queue and related variables ****************/
#define OUTQLEN 8 /*length of outq */
char outq[8]; /*serial transmit queue */
byte outq_idx; /*outq index */
/*Misc. variables ***********************************************/
byte ticks; /*real time ticks (10/sec)*/
byte suscntr; /*suspend timeout counter */
byte rcount3; /*cnt of bytes in RX FIFO3*/
byte setaddr; /*SET_ADDRESS state var. */
#ifdef COP8
byte tmp1, tmp2; /*temporary data: CAREFUL */
byte lcdchars; /*count of chars written */
bits pswimg; /*the isr saves PSW here */
bits stalld; /*bits 0-6 correspond to */
/*like-numbered endpoints */
/*and are set to indicate */
/*the endpoint is stalled.*/
bits status; /*misc. status bits */
bits dtapid; /*PID related status */
#else
byte stalld;
byte status;
byte dtapid;
#endif
/*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 */
/*This is the cmd buffer for cmds received via the cmd channel **/
/*the order here is critical, and should not change *************/
byte rsnc; /*[+0]sync code (AAh exp) */
byte rcmd; /*[+1]command op code */
byte rdta; /*[+2]data for command */
byte radh; /*[+3]address (msb) */
byte radl; /*[+4] (lsb) */
byte rcks; /*[+5]checksum */
byte brd_id; /*our unique ID */
/*must be a BCD value */
#ifdef EEPROM
byte eebufh, eebufl; /*EEPROM reg buffer */
#endif
/*this is the startup banner */
const char msg0[] = "I am a USBN9603 HID Joystick";
/* LINE0 00000000000000001111111111111111 LINE1 */
/*The descriptor data follows-----------------------------------------*/
#include "strings.h" /*suck in the strings */
const /*rpt descr. goes in ROM */
#include "joystk.h"
#define RPT_DESC_SIZE sizeof(ReportDescriptor)
#include "joydata.h"
#include "descrpt.h" /*dev and cfg descriptors*/
#define DEV_DESC_SIZE sizeof(DEV_DESC)
#define CFG_DESC_SIZE sizeof(CFG_DESC)
/**********************************************************************/
/* These are the function prototypes */
/**********************************************************************/
void bitset(byte adr, byte mask);
void bitclr(byte adr, byte mask);
void trktim(void);
void rcveih(void);
void xmitih(void);
void do_cmd(void);
void usb_isr(void);
void xmit(char c);
/**********************************************************************/
/* This is the code area */
/**********************************************************************/
#ifdef COP8
#include "codecop8.h" /*source in the COP8 code */
#endif
#ifdef X86
#include "codex86.h" /*source in the X86 code */
#endif
#ifdef HC11
#include "codehc11.h" /*source in the HC11 code */
#endif
/**********************************************************************/
/* This sets a bit in the specified 9602 register */
/**********************************************************************/
void bitset(byte adr, byte mask)
{
write_usb(adr,(read_usb(adr)|(mask))); /*set the bit (RMW) */
}
/**********************************************************************/
/* This clears a bit in the specified 9602 register */
/**********************************************************************/
void bitclr(byte adr, byte mask)
{
write_usb(adr,(read_usb(adr)&(~mask))); /*clr the bit (RMW) */
}
/**********************************************************************/
/* This subroutine loads a byte from a descriptor or report into the */
/* endpoint 0 fifo. */
/**********************************************************************/
void mlti_pkt(void)
{
byte pckt_dta;
if(desc_sze>0)
{
if(TSTBIT(status,GETDESC))
switch (desc_typ) /*descriptor **************/
{
case DEVICE:
pckt_dta=DEV_DESC[desc_idx];
break;
case CONFIGURATION:
pckt_dta=CFG_DESC[desc_idx];
break;
case XSTRING:
pckt_dta=STR_DATA[desc_idx];
break;
case HID:
pckt_dta=CFG_DESC[desc_idx];
break;
case HIDREPORT:
pckt_dta=ReportDescriptor[desc_idx];
break;
default: /*UNDEFINED */
pckt_dta=0;
break;
}
else
switch (desc_typ) /*report ******************/
{
default: /*UNDEFINED */
pckt_dta=0;
break;
}
write_usb(TXD0,pckt_dta); /*send data to the FIFO */
desc_sze--; /*decriment size */
desc_idx++; /*increment index */
}
}
/**********************************************************************/
/* This subroutine loads a byte from a descriptor or report into the */
/* specified fifo. */
/**********************************************************************/
void queue_rpt(byte txd)
{
write_usb(txd,0); /*no throttle data yet */
write_usb(txd,report[JOYX]);
write_usb(txd,report[JOYY]);
write_usb(txd,report[SWITCHES]);
}
/**********************************************************************/
/* This subroutine initializes the 9602. */
/**********************************************************************/
void init_usb(void)
{
/*toss out any previous state ***********************************/
CLR_MLTIPKT; /*exit multi-packet mode */
usb_cfg = 0;
/*give a soft reset, then set ints to push pull, active hi or lo*/
write_usb(MCNTRL,SRST);
write_usb(MCNTRL,VREG_ST+INTR_TYPE);
/*initialize the clock generator ********************************/
/* prior to this point, the clock output will be 4 Mhz. After, */
/* it will be (48 MHz/CLKDIV) */
#ifdef BUSPOWER
write_usb(CCONF,CODIS+CLKDIV-1); /*disable clock output */
/* Enable Halt on suspend, wake from USB, clear pending flags */
write_usb(WKUP,(HOS|ENUSB|WKMODE));
#else
write_usb(CCONF,CLKDIV-1); /*set clock freq */
#endif
/*set default address, enable EP0 only **************************/
write_usb(FAR,AD_EN+0); write_usb(EPC0, 0x00);
/*set up interrupt masks ****************************************/
write_usb(NAKMSK,NAK_O0+NAK_I3); /*NAK evnts*/
write_usb(TXMSK,TXFIFO0+TXFIFO1+TXFIFO3); /*TX events*/
write_usb(RXMSK,RXFIFO0+RXFIFO1+RXFIFO3); /*RX events*/
write_usb(ALTMSK,NORMAL_ALTMSK); /*ALT evnts*/
write_usb(MAMSK,(INTR_E+RX_EV+NAK+TX_EV+ALT));
/*enable the receiver and go operational ************************/
FLUSHTX0; /*flush TX0 and disable */
write_usb(RXC0,RX_EN); /*enable the receiver */
write_usb(NFSR,OPR_ST); /*go operational */
write_usb(MCNTRL,VREG_ST+INTR_TYPE+NAT); /*set NODE ATTACH */
}
/**********************************************************************/
/* The CLEAR_FEATURE request is done here */
/**********************************************************************/
void clrfeature(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:
bitclr(EPC0,STALL);
CLRBIT(stalld,BIT0);
break;
case 1:
bitclr(EPC1,STALL);
CLRBIT(stalld,BIT1);
break;
case 2:
bitclr(EPC2,STALL);
CLRBIT(stalld,BIT2);
break;
case 3:
bitclr(EPC3,STALL);
CLRBIT(stalld,BIT3);
break;
case 4:
bitclr(EPC4,STALL);
CLRBIT(stalld,BIT4);
break;
case 5:
bitclr(EPC5,STALL);
CLRBIT(stalld,BIT5);
break;
case 6:
bitclr(EPC6,STALL);
CLRBIT(stalld,BIT6);
break;
default:
break;
}
break;
default: /*UNDEFINED */
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -