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

📄 usb.c

📁 LV24000的单片机DEMO程序
💻 C
📖 第 1 页 / 共 3 页
字号:
/************************************************************************/
/*    	USB.C								*/
/*				       					*/
/*	Project	:	USB SAMPLE-BULK					*/
/*			:						*/
/*	Data	:	2004.05.26					*/
/*									*/
/*	Memo	:	USB device sample-firmware for LC87F1564	*/
/*																		*/
/*	Copyrigth(c) 2004 SANYO Electric Co., Ltd. All Rights Reserved.	*/
/************************************************************************/

/*===================================================================
	Endpoint mapping for interface 0: (USB - ItoM 3-wire)
                EP0:    control endpoint (required)
                EP1:    Bulk IN  (64 bytes - address 0x81)
                EP2:    Bulk OUT (64 bytes - address 0x02)
=================================================================*/
#include "SYSDEF.h"
//#include "Common.h"
#include "LC87F15.h"
#include "Lv24Ekit.h"	// for compiler options

#include "stdusb.h"
#include "vendor.h"

/*-------------------------------------------------------------------
	敪怳廃攇悢慖戰
-------------------------------------------------------------------*/
/* Moved to Lv24Ekit.h
#define	CF8MHZ
//#define	CF9MHZ
//#define	CF10MHZ
//#define	CF11MHZ
//#define	CF12MHZ
//#define	CF14MHZ
//#define	CF15MHZ
//#define	CF16MHZ
*/

// ==================================================================
#ifdef USE_USB	// The whole file can be discarded if USB is not used
// ==================================================================

/*-------------------------------------------------------------------
        僌儘乕僶儖曄悢
-------------------------------------------------------------------*/
struct DeviceInfoST     DeviceInfo;                     /* Device Information */
struct EndpointST       EndpointInfo[NUM_OF_EP];        /* Endpoint Information */
struct InterfaceST      InterfaceInfo[NUM_OF_IF];       /* Alternate Setting */

unsigned int            LengthRemain;                   // 
unsigned int            DataRemain;                     // 僐儞僩儘乕儖揮憲梡僨乕僞偺巆傝僶僀僩悢
unsigned char _rom *    pTxData;                        // 揮憲梡僨乕僞攝楍傊偺億僀儞僞丄僙僢僩傾僢僾庴怣帪偵愝掕丅
unsigned char *         pRxData;                        // 庴怣僨乕僞奿擺椞堟傊偺億僀儞僞丄僙僢僩傾僢僾庴怣帪偵愝掕丅

unsigned char           RxData[32];

union EP0RxUN           EP0Rx           _at(0x0200);
union EP0TxUN           EP0Tx           _at(0x0240);

union BulkUN            EP1BK0          _at(0x0280);
union BulkUN          	EP1BK1          _at(0x02C0);
//union BulkUN          EP2BK0          _at(0x02C0);
union BulkUN          	EP2BK0         	_at(0x0300);
//union BulkUN          EP2BK1         	_at(0x0340);

/*
union BulkUN            EP1BK1          _at(0x02c0);
union BulkUN            EP3BK0          _at(0x0380);
union BulkUN            EP3BK1          _at(0x03c0);
union BulkUN            EP4BK0          _at(0x0400);
union BulkUN            EP4BK1          _at(0x0440);
*/

/*-------------------------------------------------------------------
        Descriptors
-------------------------------------------------------------------*/
// 昗弨僨傿僗僋儕僾僞
// 僨僶僀僗僨傿僗僋儕僾僞
struct DeviceDescriptorST _rom DeviceDescriptor = {
    0x12,           // bLength
    DEVICE,         // bDescriptorType(Device)
    0x0110,         // bcdUSB(rev. 01.10)
    0xff,           // bDeviceClass
    0x00,           // bDeviceSubClass
    0xff,           // bDeviceProtocol
    0x40,           // bMaxPacketSize0
    VENDORID,       // idVendor
    PRODUCTID,      // idProduct
    DEVICEID,       // bcdDevice
    0x01,           // iManufacturer
    0x02,           // iProduct
    0x00,           // iSerialNumber
    0x01,           // bNumConfigurations(1)
};

unsigned char _rom ConfigurationDescriptor[] = {
    // 僐儞僼傿僌儗乕僔儑儞僨傿僗僋儕僾僞
    0x09,           // bLength
    CONFIGURATION,  // bDescriptorType(CONFIGRATION)
    0x20, 0x00,     // wTotalLength
    0x01,           // bNumInterfaces(1)
    0x01,           // bConfigurationValue(1)
    0x00,           // iConfiguration
    0x80,           // bmAttributes
    0x1e,           // MaxPower
    // Interface Descriptor
    0x09,           // bLength
    0x04,           // bDescriptorType(INTERFACE)
    0x00,           // bInterfaceNumber(0)
    0x00,           // bAlternateSetting(0)
    0x02,           // bNumEndpoints(2)
    0xff,           // bInterfaceClass(Vendor Specific)
    0x00,           // bInterfaceSubclass
    0xff,           // bInterfaceProtocol(Vendor Specific)
    0x00,           // iInterface
    // Endpoint Descriptor(Bulk, IN)
    0x07,           // bLength
    0x05,           // bDescriptorType(ENDPOINT)
    0x81,           // bEndpointAddress(IN, 1)
    0x02,           // bmAttributes(Bulk)
    0x40, 0x00,     // wMaxPacketSize(64bytes)
    0x00,           // bInterval(Don't care)
    // Endpoint Descriptor(Bulk, OUT)
    0x07,           // bLength
    0x05,           // bDescriptorType(ENDPOINT)
    0x02,           // bEndpointAddress(OUT, 2)
    0x02,           // bmAttributes(Bulk)
    0x40, 0x00,     // wMaxPacketSize(64bytes)
    0x00,           // bInterval(Don't care)
};

// 僗僩儕儞僌僨傿僗僋儕僾僞
struct UnicodeST _rom Unicode = 
{
    0x04,                   // bLength(4 bytes)
    STRING,                 // bDescriptorType
    0x0409,                 // LANGID
};

unsigned char _rom iManufacturer[] = 
{
    0x30,                   // bLength(48bytes)
    STRING,                 // bDescriptorType 
    'S',0x00,               // S
    'A',0x00,               // A
    'N',0x00,               // N
    'Y',0x00,               // Y
    'O',0x00,               // O
    ' ',0x00,               //  
    'E',0x00,               // E
    'l',0x00,               // l
    'e',0x00,               // e
    'c',0x00,               // c
    't',0x00,               // t
    'r',0x00,               // r
    'i',0x00,               // i
    'c',0x00,               // c
    ' ',0x00,               //  
    'C',0x00,               // C
    'o',0x00,               // o
    '.',0x00,               // .
    ',',0x00,               // ,
    'L',0x00,               // L
    't',0x00,               // t
    'd',0x00,               // d
    '.',0x00,               // .
};

unsigned char _rom iProduct[] = 
{
    0x2A,                   // bLength(42 bytes)
    STRING,                 // bDescriptorType 
    'S',0x00,               // S
    'A',0x00,               // A 
    'N',0x00,               // N 
    'Y',0x00,               // Y 
    'O',0x00,               // O 
    '/',0x00,               //   
    'I',0x00,               // U 
    't',0x00,               // S 
    'o',0x00,               // B 
    'M',0x00,               //   
    ' ',0x00,               // S 
    'U',0x00,               // a 
    'S',0x00,               // m 
    'B',0x00,               // p 
    '3',0x00,               // l 
    'W',0x00,               // e 
    ' ',0x00,               // - 
    'K',0x00,               // B 
    'i',0x00,               // u 
    't',0x00,               // l 
};

/*-------------------------------------------------------------------
 Prototype Declaration
-------------------------------------------------------------------*/
void EndpointAck(void);
void Ep0Ack (void);
void Ep1Ack (void);
void Ep2Ack (void);
void Ep3Ack (void);
void Ep4Ack (void);
void Ep5Ack (void);
void Ep6Ack (void);
void Ep5Err (void);
unsigned char Setup  (struct RequestST* pRequest);
unsigned char SetInterface (unsigned char Number, unsigned char AltSetting);
unsigned char GetInterface (unsigned char Number);
unsigned char ClearFeatureDevice (unsigned char FeatureSelector);
unsigned char SetAddress (unsigned char NewAddress);
unsigned char SetConfiguration (unsigned char ConfigNo);
unsigned char SetFeatureDevice (unsigned char FeatureSelector);
unsigned char ClearFeatureEndpoint (unsigned char FeatureSelector, unsigned char Number);
unsigned char SetFeatureEndpoint (unsigned char FeatureSelector, unsigned char Number);
unsigned char GetConfiguration (void);
void MakeDescriptor(_rom unsigned char * DescTable, int TableSize);
unsigned char GetDescriptor (unsigned char Type, unsigned char Index, unsigned int Length);
//unsigned char DescriptorRead (unsigned int * pLengthRemain, unsigned int * pDataRemain, unsigned char * _rom * ppTxData);
unsigned char DescriptorRead (unsigned int * pLengthRemain, unsigned int * pDataRemain);
unsigned char GetStatusDevice (void);
unsigned char GetStatusInterface (unsigned char Number);
unsigned char GetStatusEndpoint (unsigned char Number);
void ControlRead (unsigned char Count);
unsigned char NoDataControl (void);
void BusActive(void);
void BusReset(void);
void BaseTimer(void);
void USBInitialSetting(void);
void EndpointReset(void);
unsigned char ControlWrite(unsigned int RxCount, unsigned int * pLengthRemain, unsigned char ** ppRxData);

