usbn960x.c
来自「TI公司的CCS一些常用的函数库」· C语言 代码 · 共 1,581 行 · 第 1/4 页
C
1,581 行
else if (tgl==2) {status &= 0xFB;} //clear toggle (DATA0)
status |= 1; //set TX_EN
//debug(debug_txb, "D%U ",(status & 0x04)==0x04); //show DATA1/DATA0
debug(debug_txb, "%X ",status);
usbn_write(address+2,status);
return(1);
}
}
}
}
return(0);
}
int8 currently_opened_epc;
int8 currently_opened_address;
int1 usb_buffer_open(int endpoint, int len) {
int8 status, tcount;
int8 i;
if (endpoint < 16) {
currently_opened_epc=usb_find_epc(endpoint,1);
if (currently_opened_epc != 0xFF) {
currently_opened_address=USBN_TX_FIFOx[currently_opened_epc]; //address = USBN_TXDx
currently_opened_address+=2; //TXCx
status=usbn_read(currently_opened_address); //TXCx
if ((!bit_test(status,1))||(!endpoint)) { //make sure last isnt set
status |= 0x08;
usbn_write(currently_opened_address,status); //FLUSH
i=0;
do {
i++;
status=usbn_read(currently_opened_address);
} while ((bit_test(status,3))&&(i!=0)); //wait until flush is clear
currently_opened_address-=2;
status=usbn_read(currently_opened_address+1); //TXSx
tcount=status & 0x1F; //find open space on fifo buffer
if (tcount!=0) { //check space in tx buffer
return(1); //buffer is full
}
}
}
}
return(0);
}
void usb_buffer_write(int8 data) {
usbn_write(currently_opened_address, data);
}
void usb_buffer_close(PID_TOGGLE tgl) {
int8 status;
if (!currently_opened_epc) { //if endpoint 0 we need to disable rx_en on RXC0
status=usbn_read(USBN_RXC0);
status &= 0xFE;
usbn_write(USBN_RXC0, status); //disable RC_EN
}
status=usbn_read(currently_opened_address+2); //TXCx
if (currently_opened_epc) {status |= 0x02;} //set LAST bit
if (tgl==1) {status ^= 0x04;} //toggle TOGGLE bit (TOGGLE DATAx)
else if (tgl==0) {status |= 0x04;} //set toggle (DATA1)
else if (tgl==2) {status &= 0xFB;} //clear toggle (DATA0)
status |= 1; //set TX_EN
usbn_write(currently_opened_address+2,status);
}
/*******************************************************************************
/* usbn_get_version()
/*
/* Summary: Stictly for debugging, this function gets the revision number of the
/* USBN960x you are connected to. Good to see if you're connection
/* to the USBN960x is good.
/*
/********************************************************************************/
int8 usbn_get_version(void) {
return(usbn_read(USBN_RID));
}
/// END User Functions
/// BEGIN Hardware layer functions required by USB.C
/*******************************************************************************
/* usb_get_packet(endpoint, *ptr, max)
/*
/* Input: endpoint - endpoint to get data from
/* ptr - where to save data to local PIC RAM
/* max - max amount of data to receive from buffer
/*
/* Output: the amount of data taken from the buffer. If no data is available in the buffer,
/* or there was an error, will return FALSE.
/*
/* Summary: Gets a packet of data from the USBN buffer and puts into local PIC RAM.
/* You could poll usb_get_packet() until it doesn't return 0, or you could
/* poll usb_epX_rx_status.rx (where X is endpoint number) until .rx = 1.
/* usb_kbhit(endpoint) (located in usb.c) will poll usb_epX_rx_status.rx,
/* but is easier to read.
/*
/********************************************************************************/
int8 usb_get_packet(int8 endpoint, int8 * ptr, int8 max) {
int8 epc, address, rx=0, status, len, i;
if (endpoint < 16) {
epc=usb_find_epc(endpoint,0);
if (epc!=0xFF) {
address=USBN_RX_FIFOx[epc]; //RXDx
status=usbn_read(address+1); //RXSx
if ( ( (!bit_test(status,7)) && (bit_test(status,4) ) ) || (!endpoint) ) { //check make sure RX_ERR==0 and RX_LAST==1, unless endpoint==0
do {
status=usbn_read(address+1); //RXSx
len=status & 0x0F;
i=len;
debug(debug_txb,"\r\nRX %X %X: ", endpoint,len);
while ( (rx < max) && (i) ) {
*ptr=usbn_read(address);
debug(debug_txb, "%X ",*ptr);
rx++;
ptr++;
i--;
}
} while ( (rx < max) && (len) );
//debug(debug_txb, " D%U ",(status & 0x20)==0x20); //display DATA1/DATA0
debug(debug_txb, " %X ",status);
if (endpoint) { //re-enable reception for other endpoints, and flush rx-buffer
status=usbn_read(address+2);
status |= 0x09;
usbn_write(address+2,status);
}
return(rx);
}
}
}
return(0);
}
/*******************************************************************************
/* usb_stall_ep(endpoint,direction)
/*
/* Input: endpoint - endpoint to stall.
/* direction - direction of endpoint. 0 == OUT or CONTROL, 1 == IN
/*
/* Summary: Stalls specified endpoint. If endpoint is stalled it will NAK any tokens
/* destined to that endpoint.
/*
/********************************************************************************/
void usb_stall_ep(int8 endpoint, int1 direction) {
int8 epc, address, value;
if (endpoint < 16) {
epc=usb_find_epc(endpoint,direction);
if (epc!=0xFF) {
address=USBN_EPCx[epc];
value=usbn_read(address);
value |= 0x80;
usbn_write(address,value);
debug(debug_txb, " SE%X",endpoint | (direction << 7));
}
}
}
/*******************************************************************************
/* usb_unstall_ep(endpoint, direction)
/*
/* Input: endpoint - endpoint to un-stall.
/* direction - direction of endpoint. 0 == OUT or CONTROL, 1 == IN
/*
/* Summary: Un-stalls endpoint.
/*
/********************************************************************************/
void usb_unstall_ep(int8 endpoint, int1 direction) {
int8 epc, address, value;
if (endpoint < 16) {
epc=usb_find_epc(endpoint,direction);
if (epc!=0xFF) {
address=USBN_EPCx[epc];
value=usbn_read(address);
value &= 0x7F;
usbn_write(address,value);
debug(debug_txb, " USE%X",endpoint | (direction << 7));
}
}
}
/*******************************************************************************
/* usb_endpoint_stalled(endpoint, direction)
/*
/* Input: endpoint - endpoint to check
/* direction - direction of endpoint. 0 == OUT or CONTROL, 1 == IN
/*
/* Output: returns a TRUE if endpoint is stalled, FALSE if it is not.
/*
/* Summary: Looks to see if an endpoint is stalled, or not.
/*
/********************************************************************************/
int1 usb_endpoint_stalled(int8 endpoint, int1 direction) {
int8 epc, address, value;
if (endpoint < 16) {
epc=usb_find_epc(endpoint,direction);
if (epc!=0xFF) {
address=USBN_EPCx[epc];
value=usbn_read(address);
return(bit_test(value,7));
}
}
return(1);
}
/*******************************************************************************
/* usb_set_address(address)
/*
/* Input: address - address the host specified that we use
/*
/* Summary: Configures the USBN960x device for the specified device address
/*
/********************************************************************************/
void usb_set_address(int8 address) {
usbn_write(USBN_EPC0, usbn_read(USBN_EPC0) | 0x40);
usbn_write(USBN_FAR, address | 0x80);
}
/*******************************************************************************
/* usb_set_configured(config)
/*
/* Input: config - Configuration to use. 0 to uncofigure device.
/*
/* Summary: Configures or unconfigures device. If configuring device it will
/* enable all the endpoints. If un-configuring device it will disable all
/* endpoints.
/*
/* NOTE: CCS only provides code to handle 1 configuration.
/*
/********************************************************************************/
void usb_set_configured(int config) {
USB_Curr_Config=config;
if (config!=0) { //if config!=0 then enable required endpoints
//configure IN ep1
#IF USB_EP1_TX_ENABLE==1
usb_enable_endpoint(1,1,0); //endpoint 1, IN
#ELIF USB_EP1_TX_ENABLE==2
usb_enable_endpoint(1,1,1); //endpoint 1, IN, ISO
#ENDIF
//configure OUT ep1
#IF USB_EP1_RX_ENABLE==1
usb_enable_endpoint(1,0,0); //endpoint 1, OUT
#ELIF USB_EP1_RX_ENABLE==2
usb_enable_endpoint(1,0,1); //endpoint 1, OUT, ISO
#ENDIF
//configure IN ep2
#IF USB_EP2_TX_ENABLE==1
usb_enable_endpoint(2,1,0); //endpoint 2, IN
#ELIF USB_EP2_TX_ENABLE==2
usb_enable_endpoint(2,1,1); //endpoint 2, IN, ISO
#ENDIF
//configure OUT ep2
#IF USB_EP2_RX_ENABLE==1
usb_enable_endpoint(2,0,0); //endpoint 2, OUT
#ELIF USB_EP2_RX_ENABLE==2
usb_enable_endpoint(2,0,1); //endpoint 2, OUT, ISO
#ENDIF
//configure IN ep3
#IF USB_EP3_TX_ENABLE==1
usb_enable_endpoint(3,1,0); //endpoint 3, IN
#ELIF USB_EP3_TX_ENABLE==2
usb_enable_endpoint(3,1,1); //endpoint 3, IN, ISO
#ENDIF
//configure OUT ep3
#IF USB_EP3_RX_ENABLE==1
usb_enable_endpoint(3,0,0); //endpoint 3, OUT
#ELIF USB_EP3_RX_ENABLE==2
usb_enable_endpoint(3,0,1); //endpoint 3, OUT, ISO
#ENDIF
//configure IN ep4
#IF USB_EP4_TX_ENABLE==1
usb_enable_endpoint(4,1,0); //endpoint 4, IN
#ELIF USB_EP4_TX_ENABLE==2
usb_enable_endpoint(4,1,1); //endpoint 4, IN, ISO
#ENDIF
//configure OUT ep4
#IF USB_EP4_RX_ENABLE==1
usb_enable_endpoint(4,0,0); //endpoint 4, OUT
#ELIF USB_EP4_RX_ENABLE==2
usb_enable_endpoint(4,0,1); //endpoint 4, OUT, ISO
#ENDIF
//configure IN ep5
#IF USB_EP5_TX_ENABLE==1
usb_enable_endpoint(5,1,0); //endpoint 5, IN
#ELIF USB_EP5_TX_ENABLE==2
usb_enable_endpoint(5,1,1); //endpoint 5, IN, ISO
#ENDIF
//configure OUT ep5
#IF USB_EP5_RX_ENABLE==1
usb_enable_endpoint(5,0,0); //endpoint 5, OUT
#ELIF USB_EP5_RX_ENABLE==2
usb_enable_endpoint(5,0,1); //endpoint 5, OUT, ISO
#ENDIF
//configure IN ep6
#IF USB_EP6_TX_ENABLE==1
usb_enable_endpoint(6,1,0); //endpoint 6, IN
#ELIF USB_EP6_TX_ENABLE==2
usb_enable_endpoint(6,1,1); //endpoint 6, IN, ISO
#ENDIF
//configure OUT ep6
#IF USB_EP6_RX_ENABLE==1
usb_enable_endpoint(6,0,0); //endpoint 6, OUT
#ELIF USB_EP6_RX_ENABLE==2
usb_enable_endpoint(6,0,1); //endpoint 6, OUT, ISO
#ENDIF
//configure IN ep7
#IF USB_EP7_TX_ENABLE==1
usb_enable_endpoint(7,1,0); //endpoint 7, IN
#ELIF USB_EP7_TX_ENABLE==2
usb_enable_endpoint(7,1,1); //endpoint 7, IN, ISO
#ENDIF
//configure OUT ep7
#IF USB_EP7_RX_ENABLE==1
usb_enable_endpoint(7,0,0); //endpoint 7, OUT
#ELIF USB_EP7_RX_ENABLE==2
usb_enable_endpoint(7,0,1); //endpoint 7, OUT, ISO
#ENDIF
//configure IN ep8
#IF USB_EP8_TX_ENABLE==1
usb_enable_endpoint(8,1,0); //endpoint 8, IN
#ELIF USB_EP8_TX_ENABLE==2
usb_enable_endpoint(8,1,1); //endpoint 8, IN, ISO
#ENDIF
//configure OUT ep8
#IF USB_EP8_RX_ENABLE==1
usb_enable_endpoint(8,0,0); //endpoint 8, OUT
#ELIF USB_EP8_RX_ENABLE==2
usb_enable_endpoint(8,0,1); //endpoint 8, OUT, ISO
#ENDIF
//configure IN ep9
#IF USB_EP9_TX_ENABLE==1
usb_enable_endpoint(9,1,0); //endpoint 9, IN
#ELIF USB_EP9_TX_ENABLE==2
usb_enable_endpoint(9,1,1); //endpoint 9, IN, ISO
#ENDIF
//configure OUT ep9
#IF USB_EP9_RX_ENABLE==1
usb_enable_endpoint(9,0,0); //endpoint 9, OUT
#ELIF USB_EP9_RX_ENABLE==2
usb_enable_endpoint(9,0,1); //endpoint 9, OUT, ISO
#ENDIF
//configure IN ep10
#IF USB_EP10_TX_ENABLE==1
usb_enable_endpoint(10,1,0); //endpoint 10, IN
#ELIF USB_EP10_TX_ENABLE==2
usb_enable_endpoint(10,1,1); //endpoint 10, IN, ISO
#ENDIF
//configure OUT ep10
#IF USB_EP10_RX_ENABLE==1
usb_enable_endpoint(10,0,0); //endpoint 10, OUT
#ELIF USB_EP10_RX_ENABLE==2
usb_enable_endpoint(10,0,1); //endpoint 10, OUT, ISO
#ENDIF
//configure IN ep11
#IF USB_EP11_TX_ENABLE==1
usb_enable_endpoint(11,1,0); //endpoint 11, IN
#ELIF USB_EP11_TX_ENABLE==2
usb_enable_endpoint(11,1,1); //endpoint 11, IN, ISO
#ENDIF
//configure OUT ep11
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?