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

📄 iusbhandledevreq.c

📁 How to control USB interface under SZ platform
💻 C
字号:
// This routine handle device requests from host.

#include "USB_pptt.h"
#include "USB_pdef.h"

void _iUsbHandleGetDevDscptr(void);
void _iUsbHandleGetConfDscptr(void);
void _iUsbHandleGetStrDscptr(void);
void _iUsbReturnStrDscptr0(void);
void _iUsbReturnStrDscptr2(void);
void _iUsbReturnStrDscptr3(void);

U8  _gUsbDevReq[8];     /* device request from host */
const U16 _gUsbDevDscptr[9] = {
    0x1201, 0x0001, 0xFF00, 0x0008, 0xC407, 0x05A0, 0x0D01, 0x0102, 0x0301 };
const U16 _gUsbConfDscptr[16] = {    
    0x0902, 0x2000, 0x0101, 0x0080, 0x3209, 0x0400, 0x0002, 0x0500,
    0x0000, 0x0705, 0x0302, 0x4000, 0x0007, 0x0584, 0x0240, 0x0000};
const U16 _gUsbStrDscptr0[2] = {0x0403, 0x0904};
const U16 _gUsbStrDscptr2[12] = {
    0x1803, 0x5300, 0x5A00, 0x2000, 0x5500, 0x5300, 0x4200, 0x2000,
    0x6400, 0x6500, 0x6D00, 0x6F00};
const U16 _gUsbStrDscptr3[11] = {
    0x1603, 0x4300, 0x3900, 0x4600, 0x3900, 0x3100, 0x4300, 0x3100,
    0x4100, 0x4600, 0x3900};


void _iUsbHandleDevReq()
{
    U16 fifoWord;
    U8  i, j;
    
    /* read device request data from FIFO */
    j = 0;
    for (i=0; i<4; i++)
    {
        fifoWord = _reg_USB_EP0_FDAT;
        _gUsbDevReq[j++] = (U8)(fifoWord >> 8);
        _gUsbDevReq[j++] = (U8)fifoWord;
    }
    
    /* decode device request */
    if ((_gUsbDevReq[0] = 0x80) && (_gUsbDevReq[1] = 0x06))
    {
        switch (_gUsbDevReq[3])
        {   
            case 0x01:  // device descriptor                
                _iUsbHandleGetDevDscptr();
                break;
            case 0x02:  // configuration descriptor
                _iUsbHandleGetConfDscptr();
                break;
            case 0x03:  // string descriptor
                _iUsbHandleGetStrDscptr();
                break;
        }
    }        
}

void _iUsbHandleGetDevDscptr()
{
    /* 8 bytes per packet */
    _reg_USB_EP0_FDAT = _gUsbDevDscptr[0];
    _reg_USB_EP0_FDAT = _gUsbDevDscptr[1];
    _reg_USB_EP0_FDAT = _gUsbDevDscptr[2];
    _reg_USB_EP0_FCTRL |= 0x20000000;  // next write is last one of packet
    _reg_USB_EP0_FDAT = _gUsbDevDscptr[3];

//    _reg_USB_CTRL |= USB_CTRL_CMOV_MASK;
    
    /* if length is not exactly 0x12, just return one packet */
    /* otherwise, return all 0x12 bytes */
    if (_gUsbDevReq[6] == 0x12)
    {
        _reg_USB_EP0_FDAT = _gUsbDevDscptr[4];
        _reg_USB_EP0_FDAT = _gUsbDevDscptr[5];
        _reg_USB_EP0_FDAT = _gUsbDevDscptr[6];
        _reg_USB_EP0_FCTRL |= 0x20000000;  // next write is last one of packet
        _reg_USB_EP0_FDAT = _gUsbDevDscptr[7];
        
        _reg_USB_EP0_FCTRL |= 0x20000000;  // next write is last one of packet
        _reg_USB_EP0_FDAT = _gUsbDevDscptr[8];
    }
}

