📄 usbdrv.c
字号:
tmp &= ~DMA_DTGL;
endpoint_stat[DMA_ep]->toggle_bit = 0;
}
/*----------------------------
* re-enable USB DMA operation
------------------------------*/
write_usb(DMACNTRL,tmp);
USB_start_dma(9 /* may be any number from 0 to 255 */);
/*------------------------
* Insert your code here
*------------------------*/
break;
case DMA_DERR:
break;
case DMA_DSHLT:
break;
} //*/
break;
case ALT_RESET:
//Uart_Printf("\nalt2");
CLEAR_STALL_EP0;
GOTO_STATE(RST_ST);//node enter reset status
/*--------------------
* set default address
*--------------------*/
write_usb(FAR,AD_EN+0);//address enable=0
/*----------------
* enable EP0 only
*----------------*/
write_usb(EPC0, 0x00);
/*---------------
* Go Reset state
*---------------*/
reset_usb();
usb_device_reset();
/*--------------
* Go operational
*---------------*/
GOTO_STATE(OPR_ST);
break;
case ALT_SD3:
case ALT_SD5:
//Uart_Printf("\nalt3");
/*------------------
* Enable Resume int
*------------------*/
ENABLE_ALT_INTS(ALT_SD3|ALT_RESET|ALT_RESUME|ALT_DMA);
/*-------------------
* enter suspend state
*------------------*/
GOTO_STATE(SUS_ST);
break;
case ALT_RESUME:
//Uart_Printf("\nalt4");
/*------------------
* Disabe Resume int
*------------------*/
ENABLE_ALT_INTS(ALT_SD3|ALT_RESET|ALT_DMA);
/*----------------
* Enable receving
*---------------*/
GOTO_STATE(OPR_ST);
/*-----------------
* Operational state
*-----------------*/
ENABLE_RX(ENDPOINT_0);
break;
case ALT_EOP:
//Uart_Printf("\nalt5");
break;
default:
//Uart_Printf("\nalt6");
/*------------------------------
* No event with this event mask
*------------------------------*/
break;
}
alt_event &= ~evnt_mask;
evnt_mask = evnt_mask << 1;
}
}
byte ep2diag = 0;
byte mac2 = 0;
/*----------------------------------------------------------------------------------------------
* Prototype
* void USBN9604_rx_event_handler()
*
* Parameters
* None
*
* Returns
* None
*
* Description
* RX event handler
----------------------------------------------------------------------------------------------*/
/*__inline*/ void USBN9604_rx_event_handler(void)
{
volatile byte rx_event = RX_EVENTS;
byte evnt_mask = 1;
volatile byte rxstat;
byte Toggle;
byte i;
int request_length;
endpoint_t ep_num = ENDPOINT_0;
while(rx_event)
{
switch(rx_event & evnt_mask)
{
case RX_FIFO0:
/*-----------
* endpoint 0
*-----------*/
ep_num = ENDPOINT_0;
clear_control_buffer(&control_receive_buffer);
/*-------------------------------------------------------------
* Is this a setup packet? rcv 8B data packet after setup packet
*--------------------------------------------------------------*/
rxstat = EP_RXSTATUS(ENDPOINT_0);
if (rxstat & SETUP_R)//setup packet has been received
{
/*----------------------
* Clear STALL condition
*----------------------*/
//Uart_Printf("\nCTL: ");
endpoint_stat[0]->toggle_bit = 0x01;
CLEAR_STALL_EP0;
request_length = mini(fifo_sizes[ep_num], RX_EP_COUNTER(ep_num));
//request length in RCOUNT of RXS0
/*-------------------------
* Receive request from hub
*-------------------------*/
for(i=0; i<request_length; i++)
{
request_buffer[i] = EP_FIFO_READ(ENDPOINT_0);
//Uart_Printf("%x-",request_buffer[i]);
}
control_receive_buffer.data = (byte *)request_buffer;
control_receive_buffer.bytes_counter = request_length;
FLUSH_TXEP(ENDPOINT_0);
FLUSH_RXEP(ENDPOINT_0);
USB_device_req_handler();//deal with 8B request
}
/*------------------------------------------------------------
* OUT packet of Status stage in control read/write sequence
*----------------------------------------------------------*/
else
{
//Uart_Printf("\nrO");
/*--------------------
* Re-enable receiving
*-------------------*/
FLUSHTX0;
FLUSHRX0;
ENABLE_RX(ENDPOINT_0);
clear_control_buffer(&control_send_buffer);
}
// Consider that zero length data after OUT packet in Status
// stage, only ACK, NAK, STALL to host (chip do it aotumatic?)
break;
case RX_FIFO1:
/*-------------------
* endpoint 2 bulk OUT
*-------------------*/
ep_num = ENDPOINT_2;
rxstat = EP_RXSTATUS(ep_num);
if (rxstat & RX_ERR)
{
/*-------------------
* receive media error
*--------------------*/
FLUSH_RXEP(ep_num);
ENABLE_RX(ep_num);
break;
}
Toggle = endpoint_stat[ep_num]->toggle_bit;
Toggle <<= 5;
//a packet data have a DATA0/DATA1
if ((rxstat & RX_LAST) && ((rxstat & RX_TOGL) == Toggle ))
{
/*-------------------------
* received data is correct
*-------------------------*/
endpoint_stat[ep_num]->toggle_bit = endpoint_stat[ep_num]->toggle_bit ? 0 : 1;
//send_event(EVT_USB_BULK_RX);
BulkReceiveEvent();
}
else
{
/*--------------------------------
* response to the toggle bit error
* response to the receive error
*--------------------------------*/
FLUSH_RXEP(ep_num);
ENABLE_RX(ep_num);
}
break;
case RX_FIFO2:
/*----------------
* endpoint 4
* isochronous OUT
*----------------*
rxstat = EP_RXSTATUS(ENDPOINT_4);
if ((rxstat & RX_ERR) &&!(rxstat & RX_LAST))
{
*-------------------
* receive media error
*--------------------*
FLUSH_RXEP(ENDPOINT_4);
ENABLE_RX(ENDPOINT_4);
break;
}
*-------------------------
* received data is correct
*-------------------------*/
//send_event(EVT_USB_ISO_RX);
break;
case RX_FIFO3:
break;
case RX_OVRN0:
break;
case RX_OVRN1:
break;
case RX_OVRN2:
break;
case RX_OVRN3:
break;
default:
/*------------------------------
* No event with this event mask
*------------------------------*/
break;
}
rx_event &= ~evnt_mask;
evnt_mask = evnt_mask << 1;
}
}
/*=====================================================================
* Usb module external interface implementations
*=====================================================================*
*----------------------------------------------------------------------------------------------
* Prototype
* void USB_Transmit_Data (int data_size, BYTE* data_ptr, endpoint_t ep_num)
*
* Parameters
* data_size - size of data (in bytes) to be sent
* data_ptr - pointer to data to be sent
* ep_num - number of transmit endpoint
*
* Returns
* Number of byes that was write to the fifo
*
* Description
* Fill corresponding endpoint with the new data. The data size to be written to the FIFO
* is the minimum between requested size and FIFO size.
-------------------------------------------------------------------------------------------------*/
byte USB_Transmit_Data (int data_size, byte* data_ptr, endpoint_t ep_num)
{
byte i;
byte size;
//_disable_();
FLUSH_TXEP(ep_num);
/*-------------------------------------------------
* assure that data size is no more then fifo length
*--------------------------------------------------*/
size = mini(data_size, fifo_sizes[ep_num]);
/*-------------------------------------------
* Now data size is no more than the fifo length,
* transfer the data directly to the fifo
*--------------------------------------------*/
USBADDR = usbn9604_tx_endpoint_addr[ep_num];
for (i=0;i<size;i++)
/*-----------------------------------
* transfer data directly to the fifo
* address updating is not needed
*-----------------------------------*/
USBDATA = data_ptr[i];
/*---------------------------
* enable data transmittion
*---------------------------*/
usbn9604_tx_enable(ep_num);
//_enable_();
return size;
}
/*----------------------------------------------------------------------------------------------
* Prototype
* byte USB_Receive_Data (BYTE* data_ptr, endpoint_t ep_num)
*
* Parameters
* data_ptr - pointer to the local buffer
* ep_num - number of receive endpoint
*
* Returns
* Number of byes that was read from the FIFO.
*
* Description
* Read whole corresponding endpoint to the local buffer.
-------------------------------------------------------------------------------------------------*/
byte USB_Receive_Data (byte* data_ptr,endpoint_t ep_num)
{
byte bytes_count;
byte bytes_sum = 0;
int i;
_disable_();
/*-------------------------------------------
* Read data from the fifo until it's empty
*-------------------------------------------*/
do
{
/*-----------------------------------------
* Read count of bytes presently in the FIFO
*-----------------------------------------*/
bytes_count = RX_EP_COUNTER(ep_num);
/*---------------------------------------
* update address register in the Voyager
* with address of Receive Data register
*---------------------------------------*/
USBADDR = usbn9604_rx_endpoint_addr[ep_num];
for(i=0;i<bytes_count;i++)
{
/*-----------------------------------
* read data directly from the fifo
* address updating is not needed
*-----------------------------------*/
*(data_ptr ++) = USBDATA;
}
bytes_sum += bytes_count;
}
/*-----------------------------------------------------
* If the FIFO containes more than 15 bytes, a value 15 is
* reported in the counter. Therefore continue reading from the
* fifo until the counter is less then 15.
*------------------------------------------------------*/
while (bytes_count == 0xf);
/*----------------------------------------------
* Flush the fifo and re-enable receive operation
*-----------------------------------------------*/
FLUSH_RXEP(ep_num);
ENABLE_RX(ep_num);
//_enable_();
return bytes_sum;
}
/*=====================================================================
* Usb module services implementations
*=====================================================================*/
/*----------------------------------------------------------------------------------------------
* Prototype
* void send_control_data(byte *data_ptr, int data_size)
*
* Parameters
* data_ptr - pointer to the data buffer to be send
* data_size - data buffer size
*
* Returns
* None
*
* Description
* Sends data through the control pipe
-------------------------------------------------------------------------------------------------*/
void send_control_data(byte *data_ptr, int data_size)
{
//Uart_Printf("\nsi");
FLUSHTX0;
control_send_buffer.data = data_ptr;
control_send_buffer.bytes_counter = data_size;
fill_control_fifo();
usbn9604_tx_enable(ENDPOINT_0);
}
/*-------------------------------------------------------------
* Prototype
* void zero_length_data_response(endpoint_t ep_num)
*
* Parameters
* ep_num - endpoint number
*
* Returns
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -