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

📄 usb.c

📁 LV24000的单片机DEMO程序
💻 C
📖 第 1 页 / 共 3 页
字号:
            RemoteWakeup = 0x00;
        EP0Tx.B[0] = RemoteWakeup | SELF_POWERED;
        EP0Tx.B[1] = 0x00;
        ControlRead(2);
        return(C_READ_END);
    }
    else
        return(ERROR);
}
/*-------------------------------------------------------------------
        Get Status Interface
-------------------------------------------------------------------*/
unsigned char   GetStatusInterface (unsigned char Number) 
{
    if((DeviceInfo.State.bm.Addressed && (Number==0)) ||
       (DeviceInfo.State.bm.Configured && (Number < NUM_OF_IF))) 
    {
        EP0Tx.W[0] = 0x0000;
        ControlRead(2);
        return(C_READ_END);
    }
    else
        return(ERROR);
}
/*-------------------------------------------------------------------
        Get Status Endpoint
-------------------------------------------------------------------*/
unsigned char   GetStatusEndpoint (unsigned char Number) 
{
    if((DeviceInfo.State.bm.Addressed && (Number==0)) ||
       (DeviceInfo.State.bm.Configured && (Number < NUM_OF_EP))) 
    {
        EP0Tx.B[0] = EndpointInfo[Number].HaltFeature;
        EP0Tx.B[1] = 0x00;
        ControlRead(2);
        return(C_READ_END);
    }
    else
        return(ERROR);
}

/*-------------------------------------------------------------------
  9.4.6  Set Address
-------------------------------------------------------------------*/
unsigned char   SetAddress (unsigned char NewAddress) 
{
    /* not << Configured >> */
    if(~DeviceInfo.State.bm.Configured) 
    {
        DeviceInfo.Address = NewAddress;// 怴傾僪儗僗懸旔
//      _E0CST = 1;                     // No Data Control
//      _E0ACK = 1;
        _EP0STA |= 0x0a;
        return(C_ADDR);
    }
    else
        return(ERROR);
}

/*-------------------------------------------------------------------
  9.4.7  Set Configuration
-------------------------------------------------------------------*/
unsigned char SetConfiguration(unsigned char ConfigNo) 
{
    unsigned char       Count;

    if(DeviceInfo.State.bm.Addressed) 
    {
        switch(ConfigNo) 
        {
        case 0:
            DeviceInfo.State.bm.Configured = FALSE;
            break;
        case 1:
            DeviceInfo.State.bm.Configured = TRUE;
            break;
        default:
            return(ERROR);
        }
        DeviceInfo.RemoteWakeup = FALSE;

        EndpointReset();
        ///////////////////////////////////////////////////
        _EP2INT = 0x40;
        _EP2STA = 0x88;		// EP2(BulkIn) Ack
        _EP2CNT = 0x40;

	_EP1INT = 0x40;		// HLE added - Enable Endpoint 1 ACK-interrupt
        ///////////////////////////////////////////////////
        for (Count=0;Count<NUM_OF_IF;Count++)
            InterfaceInfo[Count].AltSetting = 0;
        return(NoDataControl());
    }
    else
        return(ERROR);
} // End SetConfiguration

/*-------------------------------------------------------------------
   9.4.8  Set Descriptor
-------------------------------------------------------------------*/
// not supported

/*-------------------------------------------------------------------
   9.4.9  Set Feature
-------------------------------------------------------------------*/
/*-------------------------------------------------------------------
        Set Feature Device
-------------------------------------------------------------------*/
unsigned char   SetFeatureDevice (unsigned char FeatureSelector) {
    /* << Addressed >> */
    if((FeatureSelector == DEVICE_REMOTE_WAKEUP) && DeviceInfo.State.bm.Addressed) {
        DeviceInfo.RemoteWakeup = TRUE;
        return(NoDataControl());
    }
    else
        return(ERROR);
}
/*-------------------------------------------------------------------
        Set Feature Endpoint
-------------------------------------------------------------------*/
unsigned char   SetFeatureEndpoint(unsigned char FeatureSelector, unsigned char Number) {
    if((FeatureSelector == ENDPOINT_HALT) &&
       ((DeviceInfo.State.bm.Addressed && (Number==0)) ||
        (DeviceInfo.State.bm.Configured && (Number < NUM_OF_EP)))) {
        EndpointInfo[Number].HaltFeature = TRUE;
        return(NoDataControl());
    }
    else
        return(ERROR);
}

