


版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、Usb初始化intusb_init(void)2intusb_lowlevel_init(void)3voidusb_scan_devices(void)6intusb_new_device(structusb_device*dev)7STEP1主機自動設別USB®備的插入5rio+FuH-speedUSBTransceiver1<IRpuD+Low-speedUSBTransceiverSlowSlewRateBuffersiD-Low-spee<lFunctionD-HubUpstreamPortorFull-speedFunctionUSE®過D-,D+信
2、號的狀態判斷設備的插入,如上圖所示,D+S上拉電阻為高速設備,D-接下拉為低速設備。很多嵌入示系統利用這一點,來控制USES備,使其復位(D+,D_,均為0大丁2.5us)。STEP2識別到USBt,主機初始化設備所有的US卵有endpoint0.如此類似還有別的endpoint如1,2,3,4等等,endpoint0,是初始化的默認通道,一旦設備初始化完成后無任何意義。初始化主要完成以下事情。Reset設備讀出設備描敘符讀出設備配置符讀出接口信息讀出Endpoint信息讀出STRING言息配置設備描敘符STEP3主機初始化OK即可正常通訊對于數據量大的數據可以采用DMA!信,小數據量采用中斷
3、通信,這兩個比較簡單,把握好基本流程即可以實現。/*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,scanthebusfordevic
4、esi.e.searchHUBsandconfigurethem*/if(result=0)running=1;usb_scan_devices();return0;elses_UartPrint("Error,couldn'tinitLowlevelpartn");return-1;intusb_lowlevel_init(void)unsignedlongupllvalue;S3C24X0_CLOCK_POWER*constclk_powerS3C24X0_GetBase_CLOCK_POWER();S3C24X0_GPIO*constgpio=S3C24X0_
5、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-&g
6、t;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=DEVICEgpio->MISCCR&=(MISCCR_USB0_SUSPEND|MISCCR_USB1_SUSPEND);/1=usepadsrelatedUSBforUSBhos
7、t/clk_power->CLKSLOW&=(UCLK_ON|MPLL_OFF|SLOW_BIT);/clk_power->CLKSLOW&=UCLK_ON;/EnableUSBhostclock.clk_power->CLKCON|=CLKCON_USBH;/changebywqhmemset(&gohci,0,sizeof(ohci_t);memset(&urb_priv,0,sizeof(urb_priv_t);/*alignthestorage*/s_UartPrint("Hccaaddr=%04x”,(U32)&gh
8、cca0);if(U32)&ghcca0&0xff)err("HCCAnotaligned!");return-1;phcca=&ghcca0;info("alignedghcca%p",phcca);memset(&ohci_dev,0,sizeof(structohci_device);if(U32)&ohci_dev.ed0&0x7)err("EDsnotaligned!");return-1;memset(gtd,0,sizeof(td_t)*(NUM_TD+1);if(U32)
9、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_re
10、lease_ohci(&gohci);/*Initializationfailed*/clk_power->CLKCON&=CLKCON_USBH;err("init_step2n");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&qu
11、ot;,gohci.slot_name);hc_release_ohci(&gohci);/*Initializationfailed*/clk_power->CLKCON&=CLKCON_USBH;return-1;#ifdefDEBUGohci_dump(&gohci,1);#elsewait_ms(1);#endifohci_inited=1;urb_finished=1;return0;/*builddeviceTree*/voidusb_scan_devices(void)inti;structusb_device*dev;/*firstmakealld
12、evicesunknown*/for(i=0;i<USB_MAX_DEVICE;i+)memset(&usb_devi,0,sizeof(structusb_device);usb_devi.devnum=-1;dev_index=0;/*device0isalwayspresent(roothub,soletitanalyze)*/s_UartPrint("scan_step1n");dev=usb_alloc_new_device();s_UartPrint("scan_step2n");s_UartPrint("dev=%d
13、”,dev);usb_new_device(dev);/s_UartPrint("scan_step3n");/s_UartPrint("%dUSBDevice(s)foundn",dev_index);/*insert"driver"ifpossible*/* Bythetimewegethere,thedevicehasgottenanewdeviceID* andisinthedefaultstate.Weneedtoidentifythethingand* gettheballrolling.* Returns0forsucc
14、ess,!=0forerror.*/intusb_new_device(structusb_device*dev)intaddr,err;inttmp;unsignedchartmpbufUSB_BUFSIZ;dev->descriptor.bMaxPacketSize0=8;/*Startoffat8bytes*/dev->maxpacketsize=0;/*Defaultto8bytemaxpacketsize*/dev->epmaxpacketin0=8;dev->epmaxpacketout0=8;/*Westillhaven'tsettheAddres
15、syet*/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
16、_device*parent=dev->parent;unsignedshortportstatus;* /*send64-byteGET-DEVICE-DESCRIPTORrequest.Sincethedescriptorisonly18byteslong,thiswillterminatewithashortpacket.Butifthemaxpacketsizeis8or16thedevicemaybewaitingtotransmitsomemore.*/desc=(structusb_device_descriptor*)tmpbuf;desc->bMaxPacketS
17、ize0=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:64bytedescrn");break;dev->descriptor.bMaxPacketSize0=desc->bMaxPacketSize0;/*findtheportnumberwe'reat*/if(parent)for(j=0;j<parent->maxchild;j+)if(parent->c
18、hildrenj=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%in",port);return1;#else/*andthisisth
19、eoldandknownwayofinitializingdevices*/s_UartPrint("NEW_step1n");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=%dn",err);#endifde
20、v->epmaxpacketin0=dev->descriptor.bMaxPacketSize0;dev->epmaxpacketout0=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;br
21、eak;dev->devnum=addr;/s_UartPrint("NEW_step4n");err=usb_set_address(dev);/*setaddress*/s_UartPrint("NEW_step5n");if(err<0)s_UartPrint("nUSBdevicenotacceptingnewaddress(error=%lX)n”,dev->status);s_UartPrint("NEW_step6n");return1;wait_ms(10);/*LettheSET_ADDR
22、ESSsettle*/tmp=sizeof(dev->descriptor);/tmp=8;/s_UartPrint("NEW_step7n");err=usb_get_descriptor(dev,USB_DT_DEVICE,0,&dev->descriptor,sizeof(dev->descriptor);if(err<tmp)(if(err<0)s_UartPrint("unabletogetdevicedescriptor(error=%d)n",err);elses_UartPrint("USB
23、devicedescriptorshortread(expected%i,got%i)n",tmp,err);s_UartPrint("NEW_step8n");return1;/dev->epmaxpacketin0=dev->descriptor.bMaxPacketSize0;/dev->epmaxpacketout0=dev->descriptor.bMaxPacketSize0;/*correctlevalues*/dev->descriptor.bcdUSB=swap_16(dev->descriptor.bcdUS
24、B);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=%dn",sizeof(dev->descriptor);USB_PRINTF("bLength%xn”
25、,dev->descriptor.bLength);USB_PRINTF("bDescriptorTypeUSB_PRINTF("bcdUSBUSB_PRINTF("bDeviceClassUSB_PRINTF("bDeviceSubClassUSB_PRINTF("bDeviceProtocolUSB_PRINTF("bMaxPacketSize0USB_PRINTF("idVendorUSB_PRINTF("idProductUSB_PRINTF("bcdDevice%xn",dev-
26、>descriptor.bDescriptorType);%xn",dev->descriptor.bcdUSB);%xn",dev->descriptor.bDeviceClass);%xn",dev->descriptor.bDeviceSubClass);%xn",dev->descriptor.bDeviceProtocol);%xn”,dev->descriptor.bMaxPacketSize0);%xn",dev->descriptor.idVendor);%xn",dev-&g
27、t;descriptor.idProduct);%xn",dev->descriptor.bcdDevice);USB_PRINTF("iManufacturer%xn”,dev->descriptor.iManufacturer);USB_PRINTF("iProduct%xn",dev->descriptor.iProduct);USB_PRINTF("iSerialNumber%xn”,dev->descriptor.iSerialNumber);USB_PRINTF("bNumConfiguration
28、s%xn",dev->descriptor.bNumConfigurations);/*onlysupportforoneconfigfornow*/usb_get_configuration_no(dev,&tmpbuf0,0);/s_UartPrint("NEW_step10n");usb_parse_config(dev,&tmpbuf0,0);/s_UartPrint("NEW_step11n");usb_set_maxpacket(dev);/s_UartPrint("NEW_step12n");/*wesetthedefaultconfigurationhere*/if(usb_set_configuration(dev,dev->config.bConfigurationValue)s_UartPrint("
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- GB/Z 45388.1-2025工業過程測量、控制和自動化第1部分:工業設施和智能電網之間的系統接口
- 天然氣輸配過程中能耗降低技術考核試卷
- 橡膠制品的供應鏈管理與協同創新考核試卷
- 綠色農業與食品安全考核試卷
- 寶石的結晶學與晶體生長研究進展評價考核試卷
- 禮儀用品企業環境管理體系考核試卷
- 遼寧省葫蘆島市六校聯考2025屆普通高中畢業班教學質量監測物理試題含解析
- 昆山杜克大學《學校體育學A》2023-2024學年第一學期期末試卷
- 永州市冷水灘區2025屆三年級數學第二學期期末統考模擬試題含解析
- 山東醫學高等專科學校《數學規劃》2023-2024學年第一學期期末試卷
- 山東省高中名校2025屆高三4月校際聯合檢測大聯考生物試題及答案
- 2025年武漢數學四調試題及答案
- 【MOOC】數學建模精講-西南交通大學 中國大學慕課MOOC答案
- 職業病防護設施與個體防護用品的使用和維護
- 2024年全國高中數學聯賽北京賽區預賽一試試題(解析版)
- 綠化養護服務投標方案(技術標)
- 中國紡織文化智慧樹知到期末考試答案2024年
- (正式版)HGT 6313-2024 化工園區智慧化評價導則
- GB/T 3091-2015低壓流體輸送用焊接鋼管
- 實際控制人股東會決議
- 混凝土攪拌機設計論文
評論
0/150
提交評論