📄 usbci.c
字号:
if (endp == 0)
{
UD_ICSR1 = UD_ICSR1 & (~EP0_WR_BITS) | EP0_SERVICED_OUT_PKT_RDY; /* clear the SERVICED_SETUP_END of EP0_CSR */
}
else
{
UD_OCSR1 &= ~(EPO_OUT_PKT_READY); /* 清空 OUT FIFO. clear the FIFO of OUT endpoint */
}
}
/***************************************************************************************************************
** 函数名称 : USB_ValidBuffer() Name : USB_ValidBuffer()
** 功能描述 : 使能 IN 端点缓冲区 Function : validate the IN endpoint buffer
** 输 入 : INT8U endp: 物理端点号 Input : INT8U endp: the physical endpoint number
** 输 出 : 无 Output : NULL
** 注 意 : 本函数不适合于 Ep0, USB_ValidBufferEp0 Note : the function is not adapter to EP0, if you
才合适 use Ep0, please use USB_ValidBufferEp0()
****************************************************************************************************************/
void USB_ValidBuffer(INT8U endp)
{
USB_SetCurEndpoint(endp); /* 选择端点. select the endpoint */
if (endp != 0)
{
UD_ICSR1 |= EPI_IN_PKT_READY; /* 使能 IN 端点 FIFO. enable the FIFO of IN endpoint */
}
}
/***************************************************************************************************************
** 函数名称 : USB_ValidBufferEp0() Name : USB_ValidBufferEp0()
** 功能描述 : 使能 IN 端点(0 端点)缓冲区 Function : validate the IN endpoint(0 endpoint) buffer
** 输 入 : INT8U bDataEnd: Input : INT8U bDataEnd:
1: 数据阶段的最后一个IN包 1: the IN packet is the last packet of data phase
0: 不是最后一个IN包 0: the IN packet is not the last packet of data phase
** 输 出 : 无 Output : NULL
****************************************************************************************************************/
void USB_ValidBufferEp0(INT8U bDataEnd)
{
USB_SetCurEndpoint(0);
if (bDataEnd)
UD_ICSR1 = UD_ICSR1 & (~EP0_WR_BITS) | (EP0_DATA_END | EP0_IN_PKT_READY); /* clear DATA_END and IN_PKT_RDY bit */
else
UD_ICSR1 = UD_ICSR1 & (~EP0_WR_BITS) | EP0_IN_PKT_READY; /* clear IN_PKT_RDY bit */
}
/***************************************************************************************************************
** 函数名称 : USB_ReadEndpoint() Name : USB_ReadEndpoint()
** 功能描述 : 读物理端点 Function : Read data from USB endpoint
** 入口参数 : INT8U endp: 物理端点号 Input : INT8U endp: the physical endpoint number
INT8U len : 要读的字节长度 INT8U len : the length that will be read
INT8U *buf: 接收缓冲区 INT8U *buf: receiving buffer
** 输 出 : 实际读到的字节数 Output : the actual length that will be read
****************************************************************************************************************/
INT32U USB_ReadEndpoint(INT8U endp, INT32U len, INT8U *buf)
{
INT32U i,cnt;
INT8U *pFIFO = (INT8U *)&bUD(EP0_FIFO_OFFSET + (endp << 2)); /* 取得端点FIFO地址 get the FIFO address of the endpoint */
USB_SetCurEndpoint(endp);
cnt = UD_OFCNTL; /* 读取收到的字节数 read the byte lenghth received */
if (cnt > len)
cnt = len;
for (i = 0; i < cnt; i++) /* 从端点FIFO中读取数据 read data from the endpoint FIFO */
buf[i] = *pFIFO;
USB_ClearBuffer(endp);
return cnt; /* 返回实际读出数据长度 return the actual length that have being read */
}
/***************************************************************************************************************
** 函数名称 : USB_WriteEndpoint() Name : USB_WriteEndpoint()
** 功能描述 : 向物理端点写入数据 Function : Write data to USB endpoint
** 入口参数 : INT8U endp: 物理端点号 Input : INT8U endp: the physical endpoint number
INT8U len : 要写的字节长度 INT8U len : the length that will be written
INT8U *buf: 发送缓冲区 INT8U *buf: sending buffer
** 输 出 : 实际写入的字节数 Output : the actual length that have being written
****************************************************************************************************************/
INT32U USB_WriteEndpoint(INT8U endp, INT32U len, INT8U *buf)
{
INT32U i;
INT8U *pFIFO = (INT8U *)&bUD(EP0_FIFO_OFFSET + (endp << 2)); /* 取得端点FIFO地址 get the FIFO address of the endpoint */
for(i = 0; i < len; i++) /* 从FIFO中读取数据 read the data from FIFO */
*pFIFO = buf[i];
USB_ValidBuffer(endp); /* 使能FIFO数据有效 validate the FIFO data */
return len; /* 返回写入数据长度 return the actual length that have being written*/
}
/***************************************************************************************************************
** 函数名称 : USB_EndSetup() Name : USB_EndSetup()
** 功能描述 : 结束SETUP Function : End a SETUP
****************************************************************************************************************/
void USB_EndSetup(void)
{
USB_SetCurEndpoint(0);
UD_ICSR1 = UD_ICSR1 & (~EP0_WR_BITS) | (EP0_SERVICED_OUT_PKT_RDY | EP0_DATA_END);
}
/***************************************************************************************************************
** 函数名称 : USB_Clr_EP0_SETUP_END() Name : USB_Clr_EP0_SETUP_END()
** 功能描述 : 清除 EP0_CSR 寄存器的 SETUP_END 位 Function : Clear the SETUP_END bit of EP0_CSR
****************************************************************************************************************/
void USB_Clr_EP0_SETUP_END(void)
{
USB_SetCurEndpoint(0);
UD_ICSR1 = (UD_ICSR1 & (~EP0_WR_BITS) | (EP0_SERVICED_SETUP_END));
}
/***************************************************************************************************************
** 函数名称 : USB_Clr_EP0_SENT_STALL() Name : USB_Clr_EP0_SENT_STALL()
** 功能描述 : 清除 EP0_CSR 寄存器的 SENT_STALL 位 Function : Clear the SENT_STALL bit of EP0_CSR
****************************************************************************************************************/
void USB_Clr_EP0_SENT_STALL(void)
{
USB_SetCurEndpoint(0);
UD_ICSR1 = (UD_ICSR1 & (~EP0_WR_BITS) & (~EP0_SENT_STALL));
}
/***************************************************************************************************************
** 函数名称 : USB_ReadINEpStatus() Name : USB_ReadINEpStatus()
** 功能描述 : 读 IN 端点状态 Function : read IN endpoint status
** 入口参数 : INT8U endp: 物理端点号 Input : INT8U endp: the physical endpoint number
** 输 出 : IN 端点状态 Output : the IN endpoint status
****************************************************************************************************************/
INT8U USB_ReadINEpStatus(INT8U endp)
{
USB_SetCurEndpoint(endp);
return UD_ICSR1;
}
/***************************************************************************************************************
** 函数名称 : USB_ReadOUTEpStatus() Name : USB_ReadOUTEpStatus()
** 功能描述 : 读 OUT 端点状态 Function : read OUT endpoint status
** 入口参数 : INT8U endp: 物理端点号 Input : INT8U endp: the physical endpoint number
** 输 出 : OUT 端点状态 Output : the OUT endpoint status
****************************************************************************************************************/
INT8U USB_ReadOUTEpStatus(INT8U endp)
{
USB_SetCurEndpoint(endp);
return UD_OCSR1;
}
/*******************************************************************************************************
** End Of File
********************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -