📄 usb.c
字号:
/**************** (c) 1998 STMicroelectronics **********************
PROJECT : USB - ST7
COMPILER : ST7 HICROSS C (HIWARE)
MODULE : usb.c
VERSION : V 1.1
CREATION DATE : 25/05/98
AUTHOR : / MICROCONTROLLER DIVISION / ST Rousset
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
DESCRIPTION : ST7263 USB driver.
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
MODIFICATIONS :
Rev. 1.1 23/09/98
-Modified RemoteWakeup() to fit a least 20mS resume state (see USB spec.)
******************************************************************************/
#include <hidef.h>
#include "lib_bits.h"
#include "usb_var.h"
#include "descript.h"
#include "usr_var.h"
#include "define.h"
#include "map_7263.h"
#include "usbrc.h"
#include "usb.h"
#include "CondComp.h"
#include "usb_ail.h"
#include "usb_cpt.h"
/*-----------------------------------------------------------------------------
ROUTINE NAME : SetFastMode
INPUT/OUTPUT : None
DESCRIPTION : Set the micro in fast mode (8 MHz)
-----------------------------------------------------------------------------*/
void SetFastMode(void)
{
Set8Mhz();
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : UsbReset
INPUT/OUTPUT : None
DESCRIPTION : Reset the USB cell and initialize the registers
-----------------------------------------------------------------------------*/
void UsbReset(void)
{
// Variables initialization
bmUsbState = 0;
bmUsbIntFlag = 0x00; // Clear CTR Int. flags
UsbCtrStatus = 0x00;// Clear Control status flags
UsbImr = 0xA7; // set USB interrupt mask to Enable SUSPM|CTRM|ESUPM|RESETM|SOFM
UsbbmRequestType = 0x00;
UsbbRequest = 0x00;
UsbDataTrDir = 0x00;
UsbEndpNumber = 0x00;
ConfigurationValue = 0x00; // Device unconfigured
InterfaceValue = 0x00;
InterfacebmAttributes = ConfDescriptor[26]; // bmAttributes value initialization
DeviceStatusInfo = 0x00;
if (ValBit(InterfacebmAttributes, 5))
SetBit(DeviceStatusInfo, 1); // Remote Wakeup enabled
if (ValBit(InterfacebmAttributes, 6))
SetBit(DeviceStatusInfo, 0); // Self Powered Device
// Endpoint0 initialization
SetEP0RxStatus(VALID); // Enable reception on Endpoint 0
SetEP0TxTbc(0); // Disable transmission on Endpoint 0
SetEP0TxStatus(NAK);
ClearEP0StatusOut();
Endpoint0StatusInfo = 0x00;
// Endpoint1 initialization
#ifdef USE_ENDPOINT1
SetEP1Add(); //Set endpoint 1 address
Endpoint1StatusInfo = 0x00;
#endif
#ifdef USE_ENDPOINT1_OUT // endpoint 1 used in interrupt output pipe
SetEP1RxStatus(STALL);
#endif
#ifdef USE_ENDPOINT1_IN // endpoint 1 used in interrupt input pipe
SetEP1TxStatus(NAK); // Set the Endpoint 1 Tx status to NAK
ClearEP1StatusOut(); // Clear status OUT bit of endpoint 1
#endif
// Endpoint2 initialization
#ifdef USE_ENDPOINT2
SetEP2Add(); //Set endpoint 2 address
Endpoint2StatusInfo = 0x00;
#endif
#ifdef USE_ENDPOINT2_OUT // endpoint 2 used in interrupt output pipe
SetEP2RxStatus(STALL);
#endif
#ifdef USE_ENDPOINT2_IN // endpoint 2 used in interrupt input pipe
SetEP2TxStatus(NAK); // Set the Endpoint 1 Tx status to NAK
ClearEP2StatusOut(); // Clear status OUT bit of endpoint 1
#endif
// USB Device Initialization (see USB spec. 1.0)
SetDmaAdd((Word) &Endpoint0.Rx[0]); // set DMA starting address
ClearISTR(); // Clear ISTR register
SetIMR(UsbImr); // Set Interrupt mask
ClearDADDR(); // Clear Device address
InitCTLR(); // Turn on the 3.3V regulator
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : UsbIntSof
INPUT/OUTPUT : None
DESCRIPTION : USB Start of Frame interrupt
-----------------------------------------------------------------------------*/
void UsbIntSof(void)
{
ClrBit(bmUsbIntFlag,0);
bmUsbState |= 0x04;
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : UsbSuspend
INPUT/OUTPUT : None
DESCRIPTION : Set the USB cell in Suspend Mode
-----------------------------------------------------------------------------*/
void UsbSuspend(void)
{
bmUsbIntFlag &= ~Int_Susp;
bmUsbState |= 0x02;
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : UsbEndSuspend
INPUT/OUTPUT : None
DESCRIPTION : Clear the Esusp Interrupt Flag
-----------------------------------------------------------------------------*/
void UsbEndSuspend(void)
{
bmUsbIntFlag &= ~Int_Esusp;
bmUsbState &= ~SUSPEND;
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : RemoteWakeup
INPUT/OUTPUT : None
DESCRIPTION : Send a Remote Wake-up signaling to the Host
-----------------------------------------------------------------------------*/
void RemoteWakeup(void)
{ Byte i;
unsigned int j;
ClearSuspend();
SetResume();
if (ConfigurationValue == 0x01)
for (i=21; i>0; i--) // loop to keep the SE0 signal for 21ms
for(j=340; j>0;j --){ // 1ms loop
asm nop;
}
ClearResume();
UsbEndSuspend();
bmUsbState &= ~REMOTE_WAKEUP;
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : SetAddress
INPUT/OUTPUT : None
DESCRIPTION : Set the Device address after completion of the Status Stage
-----------------------------------------------------------------------------*/
void SetAddress(void)
{
if (!(UsbCtrStatus & ADDRESS2SET))
{
UsbCtrStatus |= ADDRESS2SET;
}
else // handshake received from Host
{
SetDeviceAddress(UsbwValue[0]);
UsbCtrStatus &= ~ADDRESS2SET;
SetEP0RxStatus(VALID); // Set Endpoint0 Rx Valid for next reception
}
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : GetDescriptor
INPUT/OUTPUT : None
DESCRIPTION : Send the descriptor requested by the Host
-----------------------------------------------------------------------------*/
void GetDescriptor(void)
{
if ((UsbbmRequestType & RECIPIENT) == REDEVICE)
{
if (UsbwValue[1] == DEVICE) // Device Descriptor
WriteEP0(&DeviceDescriptor[CurrentDescAddPointer]);
else
if (UsbwValue[1] == STRING) // String Descriptor
WriteEP0(&StringDescriptor[CurrentDescAddPointer]);
else
if (UsbwValue[1] == CONFIGURATION) // Configuration Descriptor
WriteEP0(&ConfDescriptor[CurrentDescAddPointer]);
else
if (UsbwValue[1] == INTERFACE) // Interface Descriptor
WriteEP0(&ConfDescriptor[CurrentDescAddPointer]);
}
else
if ((UsbbmRequestType & RECIPIENT) == REINTERFACE)
{
if (UsbwValue[1] == HID) // HID Descriptor
WriteEP0(&ConfDescriptor[CurrentDescAddPointer]);
else
if (UsbwValue[1] == REPORT) // Report Descriptor
{
WriteEP0(&ReportDescriptor[CurrentDescAddPointer]);
bmUsbState |= 0x08; // Device is enumerated
}
}
else
if ((UsbbmRequestType & RECIPIENT) == REENDPOINT)
{
if (UsbwValue[1] == REPORT) // Report Descriptor
WriteEP0(&ReportDescriptor[CurrentDescAddPointer]);
else
if (UsbwValue[1] == HID) // HID Descriptor
WriteEP0(&ConfDescriptor[CurrentDescAddPointer]);
}
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : WriteEP0
INPUT/OUTPUT : Endpoint#, data pointer, byte number
DESCRIPTION : Write data in the RAM space of the USB DMA and send these data
-----------------------------------------------------------------------------*/
void WriteEP0(Byte *Data)
{ int i;
unsigned int ByteNumber;
if (CurrentUsbbLength < MAXPACKETSIZE)
ByteNumber = CurrentUsbbLength;
else
ByteNumber = MAXPACKETSIZE;
CurrentUsbbLength -= ByteNumber;
CurrentDescAddPointer += ByteNumber;
for (i=0; i<ByteNumber; i++)
Endpoint0.Tx[i] = *(Data+i);
SetEP0TxTbc(ByteNumber);
SetEP0TxStatus(VALID);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -