////////////////////////////////////////////////////////////////////// // // THREAD FUNCTION TO RUN USB HOST PORT // ////////////////////////////////////////////////////////////////////// void RunHostPort(HOST_PORT_DATA *pHostData) { unsigned char status; unsigned char buf[64]; unsigned short num_read; unsigned int handle; usbhostGeneric_ioctl_t generic_iocb; usbhostGeneric_ioctl_cb_attach_t genericAtt; usbhost_device_handle_ex ifDev; usbhost_ioctl_cb_t hc_iocb; usbhost_ioctl_cb_vid_pid_t hc_iocb_vid_pid; gpio_ioctl_cb_t gpio_iocb; SMQ_MSG msg; msg.status = 0x90; // wait for setup to complete vos_wait_semaphore(&setupSem); vos_signal_semaphore(&setupSem); // loop forever while(1) { // is the device enumerated on this port? if (usbhost_connect_state(pHostData->hUSBHOST) == PORT_STATE_ENUMERATED) { // user ioctl to find first hub device hc_iocb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_GET_NEXT_HANDLE; hc_iocb.handle.dif = NULL; hc_iocb.set = NULL; hc_iocb.get = &ifDev; if (vos_dev_ioctl(pHostData->hUSBHOST, &hc_iocb) == USBHOST_OK) { hc_iocb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_GET_VID_PID; hc_iocb.handle.dif = ifDev; hc_iocb.get = &hc_iocb_vid_pid; // attach the Launchpad device to the USB host device pHostData->hUSBHOSTGENERIC = vos_dev_open(pHostData->uchDeviceNumber); genericAtt.hc_handle = pHostData->hUSBHOST; genericAtt.ifDev = ifDev; generic_iocb.ioctl_code = VOS_IOCTL_USBHOSTGENERIC_ATTACH; generic_iocb.set.att = &genericAtt; if (vos_dev_ioctl(pHostData->hUSBHOSTGENERIC, &generic_iocb) == USBHOSTGENERIC_OK) { setLed(pHostData->uchActivityLed, 1); // now we loop until the launchpad is detached while(1) { int pos = 0; byte param = 1; // read data status = vos_dev_read(pHostData->hUSBHOSTGENERIC, buf, 64, &num_read); if(status != USBHOSTGENERIC_OK) break; setLed(pHostData->uchActivityLed, 0); // interpret MIDI data and pass to application thread while(pos < num_read) { if(buf[pos] & 0x80) { msg.status = buf[pos]; param = 1; } else if(param == 1) { msg.param1 = buf[pos]; param = 2; } else if(param == 2) { msg.param2 = buf[pos]; SMQWrite(&msg); param = 1; } ++pos; } setLed(pHostData->uchActivityLed, 1); } } vos_dev_close(pHostData->hUSBHOSTGENERIC); } } setLed(pHostData->uchActivityLed, 0); } }
////////////////////////////////////////////////////////////////////// // // RUN USB HOST PORT // ////////////////////////////////////////////////////////////////////// void RunHostPort(HOST_PORT_DATA *pHostData) { int i; int midiParam; unsigned char status; unsigned char buf[64]; unsigned short num_bytes; unsigned int handle; usbhostGeneric_ioctl_t generic_iocb; usbhostGeneric_ioctl_cb_attach_t genericAtt; usbhost_device_handle_ex ifDev; usbhost_ioctl_cb_t hc_iocb; usbhost_ioctl_cb_vid_pid_t hc_iocb_vid_pid; gpio_ioctl_cb_t gpio_iocb; VOS_HANDLE hUSB; // wait for setup to complete vos_wait_semaphore(&setupSem); vos_signal_semaphore(&setupSem); // Open the base USB Host driver pHostData->hUSBHOST = vos_dev_open(pHostData->uchDeviceNumberBase); // loop forever while(1) { vos_delay_msecs(10); // is the device enumerated on this port? if (usbhost_connect_state(pHostData->hUSBHOST) == PORT_STATE_ENUMERATED) { // user ioctl to find first hub device hc_iocb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_GET_NEXT_HANDLE; hc_iocb.handle.dif = NULL; hc_iocb.set = NULL; hc_iocb.get = &ifDev; if (vos_dev_ioctl(pHostData->hUSBHOST, &hc_iocb) == USBHOST_OK) { // query the device VID/PID hc_iocb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_GET_VID_PID; hc_iocb.handle.dif = ifDev; hc_iocb.get = &hc_iocb_vid_pid; // Load the function driver hUSB = vos_dev_open(pHostData->uchDeviceNumber); // Attach the function driver to the base driver genericAtt.hc_handle = pHostData->hUSBHOST; genericAtt.ifDev = ifDev; generic_iocb.ioctl_code = VOS_IOCTL_USBHOSTGENERIC_ATTACH; generic_iocb.set.att = &genericAtt; if (vos_dev_ioctl(hUSB, &generic_iocb) == USBHOSTGENERIC_OK) { // Turn on the LED for this port setGpioA(pHostData->uchActivityLed, pHostData->uchActivityLed); // flag that the port is attached VOS_ENTER_CRITICAL_SECTION; pHostData->hUSBHOSTGENERIC = hUSB; VOS_EXIT_CRITICAL_SECTION; // now we loop until the launchpad is detached while(1) { // listen for data from launchpad uint16 result = vos_dev_read(hUSB, buf, 64, &num_bytes); if(0 != result) break; // break when the launchpad is detached fifo_write(&stSPIWriteFIFO, buf, (int)num_bytes); } // flag that the port is no longer attached VOS_ENTER_CRITICAL_SECTION; pHostData->hUSBHOSTGENERIC = NULL; VOS_EXIT_CRITICAL_SECTION; // turn off the activity LED setGpioA(pHostData->uchActivityLed, 0); } // close the function driver vos_dev_close(hUSB); } } } }
void firmware() { /* Thread code to be added here */ // communications ioctl request blocks common_ioctl_cb_t uart_iocb; usbHostHID_ioctl_t hid_iocb; usbhost_device_handle ifDev; // device handle usbhost_ioctl_cb_t hc_iocb; // Host Controller ioctl request block unsigned char byteCount,status; unsigned short num_read; do { open_drivers(); // UART set baud rate uart_iocb.ioctl_code = VOS_IOCTL_UART_SET_BAUD_RATE; uart_iocb.set.uart_baud_rate = DEF_UART_BAUD; vos_dev_ioctl(hUART, &uart_iocb); do { vos_delay_msecs(1000); // wait for enumeration to complete // message("Waiting for enumeration\r\n"); status = usbhost_connect_state(hUSBHOST_2); } while (status != PORT_STATE_ENUMERATED); if (status == PORT_STATE_ENUMERATED) { // message("Enumeration complete\r\n"); vos_gpio_set_port_mode(GPIO_PORT_A, 0b00000100); // A2 на выход vos_gpio_write_port(GPIO_PORT_A, 0b00000100); // зажечь светодиод attach_drivers(); if (hUSBHOST_HID == NULL) { // message("No Wingman Found - code "); // number(status); // message(eol); break; } // get report descriptor hid_iocb.descriptorType = USB_DESCRIPTOR_TYPE_REPORT; hid_iocb.descriptorIndex = USB_HID_DESCRIPTOR_INDEX_ZERO; hid_iocb.Length = 0x40; hid_iocb.get.data=&buf[0]; hid_iocb.ioctl_code = VOS_IOCTL_USBHOSTHID_GET_DESCRIPTOR; status = vos_dev_ioctl(hUSBHOST_HID, &hid_iocb); if (status != USBHOSTHID_OK) { // message("Get report descriptor failed - code "); // number(status); // message(eol); break; } // set idle hid_iocb.reportID = USB_HID_REPORT_ID_ZERO; hid_iocb.idleDuration = 0x20; hid_iocb.ioctl_code = VOS_IOCTL_USBHOSTHID_SET_IDLE; status = vos_dev_ioctl(hUSBHOST_HID, &hid_iocb); if (status != USBHOSTHID_OK) { // message("Set Idle failed - code "); // number(status); // message(eol); break; } // get idle hid_iocb.reportID = USB_HID_REPORT_ID_ZERO; hid_iocb.get.data=&buf[0]; hid_iocb.ioctl_code = VOS_IOCTL_USBHOSTHID_GET_IDLE; status = vos_dev_ioctl(hUSBHOST_HID, &hid_iocb); if (status != USBHOSTHID_OK) { // message("Get Idle failed - code "); // number(status); // message(eol); break; } if (status == USBHOSTHID_OK) { while (1) { if (vos_dev_read(hUSBHOST_HID, buf, 8, &num_read) == USBHOSTHID_OK) { for (byteCount = 0; byteCount < num_read; byteCount++) { number(buf[byteCount]); } // message(eol); } else { // message("USB Read Failed - code "); // number(status); // message(eol); break; } } } // message("Disconnected!\r\n"); vos_gpio_write_port(GPIO_PORT_A, 0b00000000); // погасить все } // end of if PORT_STATE_ENUMERATED vos_dev_close(hUSBHOST_HID); close_drivers(); } while (1); }