📄 otgcontroller.c
字号:
///////////////////////////////////////////////////////////////////////////////
//
// File name: OTGController.c
// Version: 1.0
// Date: 2005/1/31
//
// Author: Bruce
// Company: Faraday Tech. Corp.
//
// Description:
// This function will
///////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "Pe_usb.h"
#include "chipset.h"
#include "flib.h"
#include "fa510.h"
#include "FOTG200_peripheral.h"
#include "OTGController.h"
#include "Lib_Host20.h"
#include "GPIO.h"
OTGC_STS OTG;
UINT8 gWaitingSRPCounter;
UINT8 bRoleChangeAutoTestEnable=0;
UINT32 wRoleChangeAutoTestCounter=0;
UINT32 wRoleChangeAutoTestCounter_Input=10;
UINT8 bOPT_Device_A_Auto=0;
//*****************************************************************************
//*****************************************************************************
//*********************** Group-1:Main Function *****************************
//*****************************************************************************
//*****************************************************************************
//============================================================================= ok
// OTGC_Read_Current_ID()
// Description:
// input: Reserved
// output: Reserved
//=============================================================================
int OTGC_Read_Current_ID(void)
{
// 0:A-Device 1:B-Device
return(mdwOTGC_Control_ID_Rd());
}
//============================================================================= ok
// main()
// Description:Function entry
// input: Reserved
// output: Reserved
//=============================================================================
int OTG_200_main(int argc, int *argv[])
{
UINT32 *pwData;
//john : remove force speed
//bForceSpeed=0;
printf("*** OTG Ready **************************** \n");
//<1>.Disable the D-Cache
CPUCleanInvalidateDCacheAll();
fLib_DisableDCache();
printf("*** OTG Ready-1 **************************** \n");
//<2>.Init the Basic Initial
mwPeri20_Control_ChipEnable_Set();
//Bruce;;06222005;; mwPeri20_Control_HALFSPEEDEnable_Set();
//mwOTG20_Interrupt_OutPut_High_Set();//For A320
// OTGC_A_PHY_Reset();
//Change the AHB Master Priority to High Level
pwData=0x90100080;
*pwData=0X200;
// mdwOTGC_Control_VBUS_FLT_SEL_Set();//For peter
printf("*** Full Speed Phy Reset Fail Work around solution => Enable 0x80 Bit28\n");
mdwOTGC_Control_711MA_Phy_Issue_Set();
printf("*** OTG Ready-2 **************************** \n");
#ifdef OTGD_CARD_READER_Test
vUsb_AP_PowerOn();
#endif
//<2>.OTGC Init
OTGC_Init(); //Init the Interrupt
//<3>.OTG other Init
OTG_Init();
do {
if (mdwOTGC_Control_ID_Rd()==OTG_ID_A_TYPE) //For Type A
OTGC_A_Menu();
else //For Type B
OTGC_B_Menu();
}while(1);
}
//=============================================================================ok
// OTG_Init()
// Description:Connect interrupt to the interrupt flib interrupt controller
// function
// input: none
// output: none
//=============================================================================
void OTG_Init(void)
{
//<1>.Init Interrupt
fLib_CloseInt(FIQ_OTG);//(FIQ_MAC); //40
//fLib_SetIntTrig(FIQ_OTG,LEVEL,L_ACTIVE);//For CPE =>
fLib_SetIntTrig(FIQ_OTG,LEVEL,H_ACTIVE);
fLib_ConnectInt(FIQ_OTG, OTG_INT_ISR);
fLib_EnableInt(FIQ_OTG);
//<2>.Init Variable
OTG.bVBUSAlwaysOn=0;
}
//=============================================================================ok
// OTG_INT_ISR()
// Description:Interrupt service routine entry
// OTG_INT_ISR() will call function
// ---> For OTG Controoler ---> OTGC_INT_ISR()
// ---> For OTG Host ---> OTGH_INT_ISR() / flib_OTGH_ISR()
// ---> For OTG Peripheral ---> Save the interrupt status
// then return.
// input: none
// output: none
//=============================================================================
void OTG_INT_ISR(void)
{
UINT32 wINTStatus;
fLib_DisableInt(FIQ_OTG); //Disable Interrupt
wINTStatus = mdwOTGC_INT_STS_Rd();
//<1>.Checking the OTG layer interrupt status
if (wINTStatus>0) {
mdwOTGC_INT_STS_Clr(wINTStatus);
if ((OTG.wCurrentInterruptMask & wINTStatus)>0)
OTGC_INT_ISR(wINTStatus);
mdwOTGC_INT_STS_Clr(wINTStatus);
}
if (OTG.CurrentRole==OTG_CurrentRole_Host) { //<2>.For Host ISR
if (mwHost20_USBSTS_Rd()>0)
flib_Host20_ISR(); //For Standard Host test AP ISR
}
else { //<3>.For Peripheral ISR
if (mUsbGlobIntEnRd()>0) { //==>For peter
OTG_interrupt_level1_Save = mUsbIntGroupRegRd();
OTG_interrupt_level1_Mask = mUsbIntGroupMaskRd();
OTG_interrupt_level1 = OTG_interrupt_level1_Save & ~OTG_interrupt_level1_Mask;
if (OTG_interrupt_level1 != 0) {
return ;//Do not Enable INT
}
}//==>For peter
}
//04192005;;Bruce;;fLib_EnableInt(IRQ_UNUSED29); //Enable Interrupt
fLib_EnableInt(FIQ_OTG); //Enable Interrupt
}
//=============================================================================ok
// OTG_RoleChange()
// Description:This function will take care the event about role change.
// It will close/init some function.
//
// input:(INT8U)bRole
// 0 => Change to Host
// 1 => Change to Peripheral
// output: none
//=============================================================================
void OTG_RoleChange(INT8U bRole)
{
UINT8 wTemp;
if (bRole==0) {//Change to Host
//Bruce;;12162004;;unplug issue;;//mUsbUnPLGClr();
if (mdwOTGC_Control_ID_Rd()==0) {//Device-A: change to Host
mdwOTGC_Control_A_BUS_DROP_Clr(); //Exit when Current Role = Host
mdwOTGC_Control_A_BUS_REQ_Set();
}
else {//Device-B: Change to Host
mdwOTGC_Control_B_HNP_EN_Clr();
mdwOTGC_Control_B_DSCHG_VBUS_Clr();
}
OTGP_Close();
}
else {//Change to Peripheral
//Bruce;;12162004;;unplug issue;;//mUsbUnPLGClr();
if (mdwOTGC_Control_ID_Rd()==0) {//Device-A: change to Peripheral
mdwOTGC_Control_A_SET_B_HNP_EN_Clr();
}
else {//Device-B: Change to Peripheral
mdwOTGC_Control_B_BUS_REQ_Clr();
}
//flib_Host20_Close(0);
mwHost20_USBINTR_Set(0);
wTemp=mwHost20_USBSTS_Rd();
wTemp=wTemp&0x0000003F;
mwHost20_USBSTS_Set(wTemp);
}
}
//*****************************************************************************
//*****************************************************************************
//*********************** Group-2:OTGC Controller ****************************
//*****************************************************************************
//*****************************************************************************
//============================================================================= ok
// OTGC_VBS_Valid()
// Description:To check the VBUS
// input: none
// output: 0:OFF
// 1:ON
//=============================================================================
UINT8 OTGC_VBS_Valid(void)
{
//<1>.Read-1
if (mdwOTGC_Control_A_VBUS_VLD_Rd()==0)
return (0);
//<2>.Waiting for the 50 ms
flib_Host20_TimerEnable(10);
//<3>.Read-2
if (mdwOTGC_Control_A_VBUS_VLD_Rd()==0)
return (0);
else
return (1);
}
//============================================================================= ok
// OTGC_Init()
// Description:1.Init the OTG Structure Variable
// 2.Init the Interrupt register(OTG-Controller layer)
// 3.Call the OTG_RoleChange function to init the Host/Peripheral
// input: none
// output: none
//=============================================================================
void OTGC_Init(void)
{
UINT32 dwTemp;
//Clear the interrupt status
dwTemp=mdwOTGC_INT_STS_Rd();
mdwOTGC_INT_STS_Clr(dwTemp);
//<1>.Read the ID
if (mdwOTGC_Control_ID_Rd()>0) {//Change to B Type
//<1.1>.Init Variable
OTG.A_bASRPDET=0;
OTG.B_bBSRPDN=0;
OTG.CurrentRole=1;
OTG.wCurrentInterruptMask=OTGC_INT_B_TYPE;
//<1.2>.Init Interrupt
mdwOTGC_INT_Enable_Clr(OTGC_INT_A_TYPE);
mdwOTGC_INT_Enable_Set(OTGC_INT_B_TYPE);
//<1.3>.Init the Host/Peripheral
OTG_RoleChange(1);
}
else {//Changfe to A Type
//<2.1>.Init Variable
OTG.A_bASRPDET=0;
OTG.B_bBSRPDN=0;
OTG.CurrentRole=0;
OTG.wCurrentInterruptMask=OTGC_INT_A_TYPE;
//<2.2>. Init Interrupt
mdwOTGC_INT_Enable_Clr(OTGC_INT_B_TYPE);
mdwOTGC_INT_Enable_Set(OTGC_INT_A_TYPE);
//<2.3>.Init the Host/Peripheral
OTG_RoleChange(0);
}
}
//============================================================================= ok
// OTGC_INT_ISR()
// Description:This interrupt service routine belongs to the OTG-Controller
// layer.It will take care:
// 1.ID/Role Change 2.Error detect
// 3.Cable Remove 4.SRP detect(A) 5.SRP done(B)
//
// input: (UINT32) wINTStatus
// This is interrupt status value.
// output: none
//=============================================================================
void OTGC_INT_ISR(UINT32 wINTStatus)
{
//<1>.Check for ID_Change
if (wINTStatus&OTGC_INT_IDCHG) {
//printf(">>> OTGC INT STS => OTGC_INT_IDCHG(Device ID Change) \n");
OTG.IDCHG=1;
if (mdwOTGC_Control_ID_Rd()>0) {//Change to B Type
OTGC_Init();
}
else {//Changfe to A Type
OTGC_Init();
}
}
else {//else of " if (wINTStatus&OTGC_INT_IDCHG) "
//<2>.Check for RL_Change
if (wINTStatus&OTGC_INT_RLCHG) {
//if (OTG.CurrentRole==0)
if (mdwOTGC_Control_CROLE_Rd()>0) {//Change to Peripheral
OTG.CurrentRole=1;
OTG_RoleChange(1);
}
else {//Change to Host
if (mdwOTGC_Control_ID_Rd()>0) { //For Peripheral
//John : should not reset controller
////<1>.Issue the Bus reset
//mbHost20_USBCMD_HCReset_Set(); //04192004;;Remove
flib_Host20_PortBusReset();
}
OTG.CurrentRole=0;
OTG_RoleChange(0);
}
}
//<3>.Error Detect
if (wINTStatus&OTGC_INT_AVBUSERR) {
printf("??? Error:Interrupt OTGC_INT_AVBUSERR=1... \n");
//while(1);
}
if (wINTStatus&OTGC_INT_OVC) {
printf("??? Error:Interrupt OTGC_INT_OVC=1... \n");
// while(1);
}
//<3>.Check for Type-A/Type-B Interrupt
if (mdwOTGC_Control_ID_Rd()==OTG_ID_A_TYPE) {//For Type-A Interrupt
if (wINTStatus&OTGC_INT_A_TYPE) {
if (wINTStatus&OTGC_INT_ASRPDET) {//SRP detected => then set global variable
OTG.A_bASRPDET=1;
}
if (wINTStatus&OTGC_INT_BPLGRMV) {//Remove the B type cable => Reinit the A type
OTG.A_BPLGRMV=1;
if (OTG.bVBUSAlwaysOn==0)
OTGC_A_Bus_Drop();
}
if (wINTStatus&OTGC_INT_APLGRMV) {
//This interrupt will also generate the ID Change interrupt, so do not do anything
OTG.A_APLGRMV=1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -