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

📄 otgdriver.c

📁 smartARM2400 USB OTG例程
💻 C
📖 第 1 页 / 共 3 页
字号:
}

/*********************************************************************************************************
** Function name:       __otgHNPFailure
** Descriptions:        HNP失败处理
** input parameters:    None
** output parameters:   None
** Returned value:      None
*********************************************************************************************************/
void __otgHNPFailure (void)
{
    if (otgGetCurRole() == OTG_ROLE_DEVICE) {                           /*  从机                        */
        __isp1301DPPURAdd();                                            /*  add D+ pull-up              */
        OTGStCtrl &= ~0x01;                                             /*  U1 Device(OTG)              */
    } else {                                                            /*  主机                        */
        __isp1301ClrBDIS_ACON_EN();                                     /*  clear BDIS_ACON_EN          */
        OTGStCtrl |= 0x01;                                              /*  U1 HOST(OTG)                */
    }
}

/*********************************************************************************************************
** Function name:       __otgHNPTmrOut
** Descriptions:        HNP超时处理
** input parameters:    None
** output parameters:   None
** Returned value:      None
*********************************************************************************************************/
void __otgHNPTmrOut (void)
{
    if (otgGetCurRole() == OTG_ROLE_HOST) {                             /*  主机(仅主机会出现超时)      */
        __isp1301ClrBDIS_ACON_EN();                                     /*  clear BDIS_ACON_EN          */
        __usbVbusDisChrg();                                             /*  释放总线电压                */
    }
}

/*********************************************************************************************************
** Function name:       __GetDefaultRole
** Descriptions:        根据ID引脚状态获取默认的角色(A设备或B设备),此函数在OTG初始化时调用
** input parameters:    None
** output parameters:   None
** Returned value:      角色值, OTG_DEVICE_A 或 OTG_DEVICE_B
*********************************************************************************************************/
USB_INT8U __GetDefaultRole (void)
{
    USB_INT8U ucTmp;
    
    ucTmp = __isp1301IntSrcRead();                                      /*  读取ISP1301中断源寄存器     */
    if (ucTmp & (1 << 3)) {                     
        return OTG_DEVICE_A;                                            /*  ID = GND, A设备,默认为主机  */
    } else {
        return OTG_DEVICE_B;                                            /*  ID = FLOAT, B设备,默认为从机*/
    }
}    
    
/*********************************************************************************************************
** Function name:       __usbOtgException
** Descriptions:        OTG中断服务程序
** input parameters:    None
** output parameters:   None
** Returned value:      None
*********************************************************************************************************/
void __usbOtgException (void)
{
    USB_INT32U uiIntSt;
    
    uiIntSt = (USB_INT32U)OTGIntSt;
    if (uiIntSt & 0x01) {                                               /*  time out interrupt          */
        OTGStCtrl &= ~(1 << 5);                                         /*  clear TMR_EN                */
        OTGIntClr  = 1 << 0;                                            /*  clear TMR interrupt flags   */
        OSQPost(__GevtOtgMsgQeue, (void *)(__OTG_MSG_HNP_TMR << 24));
    } 
    if (uiIntSt & (1 << 1)) {                                           /*  REMOVE_PU interrupt         */
        OTGIntClr = 1 << 1;                                             /*  clear TMR interrupt flags   */
        OSQPost(__GevtOtgMsgQeue, (void *)(__OTG_MSG_REMOVE_PU << 24));
    } 
    if (uiIntSt & (1 << 2)) {                                           /*  HNP_FAILURE interrupt       */
        OTGStCtrl &= ~(1 << 5);                                         /*  clear TMR_EN                */
        OTGIntClr  = 1 << 2;                                            /*  clear TMR interrupt flags   */
        OSQPost(__GevtOtgMsgQeue, (void *)(__OTG_MSG_HNP_FAILED << 24));
    } 
    if (uiIntSt & (1 << 3)) {                                           /*  HNP_SUCCESS interrupt       */
        OTGStCtrl &= ~(1 << 5);                                         /*  clear TMR_EN                */
        OTGIntClr  = 1 << 3;                                            /*  clear TMR interrupt flags   */
        OSQPost(__GevtOtgMsgQeue, (void *)(__OTG_MSG_HNP_SUCESS << 24));
        USBDEBUG_SENDSTR("\r\nHNP_SUCCESS intr\r\n");
    }
}

