rusb.c

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

C
524
字号
//-----------------------------------------------------------------------------
// RUSB
//-----------------------------------------------------------------------------
#include "rusb.h"
#include "hal_gpio.h"
#include <stdlib.h>
#include <string.h>

//-----------------------------------------------------------------------------
tOHCI *ohci = NULL;
u8 *buffer = NULL;

//-----------------------------------------------------------------------------
void OHCI_Init (tOHCI_REGS *ohci_regs_base, u32 ohci_irq)
{
  rDiagMessage ("OHCI_Init.");

  //---Enable USB on ports (clear this bit)---
  CONFR->GLOBAL_CONTROL &= ~CONFR_nUSB_ENABLE;

  //---OHCI---
  ohci = rmalloc (sizeof(tOHCI),"OHCI");
  ohci->regs = ohci_regs_base;
  ohci->device        = rmalloc (sizeof(tDEVICE),"ohci->device");
  ohci->setup_pipe0_0 = rmalloc (sizeof(tPIPE),"ohci->setup_pipe0_0");
  ohci->setup_pipe1_0 = rmalloc (sizeof(tPIPE),"ohci->setup_pipe1_0");
  ohci->setup_pipe1_1 = rmalloc (sizeof(tPIPE),"ohci->setup_pipe1_1");

  //---Clear pointer registers---
  ohci->regs->HcPeriodCurrentED = NULL;
  ohci->regs->HcControlHeadED = NULL;
  ohci->regs->HcControlCurrentED = NULL;
  ohci->regs->HcBulkHeadED = NULL;
  ohci->regs->HcBulkCurrentED = NULL;
  ohci->regs->HcDoneHead = NULL;
  ohci->regs->HcHCCA = NULL;

  //---Disable interrupts---
  ohci->regs->HcInterruptDisable = 0xFFFFFFFF;
  IC_Int_OnOff    (IC_USB,DISABLE);

  //---Interval values---
  u32 FmInterval = ohci->regs->HcFmInterval & 0x3FFF;
  u32 FSMPS = (((FmInterval - 210) * 6) / 7) << 16;
  u32 Periodic_start = 9*FmInterval/10;
//u32 Periodic_start = FmInterval;

  //---Take owner chip---
  TakeOwnerShip (ohci);

  //---Software reset---
  ohci->regs->HcCommandStatus = HCCS_HCR;

  //---Set interrupts---
//IC_Register_IrqFcn (ohci_irq, OHCI_isr);
  ohci->regs->HcInterruptStatus |= 0xFFFFFFFF; //clear all interrupt flags
  IC_Int_Config   (IC_USB,IC_LOW_LEVEL);
//IC_Global_OnOff (IC_IRQ,ENABLE);
//IC_Int_OnOff    (IC_USB,ENABLE);

  //---Wait to the reset is completed---
  while (ohci->regs->HcCommandStatus & HCCS_HCR);
  TimeDelay (10000); //wait 10us

  //---HCCA area---
  ohci->hcca = (tHCCA*)rmalloc256B ("HCCA");
  ohci->regs->HcHCCA = ohci->hcca;

  //---Enter USBReset state---
  ohci->regs->HcControl = 0;
  TimeDelay (10000);

  //---Interval registers---
  ohci->regs->HcPeriodicStart = Periodic_start;
  ohci->regs->HcPeriodicStart = FmInterval;
  ohci->regs->HcFmInterval    = FSMPS | FmInterval;
  ohci->regs->HcPeriodicStart = Periodic_start;

  if (ohci->regs->HcFmInterval != (FSMPS | FmInterval))
    rExit("Failed to set HcFmInterval register");

  if (ohci->regs->HcPeriodicStart != Periodic_start)
    rExit("Failed to set HcPeriodicStart register");

  //---Enable relevant interrupts---
  ohci->regs->HcInterruptEnable = (HCI_SO  |   // scheduler overrrun
                                   HCI_WDH |   // DoneTDs
                                 //HCI_SF  |   // start of frame
                                   HCI_RD  |   // resume detect
                                   HCI_UE  |   // unrecoverable error
                                   HCI_FNO |   // frame number overflow
                                 //HCI_RHSC|   // root hub status change
                                   HCI_OC  |   // ownership change
                                   HCI_MIE |  // master int enable
                                   0);

  //---Set HUB---
  ohci->regs->HcRhDescriptorA = HCRHDA_NOCP | HCRHDA_NPS;
  ohci->regs->HcRhDescriptorB = HCRHDA_PPCM_PORT1_MASK_ON | HCRHDA_DP_PORT1_NO_REMOVABLE;
//ohci->regs->HcRhStatus &= ~HCRHS_LPSC_POWER_ON;

  //---Set USB mode to operational---
  ohci->regs->HcControl = (ohci->regs->HcControl & ~HCC_HCFS) | HCC_HCFS_USBOPERATIONAL;
  TimeDelay (100*TIME_DELAY_1MS);

  //---HcRhPortStatus---
  while ((ohci->regs->HcRhPortStatus[0] & HCRHPS_CSC) == 0);  //Active wait until the device is un-connected
//ohci->regs->HcRhPortStatus[0] |= HCRHPS_CSC;                //Device connected, clear the status change bit
  TimeDelay (100*TIME_DELAY_1MS);                             //Wait 100ms (see USB 1.1 spec, page 179)

  while (1)
  {
    OHCI_PortReset ();
    OHCI_GetDescriptor (); 
    //Check which device is connected here
    //If it's your device continue
    OHCI_PortReset ();
    OHCI_SetAddress ();
    TimeDelay (60*TIME_DELAY_1MS);
    //OHCI_GetConfiguration ();
    OHCI_SetConfiguration ();

    //reading data from the device
    while (((ohci->regs->HcRhPortStatus[0] & HCRHPS_CCS) != 0))
      OHCI_MouseIn ();
  }

}

//-----------------------------------------------------------------------------
void OHCI_GetDescriptor (void)
{
  //---setup_pipe0_0->ed & setup_pipe0_0->td_zeros---
  OHCI_Create_ed              (&(ohci->setup_pipe0_0->ed), &(ohci->setup_pipe0_0->td_zeros), 0, 0, EDC_D_SEE_TD, EDC_S_FULL_SPEED, EDC_K_NO_SKIP, EDC_F_ISOCHRONOUS, 0x20);

  //---ohci->setup_pipe0_0->td_setup---
  OHCI_Create_ControlPacket   (&(ohci->setup_pipe0_0->td_setup_buffer), BMRT_DEVICE_TO_HOST|BMRT_STANDARD|BMRT_DEVICE, BR_GET_DESCRIPTOR, WV_DEVICE_DESCRIPTOR, 0, 18);
  OHCI_Create_General_td      (&(ohci->setup_pipe0_0->ed), &(ohci->setup_pipe0_0->td_setup), &(ohci->setup_pipe0_0->td_setup_buffer), 8, TDC_R_BUFER_EXACT_FILL, TDC_DP_SETUP, 0, TDC_T_DATA0, 0, TDC_CC_SET, 1);

  //---ohci->setup_pipe0_0->td_in0---
  ohci->setup_pipe0_0->td_in0_buffer = rmalloc (8,"ohci->setup_pipe0_0->td_in0_buffer");
  OHCI_Create_General_td      (&(ohci->setup_pipe0_0->ed), &(ohci->setup_pipe0_0->td_in0), &(ohci->setup_pipe0_0->td_in0_buffer), 8, TDC_R_BUFER_EXACT_FILL, TDC_DP_IN, 0, TDC_T_DATA1, 0, TDC_CC_SET, 1);

  //---ohci->setup_pipe0_0->td_in1---
  ohci->setup_pipe0_0->td_in1_buffer = rmalloc (8,"ohci->setup_pipe0_0->td_in1_buffer");
  OHCI_Create_General_td      (&(ohci->setup_pipe0_0->ed), &(ohci->setup_pipe0_0->td_in1), &(ohci->setup_pipe0_0->td_in1_buffer), 8, TDC_R_BUFER_EXACT_FILL, TDC_DP_IN, 0, TDC_T_DATA0, 0, TDC_CC_SET, 1);

  //---ohci->setup_pipe0_0->td_in2---
  ohci->setup_pipe0_0->td_in2_buffer = rmalloc (2,"ohci->setup_pipe0_0->td_in2_buffer");
  OHCI_Create_General_td      (&(ohci->setup_pipe0_0->ed), &(ohci->setup_pipe0_0->td_in2), &(ohci->setup_pipe0_0->td_in2_buffer), 2, TDC_R_BUFER_EXACT_FILL, TDC_DP_IN, 0, TDC_T_DATA1, 0, TDC_CC_SET, 1);

  //---Enable---
  ohci->regs->HcControlHeadED = ohci->setup_pipe0_0->ed;
  ohci->regs->HcControlCurrentED = ohci->setup_pipe0_0->ed;
  ohci->regs->HcInterruptStatus |= HCI_WDH;
  ohci->regs->HcCommandStatus |= HCCS_CLF;
  ohci->regs->HcControl |= HCC_CLE;

  //---Process data---
  while ((ohci->regs->HcInterruptStatus & HCI_WDH) == 0);
  while (ohci->setup_pipe0_0->td_in2->CBP != 0);

  //---Get data---
  ohci->device->DeviceDescriptor0 = (tDeviceDescriptor0*) ohci->setup_pipe0_0->td_in0_buffer;
  ohci->device->DeviceDescriptor1 = (tDeviceDescriptor1*) ohci->setup_pipe0_0->td_in1_buffer;
  ohci->device->DeviceDescriptor2 = (tDeviceDescriptor2*) ohci->setup_pipe0_0->td_in2_buffer;

  //---Free---
  rfree    (ohci->setup_pipe0_0->td_setup_buffer);
  rfree16B (ohci->setup_pipe0_0->td_setup);
  rfree16B (ohci->setup_pipe0_0->td_in0);
  rfree16B (ohci->setup_pipe0_0->td_in1);
  rfree16B (ohci->setup_pipe0_0->td_in2);

  ohci->setup_pipe0_0->td_setup_buffer = NULL;
  ohci->setup_pipe0_0->td_in0_buffer = NULL;
  ohci->setup_pipe0_0->td_in1_buffer = NULL;
  ohci->setup_pipe0_0->td_in2_buffer = NULL;
  ohci->setup_pipe0_0->td_setup = NULL;
  ohci->setup_pipe0_0->td_in0 = NULL;
  ohci->setup_pipe0_0->td_in1 = NULL;
  ohci->setup_pipe0_0->td_in2 = NULL;
}

//-----------------------------------------------------------------------------
void OHCI_SetAddress (void)
{
  //---ed0setup_td5setup---
  OHCI_Create_ControlPacket   (&(ohci->setup_pipe0_0->td_setup_buffer), BMRT_HOST_TO_DEVICE|BMRT_STANDARD|BMRT_DEVICE, BR_SET_ADDRESS, 1, 0, 0);
  OHCI_Create_General_td      (&(ohci->setup_pipe0_0->ed), &(ohci->setup_pipe0_0->td_setup), &(ohci->setup_pipe0_0->td_setup_buffer), 8, TDC_R_BUFER_EXACT_FILL, TDC_DP_SETUP, 0, TDC_T_DATA0, 0, TDC_CC_SET, 1);

  //---ohci->setup_pipe0_0->td_in0---
  OHCI_Create_General_td      (&(ohci->setup_pipe0_0->ed), &(ohci->setup_pipe0_0->td_in0), &(ohci->setup_pipe0_0->td_in0_buffer), 0, TDC_R_BUFER_EXACT_FILL, TDC_DP_IN, 0, TDC_T_DATA1, 0, TDC_CC_SET, 1);

  //---Enable---
  ohci->regs->HcInterruptStatus |= HCI_WDH;
  ohci->regs->HcCommandStatus |= HCCS_CLF;

  //---Process data---
  while ((ohci->regs->HcInterruptStatus & HCI_WDH) == 0);
  while (ohci->setup_pipe0_0->td_in0->CBP != 0);

  //---Free---
  rfree    (ohci->setup_pipe0_0->td_setup_buffer);
  rfree16B (ohci->setup_pipe0_0->td_setup);
  rfree16B (ohci->setup_pipe0_0->td_in0);

  ohci->setup_pipe0_0->td_setup_buffer = NULL;
  ohci->setup_pipe0_0->td_setup = NULL;
  ohci->setup_pipe0_0->td_in0 = NULL;
}

//-----------------------------------------------------------------------------
void OHCI_GetConfiguration (void)
{
  //---setup_pipe0_0->ed & setup_pipe0_0->td_zeros---
  if (ohci->setup_pipe1_0->ed == NULL)
    OHCI_Create_ed              (&(ohci->setup_pipe1_0->ed), &(ohci->setup_pipe1_0->td_zeros), 1, 0, EDC_D_SEE_TD, EDC_S_FULL_SPEED, EDC_K_NO_SKIP, EDC_F_ISOCHRONOUS, 0x20);

  //---ohci->setup_pipe0_0->td_setup---
  OHCI_Create_ControlPacket   (&(ohci->setup_pipe1_0->td_setup_buffer), BMRT_DEVICE_TO_HOST|BMRT_STANDARD|BMRT_DEVICE, BR_GET_CONFIGURATION, 0, 0, 1);
  OHCI_Create_General_td      (&(ohci->setup_pipe1_0->ed), &(ohci->setup_pipe1_0->td_setup), &(ohci->setup_pipe1_0->td_setup_buffer), 8, TDC_R_BUFER_EXACT_FILL, TDC_DP_SETUP, 0, TDC_T_DATA0, 0, TDC_CC_SET, 1);

  //---ohci->setup_pipe0_0->td_in0---
  ohci->setup_pipe0_0->td_in0_buffer = rmalloc (1,"ohci->setup_pipe1_0->td_in0_buffer");
  OHCI_Create_General_td      (&(ohci->setup_pipe1_0->ed), &(ohci->setup_pipe1_0->td_in0), &(ohci->setup_pipe1_0->td_in0_buffer), 1, TDC_R_BUFER_EXACT_FILL, TDC_DP_IN, 0, TDC_T_DATA1, 0, TDC_CC_SET, 1);

  //---Enable---
  OHCI_enqueue_ed (&(ohci->regs->HcControlHeadED),&(ohci->setup_pipe1_0->ed));
  ohci->regs->HcInterruptStatus |= HCI_WDH;
  ohci->regs->HcCommandStatus |= HCCS_CLF;

  //---Process data---
  while ((ohci->regs->HcInterruptStatus & HCI_WDH) == 0);
  while (ohci->setup_pipe1_0->td_in0->CBP != 0);

  //---Get data---
  ohci->device->Configuration = ohci->setup_pipe1_0->td_in0_buffer;

  //---Free---
  rfree    (ohci->setup_pipe1_0->td_setup_buffer);
  rfree16B (ohci->setup_pipe1_0->td_setup);
  rfree16B (ohci->setup_pipe1_0->td_in0);

  ohci->setup_pipe1_0->td_setup_buffer = NULL;
  ohci->setup_pipe0_0->td_in0_buffer = NULL;
  ohci->setup_pipe1_0->td_setup = NULL;
  ohci->setup_pipe1_0->td_in0 = NULL;
}

//-----------------------------------------------------------------------------
void OHCI_SetConfiguration (void)
{
  //---setup_pipe0_0->ed & setup_pipe0_0->td_zeros---
  if (ohci->setup_pipe1_0->ed == NULL)
    OHCI_Create_ed              (&(ohci->setup_pipe1_0->ed), &(ohci->setup_pipe1_0->td_zeros), 1, 0, EDC_D_SEE_TD, EDC_S_FULL_SPEED, EDC_K_NO_SKIP, EDC_F_ISOCHRONOUS, 0x20);

  //---ohci->setup_pipe0_0->td_setup---
  OHCI_Create_ControlPacket   (&(ohci->setup_pipe1_0->td_setup_buffer), BMRT_HOST_TO_DEVICE|BMRT_STANDARD|BMRT_DEVICE, BR_SET_CONFIGURATION, 1, 0, 0);
  OHCI_Create_General_td      (&(ohci->setup_pipe1_0->ed), &(ohci->setup_pipe1_0->td_setup), &(ohci->setup_pipe1_0->td_setup_buffer), 8, TDC_R_BUFER_EXACT_FILL, TDC_DP_SETUP, 0, TDC_T_DATA0, 0, TDC_CC_SET, 1);

  //---ohci->setup_pipe0_0->td_in0---

⌨️ 快捷键说明

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