// Declare the VOS calls that I use with surrounding instrumentation uint8 i_vos_delay_msecs(BYTE ThreadID, uint16 ms) { uint8 Value; CallVOS(ThreadID); Value = vos_delay_msecs(ms); ThreadRunning(ThreadID); return Value; }
void HelloWorld(void) { BYTE Counter = 0; vos_wait_semaphore(&DevicesStarted); vos_signal_semaphore(&DevicesStarted); dprint("Hello World has started\n", 0); while (1) { vos_delay_msecs(1000); dprint("Hello World %d ", &Counter++); } }
// Declare program thread(s) // Blink thread, toggles LED3 on GPIO1 void Blink(void) { BYTE PortData = LED3; StartupDevices(); dprint("Blink has started\n", 0); while (1) { vos_delay_msecs(512); PortData ^= LED3; // Now write pattern to the GPIO port. vos_dev_write(hDevice[LEDs] ,&PortData, 1, NULL); dprint("LED is %s ", (PortData & LED3) ? "Off" : "On"); } }
void firmware() { /* Thread code to be added here */ unsigned char print = 0xff; open_drivers(); attach_drivers(); set_uart_baudrate(hUART); vos_delay_msecs(1500); //write_uart(hUART,&print,1); tcbUSBBRI = vos_create_thread_ex(30,2048,USBBRI_attach,"USBBRI_attach",0); tcbUSBTMC = vos_create_thread_ex(30,2048,USBTMC_attach,"USBTMC_attach",0); }
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 }
/* Main code - entry point to firmware */ void main(void) { /* FTDI:SDD Driver Declarations */ // UART Driver configuration context uart_context_t uartContext; // SPI Master configuration context spimaster_context_t spimContext; // GPIO Port A configuration context gpio_context_t gpioContextA; // GPIO Port B configuration context gpio_context_t gpioContextB; // USB Host configuration context usbhost_context_t usbhostContext; /* FTDI:EDD */ /* FTDI:SKI 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); /* FTDI:EKI */ iomux_setup(); /* FTDI:SDI Driver Initialisation */ // Initialise UART uartContext.buffer_size = VOS_BUFFER_SIZE_128_BYTES; uart_init(VOS_DEV_UART,&uartContext); // Initialise SPI Master spimContext.buffer_size = VOS_BUFFER_SIZE_128_BYTES; spimaster_init(VOS_DEV_SPI_MASTER,&spimContext); // Initialise GPIO A gpioContextA.port_identifier = GPIO_PORT_A; gpio_init(VOS_DEV_GPIO_PORT_A,&gpioContextA); // Initialise GPIO B gpioContextB.port_identifier = GPIO_PORT_B; gpio_init(VOS_DEV_GPIO_PORT_B,&gpioContextB); // Initialise FAT File System Driver fatdrv_init(VOS_DEV_FAT_FILE_SYSTEM); // Initialise BOMS Device Driver boms_init(VOS_DEV_BOMS); // Initialise USB HID Device usbHostHID_init(VOS_DEV_USBHOST_HID_1); // Initialise USB HID Device usbHostHID_init(VOS_DEV_USBHOST_HID_2); // Initialise USB Host usbhostContext.if_count = 8; usbhostContext.ep_count = 16; usbhostContext.xfer_count = 2; usbhostContext.iso_xfer_count = 2; if(usbhost_init(VOS_DEV_USBHOST_1, VOS_DEV_USBHOST_2, &usbhostContext) != 0) while(1); //FAILED /* FTDI:EDI */ /* FTDI:SCT Thread Creation */ tcbFIRMWARE = vos_create_thread_ex(31, 2048, firmware, "Application", 0); /* FTDI:ECT */ vos_delay_msecs(100); vos_start_scheduler(); main_loop: vos_delay_msecs(100); goto main_loop; }
void firmware() { int i; char bufmgk[20]; int idxMgk = 0; FILE *pf; /* Thread code to be added here */ vos_delay_msecs(4000); // 4 second delay for USB key to boot open_drivers(); InitializeUART(); flash_init(); InitializeHID(); printf("DekaBoard Rev: 1B\n"); vfWriteFlash = 0; PROGRAM: if (FOpenUSBKey()) { pf = fopen("deka.svf", "r"); if (pf != NULL) { ProgramFPGA(pf); fclose(pf); } CloseUSBKey(); } else { ProgramFPGA(FILE_FLASH); } while(1) { char tmp; // First check if PROG button pressed vos_dev_read(hGPIO_PORT_B, &tmp, 1, NULL); if (!(tmp & 0x20)) goto PROGRAM; // Else handle input if (FUartData()) { fread(bufmgk, 1, 1, stdin); if (bufmgk[0] == STR_MAGIC[idxMgk]) { idxMgk++; } else { idxMgk = 0; } if(idxMgk == 20) { fwrite(UART_ACK, 1, 3, stdout); fgets(bufmgk, 20, stdin); if (strcmp(bufmgk, "PROG XSVF") == 0) { fgets(bufmgk, 20, stdin); vfWriteFlash = (strcmp(bufmgk, "FLASH") == 0); fwrite(UART_ACK, 1, 3, stdout); ProgramFPGA(stdin); vfWriteFlash = 0; printf("DONE\n"); } else { printf(UART_NACK); } idxMgk = 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); } } } }
// This function is run inside the thread created in init // It will respond to standard USB requests // some of the requests are forwarded to the flash drive when appropriate void usbslaveboms_setup(usbSlaveBoms_context *ctx) { usbslave_ioctl_cb_t iocb; // this is a structure used by underlying VOS driver usb_deviceRequest_t *devReq; // this struct is defined in usb.h and is used to store the 9 byte setup request unsigned char bmRequestType; // The request type is defined by USB standard unsigned char state = UNATTACHED; // assume unattached at the start // This will wait till the flash drive is enumed so we don't // start responding to commands right away vos_wait_semaphore(&ctx->enumed); vos_signal_semaphore(&ctx->enumed); vos_wait_semaphore(&setupDoneSemaphore); // wait for setup to complete vos_signal_semaphore(&setupDoneSemaphore); // reset semaphore for next guy while (1) { switch (state) { case UNATTACHED: if (!ctx->attached) vos_delay_msecs(100); // this delay is to avoid a tight loop else { state = ATTACHED; } break; case ATTACHED: if (!ctx->attached) // check to see if we somehow became unattached { state = UNATTACHED; break; } // we now make a blocking call requesting the 9 byte setup // packet on the control endpoint iocb.ioctl_code = VOS_IOCTL_USBSLAVE_WAIT_SETUP_RCVD; iocb.request.setup_or_bulk_transfer.buffer = ctx->setup_buffer; iocb.request.setup_or_bulk_transfer.size = 9; vos_dev_ioctl(ctx->handle, &iocb); // decode the raw data by pointing to our structure devReq = (usb_deviceRequest_t *) ctx->setup_buffer; // valid types here are standard, class, and vendor // BOMS devices do not have vendor specific calls // even if they did, we wouldn't support them. bmRequestType = devReq->bmRequestType & (USB_BMREQUESTTYPE_STANDARD | USB_BMREQUESTTYPE_CLASS); // we only need to handle standard and class requests for BOMS if (bmRequestType == USB_BMREQUESTTYPE_STANDARD) { standard_request(ctx); // standard request that all USB devices support } else if (bmRequestType == USB_BMREQUESTTYPE_CLASS) { class_request(ctx); // the request is specific to this device class (only 2 in our case) } break; default: asm {HALT}; // if we somehow got here the fecal matter has hit the turbine break; } } return; }
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); }