?? usbci.c
字號:
return USB_GetData(tmp | USBDAT_SEL_ENDP);
}
/***************************************************************************************************************
** 函數名稱 : USB_SelectClrIntEndpoint() Name : USB_SelectClrIntEndpoint()
** 功能描述 : 選擇端點,并獲取端點信息和清除對應端點的 Function : select the endpoint, and get the endpoint information
中斷 and clear the interrupt of the correspond endpoint
** 輸 入 : INT8U endp: 物理端點號 Input : INT8U endp: the physical endpoint number
** 輸 出 : 對應端點的信息 Output : the endpoint information
****************************************************************************************************************/
INT8U USB_SelectClrIntEndpoint(INT8U endp)
{
INT8U i;
INT32U tmp = (INT32U)((endp + 0x40) << 16);
USB_SendCmd(tmp | USBCMD_SEL_CLRINT_ENDP, 0);
i = USB_GetData(tmp | USBDAT_SEL_CLRINT_ENDP);
USBEpIntClr = (0x01 << endp);
return i;
}
/***************************************************************************************************************
** 函數名稱 : USB_SetEndpointStatus() Name : USB_SetEndpointStatus()
** 功能描述 : 設置端點狀態 Function : set endpoint status
** 輸 入 : INT8U endp : 物理端點號 Input : INT8U endp : the physical endpoint number
INT8U bStalled: 1 - stall 端點 INT8U bStalled: 1 - stall endpoint
0 - unstall 端點 0 - unstall endpoint
** 輸 出 : 無 Output : NULL
****************************************************************************************************************/
void USB_SetEndpointStatus(INT8U endp, INT8U bStalled)
{
INT32U tmp1 = (INT32U)((endp + 0x40) << 16);
INT32U tmp2 = (INT32U)(bStalled << 16);
USB_SendCmd(tmp1 | USBCMD_SET_ENDP_STATUS, tmp2 | USBDAT_SET_ENDP_STATUS);
}
/***************************************************************************************************************
** 函數名稱 : USB_ClearBuffer() Name : USB_ClearBuffer()
** 功能描述 : 清除OUT端點緩沖區 Function : clear the OUT endpoint buffer
** 輸 入 : 無 Input : NULL
** 輸 出 : 一個字節 Output : read a byte
****************************************************************************************************************/
INT8U USB_ClearBuffer(void)
{
/* no data phase */
USB_SendCmd(USBCMD_CLEAR_BUFFER, 0); /* 該命令沒有數據階段 */
return USB_GetData(USBDAT_CLEAR_BUFFER);
}
/***************************************************************************************************************
** 函數名稱 : USB_ValidBuffer() Name : USB_ClearBuffer()
** 功能描述 : 使能 IN 端點緩沖區 Function : validate the IN endpoint buffer
** 輸 入 : 無 Input : NULL
** 輸 出 : 無 Output : NULL
****************************************************************************************************************/
void USB_ValidBuffer(void)
{
USB_SendCmd(USBCMD_VALID_BUFFER, 0);
}
/***********************************************************
端點數據傳輸 Endpoint Data Transfer
***********************************************************/
/***************************************************************************************************************
** 函數名稱 : 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 be read
****************************************************************************************************************/
INT8U USB_ReadEndpoint(INT8U endp, INT8U len, INT8U *buf)
{
INT32U tmp;
INT32U i,j;
INT32U reclen;
/* select logical endpoint and enable read operate */
USBCtrl = (INT32U)((endp >> 1) << 2) | 0x01; /* 選擇邏輯端點并使能讀操作 */
/* wait for the data ready */
if((endp % 2 == 0))
while((USBRxPLen & 0x00000C00) ==0); /* 等待數據就緒 */
/* get the receive length */
reclen = USBRxPLen & 0x3FF; /* 獲取收到的數據的字節長度 */
if (reclen > len)
reclen = len;
/* read data from ep ram */ /* 從 EP RAM 中讀取數據 */
for (i = 0; i < (reclen / 4); i++)
{
tmp = USBRxData;
*buf++ = (INT8U)tmp;
*buf++ = (INT8U)(tmp >> 8);
*buf++ = (INT8U)(tmp >> 16);
*buf++ = (INT8U)(tmp >> 24);
}
j = reclen % 4;
if (j != 0)
{
tmp = USBRxData;
for(i = 0; i < j; i++)
*buf++ = (INT8U)(tmp >> (i << 3));
}
/* clear the RxENDPKT bit in USBDevIntClr register */
USBDevIntClr = RxENDPKT; /* 清除 RxENDPKT 位 */
/* clear the OUT buffer */
USB_SelectEndpoint(endp);
USB_ClearBuffer(); /* 清除OUT緩沖區 */
USBCtrl = 0;
return reclen; /* 返回實際收到的字節數 */
}
/***************************************************************************************************************
** 函數名稱 : USB_ReadEndpoint() 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
****************************************************************************************************************/
INT8U USB_WriteEndpoint(INT8U endp, INT8U len, INT8U *buf)
{
INT32U tmp;
INT32U i,j;
/* select logical endpoint and enable writing operation */
tmp = ((endp >> 1) << 2) | 0x02; /* 選擇邏輯端點并使能寫操作 */
USBCtrl = (INT32U)tmp;
/* write the length that will be written into endpoint */
USBTxPLen = (INT32U)len; /* 寫入要發送的數據的字節長度 */
/* write Data into Transmit Data Register */ /* 寫入要發送的數據到發送數據寄存器 */
j = len >> 2;
for(i = 1; i <= j; i++)
{
tmp = (INT32U)*buf++;
tmp += (INT32U)(*buf++ << 8);
tmp += (INT32U)(*buf++ << 16);
tmp += (INT32U)(*buf++ << 24);
USBTxData = tmp;
while((USBTxPLen & 0x3FF) != len - (i << 2));
}
j = len % 4;
if (j != 0)
{
tmp = 0;
for(i = 0; i < j; i++)
{
tmp |= ((INT32U)*buf) << (i << 3);
buf++;
}
USBTxData = tmp;
while((USBTxPLen & 0x3FF) != 0); /* 等待 USBTxPLen 為 0 */
}
if (len != 0)
{
while((USBDevIntSt & TxENDPKT) ==0); /* 等待寫入完成 */
USBDevIntClr = TxENDPKT;
}
/* Validate buffer */
USB_SelectEndpoint(endp); /* 使能發送緩沖區 */
USB_ValidBuffer();
USBCtrl = 0;
return len; /* 返回實際寫入長度 */
}
/************************************************************
** Name : disconnect_USB
** Function: disconnect USB bus
************************************************************/
void disconnect_USB(void)
{
USB_SetDevStatus(0x00);
}
/************************************************************
** Name : connect_USB
** Function: connect USB bus
************************************************************/
void connect_USB(void)
{
USB_SetDevStatus(0x01);
}
/************************************************************
** Name : reconnect_USB
** Function: reconnect USB bus
************************************************************/
void reconnect_USB(void)
{
INT32U clk_cnt;
disconnect_USB(); /* disconnect USB bus */
for (clk_cnt = 0;clk_cnt<= 0x1FFFF;clk_cnt++); /* delay */
connect_USB(); /* connect USB bus */
}
/*******************************************************************************************************
** End Of File
********************************************************************************************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -