📄 usb_hw.c
字号:
*/
void USB_HW_StallEP0 (void)
{
}
/*
*********************************************************************************************************
* USB_HW_SetAddress()
*
* Description: This function is used to set the USB controller's address after a SetAddress request
* is received. The function actually sets a global variable with the Address, since the
* controller's Address Register should not be set until an acknowledgment is sent.
*
* Arguments : Adr The new address for the device
*
* Returns : None
*********************************************************************************************************
*/
void USB_HW_SetAddress (unsigned char addr)
{
USB_Tmp_Addr = addr;
/* Send a 0-byte acknowledge */
USB_REG_WR(ENDPOINT0A_LEN, 0x00);
USB_REG_WR(ENDPOINT0A_PKT, ENDPOINT_PKT_ACK | ENDPOINT_PKT_SEQUENCE);
USB_REG_WR(ENDPOINT0A_CTRL, ENDPOINT_CTRL_ARM | ENDPOINT_CTRL_ENABLE | ENDPOINT_CTRL_DIRECTION | ENDPOINT_CTRL_SEQUENCE);
}
/*
*********************************************************************************************************
* Enable Endpoint 1
*
* Description: This function is called by the target-independent code to set up Endpoint 1 as a bulk
* OUT endpoint, and set up Endpoint 2 as a bulk IN endpoint.
*
* Arguments : None
*
* Returns : None
*********************************************************************************************************
*/
void USB_HW_EnableEP1 (void)
{ /* Set up Endpoint 1 for bulk read transfers */
USB_REG_WR(ENDPOINT1A_BASE, 0xC0);
USB_REG_WR(ENDPOINT1A_LEN, 0x20);
USB_REG_WR(ENDPOINT1A_PKT, 0x00);
USB_REG_WR(ENDPOINT1A_CTRL, ENDPOINT_CTRL_ARM | ENDPOINT_CTRL_ENABLE | ENDPOINT_CTRL_SEQUENCE);
/* Set up Endpoint 2 for bulk write transfers */
USB_REG_WR(ENDPOINT2A_BASE, 0x60);
USB_REG_WR(ENDPOINT2A_LEN, 0x00);
USB_REG_WR(ENDPOINT2A_PKT, ENDPOINT_PKT_ACK | ENDPOINT_PKT_SEQUENCE);
/* Endpoint 2 will not be armed until data is ready to be sent */
USB_REG_WR(ENDPOINT2A_CTRL, ENDPOINT_CTRL_ENABLE | ENDPOINT_CTRL_SEQUENCE | ENDPOINT_CTRL_DIRECTION);
/* Enable interrupts for Endpoint 0, 1, 2, and USB Resets */
USB_REG_WR(INT_ENABLE_REG, INT_ENABLE_ENDPOINT0 | INT_ENABLE_ENDPOINT1 | INT_ENABLE_ENDPOINT2 | INT_ENABLE_USB_RESET);
/* Send a 0-byte acknowledge for the SetConfiguration request */
USB_REG_WR(ENDPOINT0A_LEN, 0x00);
USB_REG_WR(ENDPOINT0A_PKT, ENDPOINT_PKT_ACK | ENDPOINT_PKT_SEQUENCE);
USB_REG_WR(ENDPOINT0A_CTRL, ENDPOINT_CTRL_ARM | ENDPOINT_CTRL_ENABLE | ENDPOINT_CTRL_DIRECTION | ENDPOINT_CTRL_SEQUENCE);
}
/*
*********************************************************************************************************
* Send Data from Endpoint 0
*
* Description: This function is used to send data over Endpoint 0 in response to the requests received
* in setup packets. Currently requests for more than 64 bytes are not supported.
*
* Arguments : p_data A pointer to the data to send
* tx_len The amount of data to send (in bytes)
* tx_null_pkt Unused
*
* Returns : None
*********************************************************************************************************
*/
void USB_HW_EP0_Send (const unsigned char *p_data, unsigned char tx_len, char tx_null_pkt)
{
USB_U8 *send_buf;
USB_U8 wr_status;
send_buf = (USB_U8 *)p_data;
/* Notice that currently, only control transactions with data stages */
/* of less than 64 bytes are supported */
if (tx_len > USB_SETUP_MAX_PKT_SIZE) {
tx_len = USB_SETUP_MAX_PKT_SIZE;
}
/* Write the data to Endpoint 0's buffer on the SL811HS */
wr_status = USB_Reg_Wr_Buf(send_buf, ENDPOINT0A_BASE, tx_len);
/* Set up Endpoint 0 to send the data */
USB_REG_WR(ENDPOINT0A_LEN, wr_status);
USB_REG_WR(ENDPOINT0A_PKT, ENDPOINT_PKT_ACK);
USB_REG_WR(ENDPOINT0A_CTRL, ENDPOINT_CTRL_ARM | ENDPOINT_CTRL_ENABLE | ENDPOINT_CTRL_DIRECTION | ENDPOINT_CTRL_SEQUENCE);
}
/*
*********************************************************************************************************
* Prepare Endpoint 0 for the Data Stage
*
* Description: This function would be used to perform any operations necessary to move to the data stage.
* It is left empty for the Cypress SL811HS controller.
*
* Arguments : None
*
* Returns : None
*********************************************************************************************************
*/
void USB_HW_ClearOutPacketReady (void)
{
}
/*
*********************************************************************************************************
* Prepare Endpoint 0 for the Data Stage
*
* Description: This function would be called before processing a setup request. It is left empty for
* the Cypress SL811HS controller.
*
* Arguments : None
*
* Returns : None
*********************************************************************************************************
*/
void USB_HW_ClrSetupEnd (void)
{
}
/*
**********************************************************************************************************
* Bulk Endpoint Interrupts
*
* Description: This function handles interrupts on the bulk endpoints (Endpoint 1 and Endpoint 2). It
* is called from USB_HW_MainISR(), which is the interrupt handler that is registered with
* the HAL for USB interrupts.
*
* Arguments : None
*
* Returns : None
***********************************************************************************************************
*/
void USB_ISR_Function (void)
{
USB_U8 int_status;
USB_U8 len;
USB_U8 cnt;
/* If the interrupt is from Endpoint 2, a packet has been sent */
USB_REG_RD(INT_STATUS_REG, int_status);
if ((int_status & INT_STATUS_ENDPOINT2) != 0) {
if (USB_Send_State == 1) {
USB_HW_SendPkt();
} else {
USB__OnTx();
}
} else { /* If the interrupt is from Endpoint 1, the size of the data */
/* that was received must be determined */
USB_REG_RD(ENDPOINT1A_LEN, len);
USB_REG_RD(ENDPOINT1A_CNT, cnt);
len -= cnt;
USB__OnRx(len); /* Notify the hardware-independent code that a packet was received */
}
}
/*
***********************************************************************************************************
* Endpoint 0 Interrupts
*
* Description: This function handles interrupts from Endpoint 0, which occur during Control Transfers for
* enumeration. The variable "State" is used to keep track of the cause of previous
* interrupts since an interrupt does not always indicate that a setup packet was received.
*
* Arguments : None
*
* Returns : None
***********************************************************************************************************
*/
void USB_ISR_EP0 (void)
{
USB_U8 pkt;
USB_SETUP_PACKET setup_pkt;
USB_REG_RD(ENDPOINT0A_PKT, pkt);
if ((pkt & ENDPOINT_PKT_SETUP) != 0) {
USB_Reg_Rd_Buf((USB_U8*)&setup_pkt, ENDPOINT0A_BASE, USB_SETUP_PKT_SIZE);
USB_Setup_State = USB_STATE_CTRL_DATA;
USB__HandleSetup(&setup_pkt); /* Send the packet to the hardware-independent code for processing */
} else if (USB_Setup_State == USB_STATE_CTRL_DATA) {
/* This state indicates that the device has just sent a */
/* data packet, so it should prepare to receive a 0-byte */
/* acknowledgement from the host */
USB_REG_WR(ENDPOINT0A_LEN, 0x40);
USB_REG_WR(ENDPOINT0A_CTRL, ENDPOINT_CTRL_ARM | ENDPOINT_CTRL_ENABLE | ENDPOINT_CTRL_SEQUENCE);
USB_Setup_State = USB_STATE_CTRL_STATUS;
} else if (USB_Setup_State == USB_STATE_CTRL_STATUS) {
/* This state may indicate that a 0-byte acknowledgment has */
/* been sent in response to a setup request without a data */
/* stage, or that a 0-byte acknowledgment has been received */
/* in response to a setup packet request with an IN data */
/* stage. Either way, this state indicates the end of the */
/* status stage, so Endpoint 0 should be prepared to */
/* receive a new request. */
if (USB_Tmp_Addr != 0x00) { /* If the request was a Set_Address, set the address register */
USB_REG_WR(ADDR_REG, USB_Tmp_Addr);
USB_Tmp_Addr = 0x00;
}
USB_REG_WR(ENDPOINT0A_LEN, 0x40);
USB_REG_WR(ENDPOINT0A_CTRL, ENDPOINT_CTRL_ARM | ENDPOINT_CTRL_ENABLE | ENDPOINT_CTRL_SEQUENCE);
USB_Setup_State = USB_STATE_CTRL_SETUP;
}
}
/*
*********************************************************************************************************
* USB Resets
*
* Description: This function is called when a USB reset occurs. It sets up Endpoint 0 to receive setup
* packets, and it ensures that the device is still using the default address of 0x00.
*
* Arguments : None
*
* Returns : None
*********************************************************************************************************
*/
void USB_ISR_Reset (void)
{ /* Enable Endpoint 0 to receive the first setup packet */
USB_REG_WR(ENDPOINT0A_CTRL, ENDPOINT_CTRL_ARM | ENDPOINT_CTRL_ENABLE);
USB_REG_WR(ENDPOINT0A_BASE, 0x40);
USB_REG_WR(ENDPOINT0A_LEN, 0x40);
USB_REG_WR(ENDPOINT0A_PKT, 0x00);
/* Set up the default address of 0x00 for enumeration */
USB_REG_WR(ADDR_REG, 0x00);
USB_Setup_State = 0;
}
/*
*********************************************************************************************************
* USB Resets
*
* Description: This function would be called when the USB cable has been physically connected to the
* device, but this is not necessary with the SL811HS.
*
* Arguments : None
*
* Returns : None
*********************************************************************************************************
*/
void VBUS_ISR_DetectInterrupt (void)
{
}
/*
**********************************************************************************************************
* Start-of-Frame Packet
*
* Description: This function is called whenever a Start-of-Frame packet is received.
*
* Arguments : None
*
* Returns : None
**********************************************************************************************************
*/
void USB_ISR_SOF (void)
{
}
/*
**********************************************************************************************************
* Suspend State Interrupt
*
* Description: This function is called when the bus has been idle for an extended time.
*
* Arguments : None
*
* Returns : None
**********************************************************************************************************
*/
void USB_ISR_Suspend (void)
{
}
/*
**********************************************************************************************************
* Resume from Suspend State Interrupt
*
* Description: This function is called when activity resumes on the bus.
*
* Arguments : None
*
* Returns : None
**********************************************************************************************************
*/
void USB_ISR_Resume (void)
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -