?? d13driver.c
字號:
if (err != USB_NO_ERR) return err;
OS_ENTER_CRITICAL();
if (pUsb->Sem == 0){ //該端點沒有被獨占 no task use the endpoint
pUsb->Sem = 1; //獨占該端點 flag that task use the endpoint
pUsb->Cnt = 0; //發送計數值清0 clear counter to zero
pUsb->Prio = OSPrioCur; //保存該任務優先級 save the prior of the task
for (i = 0; i < buffnums; i++){
length = pUsb->Max[pUsb->Prio] - pUsb->Cnt; //計算未發送字節個數 calculate the numbers of bytes that will be transmitted
if (length > 0){ //未發送字節數大于0 the numbers is above zero
if (length >= eppsize) //寫滿緩沖區 write the entire buffer
length = D13_WriteEndpoint(endp,(INT8U)eppsize,sendbuff + pUsb->Cnt);
else //寫length個字節 write length bytes into buffer
length = D13_WriteEndpoint(endp,(INT8U)length,sendbuff + pUsb->Cnt);
pUsb->Cnt = pUsb->Cnt + length; //發送計數器加length counter is counting
}
}
}//end of if (pUsb->Sem == 0)
OS_EXIT_CRITICAL();
OS_ENTER_CRITICAL();
if (pUsb->Max[OSPrioCur] != 0){ //還未發送完成 if reception is not finished
OS_EXIT_CRITICAL();
OSTimeDly(timeout); //定義超時等待時間 define timeout
}
OS_EXIT_CRITICAL();
return (USB_RW_Result(endp,buffnums,pUsb,0,timeout)); //返回發送結果 return the result of transmittion
}
/***********************************************************************************************************************
** 函數名稱: USB_RW_Param() Name: USB_RW_Param()
** 功能描述: 填寫USB接收或發送控制塊有關參數 Function: fill the CTRL_USB structure
** 輸 入: CTRL_USB *pUsb : USB接收或發送控制塊指針 Input: CTRL_USB *pUsb: the CTRL_USB structure
INT32U len : 接收或發送字節數 INT32U len: the numbers(Bytes) that will be received/transmitted
INT8U *buff : 接收或發送緩沖區指針 INT8U *recbuff: the reception/transmittion buffer
** 輸 出: 0 調用成功 > 0 調用失敗(錯誤碼) Output: 0: sucessfully >0: fail (it is error code)
***********************************************************************************************************************/
INT8U USB_RW_Param(CTRL_USB *pUsb,INT32U len,INT8U *pbuff)
{
OS_ENTER_CRITICAL();
if (bEPPflags.bits.configuration == 0){ //USB總線未配置完成 the bus is not confirgurated
OS_EXIT_CRITICAL();
return (USB_ERR_NO_CONFIG); //USB總線未配置錯誤 the error that the bus is not configurated
}
if (pbuff == 0){
OS_EXIT_CRITICAL();
return (USB_ERR_BUFF_INVALID); //緩沖區指針無效錯誤 the pointer of buff is invalid
}
pUsb->pBuff[OSPrioCur] = pbuff; //保存該任務的緩沖區首址 save the first address of buff of the current task
pUsb->Max[OSPrioCur] = len; //保存要收發的字節數 save the numbers(Byte) of reception/transmittion
USB_InsertPrio(pUsb,OSPrioCur); //插入等待任務列表中 insert the waiting task table
OS_EXIT_CRITICAL();
return (USB_NO_ERR);
}
/**************************************************************************************************************************************
** 函數名稱: USB_RW_Result() Name: USB_RW_Result()
** 功能描述: 判斷當前任務的數據是否收發成功 Function: judge whether reception/transmittion is sucessful
** 輸 入: INT8U endp : 端點索引號 Input: INT8U endp : the index of endpoint
INT8U buffnums : 該端點緩沖區個數 INT8U buffnums : numbers of endpoint buffer
CTRL_USB *pUsb : USB接收或發送控制塊指針 CTRL_USB *pUsb : the pointer of CTRL_USB struct
INT8U bread : 當前任務狀態(1:接收 0:發送) INT8U bread : status of current task(1:receive 0:transmit)
INT16U timeout : 超過等待時間 INT16U timeout : timeout of waiting
** 輸 出: 0 收發成功 > 0 收發失敗(錯誤碼) Output: 0: sucessfully >0: fail (it is error code)
***************************************************************************************************************************************/
INT8U USB_RW_Result(INT8U endp,INT8U buffnums,CTRL_USB *pUsb,INT8U bread,INT16U timeout)
{
INT8U i,endpstatus;
OS_ENTER_CRITICAL();
if (pUsb->Max[OSPrioCur] != 0){ //收/發錯誤 transmit/receiving fail
if (pUsb->Prio == OSPrioCur){
for (i = 0; i < buffnums; i++)
{
if (bread == 1){ //當前為接收狀態 receiving currently
endpstatus = D13_GetEndpointStatusWOInteruptClear(endp);
if ((endpstatus && 0x60) != 0) //接收緩沖區不空
D13_ClearBuffer(endp); //清空收發緩沖區
}else
D13_SetEndpointStatus(endp, 0); //解禁端點 unstall IN endpoint
}
pUsb->Sem = 0; //釋放該端點的發送資源 release the transmittion resource of the endpoint
}
USB_DelPrio(pUsb,pUsb->Prio); //將該任務從等待隊列中刪除 delete the task from waiting table
OS_EXIT_CRITICAL();
return (USB_ERR_WR_TIMEOUT); //返回收/發超時錯誤 return transmit/receiving fail
}
else
{
OS_EXIT_CRITICAL();
return (USB_NO_ERR); //收/發成功 sucessfully
}
}
/**************************************************************************************************************
下面的函數操作 USB接收或發送控制塊等待列表 the function below operate the CTRL_USB structure
***************************************************************************************************************/
/************************************************************************************************************************
** 函數名稱: USB_DelPrio() Name: USB_DelPrio()
** 功能描述: 將一任務從USB接收或發送控制塊等待列表中刪除 Function: delete a task from CTRL_USB struct waiting table
** 輸 入: CTRL_USB *pUsb: USB接收或發送控制塊指針 Input: CTRL_USB *pUsb: the pointer of the CTRL_USB struct
INT8U prio: 刪除的任務的優先級 INT8U prio: the prior that will be deleted
** 輸 出: 無 Output: NULL
*************************************************************************************************************************/
void USB_DelPrio(CTRL_USB *pUsb,INT8U prio)
{
if ((pUsb -> Tbl[prio >> 3] &= ~OSMapTbl[prio & 0x07]) == 0)
pUsb -> Grp &= ~OSMapTbl[prio >> 3];
}
/*************************************************************************************************************************
** 函數名稱: USB_InsertPrio() Name: USB_InsertPrio()
** 功能描述: 將一任務插入到USB接收或發送控制塊等待列表中 Function: Insert a task to CTRL_USB struct waiting table
** 輸 入: CTRL_USB *pUsb: USB接收或發送控制塊指針 Input: CTRL_USB *pUsb: the pointer of the CTRL_USB struct
INT8U prio: 插入任務的優先級 INT8U prio: the prior that will be deleted
** 輸 出: 無 Output: NULL
**************************************************************************************************************************/
void USB_InsertPrio(CTRL_USB *pUsb,INT8U prio)
{
pUsb -> Grp |= OSMapTbl[prio >> 3];
pUsb -> Tbl[prio >> 3] |= OSMapTbl[prio & 0x07];
}
/*************************************************************************************************************************
** 函數名稱: USB_GetHighPrio() Name: USB_GetHighPrio()
** 功能描述: 取得USB接收或發送控制塊等待列表 Function: Get the priorest task from CTRL_USB struct waiting table
中優先級最高的任務優先級
** 輸 入: CTRL_USB *pUsb: USB 接收或發送控制塊指針 Input: CTRL_USB *pUsb: the pointer of the CTRL_USB struct
** 輸 出: 等待列表中優先級最高的任務優先級 Output: NULL
**************************************************************************************************************************/
INT8U USB_GetHighPrio(CTRL_USB *pUsb)
{
INT8U x,y;
INT8U prio;
y = OSUnMapTbl[pUsb->Grp];
x = OSUnMapTbl[pUsb->Tbl[y]];
prio = (INT8U)((y << 3) + x);
return prio;
}
/*************************************************************************
一些中斷處理程序 some program of interrupt
**************************************************************************/
/****************************************************************************************************************
** 函數名稱: bus_reset() Name: bus_reset()
** 功能描述: USB總線復位處理子程序 Function: the intterupt service routine of USB bus reset
** 輸 入: 無 Input: NULL
** 輸 出: 無 Output: NULL
*****************************************************************************************************************/
void bus_reset(void)
{
config_endpoint();
//這此添加總線復位處理的其它代碼
}
/*****************************************************************************************************************
** 函數名稱: usb_suspend() Name: usb_suspend()
** 功能描述: ISP1181B掛起中斷處理子程序 Function: the interrupt service routine of ISP1181B suspend
** 輸 入: 無 Input: NULL
** 輸 出: 無 Output: NULL
******************************************************************************************************************/
void usb_suspend(void)
{
INT32U i_st;
i_st = D13_ReadInterruptRegister(); //讀取D13中斷寄存器值 //read the D13 interrupt register
if(i_st & D13REG_INTSRC_BUSTATUS) //如果當前總線狀態BUSTATUS標志為1,才能進入掛起
{ //if the bus status BUSTATUS flag is 1,ISP1181B enter suspend
//在此添加:使系統所有元件做好準備,以進入掛起狀態
//執行下面的語句后,ISP1181B就進入掛起狀態了
D13_SetMode(D13REG_MODE_INT_EN|D13REG_MODE_SOFTCONNECT|D13REG_MODE_SUSPND);
D13_SetMode(D13REG_MODE_INT_EN|D13REG_MODE_SOFTCONNECT); //GOSUP bit 1-> 0, chip into suspend
}
}
/*****************************************************************************************************************
** 函數名稱: get_firmware_version() Name: get_firmware_version()
** 功能描述: 得到本固件軟件包版本號 Function: get software version of the firmware
** 輸 入: 無 Input: NULL
** 輸 出: 本固件軟件包版本號 Output: the software version of the firmware
******************************************************************************************************************/
INT16U get_ISP1181B_firmware_version(void)
{
return 0x0100; //固件軟件版本號為1.00 the software version of the firmware is 1.00
}
/***************************************************************************************
下面的程序由用戶自己編寫 the program below is wirtten by user
****************************************************************************************/
/*******************************************************************************************************************
** 函數名稱: dma_eot() Name: dma_eot()
** 功能描述: DMA結束處理 Function: deal with the end of DMA
** 輸 入: 無 Input: NULL
** 輸 出: 無 Output: NULL
*******************************************************************************************************************/
void dma_eot(void)
{
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -