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

📄 usb.c

📁 6410BSP3
💻 C
📖 第 1 页 / 共 5 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//

#include <windows.h>
#include <halether.h>
#include <bsp.h>
#include "loader.h"
#include "usb.h"

// start // from otg_dev.c

static void VIC_InterruptEnable(UINT32 intNum);
static void VIC_InterruptDisable(UINT32 intNum);
static void VIC_ClearVectAddr(void);
static void VIC_InitializeVectTable(void);
static void delayLoop(int count);


//=====================================================================
// setting the device qualifier descriptor and a string descriptor
const UINT8 aDeviceQualifierDescriptor[] =
{
    0x0a,                   //  0 desc size
    0x06,                   //  1 desc type (DEVICE)
    0x00,                   //  2 USB release
    0x02,                   //  3 => 2.00
    0x00,                   //  4 class
    0x00,                   //  5 subclass
    0x00,                   //  6 protocol
    64,                      //  7 max pack size
    0x01,                   //  8 number of other-speed configuration
    0x00,                   //  9 reserved
};

const UINT8 aDescStr0[]=
{
    4, STRING_DESCRIPTOR, LANGID_US_L, LANGID_US_H, // codes representing languages
};

const UINT8 aDescStr1[]= // Manufacturer
{
    (0x14+2), STRING_DESCRIPTOR,
    'S', 0x0, 'y', 0x0, 's', 0x0, 't', 0x0, 'e', 0x0, 'm', 0x0, ' ', 0x0, 'M', 0x0,
    'C', 0x0, 'U', 0x0,
};

const UINT8 aDescStr2[]= // Product
{
    (0x2a+2), STRING_DESCRIPTOR,
    'S', 0x0, 'E', 0x0, 'C', 0x0, ' ', 0x0, 'S', 0x0, '3', 0x0, 'C', 0x0, '6', 0x0,
    '4', 0x0, '1', 0x0, '0', 0x0, 'X', 0x0, ' ', 0x0, 'T', 0x0, 'e', 0x0, 's', 0x0,
    't', 0x0, ' ', 0x0, 'B', 0x0, '/', 0x0, 'D', 0x0
};


//=====================================================================
// global varibles used in several functions
OTGDEV oOtgDev;
USB_GET_STATUS oStatusGet;
USB_INTERFACE_GET oInterfaceGet;

UINT16 g_usConfig;
bool g_bTransferEp0 = false;

// end // from otg_dev.c


//static UINT32                    g_uDownloadFileSize;
//static UINT32                      g_uDownloadAddress = DMABUFFER;
volatile UINT32                 readPtIndex;
volatile UINT8                    *g_pDownPt;

//volatile S3C6410_USBOTG_LINK_REG *pUSBOTG_LINK;
//volatile S3C6410_USBOTG_PHY_REG *pUSBOTG_PHY;

volatile S3C6410_VIC_REG *s6410VIC0;
volatile S3C6410_VIC_REG *s6410VIC1;


void Isr_Init(void);
void C_IsrHandler(unsigned int val);
extern void ASM_IsrHandler(void);
void SetEndpoint(void);


// start // from otg_dev.c


//////////
// Function Name : OTGDEV_InitOtg
// Function Desctiption : This function initializes OTG PHY and LINK.
// Input : eSpeed, USB Speed (high or full)
// Output : NONE
// Version :
void OTGDEV_InitOtg(USB_SPEED eSpeed)
{
    UINT8 ucMode;
    volatile UINT32 uTemp;

    uTemp = Inp32SYSC(0x900);
    Outp32SYSC(0x900, uTemp|(1<<16)); // unmask usb signal

    oOtgDev.m_eSpeed = eSpeed;
    oOtgDev.m_uIsUsbOtgSetConfiguration = 0;
    oOtgDev.m_uEp0State = EP0_STATE_INIT;
    oOtgDev.m_uEp0SubState = 0;
    OTGDEV_InitPhyCon();
    OTGDEV_SoftResetCore();
    OTGDEV_InitCore();
    OTGDEV_CheckCurrentMode(&ucMode);
    if (ucMode == INT_DEV_MODE)
    {
        delayLoop(10000);
        OTGDEV_ClearSoftDisconnect();
        OTGDEV_InitDevice();
    }
    else
    {
        EdbgOutputDebugString("Error : Current Mode is Host\n");
        return;
    }
    oOtgDev.m_eSpeed = USB_HIGH;
    oOtgDev.m_uControlEPMaxPktSize = HIGH_SPEED_CONTROL_PKT_SIZE;
    oOtgDev.m_uBulkInEPMaxPktSize = HIGH_SPEED_BULK_PKT_SIZE;
    oOtgDev.m_uBulkOutEPMaxPktSize = HIGH_SPEED_BULK_PKT_SIZE;

}



//////////
// Function Name : OTGDEV_HandleEvent
// Function Desctiption : This function handles various OTG interrupts of Device mode.
// Input : NONE
// Output : NONE
// Version :
void OTGDEV_HandleEvent(void)
{
    UINT32 uGIntStatus, uDStatus;
    UINT32 ep_int_status, ep_int;
    UINT32 uPcgctl;
    UINT32 uGrstctl;

    Inp32(GINTSTS, uGIntStatus); // System status read
    Outp32(GINTSTS, uGIntStatus); // Interrupt Clear

    if (uGIntStatus & INT_RESET) // Reset interrupt
    {
        OTGDEV_SetAllOutEpNak();
        oOtgDev.m_uEp0State = EP0_STATE_INIT;
        Outp32(DAINTMSK,((1<<BULK_OUT_EP)|(1<<CONTROL_EP))<<16|((1<<BULK_IN_EP)|(1<<CONTROL_EP)));
        Outp32(DOEPMSK, CTRL_OUT_EP_SETUP_PHASE_DONE|AHB_ERROR|TRANSFER_DONE);
        Outp32(DIEPMSK, INTKN_TXFEMP|NON_ISO_IN_EP_TIMEOUT|AHB_ERROR|TRANSFER_DONE);
        Outp32(GRXFSIZ, RX_FIFO_SIZE);                    // Rx FIFO Size
        Outp32(GNPTXFSIZ, NPTX_FIFO_SIZE<<16| NPTX_FIFO_START_ADDR<<0);    // Non Periodic Tx FIFO Size
        Outp32(GRSTCTL, (1<<5)|(1<<4));      // TX and RX FIFO Flush
        OTGDEV_SetOpMode(USB_CPU);

        OTGDEV_ClearAllOutEpNak();
        Inp32(PCGCCTL,uPcgctl);
        Outp32(PCGCCTL, uPcgctl&~(1<<0));    //start pclk

        DBGUSB("\n [USB_Diag_Log]  : Reset Mode\n");
    }

    if (uGIntStatus & INT_ENUMDONE) // Device Speed Detection interrupt
    {
        DBGUSB("\n [USB_Diag_Log]  : Speed Detection interrupt \n");

        Inp32(DSTS, uDStatus); // System status read

        if (((uDStatus&0x6) >>1) == USB_HIGH)             // Set if Device is High speed or Full speed
        {
            DBGUSB("\n [USB_Diag_Log]  : High Speed Detection\n");
            OTGDEV_SetMaxPktSizes(USB_HIGH);
        }
        else if(((uDStatus&0x6) >>1) == USB_FULL)
        {
            DBGUSB("\n [USB_Diag_Log]  : full Speed Detection\n");
            OTGDEV_SetMaxPktSizes(USB_FULL);
        }
        else
        {
            DBGUSB("\n [USB_Diag_Log]  : No specific speed option\n"); //Assert(0);
            while(1);
        }

        OTGDEV_SetEndpoint();
        OTGDEV_SetDescriptorTable();
    }

    if (uGIntStatus & INT_RESUME)
    {
        Inp32(PCGCCTL,uPcgctl);
        Outp32(PCGCCTL, uPcgctl &~ (1<<0));    //start pclk
        DBGUSB("\n [USB_Diag_Log]  : Resume Mode \n");
    }

    if (uGIntStatus & INT_SUSPEND)
    {
        Inp32(PCGCCTL,uPcgctl);
        Outp32(PCGCCTL, uPcgctl |(1<<0));    //stop pclk
        DBGUSB("\n [USB_Diag_Log]  : Suspend Mode\n");
    }

    if(uGIntStatus & INT_RX_FIFO_NOT_EMPTY)
    {
        UINT32 GrxStatus;
        UINT32 fifoCntByte;

        Outp32(GINTMSK,  INT_RESUME|INT_OUT_EP|INT_IN_EP|INT_ENUMDONE|INT_RESET|INT_SUSPEND);

        Inp32(GRXSTSP, GrxStatus);

        if ((GrxStatus & SETUP_PKT_RECEIVED) == SETUP_PKT_RECEIVED)
        {
            DBGUSB("SETUP_PACKET_RECEIVED\n");
            OTGDEV_HandleEvent_EP0();
            Inp32(GRSTCTL,uGrstctl);
            Outp32(GRSTCTL, uGrstctl &~ (0x1f<<6)|(0x1<<5));    //tx fifo flush
            g_bTransferEp0 = true;
        }
        else if ((GrxStatus & OUT_PKT_RECEIVED) == OUT_PKT_RECEIVED)
        {

            fifoCntByte = (GrxStatus & 0x7ff0)>>4;
            if(((GrxStatus & 0xF)==BULK_OUT_EP)&&(fifoCntByte))
            {
                DBGUSB("Bulk Out : OUT_PKT_RECEIVED\n");
                OTGDEV_HandleEvent_BulkOut(fifoCntByte);
                if( oOtgDev.m_eOpMode == USB_CPU )
                    Outp32(GINTMSK, INT_RESUME|INT_OUT_EP|INT_IN_EP|INT_ENUMDONE|INT_RESET|INT_SUSPEND|INT_RX_FIFO_NOT_EMPTY);
                return;
            }
        }
        Outp32(GINTMSK, INT_RESUME|INT_OUT_EP|INT_IN_EP|INT_ENUMDONE|INT_RESET
            |INT_SUSPEND|INT_RX_FIFO_NOT_EMPTY); //gint unmask
    }

    if ((uGIntStatus & INT_IN_EP) || (uGIntStatus & INT_OUT_EP))
    {
        UINT32 uDmaEnCheck;
        UINT32 uRemainCnt;

        Inp32(DAINT, ep_int);

        if (ep_int & (1<<CONTROL_EP))
        {
            Inp32(DIEPINT0, ep_int_status);

            if (ep_int_status & INTKN_TXFEMP)
            {
                if (g_bTransferEp0==true)
                {
                    OTGDEV_TransferEp0();
                    g_bTransferEp0 = false;
                }
            }
            Outp32(DIEPINT0, ep_int_status); // Interrupt Clear
        }

        else if (ep_int & ((1<<CONTROL_EP)<<16))
        {
            Inp32(DOEPINT0, ep_int_status);
            OTGDEV_SetOutEpXferSize(EP_TYPE_CONTROL, 1, oOtgDev.m_uControlEPMaxPktSize);
            Outp32(DOEPCTL0, 0u<<31|1<<26);        //ep0 enable, clear nak

            Outp32(DOEPINT0, ep_int_status);         // Interrupt Clear
        }

        if(ep_int & (1<<BULK_IN_EP))
        {
            Inp32(bulkIn_DIEPINT, ep_int_status);
            Outp32(bulkIn_DIEPINT, ep_int_status); // Interrupt Clear

            if ( (ep_int_status&INTKN_TXFEMP) && oOtgDev.m_eOpMode == USB_CPU)
                OTGDEV_HandleEvent_BulkIn();

            Inp32(GAHBCFG, uDmaEnCheck);
            if ((uDmaEnCheck&MODE_DMA)&&(ep_int_status&TRANSFER_DONE))
            {
                UINT32 temp;
                DBGUSB("DMA IN : Transfer Done\n");

                Inp32(bulkIn_DIEPDMA, temp);
                oOtgDev.m_pUpPt = (UINT8 *)temp;
                uRemainCnt = oOtgDev.m_uUploadSize- ((UINT32)oOtgDev.m_pUpPt - oOtgDev.m_uUploadAddr);

                if (uRemainCnt>0)
                {
                    UINT32 uPktCnt, uRemainder;
                    uPktCnt = (UINT32)(uRemainCnt/oOtgDev.m_uBulkInEPMaxPktSize);
                    uRemainder = (UINT32)(uRemainCnt%oOtgDev.m_uBulkInEPMaxPktSize);
                    if(uRemainder != 0)
                    {
                        uPktCnt += 1;
                    }
                    if (uPktCnt> 1023)
                    {
                        OTGDEV_SetInEpXferSize(EP_TYPE_BULK, 1023, (oOtgDev.m_uBulkInEPMaxPktSize*1023));
                    }
                    else
                    {
                        OTGDEV_SetInEpXferSize(EP_TYPE_BULK, uPktCnt, uRemainCnt);
                    }
                    Outp32(bulkIn_DIEPCTL, 1u<<31|1<<26|2<<18|1<<15|BULK_IN_EP<<11|oOtgDev.m_uBulkInEPMaxPktSize<<0);        //ep1 enable, clear nak, bulk, usb active, next ep1, max pkt
                }
                else
                    DBGUSB("DMA IN : Transfer Complete\n");
            }
        }

        if (ep_int & ((1<<BULK_OUT_EP)<<16))
        {
            Inp32(bulkOut_DOEPINT, ep_int_status);
            Outp32(bulkOut_DOEPINT, ep_int_status);         // Interrupt Clear
            Inp32(GAHBCFG, uDmaEnCheck);
            if ((uDmaEnCheck&MODE_DMA)&&(ep_int_status&TRANSFER_DONE))
            {
                UINT32 temp;
                DBGUSB("DMA OUT : Transfer Done\n");
                Inp32(bulkOut_DOEPDMA, temp);
                oOtgDev.m_pDownPt = (UINT8 *)temp;

                uRemainCnt = oOtgDev.m_uDownloadFileSize - ((UINT32)oOtgDev.m_pDownPt - oOtgDev.m_uDownloadAddress + 8);

                if (uRemainCnt>0)
                {

⌨️ 快捷键说明

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