📄 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 + -