int main(void) { clock_setup(); gpio_setup(); // provides time_curr_us to usbh_poll function tim6_setup(); #ifdef USART_DEBUG usart_init(USART6, 921600); #endif LOG_PRINTF("\n\n\n\n\n###################\nInit\n"); /** * device driver initialization * * Pass configuration struct where the callbacks are defined */ hid_driver_init(&hid_config); hub_driver_init(); gp_xbox_driver_init(&gp_xbox_config); midi_driver_init(&midi_config); gpio_set(GPIOD, GPIO13); /** * Pass array of supported low level drivers * In case of stm32f407, there are up to two supported OTG hosts on one chip. * Each one can be enabled or disabled in usbh_config.h - optimization for speed * * Pass array of supported device drivers */ usbh_init(lld_drivers, device_drivers); gpio_clear(GPIOD, GPIO13); LOG_PRINTF("USB init complete\n"); LOG_FLUSH(); while (1) { // set busy led gpio_set(GPIOD, GPIO14); uint32_t time_curr_us = tim6_get_time_us(); usbh_poll(time_curr_us); // clear busy led gpio_clear(GPIOD, GPIO14); LOG_FLUSH(); // approx 1ms interval between usbh_poll() delay_ms_busy_loop(1); } return 0; }
USB_host::USB_host(callback_func _callback) { usb_host_pointer = this; callback = _callback; timer_setup(); oth_hs_setup(); hid_kbd_driver_init(&kbd_config); usbh_init(usbh_lld_stm32f4_drivers, device_drivers); }
bool tusb_init(void) { // skip if already initialized if (_initialized) return true; #if TUSB_OPT_HOST_ENABLED TU_VERIFY( usbh_init() ); // init host stack #endif #if TUSB_OPT_DEVICE_ENABLED TU_VERIFY ( usbd_init() ); // init device stack #endif _initialized = true; return TUSB_ERROR_NONE; }
void setUp(void) { helper_usbh_init_expect(); usbh_init(); dev_addr = RANDOM(CFG_TUSB_HOST_DEVICE_MAX)+1; // uint16_t length; // TEST_ASSERT_STATUS( hidh_open_subtask(dev_addr, p_mouse_interface_desc, &length) ); // // p_hidh_mouse = &mouse_data[dev_addr-1]; // // p_hidh_mouse->report_size = sizeof(hid_mouse_report_t); // p_hidh_mouse->pipe_hdl = (pipe_handle_t) { // .dev_addr = dev_addr, // .xfer_type = TUSB_XFER_INTERRUPT, // .index = 1 // }; }
static BOOL Init (U8 ctrl, U32 mode) { /* Initialize USB Host. */ U32 cnt; if (mode == DM_IO) { /* Initialise USB hardware. */ media_ok[ctrl] = __FALSE; return (usbh_init(ctrl)); } if (mode == DM_MEDIA) { for (cnt = 0; cnt < 2500; cnt++) { usbh_engine(ctrl); if (usbh_msc_status (ctrl, 0) == __TRUE) { media_ok[ctrl] = __TRUE; return (__TRUE); } Delay (1000); } } return (__FALSE); }
void usb0_host_test (usb_module_t *usbhModule) { int i, temp, periodic_base; usbPortSpeed_t usb_port_speed; // Speed of the interface usbDeviceDescriptor_t *device_descriptor; // Pointer to the Device descriptor uint8_t *config_descriptor; // Pointer to the Configuration descriptor uint8_t *interface_descriptor; // Pointer to the Interface descriptor uint8_t *hid_descriptor; // Pointer to the HID descriptor uint8_t *ep_descriptor; // Pointer to the Endpoint descriptor uint8_t *report_descriptor; // Pointer to the Report descriptor uint32_t core = (uint32_t)usbhModule->controllerID; usbhQueueHead_t *usb_qh_ep0, *usb_qh_ep1; // Pointers to Queue Heads for the endpoints usbhTransferDescriptor_t *int_qtd; // Pointer to the transfer descriptor uint8_t usbhMouseData[MAX_USB_BUFFER_SIZE]; // Buffer to receive the mouse data (from the interrupt endpoint) uint32_t int_transfer_size, int_packet_size, bytes_received; //! Allocate memory for the descriptors. device_descriptor = (usbDeviceDescriptor_t*) malloc (MAX_USB_DESC_SIZE); config_descriptor = (uint8_t*) malloc (MAX_USB_DESC_SIZE); interface_descriptor = (uint8_t*) malloc (MAX_USB_DESC_SIZE); hid_descriptor = (uint8_t*) malloc (MAX_USB_DESC_SIZE); ep_descriptor = (uint8_t*) malloc (MAX_USB_DESC_SIZE); report_descriptor = (uint8_t*) malloc (MAX_USB_DESC_SIZE); //! Initialize the USB host controller. usbh_init(usbhModule); printf("Waiting USB mouse connected...\n"); //! Wait for device connect. while(!(HW_USBC_PORTSC1_RD(core) & BM_USBC_UH1_PORTSC1_CCS)); printf("Connect detected.\n"); //! Reset the device usbh_bus_reset(usbhModule); //! Get current operating speed. usb_port_speed = usb_get_port_speed(usbhModule); /*! * Create a QueueHead to use for EndPoint0. This single QH will be the\n * asynchronous schedule during enumeration. */ switch (usb_port_speed) { case usbSpeedFull: usb_qh_ep0 = usbh_qh_init(0x40, 1, EPS_FULL, 0, 0, 0); printf("Device connected at FULL speed\n"); break; case usbSpeedLow: usb_qh_ep0 = usbh_qh_init(0x8, 1, EPS_LOW, 0, 0, 0); printf("Device connected at LOW speed\n"); break; case usbSpeedHigh: usb_qh_ep0 = usbh_qh_init(0x40, 1, EPS_HIGH, 0, 0, 0); printf("Device connected at HIGH speed\n"); break; default : return; } /*! * Put this queue head on the Asynchronous Schedule.\n * This is our first queue head on the AS so we point the controller to this QH\n * Any further queue heads will be linked to this HQ. */ HW_USBC_ASYNCLISTADDR_WR(core, (uint32_t)usb_qh_ep0); //! Enable the asynchronous schedule usbh_enable_asynchronous_schedule(usbhModule); //! Enumerate the attached device if (emuerateDevice(usbhModule, usb_qh_ep0, device_descriptor, config_descriptor, interface_descriptor, hid_descriptor, ep_descriptor, report_descriptor )) printf("\nUSB mouse enumerated!!\n"); else { printf("\nDevice is not a mouse. No further processing is performed\n"); while(1); /* Not a mouse, hang around forever */ } printf("\nMove mouse, and the mouse reports will be printed on the terminal.\n Press left button to exit.\n"); //! if we have a mouse connected. //! Initialize the periodic schedule for the interrupt endpoint periodic_base = usbh_periodic_schedule_init(usbhModule, FRAME_LIST_SIZE, (&frame_list[0])); //! Create a queue head for endpoint 1 usb_qh_ep1 = usbh_qh_init(0x8,0, EPS_LOW,1,DEVICE_ADDRESS,1); //! - Invalidate the QH horizontal pointer since the init function will point the QH back to itself. usb_qh_ep1->queueHeadLinkPointer |= USB_QH_LINK_PTR_T; /* This version of the code just polls the mouse once per iteration of * the frame list. The polling rate can be adjusted changing the size * of the frame list and/or pointing more of the frame list entries * to the interrupt QH. */ //! - Put the queue head on the periodic schedule *(uint32_t *)(periodic_base) = (uint32_t)usb_qh_ep1 + 0x002; /* * Initialize the amount of data to receive. In this case we will * receive 20 packets per loop. ep_desc[04] inidicates the size of * each packet. So transfer_size = 20 * ep_desc[04]. */ int_packet_size = ep_descriptor[04]; int_transfer_size = 20 * int_packet_size; //! Create a qTD to transfer 20 packets worth of data int_qtd = usbh_qtd_init(int_transfer_size, 1, IN_PID, (uint32_t*) usbhMouseData); //! Activate the queue head to start polling the device /* * This while(1) loop will allow for continuously receiving mouse data. Some * packets could be lost due to the time needed to reinitialize the qTD for the * next batch of transfers. So a more correct way to do this would be to create * multiple qTDs and rotate them for each iteration of the loop. For a mouse * application some data loss is acceptable, so only one qTD is used. */ while(1) { //! - Point the QH to the qTD usb_qh_ep1->nextQtd = (uint32_t) int_qtd; /* Initialize bytes received counter */ bytes_received = 0; while (bytes_received < int_transfer_size) { //! - Wait for a transaction to complete while(!(HW_USBC_USBSTS_RD(core) & BM_USBC_UH1_USBSTS_UI)); HW_USBC_USBSTS_WR(core, HW_USBC_USBSTS_RD(core) | BM_USBC_UH1_USBSTS_UI); //! - Check for errors if(HW_USBC_USBSTS_RD(core) & BM_USBC_UH1_USBSTS_UEI) { printf("ERROR!!!\n"); temp = *(uint32_t *)((HW_USBC_ASYNCLISTADDR_RD(core)) + 0x18); printf("qTD status = 0x%08x\n",temp); // Clear error flag HW_USBC_USBSTS_WR(core, HW_USBC_USBSTS_RD(core) | BM_USBC_UH1_USBSTS_UEI); } else //! - Display data if no error occurred. { printf("IN = "); for(i=0; i < int_packet_size; i++) printf("0x%02x ",usbhMouseData[bytes_received + i]); printf("\n"); } //! - If Left mouse button pressed, exit and return to calling routine if((uint8_t)usbhMouseData[bytes_received] == 1) // Left mouse button { return; // exit } /* Increment bytes received counter */ bytes_received = bytes_received + int_packet_size; /* Set the active bit in the QH to ensure that it can accept * more data if we aren't done yet. */ if (bytes_received != int_transfer_size) *(uint32_t *)((uint32_t)usb_qh_ep1+0x18) |= 0x00000080; } //! Re-initialize the qTD to accept the next 20 packets int_qtd->qtdToken |= USB_QTD_TOKEN_TRANS_SIZE(int_transfer_size) | USB_QTD_TOKEN_STAT_ACTIVE; int_qtd->qtdBuffer[0] = (uint32_t) usbhMouseData; } }