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

📄 otgdev.c

📁 6410BSP3
💻 C
📖 第 1 页 / 共 2 页
字号:
        DOEPCTL[i] =  (DOEPCTL0 + (0x20*i));
        DOEPINT[i] =  (DOEPINT0 + (0x20*i)); 
        DOEPTSIZ[i] =  (DOEPTSIZ0 + (0x20*i));
        DOEPDMA[i] =  (DOEPDMA0 + (0x20*i)); 
    }

    // Configure EP0
    OUTREG32(DIEPINT0, 0xff);
    OUTREG32(DOEPINT0, 0xff);

    OUTREG32(DIEPTSIZ0, (1<<19)|oOtgDev.m_uControlEPMaxPktSize); // packet count = 1, xfr size
    OUTREG32(DOEPTSIZ0, (1<<29)|(1<<19)|oOtgDev.m_uControlEPMaxPktSize); // setup packet count = 1, packet count = 1, xfr size

    OUTREG32(DIEPCTL0, DEPCTL_EPENA |DEPCTL_CNAK |DEPCTL0_MPS_64);   //MPS:64bytes
    
    OUTREG32(DAINTMSK, ((1<<0)<<16) | (1<<0));
    OUTREG32(DOEPCTL0, DEPCTL_EPENA |DEPCTL_CNAK);     //ep0 enable, clear nak
    g_NumEPsUsed = 1;

    memset(EndPointsInfo, 0, sizeof(EndPointsInfo));
        
    // iterate all configurations and configure rest of the endpoints
    for (i=0;
        i < (UINT32)pDeviceDesc->pUsbDeviceDescriptor->bNumConfigurations;
        i++)
    {
        pConfigDesc = pDeviceDesc->ppUsbConfigDescriptors[i];
        
        //iterate all interfaces
        for (j=0;
            j < (UINT32)pConfigDesc->pUsbConfigDescriptor->bNumInterfaces;
            j++)
        {
            pUsbDbg_IntrfcDesc = pConfigDesc->ppUsbInterfaceDescriptors[j];

            // iterate all endpoints and save the endpoint data
            for (k=0;
                k < (UINT32)pUsbDbg_IntrfcDesc->pUsbInterfaceDescriptor->bNumEndpoints;
                k++)
            {
                pEndPtDesc = pUsbDbg_IntrfcDesc->ppUsbEndPtDescriptors[k];
                
                epNum = ENDPTNUM(pEndPtDesc->bEndpointAddress);
                if (epNum >= MAX_ENDPTS)
                {
                    USBDBGMSG(USBDBG_ZONE_ERROR, (L"usbpdd: InitEndPts EpNum (%d) > Max \r\n", epNum));
                }
                else
                {
                    EndPointsInfo[epNum].EndPtAddress = pEndPtDesc->bEndpointAddress;
                    EndPointsInfo[epNum].EndPtAttributes = pEndPtDesc->bmAttributes;
                    g_NumEPsUsed++;
                }
            }
        }
    }

    // Configure the endpoints using saved data
    OTGDevice_InitNonControlEndPts();
    
    return TRUE;
}


//////////
// Function Name : OTGDevice_InitNonControlEndPts
// Function Desctiption : This function configures the non-control endpoints 
//                      based on the data saved in OTGDevice_InitEndPts.
// Input : USBDBG_DEVICE_DESCRIPTOR* pDeviceDesc
// Output : NONE
//
BOOL OTGDevice_InitNonControlEndPts()
{
    UINT32 i;
    DWORD epNum, cfg;
    BYTE bEndPtAddress;
    BYTE bEndPtAttributes;
    
    // iterate all endpoints and initialize
    for (i=0; i < MAX_ENDPTS; i++)
    {
        if (EndPointsInfo[i].EndPtAddress != 0)
        {
            bEndPtAddress = EndPointsInfo[i].EndPtAddress;
            bEndPtAttributes = EndPointsInfo[i].EndPtAttributes;

            // Create EP config
            if (bEndPtAttributes == 2)
            {
                cfg  = DEPCTL_CNAK | DEPCTL_BULK_TYPE | DEPCTL_USBACTEP;
            }
            else if (bEndPtAttributes == 3)
            {
                cfg  = DEPCTL_CNAK | DEPCTL_INTR_TYPE | DEPCTL_USBACTEP;
            }

            // Get EP address
            epNum = ENDPTNUM(bEndPtAddress);

            // RX or TX?
            if (IS_ENDPT_RX(bEndPtAddress))
            {
                UINT32 Reg = DOEPCTL[epNum];

                cfg |= oOtgDev.m_uBulkOutEPMaxPktSize;
                OUTREG32(Reg,cfg);

                SETREG32(DAINTMSK,(1<<epNum)<<16);                    
                
                // set RX config (OUT Endpoint)
                OUTREG32(DOEPTSIZ[epNum],(1<<19)|oOtgDev.m_uBulkOutEPMaxPktSize);

                USBDBGMSG(USBDBG_ZONE_VERBOSE, (
                    L"usbpdd: InitEndPoints: setting EP-%d as OUT Ep, Attr = %d\r\n", 
                    epNum, bEndPtAttributes));
            }
            else
            {
                UINT32 Reg = DIEPCTL[epNum];
                
                cfg |= oOtgDev.m_uBulkInEPMaxPktSize;
                OUTREG32(Reg,cfg);    

                SETREG32(DAINTMSK,(1<<epNum));

                // set TX config (IN Endpoint)
                OUTREG32(DIEPTSIZ[epNum],(1<<19)|oOtgDev.m_uBulkInEPMaxPktSize);

                USBDBGMSG(USBDBG_ZONE_VERBOSE, (
                    L"usbpdd: InitEndPoints: setting EP-%d as IN Ep, Attr = %d\r\n", 
                    epNum, bEndPtAttributes));
            }
        }
    }
    
    return TRUE;
}



//////////
// Function Name : OTGDEV_SetOutEpXferSize
// Function Desctiption : This function sets DOEPTSIZn CSR according to input parameters.
// Input : eType, transfer type
//          uPktCnt, packet count to transfer
// Output : NONE
// Version :
void OTGDevice_SetOutEpXferSize(UINT32 epNum, UINT32 uPktCnt)
{
    if(epNum == 0)
    {
        OUTREG32(DOEPTSIZ0, (1<<29)|(uPktCnt<<19)|(oOtgDev.m_uControlEPMaxPktSize<<0));
    }
    else
    {
        OUTREG32(DOEPTSIZ[epNum], (uPktCnt<<19)|(oOtgDev.m_uBulkOutEPMaxPktSize<<0));
    }
}


//////////
// Function Name : OTGDevice_DeInit
// Function Desctiption : This function de-initializes OTG PHY and LINK.
// Input : NONE
// Output : NONE
// Version :
void OTGDevice_DeInit()
{
    DWORD epNum;

    // Clear USB Interrupt enable registers
    OUTREG32(GINTMSK, 0); 

    // Disable all RX, TX EPs
    if (INREG32(DIEPCTL0) & DEPCTL_EPENA)
        OUTREG32(DIEPCTL0, DEPCTL_EPDIS);

    if (INREG32(DOEPCTL0) & DEPCTL_EPENA)
        OUTREG32(DOEPCTL0, DEPCTL_EPDIS);
    
    for (epNum = 1; epNum < MAX_ENDPTS; epNum++)
    {
        if (INREG32(DIEPCTL[epNum]) & DEPCTL_EPENA)
            OUTREG32(DIEPCTL[epNum], DEPCTL_EPDIS);

        if (INREG32(DOEPCTL[epNum]) & DEPCTL_EPENA)
            OUTREG32(DOEPCTL[epNum], DEPCTL_EPDIS);
    }

    SETREG32(PCGCCTL, (1<<0));   //stop pclk
}


//////////
// Function Name : OTGDevice_HandleReset
// Function Desctiption : This function handles a USB RESET.
// Input : NONE
// Output : NONE
// Version :
void OTGDevice_HandleReset()
{
    OUTREG32(GAHBCFG, MODE_SLAVE|BURST_SINGLE|GBL_INT_UNMASK);
    
    OUTREG32(GINTMSK, OTGINTMASK); // Global Interrupt Mask
    OUTREG32(DOEPMSK, CTRL_OUT_EP_SETUP_PHASE_DONE|AHB_ERROR|TRANSFER_DONE); // Out EP Int Mask
    OUTREG32(DIEPMSK, INTKN_TXFEMP|NON_ISO_IN_EP_TIMEOUT|AHB_ERROR|TRANSFER_DONE); // In EP Int Mask


    OUTREG32(GRXFSIZ, RX_FIFO_SIZE);      // Rx FIFO Size
    OUTREG32(GNPTXFSIZ, NPTX_FIFO_SIZE<<16| NPTX_FIFO_START_ADDR<<0); // Non Periodic Tx FIFO Size
    OUTREG32(GRSTCTL, (1<<5)|(1<<4));     // TX and RX FIFO Flush
    
    CLRREG32(PCGCCTL, (1<<0));   //start PHY clock
}


//////////
// Function Name : OTGDevice_HandleEnumDone
// Function Desctiption : This function handles the enumeration done interrupt from the core
// Input : NONE
// Output : NONE
// Version :
void OTGDevice_HandleEnumDone()
{
    oOtgDev.m_eSpeed = (INREG32(DSTS) & 0x6) >> 1;
    if (oOtgDev.m_eSpeed == USB_HIGH)
    {
        USBDBGMSG(USBDBG_ZONE_VERBOSE, (L"usbpdd: Enumerated at High Speed\r\n"));
        oOtgDev.m_uControlEPMaxPktSize = HIGH_SPEED_CONTROL_PKT_SIZE;
        oOtgDev.m_uBulkInEPMaxPktSize = HIGH_SPEED_BULK_PKT_SIZE;
        oOtgDev.m_uBulkOutEPMaxPktSize = HIGH_SPEED_BULK_PKT_SIZE;  
    }
    else if (oOtgDev.m_eSpeed == USB_FULL)
    {
        USBDBGMSG(USBDBG_ZONE_VERBOSE, (L"usbpdd: Enumerated at Full Speed\r\n"));
        oOtgDev.m_uControlEPMaxPktSize = FULL_SPEED_CONTROL_PKT_SIZE;
        oOtgDev.m_uBulkInEPMaxPktSize = FULL_SPEED_BULK_PKT_SIZE;
        oOtgDev.m_uBulkOutEPMaxPktSize = FULL_SPEED_BULK_PKT_SIZE;  
    }
    else
    {
        USBDBGMSG(USBDBG_ZONE_WARN, (L"usbpdd: Enumerated Speed not high or full. Speed = %d\r\n", oOtgDev.m_eSpeed));
    }

    //Re-init non control end points
    OTGDevice_InitNonControlEndPts();
}


⌨️ 快捷键说明

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