rusb.h

来自「最新版IAR FOR ARM(EWARM)5.11中的代码例子」· C头文件 代码 · 共 562 行 · 第 1/2 页

H
562
字号
//-----------------------------------------------------------------------------
// RUSB
//-----------------------------------------------------------------------------
#ifndef HAL_RUSB_H
#define HAL_RUBS_H

//-----------------------------------------------------------------------------
#include "hal_map.h"
#include "rsystem.h"

//-----------------------------------------------------------------------------
//CONFR
//-----------------------------------------------------------------------------
#define CONFR_nUSB_ENABLE 0x00000002UL /*enable means clear this bit*/

//-----------------------------------------------------------------------------
// TD (Host Controller Transfer Descriptor)
//-----------------------------------------------------------------------------
// TD->CONTROL (TDC)
//-----------------------------------------------------------------------------
#define TDC_R_BUFER_EXACT_FILL 0x00
#define TDC_R_BUFFER_ROUNDING  0x01
#define TDC_DP_SETUP           0x00
#define TDC_DP_OUT             0x01
#define TDC_DP_IN              0x02
#define TDC_DI_INTERRUPT_DELAY 0x01
#define TDC_DI_NO_INTERRUPT    0x07
#define TDC_CC_CONDITION_CODE  0x0F
#define TDC_T_DATA0            0x02
#define TDC_T_DATA1            0x03
#define TDC_EC_RETIRE          0x03  // ErrorCount, if > 2, retired this TD with ConditionCode set
#define TDC_CC_SET             0x0F  // ConditionCode

//-----------------------------------------------------------------------------
// tTD
//-----------------------------------------------------------------------------
typedef struct _TD {
  vu32         Control;
  u8          *CBP;    // phys ptr to the buffer
  struct _TD  *NextTD; // phys ptr to HC_TRANSFER_DESCRIPTOR
  u8          *BE;     // phys ptr to the end of buffer (the last byte)
} tTD;

//-----------------------------------------------------------------------------
// ED (Host Controller Endpoint Descriptor)
//-----------------------------------------------------------------------------
// ED->CONTROL (EDC)
//-----------------------------------------------------------------------------
#define EDC_FA            0x7FU
#define EDC_EN            0x0FU
#define EDC_D_SEE_TD      0x00
#define EDC_D_OUT         0x01
#define EDC_D_IN          0x02
#define EDC_S_FULL_SPEED  0x00
#define EDC_S_LOW_SPEED   0x01
#define EDC_K_NO_SKIP     0x00
#define EDC_K_SKIP        0x01
#define EDC_F_GENERAL     0x00
#define EDC_F_ISOCHRONOUS 0x01

//-----------------------------------------------------------------------------
// ED->HEADP (EDH)
//-----------------------------------------------------------------------------
#define EDH_HALT          0x00000001UL  // hardware stopped bit
#define EDH_CARRY         0x00000002UL  // hardware toggle carry bit

//-----------------------------------------------------------------------------
// tED
//-----------------------------------------------------------------------------
typedef struct _ED {
  vu32         Control;
  tTD         *TailP;   //physical pointer to HC_TRANSFER_DESCRIPTOR
  tTD         *HeadP;   //flags + phys ptr to HC_TRANSFER_DESCRIPTOR
  struct _ED  *NextED;  //phys ptr to HC_ENDPOINT_DESCRIPTOR
} tED;

//-----------------------------------------------------------------------------
// ControlPacket
//-----------------------------------------------------------------------------
// bmRequestType (BMRT)
//-----------------------------------------------------------------------------
#define BMRT_HOST_TO_DEVICE  0x00U
#define BMRT_DEVICE_TO_HOST  0x80U
//and
#define BMRT_STANDARD        0x00U
#define BMRT_CLASS           0x20U
#define BMRT_VENDOR          0x40U
//and
#define BMRT_DEVICE          0x00U
#define BMRT_INTERFACE       0x01U
#define BMRT_ENDPOINT        0x02U
#define BMRT_OTHER           0x03U

//-----------------------------------------------------------------------------
// bRequest (BR)
//-----------------------------------------------------------------------------
#define BR_GET_STATUS         0
#define BR_CLEAR_FEATURE      1
#define BR_CR_RESERVED2       2
#define BR_SET_FEATURE        3
#define BR_CR_RESERVED4       4
#define BR_SET_ADDRESS        5
#define BR_GET_DESCRIPTOR     6
#define BR_SET_DESCRIPTOR     7
#define BR_GET_CONFIGURATION  8
#define BR_SET_CONFIGURATION  9
#define BR_GET_INTERFACE     10
#define BR_SET_INTERFACE     11
#define BR_SYNCH_FRAME       12

//-----------------------------------------------------------------------------
//wValue
//-----------------------------------------------------------------------------
#define WV_DEVICE_DESCRIPTOR        (1<<8)
#define WV_CONFIGURATION_DESCRIPTOR (2<<8)
#define WV_STRING_DESCRIPTOR        (3<<8)
#define WV_INTERFACE_DESCRIPTOR     (4<<8)
#define WV_ENDPOINT_DESCRIPTOR      (5<<8)
#define WV_DEVICE_QUALIFIER         (6<<8)
#define WV_OTHER_SPEED_CONFIG       (7<<8)
#define WV_INTERFACE_POWER          (8<<8)
//#define WV_HID_DESCRIPTOR           0x21
//#define WV_HUB_DESCRIPTOR           0x29

//-----------------------------------------------------------------------------
// tControlPacket
//-----------------------------------------------------------------------------
typedef struct {  // 8 bytes
 u8  bmRequestType;
 u8  bRequest;
 u16 wValue;
 u16 wIndex;
 u16 wLength;
} tControlPacket;

//-----------------------------------------------------------------------------
// HCCA
//-----------------------------------------------------------------------------
typedef struct {                         // size: 256 bytes
   tED *InterruptList[32];               // phys ptr to HC_ENDPOINT_DESCRIPTOR
   vu16 FrameNumber;
   vu16 Pad1;                            // set to zero on each write access to HccaFrameNumber
   vu32 DoneHead;                        // phys ptr to HC_TRANSFER_DESCRIPTOR
   vu32 Reserved[30];
} tHCCA;

//-----------------------------------------------------------------------------
// REGISTERS
//-----------------------------------------------------------------------------
// HcControl (HCC)
//-----------------------------------------------------------------------------
#define HCC_CBSR                0x00000003UL
#define HCC_PLE                 0x00000004UL
#define HCC_IE                  0x00000008UL
#define HCC_CLE                 0x00000010UL
#define HCC_BLE                 0x00000020UL
#define HCC_HCFS                0x000000C0UL
#define HCC_HCFS_USBRESET       0x00000000UL
#define HCC_HCFS_USBRESUME      0x00000040UL
#define HCC_HCFS_USBOPERATIONAL 0x00000080UL
#define HCC_HCFS_USBSUSPEND     0x000000C0UL
#define HCC_IR                  0x00000100UL
#define HCC_RWC                 0x00000200UL
#define HCC_RWE                 0x00000400UL

//-----------------------------------------------------------------------------
// HcCommandStatus (HCCS)
//-----------------------------------------------------------------------------
#define HCCS_HCR       0x00000001UL
#define HCCS_CLF       0x00000002UL
#define HCCS_BLF       0x00000004UL
#define HCCS_OCR       0x00000008UL
#define HCCS_SOC       0x00030000UL

//-----------------------------------------------------------------------------
// HcInterruptEnable & HcInterruptDisable & HcInterruptStatus
//-----------------------------------------------------------------------------
#define HCI_SO         0x00000001UL
#define HCI_WDH        0x00000002UL
#define HCI_SF         0x00000004UL
#define HCI_RD         0x00000008UL
#define HCI_UE         0x00000010UL
#define HCI_FNO        0x00000020UL
#define HCI_RHSC       0x00000040UL
#define HCI_OC         0x40000000UL
#define HCI_MIE        0x80000000UL

//-----------------------------------------------------------------------------
// HcRhDescriptorA
//-----------------------------------------------------------------------------
#define HCRHDA_NOCP    0x00001000UL //NoOverCurrentProtection
#define HCRHDA_NPS     0x00000200UL //NoPoweSwithing

//-----------------------------------------------------------------------------
// HcRhDescriptorB
//-----------------------------------------------------------------------------
#define HCRHDA_PPCM_PORT1_MASK_ON     0x00020000 //PortPowerControlMask
#define HCRHDA_DP_PORT1_NO_REMOVABLE  0x00000002 //DeviceRemovable

