//------------------------------------------------------------------------------
/// Initializes the USB process
/// Depending on the mode supported (HOST/DEVICE/DUAL_ROLE) the function calls
/// the coresponding USB mode initialization function
//------------------------------------------------------------------------------
void usb_task_init(void)
{
    // DUAL ROLE DEVICE USB MODE
    Otg_message_task_init();       // OTG program needs to display event messages to the user
    b_uut_device_state = B_IDLE;   // init state machines variables
    device_state = DEVICE_UNATTACHED;
    otg_device_nb_hnp_retry = BDEV_HNP_NB_RETRY;
    Clear_all_user_request();
    Usb_enable_uid_pin();

    g_usb_mode=USB_MODE_UNDEFINED;

    if(Is_usb_id_device()) {
        g_usb_mode=USB_MODE_DEVICE;
#ifdef MSD_PROJECT
        MSDDriver_Initialize(luns, numMedias);
#else
        // HID
        HIDDMouseDriver_Initialize();
#endif
        id_changed_to_host_event = DISABLE;
    }
    else {
        Usb_send_event(EVT_USB_HOST_FUNCTION);
        g_usb_mode=USB_MODE_HOST;
        // Startup with ID=0, Ack ID pin transistion (default hwd start up is device mode)
        Usb_ack_id_transition(); 
        Usb_enable_id_interrupt();
        usb_host_task_init();
        id_changed_to_host_event = ENABLE;
    }
    // Store current usb mode, for mode change detection
    g_old_usb_mode = g_usb_mode;
}
Beispiel #2
0
int main(void)
{
    unsigned char bmButtons = 0;

    TRACE_CONFIGURE(DBGU_STANDARD, 115200, BOARD_MCK);
    printf("-- USB Device HID Mouse Project %s --\n\r", SOFTPACK_VERSION);
    printf("-- %s\n\r", BOARD_NAME);
    printf("-- Compiled: %s %s --\n\r", __DATE__, __TIME__);

    // SPP +
    int i;

    button_flag = 0;

    //Enable RESET
    m_pRSTC->RSTC_RCR = 0xA5000008;
    m_pRSTC->RSTC_RMR = 0xA5000001;
    Delay(1000);

    // Init USB device
    //AT91F_USB_Open();

    // Configure the RTT:
    *AT91C_RTTC_RTMR = BUTTON_SAMPLING;

    // Set in PIO mode and Configure in Input
    //AT91F_PIOA_CfgPMC();

    // InitMAM
    InitMMA();

    // InitADC
    ADCInit();

    // UART0 Init
    InitUSART0();

    // Stat led
    s_pPio->PIO_PER   = BIT31;
    // Configure PA31 as output
    s_pPio->PIO_OER   = BIT31;
    // Set PA31 to HIGH
    s_pPio->PIO_SODR  = BIT31;


    // Test flash
    if(TestFlash()) 
    {
      for(i=0; i<20; i++) 
      {
        s_pPio->PIO_CODR  = BIT31; Delay(150000);
        s_pPio->PIO_SODR  = BIT31; Delay(150000);
      }
    }

    // Button
    // Configure P20 as input
    s_pPio->PIO_ODR   = BIT20;
    // Enable
    s_pPio->PIO_PER   = BIT20;

    Delay(1000);

    // CALIBRATE WHEN BUTT IS PRESS
    // while((s_pPio->PIO_PDSR&BIT20)==BIT20);

    Delay(1000);
    // SPP -

    // If they are present, configure Vbus & Wake-up pins
    PIO_InitializeInterrupts(0);

    WAKEUP_CONFIGURE();

    // If there is on board power, switch it off
  #ifdef PIN_USB_POWER_ENB
  { const Pin pinUsbPwr = PIN_USB_POWER_ENB;
    PIO_Configure(&pinUsbPwr, 1);
  }
  #endif

    // Initialize key statuses and configure push buttons
#if defined(at91cap9dk)
    const Pin pinRow0 = PIN_KEYBOARD_ROW0;
    PIO_Configure(&pinRow0, 1);
#endif       
    PIO_Configure(pinsJoystick, PIO_LISTSIZE(pinsJoystick));

    // HID driver initialization
    HIDDMouseDriver_Initialize();

    // connect if needed
    VBUS_CONFIGURE();
    
    // Infinite loop
    while (1) 
    {
       // SPP +
       #define TIMEOUT  100
       static int Timeout = TIMEOUT;
        GetCoordinates();
        base_y = Coordinates[0]/NSAMPLE;
        base_x = Coordinates[1]/NSAMPLE;
        base_z = Coordinates[2]/NSAMPLE;
       // SPP -

        if( USBState == STATE_SUSPEND ) 
        {
            TRACE_DEBUG("suspend  !\n\r");
            USBState = STATE_IDLE;
            LowPowerMode();
        }
        if( USBState == STATE_RESUME ) 
        {
            // Return in normal MODE
            NormalPowerMode();
            USBState = STATE_IDLE;
            TRACE_DEBUG("resume !\n\r");
        }
        if (USBD_GetState() < USBD_STATE_CONFIGURED)
            continue;

        if (!Timeout)
        {
        
            unsigned char status;

            do 
            {
               status = HIDDMouseDriver_ChangePoints(bmButtons, base_x, base_y);
            }
            while (status != USBD_STATUS_SUCCESS);
            Timeout = TIMEOUT;
        }
        else
           Timeout--;
        
    }
}