/*********************************************************************************************************
** Function name:       __usbAtxException
** Descriptions:        外部收发器ISP1301中断服务程序
** input parameters:    None
** output parameters:   None
** Returned value:      None
*********************************************************************************************************/
void __usbAtxException (void)
{
    __isp1301IntDisHigh(0xff);                                          /*  禁止所有0-->1跳变中断,在处理*/
                                                                        /*  完中断后会重新使能中断      */
    __isp1301IntClrAll();
    OSQPost(__GevtOtgMsgQeue, (void *)(__OTG_MSG_ATX_INT << 24));
    USBINTDEBUG_SENDSTR("\r\nATX intr\r\n");
}

/*********************************************************************************************************
** Function name:       __otgHNPStart
** Descriptions:        启动主机交换协议HNP
** input parameters:    None
** output parameters:   None
** Returned value:      TRUE : 成功  FALSE : 失败
*********************************************************************************************************/
USB_BOOL __otgHNPStart (void)
{
    USB_INT8U ucErr;
    
    __otgClkInit();                                                     /*  配置 OTG CLK                */
    __otgIntInit();                                                     /*  配置 OTG INT                */
    
    if (__GsOtgInfo.ucOTGRole == OTG_ROLE_HOST) {                       /*  当前角色为主机              */
        __ohciDisEnInt(__USB_INT_RHSC);                                 /*  禁止中断连接改变中断        */
        ucErr = usbSetFeature_b_hnp_enable();                           /*  发送Set Feature(b_hnp_enable)*/
        if (ucErr != USB_ERR_SUCESS) {                                  /*  以启动主机交换协议 HNP      */
            USBDEBUG_SENDSTR("\r\nusbSetFeature_b_hnp_enable failed\r\n");
            return FALSE;
        }
        
        OTGStCtrl &= ~((1 << 5) | (0xffffUL << 16));
        OTGStCtrl &= 0x0000ffff;
        
        OTGStCtrl |= (1 << 9) | (1 << 6);                               /*  置位 A_HNP_TRACK 和 TMR_RST */
        __isp1301Write(__MODE_CTL1_SET, __MODE_CTL1_BDIS_ACON_EN);      /*  置位ISP1301的BDIS_ACON_EN   */
        OTGStCtrl = (OTGStCtrl & ~((0x03 << 2) | (1 << 4))) | (0x02 << 2);/*  Timer mode: monoshot      */
                                                                        /*  Time Scale: 1000 us         */
        OTGTmr     = 200;                                               /*  超时时间:200ms              */
        OTGStCtrl |= 1 << 5;                                            /*  set TMR_EN,启动定时器       */
        usbBusSusp();                                                   /*  挂起总线                    */
    } else {                                                            /*  当前角色为从机              */
        OTGStCtrl |= 1 << 8;                                            /*  set b_hnp_track             */
    }
    return TRUE;
}

/*********************************************************************************************************
** Function name:       __otgBusSuspDeal
** Descriptions:        A设备在从机状态时,总线挂起处理
** input parameters:    None
** output parameters:   None
** Returned value:      TRUE : 成功  FALSE : 失败
*********************************************************************************************************/
void __otgBusSuspDeal (void)
{
    USBDEBUG_SENDSTR("\r\ndo __otgBusSuspDeal\r\n");
    __otgChangeRoleToHost();
}

