⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 z228_usb_msc.cpp

📁 此压缩包为杰得开发得z228的BSP的源代码,可以实现很多功能,尤其是视频解码有很好的效果.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	}
}
#else	   	
          RETAILMSG(USB_ERROR, (_T("Dev IST creation failed\r\n")));
#endif
	   if(pContext->hevIntrEvent)   {
		      	CloseHandle(pContext->hevIntrEvent);
			pContext->hevIntrEvent = NULL;	
          }
	   InterruptDone(29);
	   InterruptDisable(29);
          continue;       
       }
       Sleep(20);
	   
	//if the usb cable have been plugged during msgbox()    
       if(z228_Hw_Init() == HOST_MODE)  {
#ifdef DEBUG_TEXT
{
 	if(fp == NULL)
		fp = fopen("\\debug.txt", "a+");
	if(fp)  {
		fprintf(fp, "Exit ISTMain!%d\r\n", bCharge);
		fflush(fp);
	}
}
#else
		  RETAILMSG(1, (_T("Exit ISTMain!%d\r\n"), bCharge));
#endif
		  pContext->fExitIST = TRUE;
		  if(pContext->hIST)  {
		  	  SetEvent(pContext->hevIntrEvent);
		  	  RETAILMSG(USB_INIT, (_T("Stop Dev IST ...\r\n")));
			  dwWait = WaitForSingleObject(pContext->hIST, 2000);
			  if ( dwWait != WAIT_OBJECT_0 ) {
                            RETAILMSG(USB_ERROR, (_T("Stop Dev IST Forcely ...\r\n")));
                            TerminateThread(pContext->hIST , DWORD(-1));
                       } 
                       CloseHandle(pContext->hIST);
                       pContext->hIST = NULL;
			  pContext->fExitIST =  FALSE;
		  }
		 pContext->fFunction = FALSE;
		 Sleep(50);
		 InterruptDone(29);
		 InterruptDisable(29);
		 if(pContext->hevIntrEvent) {
			  	 CloseHandle(pContext->hevIntrEvent);
				 pContext->hevIntrEvent = NULL;
		  }
#ifdef DEBUG_TEXT
{
 	if(fp == NULL)
		fp = fopen("\\debug.txt", "a+");
	if(fp)  {
		fprintf(fp, "charge %x\r\n", bCharge);
		fflush(fp);
	}
}
#else		 
                RETAILMSG(1, (_T("charge %x\r\n"), bCharge));
#endif

                //write the value to the register
                {
                    HKEY ActiveKey;
                    DWORD status;
                    const LPCTSTR pszKey = L"Drivers\\BuiltIn\\USB_MSC";
                    //DWORD dwValLen;

                    status = RegOpenKeyEx(
                                HKEY_LOCAL_MACHINE,
                                (LPCWSTR)pszKey,
                                0,
                                0,
                                &ActiveKey );
                   if(status !=  ERROR_SUCCESS){
                                RETAILMSG(USB_ERROR, (TEXT("500k debug: Failed to open active key \r\n")));
                   }
    
                   RETAILMSG(USB_DEBUG, (TEXT("set: Val = %d\r\n"), bCharge));

                   //set the registry 
                  status = RegSetValueEx(ActiveKey, L"MscExInfo", 0, REG_DWORD, (BYTE *)&bCharge, sizeof(DWORD));
                  if(status !=  ERROR_SUCCESS){     
                          RETAILMSG(USB_ERROR, (TEXT("Failed to set active key\r\n")));
	                   RegCloseKey(ActiveKey);
    
                 }
                 RegCloseKey(ActiveKey); 
                }                
		  SetEvent(pContext->hevHostEvent);
		  Sleep(50);    
       }
       //SetEvent(pContext->hevDevDoneEvent);
      }
    }

EXIT:
     if (pContext->hevHostEvent) {
			     CloseHandle(pContext->hevHostEvent);
           pContext->hevHostEvent = NULL;
        } 
        if (pContext->hevHostDoneEvent) {
			     CloseHandle(pContext->hevHostDoneEvent);
           pContext->hevHostDoneEvent = NULL;
        }    
        if (pContext->hevDevEvent) {
			     CloseHandle(pContext->hevDevEvent);
           pContext->hevDevEvent = NULL;
         }    
        if (pContext->hevDevDoneEvent) {
			     CloseHandle(pContext->hevDevDoneEvent);
           pContext->hevDevDoneEvent = NULL;
    }          
    RETAILMSG(1, (_T("-MainThread\r\n"))); 
    FUNCTION_LEAVE_MSG();
    return 0;
}