/*-------------------------------------------------------------------
  9.4.10  Set Interface
-------------------------------------------------------------------*/
unsigned char SetInterface(unsigned char Number, unsigned char AltSetting) 
{
    if(DeviceInfo.State.bm.Configured && (Number < NUM_OF_IF)) 
    {
        InterfaceInfo[Number].AltSetting = AltSetting;
        EndpointReset();
        switch(Number) 
        {
        //----- Interface 0 -----
        case 0:
            if (AltSetting != 0) // Only support 1 interface
                return(ERROR);
            break;
        }
        return(NoDataControl());
    }
    else
        return(ERROR);
} // End SetInterface

/*-------------------------------------------------------------------
   9.4.11  Synch Frame
-------------------------------------------------------------------*/
// not supported

/*-------------------------------------------------------------------
        僨傿僗僋儕僾僞撉傒弌偟張棟
-------------------------------------------------------------------*/
unsigned char DescriptorRead(unsigned int * pLengthRemain, unsigned int * pDataRemain) 
{
    unsigned char       Count;
    for(Count=0;Count<EP0_PAYLOAD;Count++) 
    {
        // 巜掕僨乕僞僶僀僩悢傪撉傒廔傢偭偨丄偁傞偄偼憲傞傋偒僨乕僞傪慡偰撉傒廔傢偭偨丅
        if(*pLengthRemain==0 || *pDataRemain==0) 
        {
            ControlRead(Count);
            return(C_READ_END);
        }
        EP0Tx.B[Count] = *pTxData++;
        (*pLengthRemain)--;
        (*pDataRemain)--;
    }
    ControlRead(Count);
    return(C_READ);
}// End DescriptorRead

/*-------------------------------------------------------------------
        EP0 揮憲張棟
-------------------------------------------------------------------*/
void ControlRead(unsigned char Count) 
{
    // Count僶僀僩暘偺僨乕僞傪揮憲丅
    _EP0TX = Count;                             /* transmission counter set */
//    _E0CRW = 1;
//    _E0ACK = 1;
    _EP0STA |= 0x09;
} // End ControlRead
/*-------------------------------------------------------------------
        No Data 僐儞僩儘乕儖
-------------------------------------------------------------------*/
unsigned char   NoDataControl(void) 
{
//    _E0CST = 1;                                       // No Data Control
//    _E0ACK = 1;
    _EP0STA |= 0x0a;
    return(C_NO_DATA);
}

/*-------------------------------------------------------------------
        僐儞僩儘乕儖儔僀僩
-------------------------------------------------------------------*/
unsigned char ControlWrite(unsigned int RxCount, unsigned int * pLengthRemain, unsigned char ** ppRxData) 
{
    unsigned int Count;

    if(RxCount < *pLengthRemain) 
    {               // 梊掕庴怣僨乕僞僶僀僩悢偵枹摓払
        for(Count=0;Count<RxCount;Count++) 
        {
            **ppRxData = EP0Rx.B[Count];
            (*ppRxData)++;
        }
        *pLengthRemain -= RxCount;
//      _E0ACK = 1;
        _EP0STA |= 0x08;
        return(C_WRITE);
    }
    else
    {                                       // 梊掕庴怣僨乕僞僶僀僩悢傪庴怣嵪
        for(Count=0;Count<*pLengthRemain;Count++) 
        {
            **ppRxData = EP0Rx.B[Count];
            (*ppRxData)++;
        }
//      _E0CST = 1;
//      _E0ACK = 1;
        _EP0STA |= 0x0a;
        return(C_WRITE_END);
    }
} // End ControlWrite

