📄 usb.h
字号:
#ifndef USB_H
#define USB_H
//#include "gbl_macro.h"
#define CLKDIV 6 // for 8MHz clkout
#define SYNCBYT 0xaa
#define RECV_BUF_SIZE 40
#define MANUFACTURER_STR "L\0u\0Y\0u\0,\0U\0C\0R\0 \0"
#define PRODUCT_STR "U\0S\0B\0G\0P\0S\0 \0S\0o\0f\0t\0 \0R\0e\0c\0e\0i\0v\0e\0r\0 \0"
#define VERSION_STR "1\0_\01"
typedef enum {
DEVICE_DESCRIPTOR = 1,
CONFIG_DESCRIPTOR,
STRING_DESCRIPTOR,
INTERFACE_DESCRIPTOR,
ENDPOINT_DESCRIPTOR,
USB_DEVICE_DESCRIPTOR = 0x21
} USB_descriptor_type_t;
typedef enum {
NEUTRAL = 0x0000,
SYTEM_DEFAULT = 0x0400,
ENGLISH_NEUTRAL = 0x0009,
ENGLISH_US = 0x0409,
ENGLISH_UK = 0x0809,
RUSSIAN_NEUTRAL = 0x0019
} LANGID_t;
typedef struct {
char bLength;
char bDescriptorType;
char *bstring;
} USB_string_desc_t;
#define LANGID_LENGTH 2
#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);}
/* store the status byte in FIFO0 for the chosen endpoint *************/
#define EPSTATUS(ep) \
case ep: \
if (stalled^ep) write_usb(TXD0,1); else write_usb(TXD0,0);\
break;
/* enable TX0, using the appropriate DATA PID *************************/
#define TXEN0_PID \
{ if(dta_pid0) write_usb(TXC0,TX_TOGL+TX_EN); /*DATA1*/\
else write_usb(TXC0,TX_EN); /*DATA0*/\
dta_pid0=!dta_pid0;}
/* enable TX1, using the appropriate DATA PID, but not toggling it ****/
#define TXEN1_PID_NO_TGL \
{ if(dta_pid1) write_usb(TXC1,TX_TOGL+TX_LAST+TX_EN); /*DATA1*/\
else write_usb(TXC1,TX_LAST+TX_EN); } /*DATA0*/
#define TXEN1_PID \
{ TXEN1_PID_NO_TGL; \
dta_pid1=!dta_pid1;}
/* enable TX2, using the appropriate DATA PID, but not toggling it ****/
#define TXEN2_PID_NO_TGL \
{ if(dta_pid2) write_usb(TXC2,TX_TOGL+TX_LAST+TX_EN); /*DATA1*/\
else write_usb(TXC2,TX_LAST+TX_EN); } /*DATA0*/
#define TXEN2_PID \
{ TXEN2_PID_NO_TGL; \
dta_pid2=!dta_pid2;}
/* enable TX3, using the appropriate DATA PID, but not toggling it ****/
#define TXEN3_PID_NO_TGL \
{ if(dta_pid3) write_usb(TXC3,TX_TOGL+TX_LAST+TX_EN); /*DATA1*/\
else write_usb(TXC3,TX_LAST+TX_EN); } /*DATA0*/
#define TXEN3_PID \
{ TXEN3_PID_NO_TGL; \
dta_pid3=!dta_pid3;}
/*for now the sizes and offsets below need to be hand calculated, */
/*until I can find a better way to do it */
/*for multiple byte values, LSB goes first */
#define DEV_DESC_SIZE 18
const char code DEV_DESC[] = {DEV_DESC_SIZE, /*length of this desc. */
0x01, /*DEVICE descriptor */
0x00,0x01, /*spec rev level (BCD) */
0x00, /*device class */
0x00, /*device subclass */
0x00, /*device protocol */
0x08, /*max packet size */
0x00,0x05, /*National's vendor ID */
0x5B,0xC3, /*National's product ID */
0x12,0x48, /*National's revision ID */
1, /*index of manuf. string */
2, /*index of prod. string */
0, /*index of ser. # string */
0x01 /*number of configs. */
};
const char code CFG_DESC[] = {0x09, /*length of this desc. */
0x02, /*CONFIGURATION descriptor*/
0x2E,0x00, /*total length returned */
0x01, /*number of interfaces */
0x01, /*number of this config */
0x00, /*index of config. string */
0x80, /*attr.: bus powered */
200, /*max power (400 mA) */
0x09, /*length of this desc. */
0x04, /*INTERFACE descriptor */
0x00, /*interface number */
0x00, /*alternate setting */
0x04, /*# of (non 0) endpoints */
0x00, /*interface class */
0x00, /*interface subclass */
0x00, /*interface protocol */
0x00, /*index of intf. string */
/*The WDM driver for this board references the endpoints by 'pipe' */
/*number: 0, 1, 2 et al in order below: */
/*Pipe 0 */
0x07, /*length of this desc. */
0x05, /*ENDPOINT descriptor */
0x81, /*address (IN) */
0x02, /*attributes BULK (INTERRUPT) */
0x40,0x00, /*max packet size (32) */
0xFF, /*interval (ms) */
/*Pipe 1 */
0x07, /*length of this desc. */
0x05, /*ENDPOINT descriptor */
0x02, /*address (OUT) */
0x02, /*attributes (BULK) */
0x40,0x00, /*max packet size (32) */
0xFF, /*interval (ms) */
/*Pipe 2 */
0x07, /*length of this desc. */
0x05, /*ENDPOINT descriptor */
0x85, /*address (IN) */
0x02, /*attributes (INTERRUPT) */
0x40,0x00, /*max packet size (64) */
0xFF, /*interval (ms) */
/*Pipe 3 */
0x07, /*length of this desc. */
0x05, /*ENDPOINT descriptor */
0x06, /*address (OUT) */
0x02, /*attributes (BULK) */
0x40,0x00, /*max packet size (64) */
0xFF}; /*interval (ms) */
#define CFG_DESC_SIZE sizeof(CFG_DESC)
/*-------------------
* String descriptors
*------------------*/
const struct {
char bLength;
char bDescriptorType;
int bstring;
} code langid_str_desc = {sizeof(langid_str_desc), STRING_DESCRIPTOR, ENGLISH_US};
const struct {
char bLength;
char bDescriptorType;
char bstring[sizeof(MANUFACTURER_STR)];
} code manufacturer_str_desc = {sizeof(manufacturer_str_desc), STRING_DESCRIPTOR, MANUFACTURER_STR};
const struct {
char bLength;
char bDescriptorType;
char bstring[sizeof(PRODUCT_STR)];
} code product_str_desc = {sizeof(product_str_desc), STRING_DESCRIPTOR, PRODUCT_STR};
const struct {
char bLength;
char bDescriptorType;
char bstring[sizeof(VERSION_STR)];
} code version_str_desc = {sizeof(version_str_desc), STRING_DESCRIPTOR, VERSION_STR};
/*------------------------------------------------------
* List of all string descriptors, Be sure that the order
* of the list is the same as String_index_t enum
*------------------------------------------------------*/
const USB_string_desc_t code *string_descs[] = {
(USB_string_desc_t*)&langid_str_desc,
(USB_string_desc_t*)&manufacturer_str_desc,
(USB_string_desc_t*)&product_str_desc,
(USB_string_desc_t*)&version_str_desc
};
const char code string_sze[4]={
sizeof(langid_str_desc),
sizeof(manufacturer_str_desc),
sizeof(product_str_desc),
sizeof(version_str_desc)
};
char xdata * data usb_addr; // pointer used to access usbn9603 regsiters
/* the following bits are control signal for GP2021
GPS_RD BIT P2.0
GPS_WR BIT P2.2
GPS_CS BIT P2.1
GPS_ALE BIT P2.3
GPS_OE BIT P2.4
*/
sbit GPS_ALE = P2^0;
sbit GPS_WR = P2^1;
sbit GPS_CS = P2^2;
sbit GPS_RD = P2^3;
sbit GPS_OE = P2^4;
unsigned char usb_buf[8]; /*buffer used for USB */
unsigned char desc_typ, desc_idx,desc_t_idx, desc_sze, string_idx;
unsigned int gp2021_TXD1_idx, gp2021_TXD23_idx;
unsigned char write_buf_leng, write_buf_idx;
unsigned char usb_cfg; /*usb config. setting */
unsigned char evnt, rxstat, txstat; /*USB status temp storage */
/* the following is the command buffer */
unsigned char rsnc, rcmd, rdta, radh, radl, rcks;
char bdata dtapid; /* PID related status*/
sbit dta_pid0= dtapid^0;
sbit dta_pid1= dtapid^1;
sbit dta_pid2= dtapid^2;
sbit dta_pid3= dtapid^3;
char bdata stalled; /* indicate the stall status of endpoints0-6 */
sbit stall_ep0 = stalled^0;
sbit stall_ep1 = stalled^1;
sbit stall_ep2 = stalled^2;
sbit stall_ep3 = stalled^3;
sbit stall_ep4 = stalled^4;
sbit stall_ep5 = stalled^5;
sbit stall_ep6 = stalled^6;
char bdata status;
sbit sts_getdesc = status^0; // indication of Get_Description
sbit sts_usbcmd = status^1; // indication of Do_Cms();
sbit sts_send_gp2021_TXD1 = status^2; // indication of send_gp2021_all by TXD1
sbit sts_send_gp2021_TXD23 = status^4; // indication of send_gp2021_all by TXD23, double buffers
sbit sts_send_first_end = status^5;
sbit sts_send_gp2021_all = status^6;
sbit time_send_gp2021_all = status^7;
char bdata event;
sbit get_data_ready = event^0; // indication of get new data from host
void do_cmd(void);
void rst_usb(void);
void init_usb(void);
void nak0(void);
void nak1(void);
void nak2(void);
void nak3(void);
void clrfeature(void);
void getdescriptor(void);
void getstatus(void);
void setconfiguration(void);
void setfeature(void);
void get_desc(void);
void send_all_gp2021_TXD1(void);
void send_all_gp2021_TXD3(void);
void get_usbn9603(unsigned char* tmp_b);
void send_all_usbn9603_TXD1(unsigned char* tmp_b);
void rx_0(void);
void rx_1(void);
void rx_2(void);
void rx_3(void);
void tx_0(void);
void tx_1(void);
void tx_2(void);
void tx_3(void);
void usb_alt(void);
void process_host_data(void);
void process_gp2021(char);
volatile unsigned char xdata gp2021_reg_buf[2*ONE_FRM_SIZE], gps_data[2];
volatile unsigned char xdata gp2021_reg_write[130];
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -