UsbHostCommon.c
上传用户:hjhsjcl
上传日期:2020-09-25
资源大小:11378k
文件大小:8k
源码类别:

压缩解压

开发平台:

C++ Builder

  1. /*==========================================================================
  2. Copyright (c) 2004 Acer Labs, Inc. All Rights Reserved
  3. File: UsbHostCommon.c
  4. content: 
  5. History: 
  6.  * Date By Reason
  7.  * ========== ======= ====================
  8.  * 07/2005 Allen devolepment for 5661
  9. ==========================================================================*/
  10. #define _USB_HOST_COMM_H_
  11. #include <DP8051XP.H>
  12. #include "TypeDef.h"
  13. #include "Const.h"
  14. #include "Reg5661.h"
  15. #include "Common.h"
  16. #if(_C_MODE_)
  17. #include "OledString.h"
  18. #include "OLED.h"
  19. #else
  20. #include "Lcd.h"
  21. #endif
  22. #include "Key.h" //050719
  23. #include "UsbHostCommon.h"
  24. #if(ENABLE_HOST_FUNCTION) //allen050919
  25. #include "UsbHost.h" //050810
  26. #endif
  27. #if(_USE_FOR_COPY_ENGINE_)
  28. /*****************************************************
  29.   The following variable is referenced by FAT module
  30. ******************************************************/
  31. BYTE code gbMaxRecursiveDepth = 12; //Maximum recursive depth for the FAT module
  32. /*****************************************************
  33.   The following functions is called by FAT module
  34. ******************************************************/
  35. #endif//_USE_FOR_COPY_ENGINE_
  36. /*******************************
  37. Common Function
  38. ********************************/
  39. #if(_HOST_API_POWEROFF_)
  40. void HostVbusOff(void) large
  41. {
  42. obGPIOADIR|=0x06; //Force GPIOA[1,2]
  43. obGPIOADAT|=0x02; //A1 off
  44. obGPIOADAT&=0xFB; //A2 off
  45. }
  46. API bit UsbHostPowerOff(void) large
  47. {
  48. //050719: get into suspend before PowerOff
  49. obUSBHOSTPORTCTRL |= HOST_SUSPEND;
  50. //050708: delay a while for FlashDisk Job Finish.
  51. gxbLcdCounter=0; //050708 del
  52. while(gxbLcdCounter<10) { };//050721: 20->10
  53. HostVbusOff();
  54. return TRUE;
  55. }
  56. #endif
  57. #if(_HOST_COMMON_)
  58. /*---------------------------------------------------------------
  59.  * Function_Name:
  60.  * Delay
  61.  * Arguments:
  62.  *    IN int  DelayTime -- loop count
  63.   * Return Value: 
  64.  * none
  65.  * Description:
  66.  *  delay loop time
  67.  * 
  68.  *--------------------------------------------------------------*/
  69. API void HostDelay(WORD DelayTime) large //050712
  70. {
  71. BYTE bClkCtrlTmp=obCLKMCUCTRL;
  72. //#if(USE_STEP_UP_CONVERTER)
  73. McuClockCtrlAdjust(0xD0);// WHH #1 061017
  74. //#else
  75. // obCLKMCUCTRL=0xD0; //MCU clock use XTALI 12MHz //v03210#1
  76. //#endif
  77. while(DelayTime)
  78. {
  79. DelayTime--;
  80. }
  81. //#if(USE_STEP_UP_CONVERTER)
  82. McuClockCtrlAdjust(bClkCtrlTmp);// WHH #1 061017
  83. //#else
  84. // obCLKMCUCTRL=bClkCtrlTmp; //MCU clock use XTALI 12MHz //v03210#1
  85. //#endif
  86. }
  87. /*--------------------------------------------------------------------------
  88. Description:
  89. Detect device Connect Function.
  90. Send CMD_TEST_UNIT_READY.
  91.  
  92. Arguments:
  93. Global arguments:
  94. Returns: 
  95. TRUE:  Medium presents
  96. FALSE: No mudium, and set gxbLastErrCode=ERR_HOST_NO_DEVICE;
  97. Note:
  98. 050801
  99. --------------------------------------------------------------------------*/
  100. API bit UsbHostDetect(void) large //050729 050801 050810
  101. {
  102. BYTE status;
  103. #if 1
  104. gxbErrCode=ERR_NO_ERROR;
  105. status=UmsSendPIOCmd(CMD_TEST_UNIT_READY);
  106. if(status==Error_Successful)
  107. return TRUE; //medium presents
  108. else if(status==Error_UsbConnect)
  109. {
  110. gxbErrCode=ERR_HOST_NO_DEVICE;
  111. return FALSE;
  112. }
  113. //no mudium for this lun
  114. gxbErrCode=ERR_HOST_DEVICE_NOT_READY;
  115. return FALSE;
  116. #else
  117. while(--i)
  118. {
  119. if(!UmsSendPIOCmd(CMD_TEST_UNIT_READY))
  120. {
  121. //Success
  122. return TRUE; //medium presents
  123. }
  124. HostDelay(0x6ff);
  125. }
  126. //no mudium for this lun
  127. gxbLastErrCode=ERR_HOST_NO_DEVICE;
  128. return FALSE;
  129. #endif
  130. }
  131. /*--------------------------------------------------------------------------
  132. Description:
  133. Determine Write Protect func.
  134. Send CMD_MODE_SENSE_6 command , and get data from device.
  135. Byte[2] bit 7 indicate WP status.
  136. Arguments:
  137. Global arguments:
  138. Returns: 
  139. TRUE: Write-Protect
  140. FALSE: Normal.
  141. 050801
  142. --------------------------------------------------------------------------*/
  143. API bit UsbHostIsWriteProtect(void) large
  144. {
  145. #if 1 //050106 //set 1 for lightne debug, it should set 0!! allen050815 not to check WP
  146. return FALSE;
  147. #else
  148. BYTE Status;
  149. if(Status = UmsSendPIOCmd(CMD_MODE_SENSE_6)) //protected or unsupported
  150. {
  151. if( Status == Error_Warning) //protected
  152. return TRUE;
  153. }
  154. else
  155. return FALSE;
  156. return TRUE; //impossible to arrive here
  157. #endif
  158. }
  159. //GPIOA[3] FLGJ
  160. //
  161. //
  162. bit HostOverCurrentDet(void) large
  163. {
  164. obGPIOADIR&=0xF7; //GPIOA[3] Input
  165. if(obGPIOADAT&0x08)
  166. return FALSE;
  167. else //over current
  168. {
  169. HostVbusOff();
  170. return TRUE;
  171. }
  172. }
  173. void GetHostIntFlag(void) //050713#0: use polling instead of interrupt 050714
  174. {
  175. g_USBHOSTINTFLAG1|=obUSBHOSTINTFLAG1;
  176. g_USBHOSTINTFLAG2|=obUSBHOSTINTFLAG2;
  177. }
  178. API bit UsbHostNormalCheck(void) large
  179. {
  180. if(HostOverCurrentDet()) // over current
  181. return FALSE;;
  182. if(!(obUSBHOSTPORTCTRL&HOST_PORT_CONNECT)) //disconnect interrupt 050713#1 add
  183. {
  184. return FALSE;
  185. }
  186. GetHostIntFlag(); //050714
  187. if(g_USBHOSTINTFLAG1 & USB_PORT_ERROR)  // port error interrupt 050714 move in normal check
  188. {
  189. return FALSE;//Error_UsbPort; 050714 ???
  190. }
  191. return TRUE;
  192. }
  193. #endif
  194. #if(_HOST_API_INIT_)
  195. //allen050705
  196. //GPIOA[1]: control MAX1797: 
  197. // HI---off---
  198. // LO---on--- 5V
  199. //GPIOA[2]: control RT9702
  200. // HI---on--- 5V
  201. // LO---off--- switch off
  202. void HostVbusOn(void) large //050708 050712
  203. {
  204. BYTE bTmp;
  205. obGPIOADIR|=0x06; //Force GPIOA[1,2]
  206. #if 1//050810
  207. obGPIOADAT|=0x02; //A1 off
  208. obGPIOADAT&=0xFB; //A2 off
  209. for(bTmp=0;bTmp<200;bTmp++)
  210. {
  211. obGPIOADAT|=0x02; //A1 off
  212. obGPIOADAT&=0xFD; //A1 on
  213. }
  214. HostDelay(0xFFF);
  215. for(bTmp=0;bTmp<200;bTmp++)
  216. {
  217. obGPIOADAT&=0xFB; //A2 off
  218. obGPIOADAT|=0x04; //A2 on
  219. }
  220. HostDelay(0xFFFF);
  221. #else
  222. for(bTmp=0;bTmp<100;bTmp++)
  223. {
  224. obGPIOADAT|=0x02; //A1 off
  225. obGPIOADAT&=0xFB; //A2 off
  226. obGPIOADAT&=0xFD; //A1 on
  227. obGPIOADAT|=0x04; //A2 on
  228. }
  229. #endif
  230. //allen050712: Wait a few time to let voltage stable.
  231. HostDelay(0xFFFF);
  232. HostDelay(0xFFFF);
  233. }
  234. bit HostWaitConnect(WORD wPeriod,BYTE bRetryCnt) large //allen050712 050720
  235. {
  236. WORD wCnt;
  237. WORD wConnect;
  238. BYTE bClkCtrlTmp=obCLKMCUCTRL;
  239. bit fRet=FALSE;
  240. // BYTE bKey;
  241. //#if(USE_STEP_UP_CONVERTER)
  242. McuClockCtrlAdjust(0xD0);// WHH #1 061017
  243. //#else
  244. // obCLKMCUCTRL=0xD0; //MCU clock use XTALI 12MHz //v03210#1
  245. //#endif
  246. gfCancel=FALSE;
  247. while(bRetryCnt)
  248. {
  249. #if(_PM_HST_) //050921
  250. if(gfCancel)
  251. break;
  252. /* KeyGet(bKey);
  253. if(bKey==KEY_HOST_CANCEL)
  254. break;*/
  255. #endif
  256. if(HostOverCurrentDet()) // over current
  257. {
  258. break;
  259. }
  260. if(bRetryCnt!=0xFF)
  261. bRetryCnt--;
  262. //0x4000 -> about 200ms
  263. for(wCnt=0,wConnect=0;wCnt<wPeriod;wCnt++)
  264. {
  265. if(obUSBHOSTPORTCTRL&HOST_PORT_CONNECT)
  266. wConnect++;
  267. }
  268. if(wPeriod==wConnect) //connect a period
  269. {
  270. fRet=TRUE; break;
  271. }
  272. }
  273. //#if(USE_STEP_UP_CONVERTER)
  274. McuClockCtrlAdjust(bClkCtrlTmp);// WHH #1 061017
  275. //#else
  276. // obCLKMCUCTRL=bClkCtrlTmp; //MCU clock use XTALI 12MHz //v03210#1
  277. //#endif
  278. return fRet;
  279. }
  280. API bit UsbHostPowerOn(void) large
  281. {
  282. HostVbusOn();
  283. if(!HostWaitConnect(0x4000,90))// 90->10sec 0xFF))//32)) //allen050712: about 5sec
  284. return FALSE;
  285. return TRUE;
  286. }
  287. #endif
  288. #if(_USE_FOR_COPY_ENGINE_)
  289. /*---------------------------------------------------------------
  290.  * Function_Name:
  291.  * int0
  292.  * Arguments:
  293.  *    none
  294.   * Return Value: 
  295.  * none
  296.  * Description:
  297.  *  interrupt0 service routine
  298.  * 
  299.  *--------------------------------------------------------------*/
  300. extern void DcvIsr(void);
  301. void ExtInt0 (void) interrupt 0 using 1 //050714: from UsbHost.c
  302. {
  303. BYTE bDcvTemp;
  304. g_SYSINT0FLAG = obSYSINT0FLAG;
  305. /* if(g_SYSINT0FLAG & USB_HST_INT1) 050713#0 use polling
  306. {
  307. g_USBHOSTINTFLAG1 |= obUSBHOSTINTFLAG1; //allen050712#2 |=
  308. }
  309. if(g_SYSINT0FLAG & USB_HST_INT2)
  310. {
  311. g_USBHOSTINTFLAG2 |= obUSBHOSTINTFLAG2; //allen050712#2 |=
  312. }*/
  313. //add by tne 2005/04/08
  314. if(g_SYSINT0FLAG & DCV_INT)
  315. {
  316.        bDcvTemp = obDCVARRD;
  317. DcvIsr();
  318.        obDCVARRD = bDcvTemp;
  319. }
  320. }
  321. #endif