void _iUsbHandleGetConfDscptr()
{
    /* if length is exactly 9, return 9 bytes only */
    if (_gUsbDevReq[6] == 0x09)
    {
        _reg_USB_EP0_FDAT = _gUsbConfDscptr[0];
        _reg_USB_EP0_FDAT = _gUsbConfDscptr[1];
        _reg_USB_EP0_FDAT = _gUsbConfDscptr[2];
        _reg_USB_EP0_FCTRL |= 0x20000000;  // next write is last one of packet
        _reg_USB_EP0_FDAT = _gUsbConfDscptr[3];

        /* here comes the last byte */
        _reg_USB_EP0_FCTRL |= 0x20000000;  // next write is last one of packet
        *((volatile U8 *)&_reg_USB_EP0_FDAT) = (U8)(_gUsbConfDscptr[4]>>8);
    }
    else    /* otherwise, return all 32 bytes */
    {
        _reg_USB_EP0_FDAT = _gUsbConfDscptr[0];
        _reg_USB_EP0_FDAT = _gUsbConfDscptr[1];
        _reg_USB_EP0_FDAT = _gUsbConfDscptr[2];
        _reg_USB_EP0_FCTRL |= 0x20000000;  // next write is last one of packet
        _reg_USB_EP0_FDAT = _gUsbConfDscptr[3];

        _reg_USB_EP0_FDAT = _gUsbConfDscptr[4];
        _reg_USB_EP0_FDAT = _gUsbConfDscptr[5];
        _reg_USB_EP0_FDAT = _gUsbConfDscptr[6];
        _reg_USB_EP0_FCTRL |= 0x20000000;  // next write is last one of packet
        _reg_USB_EP0_FDAT = _gUsbConfDscptr[7];

        _reg_USB_EP0_FDAT = _gUsbConfDscptr[8];
        _reg_USB_EP0_FDAT = _gUsbConfDscptr[9];
        _reg_USB_EP0_FDAT = _gUsbConfDscptr[10];
        _reg_USB_EP0_FCTRL |= 0x20000000;  // next write is last one of packet
        _reg_USB_EP0_FDAT = _gUsbConfDscptr[11];

        _reg_USB_EP0_FDAT = _gUsbConfDscptr[12];
        _reg_USB_EP0_FDAT = _gUsbConfDscptr[13];
        _reg_USB_EP0_FDAT = _gUsbConfDscptr[14];
        _reg_USB_EP0_FCTRL |= 0x20000000;  // next write is last one of packet
        _reg_USB_EP0_FDAT = _gUsbConfDscptr[15];

        /* need to end with a zero length packet */
        _reg_USB_EP0_STAT |= USB_ZLPS_MASK;    // next one is a zero length packet
    }    
}

void _iUsbHandleGetStrDscptr()
{
    switch (_gUsbDevReq[2])
    {
        case 0:
            _iUsbReturnStrDscptr0();
            break;
        case 2:
            _iUsbReturnStrDscptr2();
            break;
        case 3:
            _iUsbReturnStrDscptr3();
            break;
    }
}

void _iUsbReturnStrDscptr0()
{   
    /* if length is 2, return 2 bytes only */
    if (_gUsbDevReq[6] == 0x02)
    {
        _reg_USB_EP0_FCTRL |= 0x20000000;  // next write is last one of packet
        _reg_USB_EP0_FDAT = _gUsbStrDscptr0[0];
    }
    else    /* otherwise, return all 4 bytes */
    {
        _reg_USB_EP0_FDAT = _gUsbStrDscptr0[0];
        _reg_USB_EP0_FCTRL |= 0x20000000;  // next write is last one of packet
        _reg_USB_EP0_FDAT = _gUsbStrDscptr0[1];
    }    
//    _reg_USB_EP0_ISR |= USB_EPINTR_EOT_MASK; // clear EOT intr
//    _reg_USB_EP0_MASK = ENABLE_DEVREQ & ENABLE_EOT;
}