/*-------------------------------------------------------------------
        Endpoint Initialization (except for Default Endpoint)
-------------------------------------------------------------------*/
void EndpointReset(void) 
{
    unsigned char Count;

    for(Count=1;Count<NUM_OF_EP;Count++) 
    {
        EndpointInfo[Count].HaltFeature = 0;
        EndpointInfo[Count].DataToggle  = 0;
        EndpointInfo[Count].State       = DISABLE;
    }
    // Endpoint 1: Bit 7: Enable endpoint. Bit 3: perform ACK on token processing. Bit 2=1: IN endpoint	
    //_EP1STA = DISABLE;
    _EP1STA = 0x8C;	// HLE added
    // Endpoint 2: Bit 7: Enable endpoint. Bit 3: perform ACK on token processing. Bit 2=1: OUT endpoint	
    _EP2STA = 0x88;
    _EP3STA = DISABLE;
    _EP4STA = DISABLE;
    _EP5STA = DISABLE;
    _EP6STA = DISABLE;
} // End EndpointReset

/*-------------------------------------------------------------------
        Initial Setting
-------------------------------------------------------------------*/
void USBInitialSetting(void) 
{
    unsigned char       Count;

	/*----------------------------------------------
		USB Clock Setting
	----------------------------------------------*/
#if defined CF8MHZ
    _PLLCNT = 0x00;             // 8MHz oscillation ES2
#elif defined CF12MHZ
    _PLLCNT = 0x80;             // 12MHz oscillation ES2
#endif

    /*---------------------------------------------------------------
        USB Variables' Initialization
    ---------------------------------------------------------------*/
    // 僨僶僀僗忣曬弶婜壔
    DeviceInfo.State.B = DISABLE;
    DeviceInfo.Address = 0;
    DeviceInfo.RemoteWakeup = 0;
    DeviceInfo.Standby = 0;

    // 僄儞僪億僀儞僩忣曬弶婜壔
    for(Count=0;Count<NUM_OF_EP;Count++) 
    {
        EndpointInfo[Count].HaltFeature  = 0;
        EndpointInfo[Count].DataToggle   = 0;
        EndpointInfo[Count].TransferType = 0;
        EndpointInfo[Count].State        = DISABLE;
    }

    /*---------------------------------------------------------------
        BaseTimer setting
                                1Tcyc(ns.)      2048Tcyc(us.)
                -  8MHz :       375              768
                -  9MHz :       333              683
                - 10MHz :       300              614
                - 11MHz :       272              559
                - 12MHz :       250              512
                - 14MHz :                        439
                - 15MHz :                        410
                - 16MHz :                        384
    ---------------------------------------------------------------*/
    _ISL  = 0x10;                               // BaseTimer 僋儘僢僋慖戰 (僒僀僋儖僋儘僢僋)
    _BTCR = 0x74;                               // BT_1廃婜 = 2048Tcyc, BT start, BT_1 Interrupt Enable

    //----- USB Block Run       -----
    _USCTRL = 0xc0;                             // USBON=1, USBRUN=1

    _BRSEN = 1;                                 // 僶僗儕僙僢僩妱傝崬傒嫋壜
    DeviceInfo.State.bm.Powered = 1;

    // HLE added: initalize the buffer pointer for holding USB-reply data
    g_byUsbRxCnt = 0; // Nothing received yet from USB-3W host
    g_ReplyBuf = EP1BK0.B;
} // End USBInitialSetting

/*
BOOL IsUsbConnected(void)
{
	// Determine the USB connect state
	// Return: 	TRUE:  USB is connected
	//		FALSE: USB is not connected
	return(DeviceInfo.State.bm.Addressed);
} // End IsUsbConnected
*/

void SendReply(BYTE byCount)
{
	// Send USB reply throught endpoint 1
	// Data must be filled in EP1BK0
	//_EP1INT = 0x40;	// Enable Endpoint 1 ACK-interrupt
    	_EP1CNT = byCount;	// Byte to be transmitted
    	_EP1STA |= 0x8C;	// Bit 7: Enable endpoint 1. Bit 3: perform ACK on token processing. Bit 2: IN endpoint	
} // End SendReply

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

⌨️ 快捷键说明

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