// Declare Main application void main(void) { // Initialize variables vos_init_semaphore(&DevicesStarted, 0); vos_init_mutex(&dprintLock, VOS_MUTEX_UNLOCKED); Delay = 511; vos_init_semaphore(&EnumerationComplete, 0); vos_init_semaphore(&SendKeyboardReport, 0); // Initialise RTOS vos_init(VOS_QUANTUM, VOS_TICK_INTERVAL, NUMBER_OF_DEVICES); // Sets the CPU frequency of the connected device. vos_set_clock_frequency(VOS_48MHZ_CLOCK_FREQUENCY); // Initialise devices if (InitDevices()) { // Initialise threads - pass a ThreadID to each thread vos_create_thread(28, SIZEOF_tcb, &Blink, 1, 1); vos_create_thread(27, SIZEOF_tcb, &EnumerateSlave, 1, 2); vos_create_thread(26, SIZEOF_tcb, &FindKeyboard, 1, 4); vos_create_thread(25, SIZEOF_tcb, &SendReports, 1, 8); vos_create_thread( 1, SIZEOF_tcb, &MyIdleTask, 0); // Start Scheduler to kick off the created thread(s) vos_start_scheduler(); } // It is an error to get here, use a breakpoint to catch this while (1) { CheckStatus(1, ErrorSchedulerDidNotStart); } }
void fifo_init(FIFO_TYPE *pfifo) { memset(pfifo, 0, sizeof(FIFO_TYPE)); vos_init_semaphore(&pfifo->semRead, 0); vos_init_semaphore(&pfifo->semWrite, SZ_FIFO-1); vos_init_mutex(&pfifo->mutex, VOS_MUTEX_UNLOCKED); }
void CreateKeystrokes(BYTE ThreadID) { // Wait for the keyboard to be enumerated then send keystrokes // Owns KeyboardMessage at startup vos_semaphore_t ReturnedSemaphore; BYTE Report[8]; BYTE i = 0; memset(&Report[0], 0, sizeof(Report)); WaitForDevicesStarted(); vos_init_semaphore(&ReturnedSemaphore, 0); vos_wait_semaphore(&EnumerationComplete); dprint("\nEnumeration complete, sending keystrokes ", 0); KeyboardMessage.ReportBuffer = &Report[0]; KeyboardMessage.ReportLength = sizeof(Report); KeyboardMessage.ResponseSemaphore = &ReturnedSemaphore; while (1) { Report[0] = ModifierCodes[i]; Report[2] = UsageCodes[i]; i_vos_signal_semaphore(ThreadID, &SendKeyboardReport); // Keydown i_vos_wait_semaphore(ThreadID, &ReturnedSemaphore); i_vos_delay_msecs(ThreadID, 50); Report[0] = 0; Report[2] = 0; i_vos_signal_semaphore(ThreadID, &SendKeyboardReport); // Keyup i_vos_wait_semaphore(ThreadID, &ReturnedSemaphore); i_vos_delay_msecs(ThreadID, 200); if (++i >= sizeof(UsageCodes)) { i=0; i_vos_delay_msecs(ThreadID, 1000); } } }
// Declare Main application void main(void) { // Initialize variables vos_init_semaphore(&DevicesStarted, 0); // Initialise RTOS vos_init(VOS_QUANTUM, VOS_TICK_INTERVAL, NUMBER_OF_DEVICES); // Sets the CPU frequency of the connected device. vos_set_clock_frequency(VOS_48MHZ_CLOCK_FREQUENCY); // Initialise devices if (InitDevices()) { // Initialise thread(s) vos_create_thread(27, SIZEOF_tcb, &HelloWorld, 0); vos_create_thread(28, SIZEOF_tcb, &Blink, 0); // Start Scheduler to kick off the created thread(s) vos_start_scheduler(); } // It is an error to get here, use a breakpoint to catch this while (1) { CheckStatus(FALSE, ErrorSchedulerDidNotStart); } }
///////////////////////////////////////////////////////////////////////////// // INIT void SMQInit() { head = 0; tail = 0; vos_init_semaphore(&semAvailable,0); vos_init_mutex(&mAccess,0); }
// This function MUST BE CALLED BEFORE THE SCHEDULER IS STARTED // It initializes (initialises for you Brits reading this) variables // in the context structure, registers our driver with the // VOS device manager, and creates the thread for handling // requests on the control endpoint. unsigned char usbslaveboms_init(unsigned char vos_dev_num) { vos_driver_t *usbSlaveBoms_cb; slaveBomsCtx = vos_malloc(sizeof(usbSlaveBoms_context)); if (slaveBomsCtx == NULL) return USBSLAVEBOMS_ERROR; //somehow ran out of RAM usbSlaveBoms_cb = vos_malloc(sizeof(vos_driver_t)); if (usbSlaveBoms_cb == NULL) { vos_free(slaveBomsCtx); return USBSLAVEBOMS_ERROR; } // Set up function pointers for our driver usbSlaveBoms_cb->flags = 0; usbSlaveBoms_cb->read = usbSlaveBoms_read; usbSlaveBoms_cb->write = usbSlaveBoms_write; usbSlaveBoms_cb->ioctl = usbSlaveBoms_ioctl; usbSlaveBoms_cb->interrupt = (PF_INT) NULL; usbSlaveBoms_cb->open = (PF_OPEN) NULL; usbSlaveBoms_cb->close = (PF_CLOSE) NULL; // OK - register with device manager vos_dev_init(vos_dev_num, usbSlaveBoms_cb, slaveBomsCtx); // defaults to not connected and no flash drive yet slaveBomsCtx->attached = 0; slaveBomsCtx->flashConnected = 0; // create the thread that handles standard control requests slaveBomsCtx->tcbSetup = vos_create_thread_ex(31, SIZEOF_BOMS_SETUP_MEMORY, usbslaveboms_setup, "BOMSSetup", 2, slaveBomsCtx); // initialize the sempahore to 0 so that anyone waiting for // the device to enum will block vos_init_semaphore(&slaveBomsCtx->enumed, 0); if (slaveBomsCtx->tcbSetup) return USBSLAVEBOMS_OK; return USBSLAVEBOMS_ERROR; }
usbhost_device_handle* WaitForVIDPID(BYTE ThreadID, WORD VID, WORD PID) { usbhost_ioctl_cb_t hc_iocb; usbhost_device_handle* ifDev; usbhost_ioctl_cb_vid_pid_t hc_iocb_VidPid; BYTE State, Status; // Return when a device matching VID and PID has been found while (1) { dprint(" .", 0); i_vos_delay_msecs(ThreadID, 1000); // Check that the host has finished enumeration hc_iocb.ioctl_code = VOS_IOCTL_USBHOST_GET_CONNECT_STATE; hc_iocb.get = &State; i_vos_dev_ioctl(ThreadID, hDevice[Host], &hc_iocb); if (State == PORT_STATE_ENUMERATED) { // Check for a specific device hc_iocb_VidPid.vid = VID; hc_iocb_VidPid.pid = PID; hc_iocb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_FIND_HANDLE_BY_VID_PID; hc_iocb.handle.dif = NULL; hc_iocb.set = &hc_iocb_VidPid; hc_iocb.get = &ifDev; i_vos_dev_ioctl(ThreadID, hDevice[Host], &hc_iocb); if (ifDev) { // Now get handles for the Data Endpoints hc_iocb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_GET_INT_IN_ENDPOINT_HANDLE; hc_iocb.handle.dif = ifDev; hc_iocb.get = &IntIn; Status = i_vos_dev_ioctl(ThreadID, hDevice[Host], &hc_iocb); if (Status != USBHOST_OK) return dprint("Could not get IntIn Endpoint (%d) ", &Status); hc_iocb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_GET_INT_OUT_ENDPOINT_HANDLE; // hc_iocb.handle.dif = ifDev; hc_iocb.get = &IntOut; Status = i_vos_dev_ioctl(ThreadID, hDevice[Host], &hc_iocb); if (Status != USBHOST_OK) return dprint("Could not get IntOut Endpoint (%d) ", &Status); vos_init_semaphore(&IntTransferComplete, 0); return ifDev; } } // Keyboard is not attached, wait until current device(s) is/are removed and check again dprint("\nDevice not found, waiting for more devices", 0); vos_delay_msecs(5000); } return 0; // Keep compiler happy }
// USBHOSTBoms read function unsigned char usbhostBoms_read(char *buf, unsigned short num_to_read, unsigned short *num_read, usbhostBoms_context_t *ctx) { unsigned short actual_read = 0; unsigned char status = USBHOSTBOMS_NOT_FOUND; usbhost_xfer_t xfer; vos_semaphore_t s; if (ctx->hc) { vos_init_semaphore(&s, 0); vos_memset(&xfer, 0, sizeof(usbhost_xfer_t)); xfer.buf = buf; xfer.len = num_to_read; xfer.ep = ctx->epBulkIn; xfer.s = &s; xfer.cond_code = USBHOST_CC_NOTACCESSED; xfer.flags = USBHOST_XFER_FLAG_START_BULK_ENDPOINT_LIST | USBHOST_XFER_FLAG_ROUNDING; status = vos_dev_read(ctx->hc, (unsigned char *) &xfer, sizeof(usbhost_xfer_t), NULL); if (status == USBHOST_OK) { status = (unsigned char)USBHOSTBOMS_OK; actual_read = xfer.len; } else { status |= (unsigned char)USBHOSTBOMS_USBHOST_ERROR; } } if (num_read) { *num_read = actual_read; } return status; }
////////////////////////////////////////////////////////////////////// // // MAIN // ////////////////////////////////////////////////////////////////////// void main(void) { usbhost_context_t usbhostContext; gpio_context_t gpioCtx; spislave_context_t spiSlaveContext; // Kernel initialisation vos_init(50, VOS_TICK_INTERVAL, VOS_NUMBER_DEVICES); vos_set_clock_frequency(VOS_48MHZ_CLOCK_FREQUENCY); vos_set_idle_thread_tcb_size(512); // Set up the io multiplexing iomux_setup(); spiSlaveContext.slavenumber = SPI_SLAVE_0; spiSlaveContext.buffer_size = 64; spislave_init(VOS_DEV_SPISLAVE, &spiSlaveContext); // Initialise GPIO port A gpioCtx.port_identifier = GPIO_PORT_A; gpio_init(VOS_DEV_GPIO_A,&gpioCtx); // Initialise USB Host devices usbhostContext.if_count = 8; usbhostContext.ep_count = 16; usbhostContext.xfer_count = 2; usbhostContext.iso_xfer_count = 2; usbhost_init(VOS_DEV_USBHOST_1, VOS_DEV_USBHOST_2, &usbhostContext); // Initialise the USB function device usbhostGeneric_init(VOS_DEV_USBHOSTGENERIC_1); usbhostGeneric_init(VOS_DEV_USBHOSTGENERIC_2); PortA.uchActivityLed = LED_USB_A; PortA.uchMsg = MSG_PORTA; PortA.uchDeviceNumberBase = VOS_DEV_USBHOST_1; PortA.uchDeviceNumber = VOS_DEV_USBHOSTGENERIC_1; PortB.uchActivityLed = LED_USB_B; PortB.uchMsg = MSG_PORTB; PortB.uchDeviceNumberBase = VOS_DEV_USBHOST_2; PortB.uchDeviceNumber = VOS_DEV_USBHOSTGENERIC_2; vos_init_semaphore(&setupSem,0); fifo_init(&stSPIReadFIFO); fifo_init(&stSPIWriteFIFO); tcbSetup = vos_create_thread_ex(10, 1024, Setup, "Setup", 0); tcbHostA = vos_create_thread_ex(20, 1024, RunHostPort, "RunHostPortA", sizeof(HOST_PORT_DATA*), &PortA); //tcbHostB = vos_create_thread_ex(21, 1024, RunHostPort, "RunHostPortB", sizeof(HOST_PORT_DATA*), &PortB); tcbRunSPISend = vos_create_thread_ex(20, 1024, RunSPISend, "RunSPISend", 0); tcbRunSPIReceive = vos_create_thread_ex(19, 1024, RunSPIReceive, "RunSPIReceive", 0); tcbRunUSBSend = vos_create_thread_ex(20, 1024, RunUSBSend, "RunUSBSend", 0); // And start the thread vos_start_scheduler(); main_loop: goto main_loop; }
////////////////////////////////////////////////////////////////////// // // MAIN // ////////////////////////////////////////////////////////////////////// void main(void) { /* FTDI:SDD Driver Declarations */ // UART Driver configuration context uart_context_t uartContext; // USB Host configuration context usbhost_context_t usbhostContext; gpio_context_t gpioCtx; // Kernel initialisation vos_init(50, VOS_TICK_INTERVAL, VOS_NUMBER_DEVICES); vos_set_clock_frequency(VOS_48MHZ_CLOCK_FREQUENCY); vos_set_idle_thread_tcb_size(512); // Set up the io multiplexing iomux_setup(); // Initialise GPIO port A gpioCtx.port_identifier = GPIO_PORT_A; gpio_init(VOS_DEV_GPIO_A,&gpioCtx); // // Initialise UART uartContext.buffer_size = VOS_BUFFER_SIZE_128_BYTES; uart_init(VOS_DEV_UART, &uartContext); // Initialise USB Host devices usbhostContext.if_count = 8; usbhostContext.ep_count = 16; usbhostContext.xfer_count = 2; usbhostContext.iso_xfer_count = 2; //usbhost_init(VOS_DEV_USBHOST_1, VOS_DEV_USBHOST_2, &usbhostContext); usbhost_init(VOS_DEV_USBHOST_1, -1, &usbhostContext); // Initialise the USB function device usbhostGeneric_init(VOS_DEV_USBHOSTGENERIC_1); //usbhostGeneric_init(VOS_DEV_USBHOSTGENERIC_2); PortA.uchDeviceNumber = VOS_DEV_USBHOSTGENERIC_1; PortA.uchMidiChannel = 0; PortA.uchActivityLed = 0b00000010; //PortB.uchDeviceNumber = VOS_DEV_USBHOSTGENERIC_2; //PortB.uchMidiChannel = 1; //PortB.uchActivityLed = 0b00000100; // setup the event queue SMQInit(); // Initializes our device with the device manager. tcbSetup = vos_create_thread_ex(10, 1024, Setup, "Setup", 0); tcbHostA = vos_create_thread_ex(20, 1024, RunHostPort, "RunHostPortA", sizeof(HOST_PORT_DATA*), &PortA); //tcbHostB = vos_create_thread_ex(20, 1024, RunHostPort, "RunHostPortB", sizeof(HOST_PORT_DATA*), &PortB); tcbPlaypad = vos_create_thread_ex(15, 1024, RunPlaypad, "RunPlaypad", 0); tcbMetro = vos_create_thread_ex(15, 1024, RunMetronome, "RunMetronome", 0); tcbMIDIInput = vos_create_thread_ex(15, 1024, RunMIDIInput, "RunMIDIInput", 0); vos_init_semaphore(&setupSem,0); // And start the thread vos_start_scheduler(); main_loop: goto main_loop; }
////////////////////////////////////////////////////////////////////// // // MM MM IIII DDDDD IIII // MMM MMM II DD DD II // MM MM MM II DD DD II // MM MM II DD DD II // MM MM IIII DDDDD IIII // void run_midi_class_host(VOS_HANDLE usb_handle) { unsigned char state; int status; usbhost_device_handle_ex interface_handle; usbhost_ep_handle_ex tx_endpoint; usbhost_ep_handle_ex rx_endpoint; usbhost_ep_handle_ex ctrl_endpoint; vos_semaphore_t read_completion_event; usbhost_ioctl_cb_t usbhost_cmd; usbhost_ioctl_cb_class_t device_class; usbhost_ioctl_cb_vid_pid_t vid_pid; usbhost_xfer_t transfer_block; usb_deviceRequest_t device_request; usbhost_ioctl_cb_ep_info_t endpoint_descriptor; vos_init_semaphore(&read_completion_event, 0); device_class.dev_class = USB_CLASS_AUDIO; device_class.dev_subclass = USB_SUBCLASS_AUDIO_MIDISTREAMING; device_class.dev_protocol = USB_PROTOCOL_ANY; usbhost_cmd.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_FIND_HANDLE_BY_CLASS; usbhost_cmd.handle.dif = NULL; usbhost_cmd.set = &device_class; usbhost_cmd.get = &interface_handle; status = vos_dev_ioctl(usb_handle, &usbhost_cmd); if(USBHOST_OK != status) { return; } usbhost_cmd.ioctl_code = VOS_IOCTL_USBHOST_GET_USB_STATE; usbhost_cmd.handle.dif = interface_handle; usbhost_cmd.get = &state; usbhost_cmd.set = NULL; status = vos_dev_ioctl(usb_handle, &usbhost_cmd); if(USBHOST_OK != status) { return; } usbhost_cmd.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_GET_VID_PID; usbhost_cmd.handle.dif = interface_handle; usbhost_cmd.get = &vid_pid; usbhost_cmd.set = NULL; status = vos_dev_ioctl(usb_handle, &usbhost_cmd); if(USBHOST_OK != status) { return; } usbhost_cmd.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_GET_BULK_OUT_ENDPOINT_HANDLE; usbhost_cmd.handle.dif = interface_handle; usbhost_cmd.get = &tx_endpoint; usbhost_cmd.set = NULL; status = vos_dev_ioctl(usb_handle, &usbhost_cmd); if(USBHOST_OK != status) { return; } usbhost_cmd.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_GET_BULK_IN_ENDPOINT_HANDLE; usbhost_cmd.handle.dif = interface_handle; usbhost_cmd.get = &rx_endpoint; usbhost_cmd.set = NULL; status = vos_dev_ioctl(usb_handle, &usbhost_cmd); if(USBHOST_OK != status) { return; } usbhost_cmd.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_GET_CONTROL_ENDPOINT_HANDLE; usbhost_cmd.handle.dif = interface_handle; usbhost_cmd.get = &ctrl_endpoint; usbhost_cmd.set = NULL; status = vos_dev_ioctl(usb_handle, &usbhost_cmd); if(USBHOST_OK != status) { return; } // send a SetIdle to the device device_request.bmRequestType = USB_BMREQUESTTYPE_HOST_TO_DEV | USB_BMREQUESTTYPE_CLASS | USB_BMREQUESTTYPE_INTERFACE; device_request.bRequest = 0x0a; device_request.wValue = 0; device_request.wIndex = 0; device_request.wLength = 0; usbhost_cmd.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_SETUP_TRANSFER; usbhost_cmd.handle.ep = ctrl_endpoint; usbhost_cmd.set = &device_request; status = vos_dev_ioctl(usb_handle, &usbhost_cmd); while (1) { vos_memset(&transfer_block, 0, sizeof(transfer_block)); transfer_block.cond_code = USBHOST_CC_NOTACCESSED; transfer_block.flags = USBHOST_XFER_FLAG_START_BULK_ENDPOINT_LIST|USBHOST_XFER_FLAG_ROUNDING; transfer_block.s = &read_completion_event; transfer_block.ep = rx_endpoint; transfer_block.buf = usb_rx_data; transfer_block.len = SZ_USB_RX_DATA; status = vos_dev_read(usb_handle, (byte*)&transfer_block, sizeof(transfer_block), NULL); if(status != USBHOST_OK) { break; } send_output(usb_rx_data, transfer_block.len); } }