//-----------------------------------------------------------------------------
// HcRhStatus (HCRHS)
//-----------------------------------------------------------------------------
#define HCRHS_LPSC_POWER_ON 0x00010000

//-----------------------------------------------------------------------------
// HcRhPortStatus Register (HCRHPS)
//-----------------------------------------------------------------------------
#define HCRHPS_CCS  0x00000001UL //CurrentConnectStatus: 0 = no device connected, 1 = device connected
#define HCRHPS_PES  0x00000002UL //PortEnableStatus: 0 = port is disabled, 1 = port is enabled
#define HCRHPS_PSS  0x00000004UL //PortSuspendStatus:0 = port is not suspended, 1 = port is suspended
#define HCRHPS_POCI 0x00000008UL //PortOverCurrentIndicator: 0 = no overcurrent condition. 1 = overcurrent condition detected
#define HCRHPS_PRS  0x00000010UL //PortResetStatus: 0 = port reset signal is not active, 1 = port reset signal is active
#define HCRHPS_PPS  0x00000100UL //PortPowerStatus: 0 = port power is off, 1 = port power is on
#define HCRHPS_LSDA 0x00000200UL //LowSpeedDeviceAttached: 0 = full speed device attached, 1 = low speed device attached
#define HCRHPS_CSC  0x00010000UL //ConnectStatusChange
#define HCRHPS_PESC 0x00020000UL //PortEnableStatusChange
#define HCRHPS_PSSC 0x00040000UL //PortSuspendStatusChange
#define HCRHPS_OCIC 0x00080000UL //PortOverCurrentIndicatorChange
#define HCRHPS_PRSC 0x00100000UL //PortResetStatusChange
#define HCRHPS_CHANGES_MASK (HCRHPS_CSC | HCRHPS_PESC | HCRHPS_PSSC | HCRHPS_OCIC | HCRHPS_PRSC)

//-----------------------------------------------------------------------------
// USB setup structures
//-----------------------------------------------------------------------------
// Descriptor
//-----------------------------------------------------------------------------
typedef struct {
  u8 bLength;              // length in bytes of this descriptor
  u8 bDescriptorType;      // descriptor ID (RTU_DEVICE_DESCRIPTOR = 1)
  u16 bcdUSB;              // USB version (BCD format)
  u8 bDeviceClass;         // the device's class
  u8 bDeviceSubClass;      // and subclass
  u8 bDeviceProtocol;      // and protocol
  u8 bMaxPacketSize0;      // default control pipe max packet len
} tDeviceDescriptor0;

typedef struct {
   u16 idVendor;             // vendor ID
   u16 idProduct;            // product ID
   u16 bcdDevice;            // device's version (BCD format)
   u8  iManufacturer;        // index of vendor's string descriptor
   u8  iProduct;             // index of product's string descriptor
} tDeviceDescriptor1;

typedef struct {
   u8  iSerialNumber;        // index of product's serial number
   u8  bNumConfigurations;   // number of available configurations
} tDeviceDescriptor2;

//-----------------------------------------------------------------------------
typedef struct {
   u8  Buttons;
   s8  DeltaX;
   s8  DeltaY;
   s8  DeltaZ;
} tMouseData;

//-----------------------------------------------------------------------------
typedef struct {
  tDeviceDescriptor0 *DeviceDescriptor0;
  tDeviceDescriptor1 *DeviceDescriptor1;
  tDeviceDescriptor2 *DeviceDescriptor2;
  u8                 *Configuration;
  u8                 *Interface;
  tMouseData         *MouseData;
  u8                  DataValid;
} tDEVICE;

//-----------------------------------------------------------------------------
typedef struct {
  vu32                     HcRevision,
                           HcControl,
                           HcCommandStatus,
                           HcInterruptStatus,
                           HcInterruptEnable,
                           HcInterruptDisable;
  tHCCA                   *HcHCCA;             // phys ptr to HCCA
  tED                     *HcPeriodCurrentED,  // phys ptr to HC_ENDPOINT_DESCRIPTOR
                          *HcControlHeadED,    // phys ptr to HC_ENDPOINT_DESCRIPTOR
                          *HcControlCurrentED, // phys ptr to HC_ENDPOINT_DESCRIPTOR
                          *HcBulkHeadED,       // phys ptr to HC_ENDPOINT_DESCRIPTOR

⌨️ 快捷键说明

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