打印

主机restart后,USB通信有问题

[复制链接]
1849|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
maoamao|  楼主 | 2013-5-13 13:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
菜鸟求助:使用CY8C24794的USB模块实现按键扫描功能,调试时发现当主机重启后,板卡与主机就不能通信了,将板卡重新启动后,又能恢复正常,下面是程序代码,有劳大家帮忙看看问题在哪,谢谢了~

#include <m8c.h>        
#include "PSoCAPI.h"   

BYTE report[8];
BYTE led;

void main()
{
        M8C_EnableGInt;
        PRT0DR=0xFF;
        PRT2DR=0xFF;
        USB_Start(0,USB_5V_OPERATION);
        while(!USB_bGetConfiguration());
       
        PRT0DR &= 0xFE;
        USB_LoadInEP(1,report,8,USB_NO_TOGGLE);
        USB_EnableOutEP(2);
        report[2]=0x00;
        while(1)
        {
                while(!USB_bGetEPAckState(1)) PRT0DR |= 0x02;
                PRT0DR &= 0xFD;
                if ((PRT2DR & 0x01) == 0x00)
                {
                        report[2]=0x4F;
                        PRT0DR &= 0xFB;
                }
                else if ((PRT2DR & 0x02) == 0x00)
                {
                        report[2]=0x50;
                        PRT0DR &= 0xFB;
                }
                else if ((PRT2DR & 0x04) == 0x00)
                {
                        report[2]=0x52;
                        PRT0DR &= 0xFB;
                }
                else if ((PRT2DR & 0x08) == 0x00)
                {
                        report[2]=0x51;
                        PRT0DR &= 0xFB;
                }
                else if ((PRT2DR & 0x10) == 0x00)
                {
                        report[0]=0x10;
                        PRT0DR &= 0xFB;
                }
                else if ((PRT2DR & 0x20) == 0x00)
                {
                        report[0]=0x40;
                        PRT0DR &= 0xFB;
                }
                else if ((PRT2DR & 0x80) == 0x00)
                {
                        report[2]=0x45;
                        PRT0DR &= 0xFB;
                }
                else
                {
                        report[0]=0x00;
                        report[2]=0x00;
                        PRT0DR |= 0x04;
                }
               
                USB_LoadInEP(1,report,8,USB_TOGGLE);
               
                if (USB_bGetEPAckState(2))
                {       
                        USB_bReadOutEP(2,&led,1);
                        if ((led & 0x01)== 0x01 ) PRT0DR &= 0xF7;
                        else PRT0DR |= 0x08;
                        USB_EnableOutEP(2);
                }

        }
}
沙发
小工程师| | 2013-5-13 18:28 | 只看该作者
我觉得楼主,需要考虑,重启主机,主机是否有检测USB设备的功能或者你的USB设备是否能检测主机重启的功能。
因为主机重启对于USB来说,需要一个重新枚举的过程。

使用特权

评论回复
评分
参与人数 1威望 +1 收起 理由
maoamao + 1 赞一个!
板凳
maoamao|  楼主 | 2013-5-14 09:28 | 只看该作者
"重启主机,主机是否有检测USB设备的功能"这个功能应该是有的,连接其他设备重启后都可以识别,换成自己做的板卡就不识别了。
“主机重启对于USB来说,需要一个重新枚举的过程”或许问题出在里,主机重启的过程中,外接的设备没有断电,所以程序一直执行while循环,没有再次枚举对吗?弱弱的问一句,该怎么解决这个问题呢?

使用特权

评论回复
地板
maoamao|  楼主 | 2013-5-15 15:29 | 只看该作者
问题解决了

USB device not working after restarting the Windows 7 PC. Why?

Till Windows Vista RTm, all the USB devices attached to the PC undergoes a USB reset, when the OS resumes or restarts. This makes sure that the device enumerates once the PC is restarted or resumed. However, from Windows 7 onwards, this bus wide USB reset is disabled. This enables the devices connected to the PC to maintain device-specific states even after restarting the PC. This significantly decreases the time it takes for the USB devices to be available after system resume.

However, this feature of the OS may lead to malfunctioning of the device, if the device firmware is written in such a way that enumeration of USB is done outside the main while(1) loop. In this case, the device is never reset when the PC restarts and hence, the enumeration API is never executed again.

In the attached project, enumeration of the device is done in a conditional IF statement within the main while(1) loop by checking the 'bDeviceEnumerated' which is initialised to FALSE. Once the enumeration is complete, this flag is set to TRUE . Hence, control doesn't reach the enumeration API once device is enumerated.

The firmware is written to detect the PC restart condition. This is done by monitoring the SOF packets. On reception of every SOF packets, a counter 'bCtr' is cleared in the SOF ISR. Within the SLEEP Timer ISR, the same counter is incremented. If the SOF packets are not received for some time, the bCtr counter value will get incremented above definable threshold value at which the 'bDeviceEnumerated' flag is set to FALSE value.

Once the flag is set to FALSE, the firmware control reaches the enumeration API and  enumerates the device on PC. With this firmware implementation, device works even after restarting the PC

This firmware is compliant with USB chapter 9 tests

SOF_det_reset.zip

569.77 KB

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

4

帖子

0

粉丝