// HLE added
/*
void SendReply(BYTE byCount);
extern BYTE g_ReceiveBuf[64];
extern BYTE *g_ReplyBuf;
extern BYTE g_byUsbRxCnt;
*/

/*-------------------------------------------------------------------
  Bus Active Interrupt
  -----------------------------------------------------------------*/
void BusActive(void) 
{
    #if defined SUSPEND_ON
    char        i;
    for(i=0;i<100;i++);                         /* Wait for Clock stabilization */
    _OCR = 0x90;
    #endif
    _USCTRL = 0xc0;                             /* USBRUN = 1, USBON=1 */
    DeviceInfo.State.bm.Suspended = 0;          /* Device Resumed */
    _BACEN = 0;                                 /* BusActive Interrupt Disabled */
    _BRSEN = 1;                                 /* BusReset Interrupt Disable */
    _ENPEN = 1;                                 /* Endpoint Interrupt Disable */
    _BTIE1 = 1;                                 /* BaseTimer int1 enable */
    _BTON = 1;                                  /* BaseTimer count start */
    DeviceInfo.Standby = 0;                     /* standby flag clear */
} // End BusActive

/*-------------------------------------------------------------------
  Base Timer Interrupt
  -----------------------------------------------------------------*/
void BaseTimer(void) 
{
    static char Suspend_Counter = 0;            	/* count-up every 1.024ms, */
                                                        /* if bus is non-active  */
    _BTIF1 = 0;                                         /* BaseTimer int1 flag clear */

    if (_BACFG) 
    {                                         		/* Bus-Active */
        _BACFG = 0;
        Suspend_Counter = 0;
    }
    else
    {                                               	/* no Bus-Activity */
        Suspend_Counter ++;
#if	defined CF8MHZ
        if(Suspend_Counter == 4) {                       /* go to Suspend-state */
#elif defined CF9MHZ
        if(Suspend_Counter == 5) {                       /* go to Suspend-state */
#elif defined CF10MHZ
        if(Suspend_Counter == 5) {                       /* go to Suspend-state */
#elif defined CF11MHZ
        if(Suspend_Counter == 6) {                       /* go to Suspend-state */
#elif defined CF12MHZ
        if(Suspend_Counter == 6) {                       /* go to Suspend-state */
#elif defined CF14MHZ
        if(Suspend_Counter == 7) {                       /* go to Suspend-state */
#elif defined CF15MHZ
        if(Suspend_Counter == 8) {                       /* go to Suspend-state */
#elif defined CF16MHZ
        if(Suspend_Counter == 8) {                       /* go to Suspend-state */
#endif
            _USCTRL = 0;                                /* USBRUN=0, USBON=0 */
            DeviceInfo.State.bm.Suspended = 1;          /* << Suspended >> */
            Suspend_Counter = 0;                        /* Suspend counter clear */
            _BACEN = 1;                                 /* BusActive Interrupt Enable */
            _BRSEN = 0;                                 /* BusReset Interrupt Disable */
            _ENPEN = 0;                                 /* Endpoint Interrupt Disable */
            _BTIE1 = 0;                                 /* BaseTimer int1 Disable */
            _BTON = 0;                                  /* BaseTimer count stop */
            DeviceInfo.Standby = 1;                     /* standby flag set */
        }
    }
}

/*-------------------------------------------------------------------
  Bus Reset Interrupt
  -----------------------------------------------------------------*/
void BusReset(void) 
{
    unsigned char       Count;

    _USBADR = 0;                                        /* Device Address reset */
    // 僨僶僀僗忣曬弶婜壔
    DeviceInfo.State.B = DEFAULT_STATE;
    DeviceInfo.Address = 0;
    DeviceInfo.RemoteWakeup = 0;
    DeviceInfo.Standby = 0;

    EndpointInfo[0].HaltFeature = 0;
    EndpointInfo[0].DataToggle = 0;
    EndpointInfo[0].TransferType = CONTROL;
    EndpointInfo[0].State = SETUP_READY;
    _EP0STA = 0x80;
    _EP0MP  = EP0_PAYLOAD;

    EndpointReset();
    for(Count=0;Count<NUM_OF_IF;Count++)
        InterfaceInfo[Count].AltSetting = 0;

    _USBINT = 0x41;                                     // BRSEN=1, ENPEN=1
    _EP0INT = 0x40;                                     /* AK0EN = 1 */

    #if defined Dbg010807
    _USCTRL = 0x80;
    #endif
    while(_BRSFG)                                       /* wait for Bus-Reset end */
        _BRSFG = 0;
    #if defined Dbg010807

⌨️ 快捷键说明

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