初涉USB,初学者USB入门总结(2) 设备固件程序
时间:12-14
来源:互联网
点击:
为了更好的说明整个USB启动过程,我们可以用串口实时的跟踪各个USB中断。不过这里先不用串口进行测试,只是简单的用一组变量记录过程。测试程序如下(以下会有程序的说明):
uchar test[100];//100长度的变量,记录过程
uchar conters=0;//记录计数值,
/*------------------------------------------------------------
高校电子联盟--肖继达
QQ:258347765
-------------------------------------------------------------*/
void EXT_int(void)//USB中断响应函数
{
/*------------------------------------------------------------
Check interrupt status register to know interrupt
source.
------------------------------------------------------------*/
if(USB_BUSRESET_ASS_INT())
{/* USB bus reset */
/*for USB Rev.1.1
After USB bus reset released, 10msec recoverly time we have.
Follwing request must be processed normally.
*/
CLR_BUS_RESET_STATE();/* USB bus reset status clear */
/*------------------------------------------------------------
Endpoint0 setting
------------------------------------------------------------*/
/* Tx/Rx payload size setting */
/* Rx payload is fixed as 8-byte or 32-byte, therefor the
setting has no meaninig */
SET_PAYLOAD_EPn(EP0RX, device_deor.bMaxPacketSize0);
SET_PAYLOAD_EPn(EP0TX, device_deor.bMaxPacketSize0);
/* Stall bit, the value undefined after reset, cleared */
CLR_STALL_EPn(EP0);
/*------------------------------------------------------------
Misceronous status variable initialization
------------------------------------------------------------*/
usb_status.configuration = NULL;
usb_status.remote_wakeup = 0;
usb_status.address = 0;
usb_status.dvcstate = DEFAULT_STATE;/* Device state :DEFAULT */
usb_status.stall_req = 0;
#ifdef Debug
test[conters]=!;
conters++;
#endif
/*------------------------------------------------------------
Callback to application layer
------------------------------------------------------------*/
(*usb_status.callback)();
}
else if (SUSPENDED_INT())
{/* suspended state */
/* for USB Rev.1.1
Transit to suspended state after detect the USB line has kept idle over 3msec.
After resume detected, end suspend state in 3msec to be able to respond
the host request.
*/
CLR_SUSPENDED_STATE();
#ifdef Debug
test[conters]=@;
conters++;
#endif
}
else if(AWAKE_INT())
{/* Deveice awake state */
/* AWAKE procedure */
CLR_AWAKE_STATE();/* Request clear */
#ifdef Debug
test[conters]=#;
conters++;
#endif
}
else if(USB_BUSRESET_DES_INT())
{/* USB bus reset deassert */
/* Procedure for USB bus reset de-assert */
CLR_BUS_RESET_DES_STATE();/* Request clear */
#ifdef Debug
test[conters]=$;
conters++;
#endif
}
else if(SOF_INT())
{/* SOF interrupt status */
CLR_B_SOF_STATE();
#ifdef Debug
test[conters]=%;
conters++;
#endif
/* SOF interrupt status clear */
}/* SOF interrupt status */
if(SETUP_RDY_INT())
{/* setup ready */
#ifdef Debug
test[conters]=^;
conters++;
#endif
read_Device_Requests();
}
else if(EP1_PKTRDY_INT())
{/* EP1 packet ready */
read_FIFO(EP1);
}
else if(EP2_PKTRDY_INT())
{/* EP2 packet ready */
write_FIFO(EP2);
}
else if(EP0_RXPKTRDY_INT())
{/* EP0 receive packet ready */
read_FIFO(EP0RX);
}
else if(EP0_TXPKTRDY_INT())
{/* EP0 transmit packet ready */
write_FIFO(EP0TX);
}
}
计录的结果在变量查看中显示如下:
首先我解释一下,这段程序是我在做USB设备时的中断函数。主控(就是你往里面写固件程序的那个东西)会在要求设备进行操作时,产生一个相应的中断(我们可以用中断的方式,也可以用查询的方式,中断的方式的好处就是主机有需要操作的都会叫你,而用查询你必须不断的问主机“有事么”,这里采用中断方式),比如主机给设备设置地址,主机会通过固定的通道(point0)发送一个‘设定地址’包,设备主控接到包后会产生中断,并且把响应的状态保存在相应的寄存器中,我们只要在中断程序判断各个寄存器就能完成主机的任务。
程序中蓝色字是中断类型的判断,其对应的宏定义就不列出来了。如果是这个中断就会执行相应的中断操作。并且一次中断只有一种中断类型,我们在每个中断响应中加一段红色字的程序,是为了保存每次中断的状态,比如刚插上设备,来了一次BUSRESET
uchar test[100];//100长度的变量,记录过程
uchar conters=0;//记录计数值,
/*------------------------------------------------------------
高校电子联盟--肖继达
QQ:258347765
-------------------------------------------------------------*/
void EXT_int(void)//USB中断响应函数
{
/*------------------------------------------------------------
Check interrupt status register to know interrupt
source.
------------------------------------------------------------*/
if(USB_BUSRESET_ASS_INT())
{/* USB bus reset */
/*for USB Rev.1.1
After USB bus reset released, 10msec recoverly time we have.
Follwing request must be processed normally.
*/
CLR_BUS_RESET_STATE();/* USB bus reset status clear */
/*------------------------------------------------------------
Endpoint0 setting
------------------------------------------------------------*/
/* Tx/Rx payload size setting */
/* Rx payload is fixed as 8-byte or 32-byte, therefor the
setting has no meaninig */
SET_PAYLOAD_EPn(EP0RX, device_deor.bMaxPacketSize0);
SET_PAYLOAD_EPn(EP0TX, device_deor.bMaxPacketSize0);
/* Stall bit, the value undefined after reset, cleared */
CLR_STALL_EPn(EP0);
/*------------------------------------------------------------
Misceronous status variable initialization
------------------------------------------------------------*/
usb_status.configuration = NULL;
usb_status.remote_wakeup = 0;
usb_status.address = 0;
usb_status.dvcstate = DEFAULT_STATE;/* Device state :DEFAULT */
usb_status.stall_req = 0;
#ifdef Debug
test[conters]=!;
conters++;
#endif
/*------------------------------------------------------------
Callback to application layer
------------------------------------------------------------*/
(*usb_status.callback)();
}
else if (SUSPENDED_INT())
{/* suspended state */
/* for USB Rev.1.1
Transit to suspended state after detect the USB line has kept idle over 3msec.
After resume detected, end suspend state in 3msec to be able to respond
the host request.
*/
CLR_SUSPENDED_STATE();
#ifdef Debug
test[conters]=@;
conters++;
#endif
}
else if(AWAKE_INT())
{/* Deveice awake state */
/* AWAKE procedure */
CLR_AWAKE_STATE();/* Request clear */
#ifdef Debug
test[conters]=#;
conters++;
#endif
}
else if(USB_BUSRESET_DES_INT())
{/* USB bus reset deassert */
/* Procedure for USB bus reset de-assert */
CLR_BUS_RESET_DES_STATE();/* Request clear */
#ifdef Debug
test[conters]=$;
conters++;
#endif
}
else if(SOF_INT())
{/* SOF interrupt status */
CLR_B_SOF_STATE();
#ifdef Debug
test[conters]=%;
conters++;
#endif
/* SOF interrupt status clear */
}/* SOF interrupt status */
if(SETUP_RDY_INT())
{/* setup ready */
#ifdef Debug
test[conters]=^;
conters++;
#endif
read_Device_Requests();
}
else if(EP1_PKTRDY_INT())
{/* EP1 packet ready */
read_FIFO(EP1);
}
else if(EP2_PKTRDY_INT())
{/* EP2 packet ready */
write_FIFO(EP2);
}
else if(EP0_RXPKTRDY_INT())
{/* EP0 receive packet ready */
read_FIFO(EP0RX);
}
else if(EP0_TXPKTRDY_INT())
{/* EP0 transmit packet ready */
write_FIFO(EP0TX);
}
}
计录的结果在变量查看中显示如下:
首先我解释一下,这段程序是我在做USB设备时的中断函数。主控(就是你往里面写固件程序的那个东西)会在要求设备进行操作时,产生一个相应的中断(我们可以用中断的方式,也可以用查询的方式,中断的方式的好处就是主机有需要操作的都会叫你,而用查询你必须不断的问主机“有事么”,这里采用中断方式),比如主机给设备设置地址,主机会通过固定的通道(point0)发送一个‘设定地址’包,设备主控接到包后会产生中断,并且把响应的状态保存在相应的寄存器中,我们只要在中断程序判断各个寄存器就能完成主机的任务。
程序中蓝色字是中断类型的判断,其对应的宏定义就不列出来了。如果是这个中断就会执行相应的中断操作。并且一次中断只有一种中断类型,我们在每个中断响应中加一段红色字的程序,是为了保存每次中断的状态,比如刚插上设备,来了一次BUSRESET
USB入门总结设备固件程 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)