📄 lib_host20.c
字号:
/////////////////////////////////////////////////////////////////////////////////// File name: Lib_Host20.C// Version: 1.0// Date: 2005/01/31//// Author: Bruce// Email: // Phone: (03) 578-7888// Company: Faraday Tech. Corp.//// **Description: // This program is for USB Host lib function call.///////////////////////////////////////////////////////////////////////////////#include <stdlib.h>#include <stdarg.h>#include <stdio.h>#include <string.h>#include "chipset.h" #include "flib.h" #include "Lib_Host20.h"#define timer_no 2#define timer_ISR IRQ_TIMER2UINT8 OTGH_GETDESCRIPTOR_DEVICE_8[] = {0x80,0x06,0x00,0x01,0x00,0x00,0x08,0x00};UINT8 OTGH_GETDESCRIPTOR_DEVICE[] = {0x80,0x06,0x00,0x01,0x00,0x00,0x12,0x00}; UINT8 OTGH_GETDESCRIPTOR_CONFIG[] = {0x80,0x06,0x00,0x02,0x00,0x00,0x08,0x00}; UINT8 OTGH_SETADDRESS[] = {0x00,0x05,03,0x00,0x00,0x00,0x00,0x00}; UINT8 OTGH_SETCONFIGURATION[] = {0x00,0x09,0x01,0x00,0x00,0x00,0x00,0x00};UINT8 OTGH_SET_FEATURE_OTG[] = {0x00,0x03,0x03,0x00,0x00,0x00,0x00,0x00}; UINT8 OTGH_SETDESCRIPTOR_DEVICE[] = {0x00,0x07,0x00,0x01,0x00,0x00,0x12,0x00}; //Set Device DescriptorUINT8 OTGH_GETDESCRIPTOR_STR70[] = {0x80,0x06,0x70,0x03,0x00,0x00,0x06,0x00}; //Get String80 DescriptorUINT8 OTGH_SETDESCRIPTOR_STR70[] = {0x00,0x07,0x70,0x03,0x00,0x00,0x06,0x00}; //Set String80 DescriptorUINT8 OTGH_GETDESCRIPTOR_STR80[] = {0x80,0x06,0x80,0x03,0x00,0x00,0x12,0x00}; //Get String80 DescriptorUINT8 OTGH_SETDESCRIPTOR_STR80[] = {0x00,0x07,0x80,0x03,0x00,0x00,0x12,0x00}; //Set String80 DescriptorUINT32 waIntervalMap[11]={1,2,4,8,16,32,64,128,256,512,1024};qHD_Structure *psHost20_qHD_List_Control[3];qHD_Structure *psHost20_qHD_List_Bulk[3]; qHD_Structure *psHost20_qHD_List_Interrupt[3]; Periodic_Frame_List_Structure *psHost20_FramList;UINT8 bAddressCounter=1;UINT8 Host20_qTD_Manage[Host20_qTD_MAX]; //1=>Free 2=>usedUINT8 Host20_iTD_Manage[Host20_iTD_MAX]; //1=>Free 2=>used UINT8 Host20_DataPage_Manage[Host20_Page_MAX]; //1=>Free 2=>used Host20_Init_Condition_Structure sInitCondition;volatile Host20_Attach_Device_Structure sAttachDevice;volatile UINT32 gwOTG_Timer_Counter;volatile UINT32 gwLastqTDSendOK;volatile UINT32 gwLastiTDSendOK;volatile UINT32 gwFrameList0Interrupt; Host20_ISO_FixBufferMode_Structure sISOFixBufferMode;UINT8 bForceSpeed;//0=>All Clear 1=>Full Speed 2=>High SpeedHost20_Attach_Device_Structure *psDevice_AP; //************************************************************************************************************//************************************************************************************************************// *** Group-1:Main Function ***//*************************************************************************************************************//************************************************************************************************************//====================================================================// * Function Name: flib_OTGH_Init // * Description: Init the Host HW and prepare the ED/TD// <1>.Init All the Data Structure // <1.1>.Build control list // <1.2>.Build Bulk list // <1.3>.Build Interrupt list // <1.4>.Build ISO list (Reserved)// <2>.Reset the chip// <3>.Set HW register // <3.1>.Enable FIFO-Enable(0x100->Bit5) & FPGA-Mode-Half-Speed (0x100->Bit1)// <3.2>.Enable interrupts// <3.3>.Periodic List Base Address register// <3.4>.USBCMD (Interrupt/Threshod/Frame List/Run-Stop)//// * Input: wTimeWaitForConnect_ms:The time of waitting for connecting// * OutPut: 0:Device do not connect// 1:Host init ok// 2:Bus Rest Fail//====================================================================UINT8 flib_OTGH_Init(UINT32 wTimeWaitForConnect_ms,UINT8 wForDevice_B,UINT8 bDeviceA_HNP2){ UINT32 wValue,wTimer_ms; //John, add explanation //wForDevice_B ==0 ==> Reset Controller, reset Device-port //wForDevice_B ==1 ==> DON'T Reset Controller, reset Device-port //wForDevice_B ==2 ==> DON'T Reset Controller, DON'T reset Device-port //john add, to disable async first flib_Host20_Asynchronous_Setting(HOST20_Disable); flib_Host20_Periodic_Setting(HOST20_Disable); //<1>.Waiting for the Device connect if (wForDevice_B==0) { mbHost20_USBCMD_HCReset_Set(); while(mbHost20_USBCMD_HCReset_Rd()>0) ; } wValue=0; wTimer_ms=0; do { wValue=mwHost20_PORTSC_ConnectStatus_Rd(); if (wValue==0) { flib_Host20_TimerEnable(1);//10 wTimer_ms++; } if (wTimer_ms>wTimeWaitForConnect_ms) // Case1:Waiting for 10 sec=10000 { // Case2:Waiting for 100 ms =100 printf("??? Waiting for Peripheral Connecting Fail...\n"); return (0); } } while(wValue==0); if (bDeviceA_HNP2==0) printf(">>>Device already connect...\n"); //flib_Host20_TimerEnable(24);//For OPT spec. flib_Host20_TimerEnable(25);//For OPT spec.//For phy 711MA timing //<1>.Reset the chip & Set Init Configuration mbHost20_USBCMD_ParkMode_CNT_Set(1); mwHost20_Misc_EOF1Time_Set(3); //<2>.Init All the Data Structure & write Base Address register //Write Base Address to Register mwHost20_CurrentAsynchronousAddr_Set(Host20_STRUCTURE_qHD_BASE_ADDRESS); //<4>.Enable interrupts mwHost20_USBINTR_Set(HOST20_USBINTR_SystemError|HOST20_USBINTR_PortChangeDetect|HOST20_USBINTR_USBError|HOST20_USBINTR_CompletionOfTransaction); if (wForDevice_B!=2) { //john, if (wForDevice_B==0) { if (flib_Host20_PortBusReset()>0) return(2); } flib_Host20_InitStructure(); flib_Host20_QHD_Control_Init(); return (1); }//====================================================================// * Function Name: flib_Host20_Close // * Description: // <1>.Suspend Host // <2>.Disable the interrupt// <3>.Clear Interrupt Status// <4>.Issue HW Reset// <5>.Free the Allocated Memory// * Input: // * OutPut: //====================================================================UINT8 flib_Host20_Close(UINT8 bForDeviceB){ UINT32 wTemp; if (mwHost20_USBINTR_Rd()>0) { //<1>.Suspend Host if (bForDeviceB==0) flib_Host20_Suspend(); else { //Start;;Bruce;;06282005;;Make sure the Async schedule is disbale if (mbHost20_USBCMD_AsynchronousEnable_Rd()>0) {//Disable the Asynchronous Schedule mbHost20_USBCMD_AsynchronousEnable_Clr(); while(mwHost20_USBSTS_AsynchronousStatus_Rd()>0); } //End;;Bruce;;06282005;;Make sure the Async schedule is disbale flib_Host20_StopRun_Setting(HOST20_Disable); } //<2>.Disable the interrupt mwHost20_USBINTR_Set(0); //<3>.Clear Interrupt Status wTemp=mwHost20_USBSTS_Rd(); wTemp=wTemp&0x0000003F; mwHost20_USBSTS_Set(wTemp); } return (1);}//====================================================================// * Function Name: flib_Host20_ISR // * Description: // <1>.Read the Interrupt Status// <2>.Checking for the Error type interrupt => Halt the system// <3>.Check Port Change Status(Resume/Connect Status Change/Port Enable-Disable Change)// <3.1>.Process Event-Resume & Clear Status// <3.2>.Process Event-ConnectStatusChange & Clear Status// <3.3>.Process Event-PortEnableDisableChange & Clear Status// <3.4>.Clear Port Change Status // <3.5>.Process the Event of Port Reset// <4>.Check Complete Interrupt// * Input: // * OutPut: //====================================================================UINT8 flib_Host20_ISR(void){ UINT32 wIntStatus; //<1>.Read the Interrupt data wIntStatus=mwHost20_USBSTS_Rd(); //<2>.Checking for the Error type interrupt => Halt the system if (wIntStatus&HOST20_USBINTR_SystemError) { printf("???System Error... Halt the system...\n "); while(1); } if (wIntStatus&HOST20_USBINTR_USBError) { mwHost20_USBSTS_USBError_Set(); sAttachDevice.bSendStatusError=1; } //<3>.Check Port Change Status if (wIntStatus&HOST20_USBINTR_PortChangeDetect) { //<3.1>.Process Event-Resume & Clear Status if (mwHost20_PORTSC_ForceResume_Rd()>0) { sAttachDevice.bRemoteWakeUpDetected=1; } //<3.2>.Process Event-ConnectStatusChange & Clear Status if (mwHost20_PORTSC_ConnectChange_Rd()) {// mwHost20_PORTSC_ConnectChange_Set(); if (mwHost20_PORTSC_ConnectStatus_Rd()==0) { flib_Host20_StopRun_Setting(HOST20_Disable); sAttachDevice.bConnectStatus=0; } else sAttachDevice.bConnectStatus=1; } //<3.3>.Process Event-PortEnableDisableChange & Clear Status if (mwHost20_PORTSC_EnableDisableChange_Rd()) { mwHost20_PORTSC_EnableDisableChange_Set(); } //<3.4>.Process the Event of Port Reset if (sAttachDevice.bPortReset==1) {//During Port Reset if (mwHost20_PORTSC_PortReset_Rd()==0) sAttachDevice.bPortReset=0; } //<3.5>.Process the Event of Suspend if (sAttachDevice.bSuspend==0) {//During Port Reset if (mwHost20_PORTSC_ForceSuspend_Rd()==1) sAttachDevice.bSuspend=1; } //<3.9>.Clear Status of Port Status mwHost20_USBSTS_PortChangeDetect_Set(); } //<4>.Check Complete Interrupt if (wIntStatus&HOST20_USBINTR_CompletionOfTransaction) { mwHost20_USBSTS_CompletionOfTransaction_Set(); if (psDevice_AP->bISOTransferEnable==0) { if (psDevice_AP->psSendLastqTD->bStatus_Active==0) gwLastqTDSendOK=1; } if (psDevice_AP->bISOTransferEnable==1) { gwLastiTDSendOK=1; } if (psDevice_AP->bISOTransferEnable==2) { gwLastiTDSendOK=1; flib_Host20_Issue_ISO_FixBufferMode_ISR(); } } //<5>.Checking for frameList = 0 interrupt if (wIntStatus&HOST20_USBINTR_FrameRollover) { mwHost20_USBSTS_FrameRollover_Set(); gwFrameList0Interrupt++; } return (1);}//====================================================================// * Function Name: flib_Host20_StopRun_Setting // * Description: // * Input: // * OutPut: //====================================================================void flib_Host20_StopRun_Setting(UINT8 bOption){ if (bOption==HOST20_Enable) { if (mbHost20_USBCMD_RunStop_Rd()>0) return; mbHost20_USBCMD_RunStop_Set(); while(mbHost20_USBCMD_RunStop_Rd()==0); } else if (bOption==HOST20_Disable) { if (mbHost20_USBCMD_RunStop_Rd()==0) return; //Start;;Bruce;;06282005;;Make sure the Async schedule is disbale if (mbHost20_USBCMD_AsynchronousEnable_Rd()>0) {//Disable the Asynchronous Schedule mbHost20_USBCMD_AsynchronousEnable_Clr(); while(mwHost20_USBSTS_AsynchronousStatus_Rd()>0); } //End;;Bruce;;06282005;;Make sure the Async schedule is disbale mbHost20_USBCMD_RunStop_Clr(); while(mbHost20_USBCMD_RunStop_Rd()>0); } else { printf("??? Input Error 'flib_Host20_StopRun_Setting'..."); while(1); }}//====================================================================// * Function Name: flib_Host20_Asynchronous_Setting // * Description: // * Input: // * OutPut: //====================================================================void flib_Host20_Asynchronous_Setting(UINT8 bOption){ if (bOption==HOST20_Enable) { if (mwHost20_USBSTS_AsynchronousStatus_Rd()>0) return; mbHost20_USBCMD_AsynchronousEnable_Set(); while(mwHost20_USBSTS_AsynchronousStatus_Rd()==0); } else if (bOption==HOST20_Disable) { if (mwHost20_USBSTS_AsynchronousStatus_Rd()==0) return; mbHost20_USBCMD_AsynchronousEnable_Clr(); while(mwHost20_USBSTS_AsynchronousStatus_Rd()>0); } else { printf("??? Input Error 'flib_Host20_Asynchronous_Setting'...\n"); while(1); }}//====================================================================// * Function Name: flib_Host20_Periodic_Setting // * Description: // * Input: // * OutPut: //====================================================================void flib_Host20_Periodic_Setting(UINT8 bOption){ if (bOption==HOST20_Enable) { //<1>.If Already enable => return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -