主机自动设别USB设备的插入要点.docx
- 文档编号:28219888
- 上传时间:2023-07-09
- 格式:DOCX
- 页数:14
- 大小:35.39KB
主机自动设别USB设备的插入要点.docx
《主机自动设别USB设备的插入要点.docx》由会员分享,可在线阅读,更多相关《主机自动设别USB设备的插入要点.docx(14页珍藏版)》请在冰豆网上搜索。
主机自动设别USB设备的插入要点
Usb初始化
STEP1 主机自动设别USB设备的插入
USB通过D-,D+信号的状态判断设备的插入,如上图所示,D+接上拉电阻为高速设备,D-接下拉为低速设备。
很多嵌入示系统利用这一点,来控制USB设备,使其复位(D+,D_,均为0大于2.5us)。
STEP2 识别到USB后,主机初始化设备
所有的USB都有endpoint0.如此类似还有别的endpoint如1,2,3,4等等,endpoint0,是初始化的默认通道,一旦设备初始化完成后无任何意义。
初始化主要完成以下事情。
1.Reset设备
2.读出设备描叙符
3.读出设备配置符
4.读出接口信息
5.读出Endpoint信息
6.读出STRING信息
7.配置设备描叙符
STEP3 主机初始化OK,即可正常通讯
对于数据量大的数据可以采用DMA通信,小数据量采用中断通信,这两个比较简单,把握好基本流程即可以实现。
/***************************************************************************
*InitUSBDevice
*/
intusb_init(void)
{
intresult;
//s_SetIRQHandler(INT_USBH,hc_interrupt_irq);
running=0;
dev_index=0;
asynch_allowed=1;
usb_hub_reset();//usb_hub_index=0;
/*initlow_levelUSB*/
//s_UartPrint("USB:
");
result=usb_lowlevel_init();
/*iflowlevelinitisOK,scanthebusfordevicesi.e.searchHUBsandconfigurethem*/
if(result==0)
{
running=1;
usb_scan_devices();
return0;
}
else
{
s_UartPrint("Error,couldn'tinitLowlevelpart\n");
return-1;
}
}
intusb_lowlevel_init(void)
{
unsignedlongupllvalue;
S3C24X0_CLOCK_POWER*constclk_power=S3C24X0_GetBase_CLOCK_POWER();
S3C24X0_GPIO*constgpio=S3C24X0_GetBase_GPIO();
/*
*Setthe48MHzUPLLclocking.Valuesaretakenfrom
*"PLLvalueselectionguide",6-23,s3c2410_UM.pdf.
*/
clk_power->CLKSLOW|=UCLK_ON;
clk_power->UPLLCON=0x78023;//((0x78<<12)+(2<<4)+3);
upllvalue=0x78023;//(0x78<<12)|(0x02<<4)|(0x03);
while(upllvalue!
=clk_power->UPLLCON)
{
clk_power->UPLLCON=0x78023;//((0x78<<12)+(2<<4)+3);
wait_ms
(1);
}
//gpio->MISCCR|=MISCCR_USBPAD;//1=usepadsrelatedUSBforUSBhost//
gpio->MISCCR&=~MISCCR_USBPAD;//DP0DN0=HOSTDP1DN1=DEVICE
gpio->MISCCR&=~(MISCCR_USB0_SUSPEND|MISCCR_USB1_SUSPEND);//1=usepadsrelatedUSBforUSBhost//
clk_power->CLKSLOW&=~(UCLK_ON|MPLL_OFF|SLOW_BIT);
//clk_power->CLKSLOW&=~UCLK_ON;
//EnableUSBhostclock.
clk_power->CLKCON|=CLKCON_USBH;//changebywqh
memset(&gohci,0,sizeof(ohci_t));
memset(&urb_priv,0,sizeof(urb_priv_t));
/*alignthestorage*/
//s_UartPrint("Hccaaddr=%04x",(U32)&ghcca[0]);
if((U32)&ghcca[0]&0xff)
{
err("HCCAnotaligned!
!
");
return-1;
}
phcca=&ghcca[0];
info("alignedghcca%p",phcca);
memset(&ohci_dev,0,sizeof(structohci_device));
if((U32)&ohci_dev.ed[0]&0x7)
{
err("EDsnotaligned!
!
");
return-1;
}
memset(gtd,0,sizeof(td_t)*(NUM_TD+1));
if((U32)gtd&0x7)
{
err("TDsnotaligned!
!
");
return-1;
}
ptd=gtd;
gohci.hcca=phcca;
memset(phcca,0,sizeof(structohci_hcca));
gohci.disabled=1;
gohci.sleeping=0;
gohci.irq=-1;
gohci.regs=(structohci_regs*)S3C24X0_USB_HOST_BASE;
gohci.flags=0;
gohci.slot_name="s3c2410";
if(hc_reset(&gohci)<0)
{
hc_release_ohci(&gohci);
/*Initializationfailed*/
clk_power->CLKCON&=~CLKCON_USBH;
err("init_step2\n");
return-1;
}
/*FIXMEthisisasecondHCreset;why?
?
*/
writel(gohci.hc_control=OHCI_USB_RESET,&gohci.regs->control);
wait_ms(10);
if(hc_start(&gohci)<0)
{
err("can'tstartusb-%s",gohci.slot_name);
hc_release_ohci(&gohci);
/*Initializationfailed*/
clk_power->CLKCON&=~CLKCON_USBH;
return-1;
}
#ifdefDEBUG
ohci_dump(&gohci,1);
#else
wait_ms
(1);
#endif
ohci_inited=1;
urb_finished=1;
return0;
}
/*builddeviceTree*/
voidusb_scan_devices(void)
{
inti;
structusb_device*dev;
/*firstmakealldevicesunknown*/
for(i=0;i { memset(&usb_dev[i],0,sizeof(structusb_device)); usb_dev[i].devnum=-1; } dev_index=0; /*device0isalwayspresent(roothub,soletitanalyze)*/ //s_UartPrint("scan_step1\n"); dev=usb_alloc_new_device(); s_UartPrint("scan_step2\n"); s_UartPrint("dev=%d",dev); usb_new_device(dev); //s_UartPrint("scan_step3\n"); //s_UartPrint("%dUSBDevice(s)found\n",dev_index); /*insert"driver"ifpossible*/ } /* *Bythetimewegethere,thedevicehasgottenanewdeviceID *andisinthedefaultstate.Weneedtoidentifythethingand *gettheballrolling.. * *Returns0forsuccess,! =0forerror. */ intusb_new_device(structusb_device*dev) { intaddr,err; inttmp; unsignedchartmpbuf[USB_BUFSIZ]; dev->descriptor.bMaxPacketSize0=8;/*Startoffat8bytes*/ dev->maxpacketsize=0;/*Defaultto8bytemaxpacketsize*/ dev->epmaxpacketin[0]=8; dev->epmaxpacketout[0]=8; /*Westillhaven'tsettheAddressyet*/ addr=dev->devnum; dev->devnum=0; #undefNEW_INIT_SEQ #ifdefNEW_INIT_SEQ /*thisisaWindowsschemeofinitializationsequence,withdouble *resetofthedevice.Someequipmentissaidtoworkonlywithsuch *initsequence;thispatchisbasedontheworkbyAlanStern: * */ intj; structusb_device_descriptor*desc; intport=-1; structusb_device*parent=dev->parent; unsignedshortportstatus; /*send64-byteGET-DEVICE-DESCRIPTORrequest.Sincethedescriptoris *only18byteslong,thiswillterminatewithashortpacket.Butif *themaxpacketsizeis8or16thedevicemaybewaitingtotransmit *somemore.*/ desc=(structusb_device_descriptor*)tmpbuf; desc->bMaxPacketSize0=0; for(j=0;j<3;++j) { err=usb_get_descriptor(dev,USB_DT_DEVICE,0,desc,64); if(err<0) { USB_PRINTF("usb_new_device: 64bytedescr\n"); break; } } dev->descriptor.bMaxPacketSize0=desc->bMaxPacketSize0; /*findtheportnumberwe'reat*/ if(parent) { for(j=0;j { if(parent->children[j]==dev) { port=j; break; } } if(port<0) { s_UartPrint("usb_new_device: cannotlocatedevice'sport..\n"); return1; } /*resettheportforthesecondtime*/ err=hub_port_reset(dev->parent,port,&portstatus); if(err<0) { s_UartPrint("\nCouldn'tresetport%i\n",port); return1; } } #else /*andthisistheoldandknownwayofinitializingdevices*/ //s_UartPrint("NEW_step1\n"); err=usb_get_descriptor(dev,USB_DT_DEVICE,0,&dev->descriptor,8); if(err<8) { s_UartPrint("\nUSBdevicenotresponding,givingup(status=%lX)\n",dev->status); return1; } //s_UartPrint("NEW_step3err=%d\n",err); #endif dev->epmaxpacketin[0]=dev->descriptor.bMaxPacketSize0; dev->epmaxpacketout[0]=dev->descriptor.bMaxPacketSize0; switch(dev->descriptor.bMaxPacketSize0) { case8: dev->maxpacketsize=0;break; case16: dev->maxpacketsize=1;break; case32: dev->maxpacketsize=2;break; case64: dev->maxpacketsize=3;break; } dev->devnum=addr; //s_UartPrint("NEW_step4\n"); err=usb_set_address(dev);/*setaddress*/ //s_UartPrint("NEW_step5\n"); if(err<0) { s_UartPrint("\nUSBdevicenotacceptingnewaddress(error=%lX)\n",dev->status); s_UartPrint("NEW_step6\n"); return1; } wait_ms(10);/*LettheSET_ADDRESSsettle*/ tmp=sizeof(dev->descriptor); //tmp=8; //s_UartPrint("NEW_step7\n"); err=usb_get_descriptor(dev,USB_DT_DEVICE,0,&dev->descriptor,sizeof(dev->descriptor)); if(err { if(err<0) s_UartPrint("unabletogetdevicedescriptor(error=%d)\n",err); else s_UartPrint("USBdevicedescriptorshortread(expected%i,got%i)\n",tmp,err); s_UartPrint("NEW_step8\n"); return1; } //dev->epmaxpacketin[0]=dev->descriptor.bMaxPacketSize0; //dev->epmaxpacketout[0]=dev->descriptor.bMaxPacketSize0; /*correctlevalues*/ dev->descriptor.bcdUSB=swap_16(dev->descriptor.bcdUSB); dev->descriptor.idVendor=swap_16(dev->descriptor.idVendor); dev->descriptor.idProduct=swap_16(dev->descriptor.idProduct); dev->descriptor.bcdDevice=swap_16(dev->descriptor.bcdDevice); //s_UartPrint("NEW_step9size=%d\n",sizeof(dev->descriptor)); USB_PRINTF("bLength%x\n",dev->descriptor.bLength); USB_PRINTF("bDescriptorType%x\n",dev->descriptor.bDescriptorType); USB_PRINTF("bcdUSB%x\n",dev->descriptor.bcdUSB); USB_PRINTF("bDeviceClass%x\n",dev->descriptor.bDeviceClass); USB_PRINTF("bDeviceSubClass%x\n",dev->descriptor.bDeviceSubClass); USB_PRINTF("bDeviceProtocol%x\n",dev->descriptor.bDeviceProtocol); USB_PRINTF("bMaxPacketSize0%x\n",dev->descriptor.bMaxPacketSize0); USB_PRINTF("idVendor%x\n",dev->descriptor.idVendor); USB_PRINTF("idProduct%x\n",dev->descriptor.idProduct); USB_PRINTF("bcdDevice%x\n",dev->descriptor.bcdDevice); USB_PRINTF("iManufacturer%x\n",dev->descriptor.iManufacturer); USB_PRINTF("iProduct%x\n",dev->descriptor.iProduct); USB_PRINTF("iSerialNumber%x\n",dev->descriptor.iSerialNumber); USB_PRINTF("bNumConfigurations%x\n",dev->descriptor.bNumConfigurations); /*onlysupportforoneconfigfornow*/ usb_get_configuration_no(dev,&tmpbuf[0],0); //s_UartPrint("NEW_step10\n"); usb_parse_config(dev,&tmpbuf[0],0); //s_UartPrint("NEW_step11\n"); usb_set_maxpacket(dev); //s_UartPrint("NEW_step12\n"); /*wesetthedefaultconfigurationhere*/ if(usb_set_configuration(dev,dev->config.bConfigurationValue)) { s_UartPrint("failedtosetdefaultconfigurationlen%d,status%lX\n",dev->act_len,dev->status); return-1; } USB_PRINTF("newdevicestrings: Mfr=%d,Product=%d,SerialNumber=%d\n", dev->descriptor.iManufacturer,dev->descriptor.iProduct,dev->descriptor.iSerialNumber); memset(dev->mf,0,sizeof(dev->mf)); memset(dev->prod,0,sizeof(dev->prod)); memset(dev->serial,0,sizeof(dev->serial)); if(dev->descriptor.iManufacturer) usb_string(dev,dev->descriptor.iManufacturer,dev->mf,sizeof(dev->mf)); if(dev->descriptor.iProduct) usb_string(dev,dev->desc
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 主机 自动 USB 设备 插入 要点
![提示](https://static.bdocx.com/images/bang_tan.gif)