static
CEDEVICE_POWER_STATE
SetPowerState(
    PZ228_CONTEXT        pContext,
    CEDEVICE_POWER_STATE    cpsNew
    )
{
    
    
    PREFAST_DEBUGCHK(pContext);
    DEBUGCHK(VALID_DX(cpsNew));
    ValidateContext(pContext);
    RETAILMSG(USB_DEBUG, (_T("==> SetPowerState ()\r\n")));

    // Adjust cpsNew.
    if (cpsNew != pContext->cpsCurrent) {
        if (cpsNew == D1 || cpsNew == D2) {
            // D1 and D2 are not supported.
            cpsNew = D0;
        }
        else if (pContext->cpsCurrent == D4) {
            // D4 can only go to D0.
            cpsNew = D0;
        }
    }
	RETAILMSG(USB_DEBUG, (_T("==> SetPowerState ()\r\n")));
    if (cpsNew != pContext->cpsCurrent) {
        RETAILMSG(USB_DEBUG, (_T("%s Going from D%u to D%u\r\n"),
            pszFname, pContext->cpsCurrent, cpsNew));
        RETAILMSG(USB_DEBUG, (_T("==> SetPowerState ()\r\n")));
        if ( (cpsNew < pContext->cpsCurrent) && pContext->hBusAccess ) {
            SetDevicePowerState(pContext->hBusAccess, cpsNew, NULL);
        }

        switch (cpsNew) {
        case D0:
		RETAILMSG(USB_DEBUG, (_T("D0\r\n")));
            KernelIoControl(IOCTL_HAL_DISABLE_WAKE, &pContext->dwSysIntr,  
                sizeof(pContext->dwSysIntr), NULL, 0, NULL);

            if (pContext->fRunning) {
                // Cause the IST to restart.
                pContext->fRestartIST = TRUE;
                SetInterruptEvent(pContext->dwSysIntr);
            }
            break;

        case D3:
		RETAILMSG(USB_DEBUG, (_T("D3\r\n")));
            KernelIoControl(IOCTL_HAL_ENABLE_WAKE, &pContext->dwSysIntr,  
                sizeof(pContext->dwSysIntr), NULL, 0, NULL);
            break;

        case D4:
		RETAILMSG(USB_DEBUG, (_T("D4\r\n")));
            KernelIoControl(IOCTL_HAL_DISABLE_WAKE, &pContext->dwSysIntr,  
                sizeof(pContext->dwSysIntr), NULL, 0, NULL);         
            
            break;
        }

       if ( (cpsNew > pContext->cpsCurrent) && pContext->hBusAccess ) {
            SetDevicePowerState(pContext->hBusAccess, cpsNew, NULL);
        }

		pContext->cpsCurrent = cpsNew;
    }

    return pContext->cpsCurrent;
}

