UsbHostCommon.c
资源名称:SDK_M5661.rar [点击查看]
上传用户:hjhsjcl
上传日期:2020-09-25
资源大小:11378k
文件大小:8k
源码类别:
压缩解压
开发平台:
C++ Builder
- /*==========================================================================
- Copyright (c) 2004 Acer Labs, Inc. All Rights Reserved
- File: UsbHostCommon.c
- content:
- History:
- * Date By Reason
- * ========== ======= ====================
- * 07/2005 Allen devolepment for 5661
- ==========================================================================*/
- #define _USB_HOST_COMM_H_
- #include <DP8051XP.H>
- #include "TypeDef.h"
- #include "Const.h"
- #include "Reg5661.h"
- #include "Common.h"
- #if(_C_MODE_)
- #include "OledString.h"
- #include "OLED.h"
- #else
- #include "Lcd.h"
- #endif
- #include "Key.h" //050719
- #include "UsbHostCommon.h"
- #if(ENABLE_HOST_FUNCTION) //allen050919
- #include "UsbHost.h" //050810
- #endif
- #if(_USE_FOR_COPY_ENGINE_)
- /*****************************************************
- The following variable is referenced by FAT module
- ******************************************************/
- BYTE code gbMaxRecursiveDepth = 12; //Maximum recursive depth for the FAT module
- /*****************************************************
- The following functions is called by FAT module
- ******************************************************/
- #endif//_USE_FOR_COPY_ENGINE_
- /*******************************
- Common Function
- ********************************/
- #if(_HOST_API_POWEROFF_)
- void HostVbusOff(void) large
- {
- obGPIOADIR|=0x06; //Force GPIOA[1,2]
- obGPIOADAT|=0x02; //A1 off
- obGPIOADAT&=0xFB; //A2 off
- }
- API bit UsbHostPowerOff(void) large
- {
- //050719: get into suspend before PowerOff
- obUSBHOSTPORTCTRL |= HOST_SUSPEND;
- //050708: delay a while for FlashDisk Job Finish.
- gxbLcdCounter=0; //050708 del
- while(gxbLcdCounter<10) { };//050721: 20->10
- HostVbusOff();
- return TRUE;
- }
- #endif
- #if(_HOST_COMMON_)
- /*---------------------------------------------------------------
- * Function_Name:
- * Delay
- * Arguments:
- * IN int DelayTime -- loop count
- * Return Value:
- * none
- * Description:
- * delay loop time
- *
- *--------------------------------------------------------------*/
- API void HostDelay(WORD DelayTime) large //050712
- {
- BYTE bClkCtrlTmp=obCLKMCUCTRL;
- //#if(USE_STEP_UP_CONVERTER)
- McuClockCtrlAdjust(0xD0);// WHH #1 061017
- //#else
- // obCLKMCUCTRL=0xD0; //MCU clock use XTALI 12MHz //v03210#1
- //#endif
- while(DelayTime)
- {
- DelayTime--;
- }
- //#if(USE_STEP_UP_CONVERTER)
- McuClockCtrlAdjust(bClkCtrlTmp);// WHH #1 061017
- //#else
- // obCLKMCUCTRL=bClkCtrlTmp; //MCU clock use XTALI 12MHz //v03210#1
- //#endif
- }
- /*--------------------------------------------------------------------------
- Description:
- Detect device Connect Function.
- Send CMD_TEST_UNIT_READY.
- Arguments:
- Global arguments:
- Returns:
- TRUE: Medium presents
- FALSE: No mudium, and set gxbLastErrCode=ERR_HOST_NO_DEVICE;
- Note:
- 050801
- --------------------------------------------------------------------------*/
- API bit UsbHostDetect(void) large //050729 050801 050810
- {
- BYTE status;
- #if 1
- gxbErrCode=ERR_NO_ERROR;
- status=UmsSendPIOCmd(CMD_TEST_UNIT_READY);
- if(status==Error_Successful)
- return TRUE; //medium presents
- else if(status==Error_UsbConnect)
- {
- gxbErrCode=ERR_HOST_NO_DEVICE;
- return FALSE;
- }
- //no mudium for this lun
- gxbErrCode=ERR_HOST_DEVICE_NOT_READY;
- return FALSE;
- #else
- while(--i)
- {
- if(!UmsSendPIOCmd(CMD_TEST_UNIT_READY))
- {
- //Success
- return TRUE; //medium presents
- }
- HostDelay(0x6ff);
- }
- //no mudium for this lun
- gxbLastErrCode=ERR_HOST_NO_DEVICE;
- return FALSE;
- #endif
- }
- /*--------------------------------------------------------------------------
- Description:
- Determine Write Protect func.
- Send CMD_MODE_SENSE_6 command , and get data from device.
- Byte[2] bit 7 indicate WP status.
- Arguments:
- Global arguments:
- Returns:
- TRUE: Write-Protect
- FALSE: Normal.
- 050801
- --------------------------------------------------------------------------*/
- API bit UsbHostIsWriteProtect(void) large
- {
- #if 1 //050106 //set 1 for lightne debug, it should set 0!! allen050815 not to check WP
- return FALSE;
- #else
- BYTE Status;
- if(Status = UmsSendPIOCmd(CMD_MODE_SENSE_6)) //protected or unsupported
- {
- if( Status == Error_Warning) //protected
- return TRUE;
- }
- else
- return FALSE;
- return TRUE; //impossible to arrive here
- #endif
- }
- //GPIOA[3] FLGJ
- //
- //
- bit HostOverCurrentDet(void) large
- {
- obGPIOADIR&=0xF7; //GPIOA[3] Input
- if(obGPIOADAT&0x08)
- return FALSE;
- else //over current
- {
- HostVbusOff();
- return TRUE;
- }
- }
- void GetHostIntFlag(void) //050713#0: use polling instead of interrupt 050714
- {
- g_USBHOSTINTFLAG1|=obUSBHOSTINTFLAG1;
- g_USBHOSTINTFLAG2|=obUSBHOSTINTFLAG2;
- }
- API bit UsbHostNormalCheck(void) large
- {
- if(HostOverCurrentDet()) // over current
- return FALSE;;
- if(!(obUSBHOSTPORTCTRL&HOST_PORT_CONNECT)) //disconnect interrupt 050713#1 add
- {
- return FALSE;
- }
- GetHostIntFlag(); //050714
- if(g_USBHOSTINTFLAG1 & USB_PORT_ERROR) // port error interrupt 050714 move in normal check
- {
- return FALSE;//Error_UsbPort; 050714 ???
- }
- return TRUE;
- }
- #endif
- #if(_HOST_API_INIT_)
- //allen050705
- //GPIOA[1]: control MAX1797:
- // HI---off---
- // LO---on--- 5V
- //GPIOA[2]: control RT9702
- // HI---on--- 5V
- // LO---off--- switch off
- void HostVbusOn(void) large //050708 050712
- {
- BYTE bTmp;
- obGPIOADIR|=0x06; //Force GPIOA[1,2]
- #if 1//050810
- obGPIOADAT|=0x02; //A1 off
- obGPIOADAT&=0xFB; //A2 off
- for(bTmp=0;bTmp<200;bTmp++)
- {
- obGPIOADAT|=0x02; //A1 off
- obGPIOADAT&=0xFD; //A1 on
- }
- HostDelay(0xFFF);
- for(bTmp=0;bTmp<200;bTmp++)
- {
- obGPIOADAT&=0xFB; //A2 off
- obGPIOADAT|=0x04; //A2 on
- }
- HostDelay(0xFFFF);
- #else
- for(bTmp=0;bTmp<100;bTmp++)
- {
- obGPIOADAT|=0x02; //A1 off
- obGPIOADAT&=0xFB; //A2 off
- obGPIOADAT&=0xFD; //A1 on
- obGPIOADAT|=0x04; //A2 on
- }
- #endif
- //allen050712: Wait a few time to let voltage stable.
- HostDelay(0xFFFF);
- HostDelay(0xFFFF);
- }
- bit HostWaitConnect(WORD wPeriod,BYTE bRetryCnt) large //allen050712 050720
- {
- WORD wCnt;
- WORD wConnect;
- BYTE bClkCtrlTmp=obCLKMCUCTRL;
- bit fRet=FALSE;
- // BYTE bKey;
- //#if(USE_STEP_UP_CONVERTER)
- McuClockCtrlAdjust(0xD0);// WHH #1 061017
- //#else
- // obCLKMCUCTRL=0xD0; //MCU clock use XTALI 12MHz //v03210#1
- //#endif
- gfCancel=FALSE;
- while(bRetryCnt)
- {
- #if(_PM_HST_) //050921
- if(gfCancel)
- break;
- /* KeyGet(bKey);
- if(bKey==KEY_HOST_CANCEL)
- break;*/
- #endif
- if(HostOverCurrentDet()) // over current
- {
- break;
- }
- if(bRetryCnt!=0xFF)
- bRetryCnt--;
- //0x4000 -> about 200ms
- for(wCnt=0,wConnect=0;wCnt<wPeriod;wCnt++)
- {
- if(obUSBHOSTPORTCTRL&HOST_PORT_CONNECT)
- wConnect++;
- }
- if(wPeriod==wConnect) //connect a period
- {
- fRet=TRUE; break;
- }
- }
- //#if(USE_STEP_UP_CONVERTER)
- McuClockCtrlAdjust(bClkCtrlTmp);// WHH #1 061017
- //#else
- // obCLKMCUCTRL=bClkCtrlTmp; //MCU clock use XTALI 12MHz //v03210#1
- //#endif
- return fRet;
- }
- API bit UsbHostPowerOn(void) large
- {
- HostVbusOn();
- if(!HostWaitConnect(0x4000,90))// 90->10sec 0xFF))//32)) //allen050712: about 5sec
- return FALSE;
- return TRUE;
- }
- #endif
- #if(_USE_FOR_COPY_ENGINE_)
- /*---------------------------------------------------------------
- * Function_Name:
- * int0
- * Arguments:
- * none
- * Return Value:
- * none
- * Description:
- * interrupt0 service routine
- *
- *--------------------------------------------------------------*/
- extern void DcvIsr(void);
- void ExtInt0 (void) interrupt 0 using 1 //050714: from UsbHost.c
- {
- BYTE bDcvTemp;
- g_SYSINT0FLAG = obSYSINT0FLAG;
- /* if(g_SYSINT0FLAG & USB_HST_INT1) 050713#0 use polling
- {
- g_USBHOSTINTFLAG1 |= obUSBHOSTINTFLAG1; //allen050712#2 |=
- }
- if(g_SYSINT0FLAG & USB_HST_INT2)
- {
- g_USBHOSTINTFLAG2 |= obUSBHOSTINTFLAG2; //allen050712#2 |=
- }*/
- //add by tne 2005/04/08
- if(g_SYSINT0FLAG & DCV_INT)
- {
- bDcvTemp = obDCVARRD;
- DcvIsr();
- obDCVARRD = bDcvTemp;
- }
- }
- #endif