void _iUsbReturnStrDscptr2()
{
    /* 8 bytes per packet */
    _reg_USB_EP0_FDAT = _gUsbStrDscptr2[0];
    _reg_USB_EP0_FDAT = _gUsbStrDscptr2[1];
    _reg_USB_EP0_FDAT = _gUsbStrDscptr2[2];
    _reg_USB_EP0_FCTRL |= 0x20000000;  // next write is last one of packet
    _reg_USB_EP0_FDAT = _gUsbStrDscptr2[3];

    /* 8 bytes per packet */
    _reg_USB_EP0_FDAT = _gUsbStrDscptr2[4];
    _reg_USB_EP0_FDAT = _gUsbStrDscptr2[5];
    _reg_USB_EP0_FDAT = _gUsbStrDscptr2[6];
    _reg_USB_EP0_FCTRL |= 0x20000000;  // next write is last one of packet
    _reg_USB_EP0_FDAT = _gUsbStrDscptr2[7];

    /* 8 bytes per packet */
    _reg_USB_EP0_FDAT = _gUsbStrDscptr2[8];
    _reg_USB_EP0_FDAT = _gUsbStrDscptr2[9];
    _reg_USB_EP0_FDAT = _gUsbStrDscptr2[10];
    _reg_USB_EP0_FCTRL |= 0x20000000;  // next write is last one of packet
    _reg_USB_EP0_FDAT = _gUsbStrDscptr2[11];
    
    /* the next packet is an zero-length one */
    _reg_USB_EP0_STAT |= USB_ZLPS_MASK;

//    _reg_USB_EP0_ISR |= USB_EPINTR_EOT_MASK; // clear EOT intr
//    _reg_USB_EP0_MASK = ENABLE_DEVREQ & ENABLE_EOT;
}

void _iUsbReturnStrDscptr3()
{
    /* if length is 2, return 2 bytes only */
    if (_gUsbDevReq[6] == 0x02)
    {
        _reg_USB_EP0_FCTRL |= 0x20000000;  // next write is last one of packet
        _reg_USB_EP0_FDAT = _gUsbStrDscptr3[0];
    }
    else    /* otherwise return all 22 bytes */
    {
    /* 8 bytes per packet */
    _reg_USB_EP0_FDAT = _gUsbStrDscptr3[0];
    _reg_USB_EP0_FDAT = _gUsbStrDscptr3[1];
    _reg_USB_EP0_FDAT = _gUsbStrDscptr3[2];
    _reg_USB_EP0_FCTRL |= 0x20000000;  // next write is last one of packet
    _reg_USB_EP0_FDAT = _gUsbStrDscptr3[3];

    /* 8 bytes per packet */
    _reg_USB_EP0_FDAT = _gUsbStrDscptr3[4];
    _reg_USB_EP0_FDAT = _gUsbStrDscptr3[5];
    _reg_USB_EP0_FDAT = _gUsbStrDscptr3[6];
    _reg_USB_EP0_FCTRL |= 0x20000000;  // next write is last one of packet
    _reg_USB_EP0_FDAT = _gUsbStrDscptr3[7];

    /* 8 bytes per packet */
    _reg_USB_EP0_FDAT = _gUsbStrDscptr3[8];
    _reg_USB_EP0_FDAT = _gUsbStrDscptr3[9];
    _reg_USB_EP0_FCTRL |= 0x20000000;  // next write is last one of packet
    _reg_USB_EP0_FDAT = _gUsbStrDscptr3[10];
    }

//    _reg_USB_EP0_ISR |= USB_EPINTR_EOT_MASK; // clear EOT intr
//    _reg_USB_EP0_MASK = ENABLE_DEVREQ & ENABLE_EOT;
    
}

⌨️ 快捷键说明

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