/*
static
DWORD
Find2280Hardware(
    LPCTSTR             pszActiveKey,
    HKEY                hkDevice,
    PZ228_CONTEXT    pContext
    )
{
    

    DEBUGCHK(pszActiveKey);
    PREFAST_DEBUGCHK(pContext);

    DWORD dwRet = ERROR_GEN_FAILURE;
	RETAILMSG(USB_DEBUG, (_T("==> Find2280Hardware ()\r\n")));
	pContext->dwSysIntr = 29;

/*    PHYSICAL_ADDRESS PortAddress;
    DWORD dwAddrSpace = 0;
    
	// Read window information
	DDKWINDOWINFO wini;
	wini.cbSize = sizeof(wini);
	if (DDKReg_GetWindowInfo(hkDevice, &wini) != ERROR_SUCCESS) {
	    RETAILMSG(1, (_T("%s Error getting window information\r\n"),
	        pszFname));
		goto EXIT;
	}

    // Sanity check windows
	if (wini.dwNumMemWindows < 3) {
	    RETAILMSG(1, (_T("%s Less than 3 memory windows\r\n"),
	        pszFname));
	    goto EXIT;
	}
	
	// Read ISR information
	DDKISRINFO isri;
	isri.cbSize = sizeof(isri);
	if (DDKReg_GetIsrInfo(hkDevice, &isri) != ERROR_SUCCESS) {
	    RETAILMSG(1, (_T("%s Error getting ISR information\r\n"),
	        pszFname));
		goto EXIT;
	}

    // Sanity check ISR
	if (isri.szIsrDll[0] != 0) {
		if ( isri.szIsrHandler[0] == 0 || isri.dwIrq == IRQ_UNSPECIFIED ||
             isri.dwSysintr == SYSINTR_NOP ) {
		    RETAILMSG(ZONE_WARNING, (_T("%s ISR DLL specified, but not ISR handler or IRQ\r\n"),
    	        pszFname));
            isri.szIsrDll[0] = 0;
		}
	}


	pContext->dwSysIntr = isri.dwSysintr;
	RETAILMSG(USB_DEBUG, (_T("%s IRQ 0x%X mapped to SYS_INTR 0x%X\r\n"), 
	    pszFname, isri.dwIrq, isri.dwSysintr));

    PortAddress.LowPart = wini.memWindows[WINDOW_CONFIG_NUM].dwBase;
    PortAddress.HighPart = 0;
    
	RETAILMSG(USB_DEBUG, (_T("%s config > base address -> 0x%X; length -> 0x%X\r\n"),
	    pszFname, wini.memWindows[WINDOW_CONFIG_NUM].dwBase, 
	    wini.memWindows[WINDOW_CONFIG_NUM].dwLen));

    BOOL fSuccess = BusTransBusAddrToVirtual(pContext->hBusAccess,
        (INTERFACE_TYPE)  wini.dwInterfaceType, wini.dwBusNumber, 
        PortAddress, wini.memWindows[WINDOW_CONFIG_NUM].dwLen, &dwAddrSpace,
        (PPVOID) &pContext->pbConfigAddr);
    if (!fSuccess) {
        RETAILMSG(1, (_T("%s Error translating config address\r\n"),
	        pszFname));
        goto EXIT;
    }

    PortAddress.LowPart = wini.memWindows[WINDOW_FIFO_NUM].dwBase;
    PortAddress.HighPart = 0;
    
	RETAILMSG(USB_DEBUG, 
	    (_T("%s FIFO > base address -> 0x%X; length -> 0x%X\r\n"),
	    pszFname, wini.memWindows[WINDOW_FIFO_NUM].dwBase, 
	    wini.memWindows[WINDOW_FIFO_NUM].dwLen));
	
    fSuccess = BusTransBusAddrToVirtual(pContext->hBusAccess,
        (INTERFACE_TYPE) wini.dwInterfaceType, wini.dwBusNumber,
        PortAddress, wini.memWindows[WINDOW_FIFO_NUM].dwLen, &dwAddrSpace, 
        (PPVOID) &pContext->pbFifoAddr);
    if (!fSuccess) {
        RETAILMSG(1, (_T("%s Error translating FIFO address\r\n"),
	        pszFname));
        goto EXIT;
    }
    
    dwRet = ERROR_SUCCESS; 
    return dwRet;    
}
*/

static
VOID
FreeZ228Context(
    PZ228_CONTEXT pContext
    )
{
    PREFAST_DEBUGCHK(pContext);
    DEBUGCHK(!pContext->hevIntrEvent);
    DEBUGCHK(!pContext->hIST);
    DEBUGCHK(!pContext->fRunning);

    pContext->dwSig = GARBAGE_DWORD;
    RETAILMSG(USB_DEBUG, (_T("+ FreeNet2280Context ()\r\n")));

    if (pContext->dwSysIntr) {
        KernelIoControl(IOCTL_HAL_DISABLE_WAKE, &pContext->dwSysIntr,  
    	    sizeof(pContext->dwSysIntr), NULL, 0, NULL);
    }
    
    DeleteCriticalSection(&pContext->csSharedRegisterAccess);
    LocalFree(pContext);
}