/*********************************************************************************************************
** Function name:       usbExternBusSusp
** Descriptions:        挂起总线的处理,由从机协议栈中总线挂起中断里调用
** input parameters:    None
** output parameters:   None
** Returned value:      None
*********************************************************************************************************/
void usbExternBusSusp (void)
{
    if ((otgGetDeviceAB() == OTG_DEVICE_A) && 
        (otgGetCurRole() == OTG_ROLE_DEVICE)) {
        if (otgIsDeviceReady()) {
            USBDEBUG_SENDSTR("\r\nBusSusp:OSQPost __GevtOtgMsgQeue");
            OSQPost(__GevtOtgMsgQeue, (void *)(__OTG_MSG_SHED_BUS_SUSP << 24));
            OS_ENTER_CRITICAL();
            USBDevIntClr |= 1 << 3;                                     /*  禁止同步传输帧中断          */
            USBDevIntEn &= ~(1 << 3);                                   /*  使能状态中断, 低速中断      */
            OS_EXIT_CRITICAL();
        }
    }
}

/*********************************************************************************************************
** Function name:       __usbVbusDrv
** Descriptions:        驱动VBUS,给总线供电
** input parameters:    None
** output parameters:   None
** Returned value:      
*********************************************************************************************************/
void __usbVbusDrv (void)
{
    __isp1301EnablePswOE();
}

/*********************************************************************************************************
** Function name:       __usbVbusDisChrg
** Descriptions:        释放总线电压
** input parameters:    None
** output parameters:   None
** Returned value:      
*********************************************************************************************************/
void __usbVbusDisChrg (void)
{
    __isp1301DisEnPswOE();
    __isp1301SetVbusDisCharge();                                        /*  discharge VBUS              */
}

/*********************************************************************************************************
** Function name:       usbHostClientInit
** Descriptions:        USB主机设备驱动程序初始化,此函数的实现视用户主机设备驱动程序而定
** input parameters:    None
** output parameters:   None
** Returned value:      TRUE : 成功  FALSE : 失败
*********************************************************************************************************/
USB_BOOL usbHostClientInit (void)
{
    USB_INT8U ucErr;
    
    ucErr = msHostInit(0);                                              /*  大容量类主机初始化          */
    if (ucErr != MS_ERR_SUCESS) {
        USBDEBUG_SENDSTR("\r\nmsHostInit failed!\r\n__otgChangeRoleToHost failed!\r\n");
        return FALSE;
    }
    return TRUE;
}

/*********************************************************************************************************
** Function name:       usbHostClientDeInit
** Descriptions:        USB主机设备驱动程序卸载,此函数的实现视用户主机设备驱动程序而定
** input parameters:    None
** output parameters:   None
** Returned value:      TRUE : 成功  FALSE : 失败
*********************************************************************************************************/
USB_BOOL usbHostClientDeInit (void)
{
    return msHostDeInit();
}

/*********************************************************************************************************
** Function name:       usbFunctionDeInit
** Descriptions:        初始化从机设备驱动程序,此函数的实现视用户从机设备驱动程序而定
** input parameters:    None
** output parameters:   None
** Returned value:      TRUE : 成功  FALSE : 失败
*********************************************************************************************************/
USB_BOOL usbFunctionInit (void)
{
    massDevInit(__GsOtgInfo.ucHostEnumPrio);                            /*  从机大容量类设备驱动卸载    */
    return TRUE;
}

/*********************************************************************************************************
** Function name:       usbFunctionDeInit
** Descriptions:        卸载从机设备驱动程序,此函数的实现视用户从机设备驱动程序而定
** input parameters:    None
** output parameters:   None
** Returned value:      TRUE : 成功  FALSE : 失败
*********************************************************************************************************/
USB_BOOL usbFunctionDeInit (void)
{
    massDevDeInit();                                                    /*  大容量类设备从机卸载        */
    return TRUE;
}

/*********************************************************************************************************
  END FILE
*********************************************************************************************************/

⌨️ 快捷键说明

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