void InitializeUART() { common_ioctl_cb_t ioctl = {0}; ioctl.ioctl_code = VOS_IOCTL_COMMON_ENABLE_DMA; vos_dev_ioctl(hUART, &ioctl); memset(&ioctl, 0, sizeof(common_ioctl_cb_t)); ioctl.ioctl_code = VOS_IOCTL_UART_SET_BAUD_RATE; ioctl.set.uart_baud_rate = 921600;//UART_BAUD_57600; vos_dev_ioctl(hUART, &ioctl); memset(&ioctl, 0, sizeof(common_ioctl_cb_t)); ioctl.ioctl_code = VOS_IOCTL_UART_SET_FLOW_CONTROL; ioctl.set.param = UART_FLOW_NONE; vos_dev_ioctl(hUART, &ioctl); memset(&ioctl, 0, sizeof(common_ioctl_cb_t)); ioctl.ioctl_code = VOS_IOCTL_UART_SET_DATA_BITS; ioctl.set.param = 8; vos_dev_ioctl(hUART, &ioctl); memset(&ioctl, 0, sizeof(common_ioctl_cb_t)); ioctl.ioctl_code = VOS_IOCTL_UART_SET_STOP_BITS; ioctl.set.param = 2; vos_dev_ioctl(hUART, &ioctl); stdioAttach(hUART); // VOS_HANDLE for stdio interface (typically UART) }
unsigned char hostGetDevType(VOS_HANDLE hUsbHost, usbhost_device_handle_ex ifDev) { unsigned char mask = 0; unsigned char usbClass; usbhost_ioctl_cb_t host_ioctl_cb; usbhost_ioctl_cb_t hc_iocb_ftdiCheck; usbhost_ioctl_cb_class_t hc_iocb_class; usbhost_ioctl_cb_vid_pid_t hc_iocb_vid_pid; host_ioctl_cb.handle.dif = ifDev; // find device classes present on the bus host_ioctl_cb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_GET_VID_PID; host_ioctl_cb.get = &hc_iocb_vid_pid; vos_dev_ioctl(hUsbHost, &host_ioctl_cb); if (hc_iocb_vid_pid.vid == USB_VID_FTDI) { mask = hostHasFTDI; } else { host_ioctl_cb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_GET_CLASS_INFO; host_ioctl_cb.get = &hc_iocb_class; vos_dev_ioctl(hUsbHost, &host_ioctl_cb); switch (hc_iocb_class.dev_class) { case USB_CLASS_CDC_CONTROL: mask = hostHasCDCClass; break; case USB_CLASS_PRINTER: mask = hostHasPrnClass; break; case USB_CLASS_HID: mask = hostHasHIDClass; break; case USB_CLASS_HUB: mask = hostHasHub; break; case USB_CLASS_MASS_STORAGE: mask = hostHasBOMSClass; break; default: mask = hostHasUnknown; } } return mask; }
VOS_HANDLE boms_attach(VOS_HANDLE hUSB, unsigned char devBOMS) { usbhost_device_handle_ex ifDisk = 0; usbhost_ioctl_cb_t hc_iocb; usbhost_ioctl_cb_class_t hc_iocb_class; msi_ioctl_cb_t boms_iocb; boms_ioctl_cb_attach_t boms_att; int error; VOS_HANDLE hBOMS; // find BOMS class device hc_iocb_class.dev_class = USB_CLASS_MASS_STORAGE; hc_iocb_class.dev_subclass = USB_SUBCLASS_MASS_STORAGE_SCSI; hc_iocb_class.dev_protocol = USB_PROTOCOL_MASS_STORAGE_BOMS; // user ioctl to find first hub device hc_iocb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_FIND_HANDLE_BY_CLASS; hc_iocb.handle.dif = NULL; hc_iocb.set = &hc_iocb_class; hc_iocb.get = &ifDisk; error = vos_dev_ioctl(hUSB, &hc_iocb); if (error != USBHOST_OK) { printf("ERROR: %d\n", error); printf("USB Bus 1 State: %d\n", getBusState(hUSBHOST_1)); printf("USB Bus 2 State: %d\n", getBusState(hUSBHOST_2)); return NULL; } // now we have a device, intialise a BOMS driver with it hBOMS = vos_dev_open(devBOMS); // perform attach boms_att.hc_handle = hUSB; boms_att.ifDev = ifDisk; boms_iocb.ioctl_code = MSI_IOCTL_BOMS_ATTACH; boms_iocb.set = &boms_att; boms_iocb.get = NULL; if (vos_dev_ioctl(hBOMS, &boms_iocb) != MSI_OK) { vos_dev_close(hBOMS); hBOMS = NULL; } return hBOMS; }
WORD ResetTimer0(void) { tmr_ioctl_cb_t tmr_iocb; WORD CurrentCount; BYTE Status = 0; tmr_iocb.ioctl_code = VOS_IOCTL_TIMER_STOP; Status |= vos_dev_ioctl(hDevice[Timer0], &tmr_iocb); tmr_iocb.ioctl_code = VOS_IOCTL_TIMER_GET_CURRENT_COUNT; Status |= vos_dev_ioctl(hDevice[Timer0], &tmr_iocb); CurrentCount = tmr_iocb.param; tmr_iocb.ioctl_code = VOS_IOCTL_TIMER_START; Status |= vos_dev_ioctl(hDevice[Timer0], &tmr_iocb); CheckStatus(Status, ErrorTimer0); return CurrentCount; }
////////////////////////////////////////////////////////////////////// // // Get connect state // ////////////////////////////////////////////////////////////////////// byte get_usb_host_connect_state(VOS_HANDLE usb_handle) { usbhost_ioctl_cb_t usb_cmd; byte connect_state = PORT_STATE_DISCONNECTED; if (usb_handle) { usb_cmd.ioctl_code = VOS_IOCTL_USBHOST_GET_CONNECT_STATE; usb_cmd.get = &connect_state; vos_dev_ioctl(usb_handle, &usb_cmd); if (connect_state == PORT_STATE_CONNECTED) { // repeat if connected to see if we move to enumerated vos_dev_ioctl(usb_handle, &usb_cmd); } } return connect_state; }
uint8 i_vos_dev_ioctl(BYTE ThreadID, VOS_HANDLE h, void* cb) { uint8 Value; CallVOS(ThreadID); Value = vos_dev_ioctl(h, cb); ThreadRunning(ThreadID); return Value; }
// SETUP for IOCTL call unsigned char usbhostBoms_ioctl_setup(usbhostBoms_ioctl_t *cb, usbhostBoms_context_t *ctx) { // USBHost IOCTL structure usbhost_ioctl_cb_t hc_ioctl; // setup transfer descriptor usb_deviceRequest_t desc_dev; unsigned char buf[2]; unsigned char status; // setup Boms device desc_dev.bmRequestType = USB_BMREQUESTTYPE_DEV_TO_HOST | USB_BMREQUESTTYPE_STANDARD | USB_BMREQUESTTYPE_DEVICE; desc_dev.bRequest = USB_REQUEST_CODE_GET_STATUS; desc_dev.wValue = 0; desc_dev.wIndex = 0; desc_dev.wLength = 2; hc_ioctl.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_SETUP_TRANSFER; hc_ioctl.handle.ep = ctx->epCtrl; hc_ioctl.set = &desc_dev; hc_ioctl.get = buf; status = vos_dev_ioctl(ctx->hc, &hc_ioctl); if (status == USBHOST_OK) { // return status low byte to caller cb->get.data = buf[0]; return USBHOSTBOMS_OK; } return status | USBHOSTBOMS_USBHOST_ERROR; }
VOS_HANDLE fat_attach(VOS_HANDLE hMSI, unsigned char devFAT) { fat_ioctl_cb_t fat_ioctl; fatdrv_ioctl_cb_attach_t fat_att; VOS_HANDLE hFAT; // currently the MSI (BOMS or other) must be open // open the FAT driver hFAT = vos_dev_open(devFAT); // attach the FAT driver to the MSI driver fat_ioctl.ioctl_code = FAT_IOCTL_FS_ATTACH; fat_ioctl.set = &fat_att; fat_att.msi_handle = hMSI; fat_att.partition = 0; if (vos_dev_ioctl(hFAT, &fat_ioctl) != FAT_OK) { // unable to open the FAT driver vos_dev_close(hFAT); hFAT = NULL; } return hFAT; }
// This function returns a 1 byte status code for our endpoint void get_ep_status(usbSlaveBoms_context *ctx, usbslave_ep_handle_t ep) { usbslave_ioctl_cb_t iocb; char state; iocb.ioctl_code = VOS_IOCTL_USBSLAVE_ENDPOINT_STATE; iocb.ep = ep; iocb.get = &state; vos_dev_ioctl(ctx->handle, &iocb); iocb.ioctl_code = VOS_IOCTL_USBSLAVE_SETUP_TRANSFER; iocb.handle = ctx->out_ep0; iocb.request.setup_or_bulk_transfer.buffer = (void *) &state; iocb.request.setup_or_bulk_transfer.size = 1; vos_dev_ioctl(ctx->handle, &iocb); }
void usbslaveboms_clear_bulk_out(usbSlaveBoms_context *ctx) { usbslave_ioctl_cb_t iocb; // this is a structure used by underlying VOS driver iocb.ioctl_code = VOS_IOCTL_USBSLAVE_ENDPOINT_CLEAR; iocb.ep = 0x02; // bulk out endpoint vos_dev_ioctl(ctx->handle, &iocb); }
// The following functions are for stalling and clearing bulk endpoints // There are some conditions which require us to stall these endpoints. // For example, if we return less data than expected to the PC we // must stall the endpoint so it doesn't hang forever waiting for the rest // of the data. void usbslaveboms_stall_bulk_in(usbSlaveBoms_context *ctx) { usbslave_ioctl_cb_t iocb; // this is a structure used by underlying VOS driver iocb.ioctl_code = VOS_IOCTL_USBSLAVE_ENDPOINT_STALL; iocb.ep = 0x81; // bulk in endpoint vos_dev_ioctl(ctx->handle, &iocb); }
void set_uart_baudrate(VOS_HANDLE huart) { common_ioctl_cb_t uart_iocb; //UART IOCTL request block /* set baud rate to 9600 baud */ uart_iocb.ioctl_code = VOS_IOCTL_UART_SET_BAUD_RATE; uart_iocb.set.uart_baud_rate = UART_BAUD_9600;//change the value when you need other baudrate vos_dev_ioctl(huart,&uart_iocb); }
unsigned char getBusState(VOS_HANDLE hUsbHost) { usbhost_ioctl_cb_t usbhost_iocb; unsigned char state; usbhost_iocb.ioctl_code = VOS_IOCTL_USBHOST_GET_USB_STATE; usbhost_iocb.get = &state; vos_dev_ioctl(hUsbHost, &usbhost_iocb); return state; }
VOS_HANDLE hid_attach(VOS_HANDLE hUSB, unsigned char devHID) { usbhost_device_handle_ex ifHID = 0; usbhost_ioctl_cb_t hc_iocb; usbhost_ioctl_cb_class_t hc_iocb_class; usbHostHID_ioctl_t hid_iocb; usbHostHID_ioctl_cb_attach_t hid_att; VOS_HANDLE hHID; // find HID class device hc_iocb_class.dev_class = USB_CLASS_HID; hc_iocb_class.dev_subclass = USB_SUBCLASS_ANY; hc_iocb_class.dev_protocol = USB_PROTOCOL_ANY; // user ioctl to find first hub device hc_iocb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_FIND_HANDLE_BY_CLASS; hc_iocb.handle.dif = NULL; hc_iocb.set = &hc_iocb_class; hc_iocb.get = &ifHID; if (vos_dev_ioctl(hUSB, &hc_iocb) != USBHOST_OK) { return NULL; } // now we have a device, intialise a HID driver with it hHID = vos_dev_open(devHID); // perform attach hid_att.hc_handle = hUSB; hid_att.ifDev = ifHID; hid_iocb.ioctl_code = VOS_IOCTL_USBHOSTHID_ATTACH; hid_iocb.set = &hid_att; hid_iocb.get = NULL; if (vos_dev_ioctl(hHID, &hid_iocb) != USBHOSTHID_OK) { vos_dev_close(hHID); hHID = NULL; } return hHID; }
// Most devices support only one configuration. However, every // device must respond to this call asking them to select a // configuration even if it is the default. void set_configuration_request(usbSlaveBoms_context *ctx, unsigned char config) { usbslave_ioctl_cb_t iocb; iocb.ioctl_code = VOS_IOCTL_USBSLAVE_SET_CONFIGURATION; iocb.set = (void *) config; vos_dev_ioctl(ctx->handle, &iocb); ack_request(ctx); }
// When initially attached to a host devices have no address and are // assigned an adddress after enumeration. Then the device is reset // and fully enumerated. void set_address_request(usbSlaveBoms_context *ctx, unsigned char addr) { usbslave_ioctl_cb_t iocb; iocb.ioctl_code = VOS_IOCTL_USBSLAVE_SET_ADDRESS; iocb.set = (void *) addr; vos_dev_ioctl(ctx->handle, &iocb); ack_request(ctx); }
// All commands on the control endpoint must be acknowledged. // This is done by sending a Zero Length Data Packet ZLDP // on the control in endpoint. void ack_request(usbSlaveBoms_context *ctx) { usbslave_ioctl_cb_t iocb; iocb.ioctl_code = VOS_IOCTL_USBSLAVE_SETUP_TRANSFER; iocb.handle = ctx->in_ep0; iocb.request.setup_or_bulk_transfer.buffer = (void *) 0; iocb.request.setup_or_bulk_transfer.size = 0; vos_dev_ioctl(ctx->handle, &iocb); }
void class_control_out(usbSlaveBoms_context *ctx, char *buffer, unsigned short len) { usbslave_ioctl_cb_t iocb; iocb.ioctl_code = VOS_IOCTL_USBSLAVE_SETUP_TRANSFER; iocb.handle = ctx->out_ep0; iocb.request.setup_or_bulk_transfer.buffer = (void *) buffer; iocb.request.setup_or_bulk_transfer.size = len; vos_dev_ioctl(ctx->handle, &iocb); }
unsigned char usbhost_connect_state(VOS_HANDLE hUSB) { unsigned char connectstate = PORT_STATE_DISCONNECTED; usbhost_ioctl_cb_t hc_iocb; if (hUSB) { hc_iocb.ioctl_code = VOS_IOCTL_USBHOST_GET_CONNECT_STATE; hc_iocb.get = &connectstate; vos_dev_ioctl(hUSB, &hc_iocb); // repeat if connected to see if we move to enumerated if (connectstate == PORT_STATE_CONNECTED) { vos_dev_ioctl(hUSB, &hc_iocb); } } return connectstate; }
// This function is the complement to the set_feature_request function. void clear_feature_request(usbSlaveBoms_context *ctx, unsigned char ep) { usbslave_ioctl_cb_t iocb; usbhost_ep_handle_ex hep; // host endpoint to pass stall to usbhost_ioctl_cb_t host_ioctl_cb; ack_request(ctx); // is this directed at an endpoint or the device? if (ctx->setup_buffer[0] & USB_BMREQUESTTYPE_ENDPOINT) { // directed to an endpoint //ep 1 is IN 2 is OUT on my fake device iocb.ioctl_code = VOS_IOCTL_USBSLAVE_ENDPOINT_CLEAR; iocb.ep = (ep & 0x02)?ctx->out_ep:ctx->in_ep; vos_dev_ioctl(ctx->handle, &iocb); // if flash drive is attached pass along request if (ctx->flashConnected) { //need to figure out which endpoint to clear // if b7=1 then IN else OUT if (ep & 0x80) { hep = hostBomsCtx->epBulkIn; } else { hep = hostBomsCtx->epBulkOut; } host_ioctl_cb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_CLEAR_HOST_HALT; host_ioctl_cb.handle.ep = hep; // clear halt state on endpoint vos_dev_ioctl(hostBomsCtx->hc, &host_ioctl_cb); } } else { // this is a device request host_ioctl_cb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_SETUP_TRANSFER; host_ioctl_cb.handle.ep = hostBomsCtx->epCtrl; host_ioctl_cb.set = &(ctx->setup_buffer[0]); host_ioctl_cb.get = NULL; vos_dev_ioctl(hostBomsCtx->hc, &host_ioctl_cb); } }
int FUartData() { int cb; common_ioctl_cb_t ioctl = {0}; ioctl.ioctl_code = VOS_IOCTL_COMMON_GET_RX_QUEUE_STATUS; vos_dev_ioctl(hUART, &ioctl); cb = ioctl.get.queue_stat; return (cb); }
void setup_jtag_gpio() { char tmp = LED_GREEN_OFF; gpio_ioctl_cb_t gpctl; gpctl.ioctl_code = VOS_IOCTL_GPIO_SET_MASK; gpctl.value = ~4; // ALL OUTPUT except TDO vos_dev_ioctl(hGPIO_PORT_A, &gpctl); vos_dev_write(hGPIO_PORT_A, &tmp, 1, NULL); }
void close_jtag_gpio() { char tmp = 0; gpio_ioctl_cb_t gpctl; gpctl.ioctl_code = VOS_IOCTL_GPIO_SET_MASK; gpctl.value = LED_RED_OFF | LED_GREEN_OFF; vos_dev_ioctl(hGPIO_PORT_A, &gpctl); // set all to inputs (hi-z) tmp = LED_RED_OFF; vos_dev_write(hGPIO_PORT_A, &tmp, 1, NULL); }
unsigned char hostConnected(VOS_HANDLE hUsbHost) { usbhost_ioctl_cb_t usbhost_iocb; unsigned char i; usbhost_iocb.ioctl_code = VOS_IOCTL_USBHOST_GET_CONNECT_STATE; usbhost_iocb.get = &i; vos_dev_ioctl(hUsbHost, &usbhost_iocb); return i; }
unsigned char usbslave_disconnect(VOS_HANDLE hUSB) { usbslave_ioctl_cb_t iocb; unsigned char ret; iocb.ioctl_code = VOS_IOCTL_USBSLAVE_DISCONNECT; iocb.set = (void *) 0; vos_dev_ioctl(hUSB, &iocb); return ret; }
unsigned char slaveConnected(VOS_HANDLE hUsbSlave) { usbslave_ioctl_cb_t usbslave_iocb; unsigned char i; usbslave_iocb.ioctl_code = VOS_IOCTL_USBSLAVE_GET_STATE; usbslave_iocb.get = &i; vos_dev_ioctl(hUsbSlave, &usbslave_iocb); return i; }
void HID_detach(VOS_HANDLE hHID) { usbHostHID_ioctl_t hid_iocb; if (hHID) { hid_iocb.ioctl_code = VOS_IOCTL_USBHOSTHID_DETACH; vos_dev_ioctl(hHID, &hid_iocb); vos_dev_close(hHID); } }
// This function returns a single byte. We will just fake // it and return zero. void get_interface_request(usbSlaveBoms_context *ctx) { unsigned char status = 0; usbslave_ioctl_cb_t iocb; usbhost_ioctl_cb_t hc_ioctl; iocb.ioctl_code = VOS_IOCTL_USBSLAVE_SETUP_TRANSFER; iocb.handle = ctx->in_ep0; iocb.request.setup_or_bulk_transfer.buffer = &status; iocb.request.setup_or_bulk_transfer.size = 1; vos_dev_ioctl(ctx->handle, &iocb); }
// This function will halt the control endpoint. This function will only // be called when an illegal request has been passed to the device. void set_control_ep_halt(usbSlaveBoms_context *ctx) { usbslave_ioctl_cb_t iocb; ack_request(ctx); // Performs a protocol stall on endpoint 0 // Indicates that a request is unsupported iocb.ioctl_code = VOS_IOCTL_USBSLAVE_ENDPOINT_STALL; iocb.ep = 0; vos_dev_ioctl(ctx->handle, &iocb); }
////////////////////////////////////////////////////////////////////// // // APPLICATION SETUP THREAD FUNCTION // ////////////////////////////////////////////////////////////////////// void Setup() { common_ioctl_cb_t spis_iocb; usbhostGeneric_ioctl_t generic_iocb; gpio_ioctl_cb_t gpio_iocb; common_ioctl_cb_t uart_iocb; unsigned char uchLeds; // Open up the base level drivers hGpioA = vos_dev_open(VOS_DEV_GPIO_A); PortA.hUSBHOST = vos_dev_open(VOS_DEV_USBHOST_1); //PortB.hUSBHOST = vos_dev_open(VOS_DEV_USBHOST_2); gpio_iocb.ioctl_code = VOS_IOCTL_GPIO_SET_MASK; gpio_iocb.value = 0b00001110; vos_dev_ioctl(hGpioA, &gpio_iocb); uchLeds = 0b00001000; vos_dev_write(hGpioA,&uchLeds,1,NULL); hUART = vos_dev_open(VOS_DEV_UART); /* UART ioctl call to enable DMA and link to DMA driver */ uart_iocb.ioctl_code = VOS_IOCTL_COMMON_ENABLE_DMA; vos_dev_ioctl(hUART, &uart_iocb); // Set up for MIDI uart_iocb.ioctl_code = VOS_IOCTL_UART_SET_BAUD_RATE; uart_iocb.set.uart_baud_rate = 31250; vos_dev_ioctl(hUART, &uart_iocb); uart_iocb.ioctl_code = VOS_IOCTL_UART_SET_FLOW_CONTROL; uart_iocb.set.param = UART_FLOW_NONE; vos_dev_ioctl(hUART, &uart_iocb); // Set up the metronome MetroInit(&metro, VOS_DEV_TIMER0, TIMER_0); // Release other application threads vos_signal_semaphore(&setupSem); }