//  Initialize the Z228 otg hardware.
inline static int z228_Hw_Init( void )
{

	 DWORD *Z228_GPIODATA = NULL, *reset = NULL, dwMode = -1;		
	 PHYSICAL_ADDRESS PhysicalIoBase;	
		
	 PhysicalIoBase.HighPart = 0;
	 PhysicalIoBase.LowPart = 0x2002f3fc;	//GPIO 8
	 Z228_GPIODATA = (DWORD *)MmMapIoSpace( PhysicalIoBase, 0x1000 ,FALSE );

	 PhysicalIoBase.HighPart = 0;
	 PhysicalIoBase.LowPart = Z228_RESET;
	 reset = (DWORD *)MmMapIoSpace( PhysicalIoBase, 0x1000 ,FALSE );	
		
	 //initialize z228 otg		
	  if( !Z228_GPIODATA )    	 {
        		 RETAILMSG(USB_ERROR,(TEXT("MAP Z228_GPIODATA register failed \r\n")));
    	   }
    	   else    {
    			   *(Z228_GPIODATA+1) |= 0x10;	//GP8_4 out for rest
    			   Sleep(20);
			   *(Z228_GPIODATA) &= ~0x10;
			   Sleep(30);
			   *(Z228_GPIODATA) |= 0x10;
			   Sleep(30);
			   MmUnmapIoSpace( Z228_GPIODATA, 0x1000 );
	    }
	    //must
	    Sleep(100);
		 
            //Z228 otg reset	
	     if( !reset )    		{
        		 RETAILMSG(USB_ERROR,(TEXT("MAP Z228_RESET register failed \r\n")));
    	      }
    		else    		{
				
    			  *reset &= ~(Z228_RESET_nUSBRES   | Z228_RESET_nUSB48RES
			   		| Z228_RESET_nUSBPLLRES | Z228_RESET_nUSB12RES);

			
			  *reset |=Z228_RESET_nUSB48RES;
			  Sleep(20);		 		//must
			  *reset |=Z228_RESET_nUSBPLLRES;
			  Sleep(10);
			  *reset |=Z228_RESET_nUSB12RES;
			  Sleep(10);
			  *reset |=Z228_RESET_nUSBRES;
			  Sleep(10);
			
			  MmUnmapIoSpace( reset, 0x1000 );			
		
		}
      Sleep(50);
		
	
		    //config the otg controller according to the controller's Mode
	  dwMode = (usb_read(UHC_OTG_CSR)&OTG_MODE_DEVICE)? DEV_MODE : HOST_MODE;
        RETAILMSG(USB_DEBUG, (_T("+z228_Hw_Init, Mode = %d!\r\n"),dwMode));        
        switch(dwMode) {
            case DEV_MODE:            //switch to Device Mode 
                RETAILMSG(USB_DEBUG, (_T("Switch to Device Mode!\r\n"))); 
                usb_write( UHC_HNP_CAPABLE | UHC_SRP_CAPABLE
			                                     | UHC_HNP_ENABLE_HOST
			                                     | UHC_HNP_DETECT_CHANGE
		                                       | UHC_SRP_DETECT_CHANGE
			                                     | UHC_SRP_STATUS_CHANGE, UHC_OTG_CSR);
		     	       // Disable and clear Host interrupts		    
	               usb_write(0,UHC_INTR_ENABLE);	
	               //usb_write(~0,UHC_INTR_ENABLE);
	               usb_write(~0,UHC_INTR);
		   
		             Sleep(10);


                 //necessary to set the register to device
		             usb_read(UDC_MAC_REG(0));
	               usb_write(0xfff0, UDC_MAC_REG(0));

			 // Device Mode, Full Speed, Self Powered
 	               usb_write(UDC_CONFIG_SPEEDFULL | UDC_CONFIG_SELFPOWERED | UDC_DEVICE_MODE, UDC_CONFIG);

	               usb_write(0x2000000, UDC_ENDP_INFO(0));
	               usb_write(0x20000c1, UDC_ENDP_INFO(1));
	               usb_write(0x20000d2, UDC_ENDP_INFO(2));

	               usb_write(0x10, UDC_RX_FIFOSIZE);
	               usb_write(0x10, UDC_TX_FIFOSIZE(0));
	               usb_write(0x10, UDC_TX_FIFOSIZE(2));
	
	               // Set Threshold Register
	               usb_write( (((UDC_RX_THRESHOLD/4) << 16) | (UDC_TX_THRESHOLD/4)), UDC_THRESHOLD);	              

	               // Power Down Root Hub Port
	               usb_write(0x1F0200, UDC_PORT_STATUS);
	               usb_read(UDC_PORT_STATUS);
	               usb_write(0x1F0200, UDC_PORT_STATUS);
	               usb_read(UDC_PORT_STATUS);
	               usb_write(0x1F0200, UDC_PORT_STATUS);
	               usb_read(UDC_PORT_STATUS);

	               // Clear all interrupt sources
	               usb_write(UDC_SETUP_STATUS_COMPLETE, UDC_SETUP_STATUS);
	               usb_write(UDC_RX_STATUS_COMPLETE, UDC_RX_STATUS);
				   
                 	// Flush FIFOs
	               usb_write(0, UDC_RX_CONTROL(0));
	               usb_write(UDC_RX_FLUSHFIFO, UDC_RX_CONTROL(0));
                 	usb_write(UDC_TX_NAKSENT | UDC_TX_DATASENT, UDC_TX_STATUS(0));
	               usb_write(0, UDC_TX_CONTROL(0));
	               usb_write(UDC_TX_FLUSHFIFO, UDC_TX_CONTROL(0));

	               usb_write(0, UDC_RX_CONTROL(1));
	               usb_write(UDC_RX_FLUSHFIFO, UDC_RX_CONTROL(1));
                 	usb_write(UDC_TX_NAKSENT | UDC_TX_DATASENT, UDC_TX_STATUS(2));
	               usb_write(0, UDC_T

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -