/** * Initialises an HID device. * * @param device the USB device. * @param handle configuration information. */ static void hid_initUsb(usb_device * device, hid_usbConfiguration * handle) { // Initialise/configure the USB device. usb_initDevice(device, handle->configuration); #ifdef DEBUG usb_printDeviceInfo( device ); #endif // Initialise bulk / interrupt input endpoint. usb_initEndPoint(&(device->bulk_in), handle->inputEndPointAddress); device->bulk_in.attributes = USB_ENDPOINT_XFER_INT; device->bulk_in.maxPacketSize = HID_USB_PACKETSIZE; // Initialise bulk / interrupt output endpoint. usb_initEndPoint(&(device->bulk_out), handle->outputEndPointAddress); device->bulk_out.attributes = USB_ENDPOINT_XFER_INT; device->bulk_out.maxPacketSize = HID_USB_PACKETSIZE; #ifdef DEBUG xSerialPrintf_P(PSTR("handle->inputEndPointAddress: 0x%02x\r\n"), handle->inputEndPointAddress); xSerialPrintf_P(PSTR("handle->outputEndPointAddress: 0x%02x\r\n"), handle->outputEndPointAddress); xSerialPrintf_P(PSTR("bulk_in.address: 0x%02x\r\n"), device->bulk_in.address); xSerialPrintf_P(PSTR("bulk_out.address: 0x%02x\r\n"), device->bulk_out.address); #endif }
/** * Initialises the USB layer. */ void usb_init() { max3421e_init(); max3421e_powerOn(); uint8_t i; // Initialise the USB state machine. usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE; // Initialise the device table. for (i = 0; i < (USB_NUMDEVICES + 1); i++) deviceTable[i].active = false; // Address 0 is used to configure devices and assign them an address when they are first plugged in deviceTable[0].address = 0; usb_initEndPoint(&(deviceTable[0].control), 0); }
/** * Initialises the USB layer. */ void usb_init() { max3421e_init(); max3421e_powerOn(); uint8_t i; // Initialise the USB state machine. usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE; // Initialise the device table. for (i = 0; i < (USB_NUMDEVICES + 1); i++) deviceTable[i].active = false; // Address 0 is used to configure devices and assign them an address when they are first plugged in deviceTable[0].address = 0; usb_initEndPoint(&(deviceTable[0].control), 0); // xSerialPrintf_P(PSTR("\r\nusb_init done! @ %u"), xTaskGetTickCount()); // FIXME remove this debugging }
/** * USB main task. Performs enumeration/cleanup */ void usb_poll(void) { uint8_t i; uint8_t rcode; uint8_t tmpdata; static uint16_t delay = 0; usb_deviceDescriptor deviceDescriptor; // Poll the MAX3421E device. max3421e_poll(); /* modify USB task state if Vbus changed */ tmpdata = max3421e_getVbusState(); switch (tmpdata) { case SE1: //illegal state usb_task_state = USB_DETACHED_SUBSTATE_ILLEGAL; break; case SE0: //disconnected if ((usb_task_state & USB_STATE_MASK) != USB_STATE_DETACHED) { usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE; } break; case FSHOST: //attached case LSHOST: if ((usb_task_state & USB_STATE_MASK) == USB_STATE_DETACHED) { delay = xTaskGetTickCount() + USB_SETTLE_DELAY / portTICK_RATE_MS; usb_task_state = USB_ATTACHED_SUBSTATE_SETTLE; } break; }// switch( tmpdata switch (usb_task_state) { case USB_DETACHED_SUBSTATE_INITIALIZE: // TODO right now it looks like the USB board is just reset on disconnect. Fire disconnect for all connected // devices. for (i = 1; i < USB_NUMDEVICES; i++) if (deviceTable[i].active) usb_fireEvent(&(deviceTable[i]), USB_DISCONNECT); usb_init(); usb_task_state = USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE; break; case USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE: //just sit here break; case USB_DETACHED_SUBSTATE_ILLEGAL: //just sit here break; case USB_ATTACHED_SUBSTATE_SETTLE: //settle time for just attached device if (delay < xTaskGetTickCount() ) { usb_task_state = USB_ATTACHED_SUBSTATE_RESET_DEVICE; } break; case USB_ATTACHED_SUBSTATE_RESET_DEVICE: // Issue bus reset. max3421e_write(MAX_REG_HCTL, bmBUSRST); usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE; break; case USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE: if ((max3421e_read(MAX_REG_HCTL) & bmBUSRST) == 0) { tmpdata = max3421e_read(MAX_REG_MODE) | bmSOFKAENAB; //start SOF generation max3421e_write(MAX_REG_MODE, tmpdata); // max3421e_regWr( rMODE, bmSOFKAENAB ); usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_SOF; delay = xTaskGetTickCount() + 20 / portTICK_RATE_MS; //20ms wait after reset per USB spec } break; case USB_ATTACHED_SUBSTATE_WAIT_SOF: //todo: change check order if (max3421e_read(MAX_REG_HIRQ) & bmFRAMEIRQ) { //when first SOF received we can continue if (delay < xTaskGetTickCount() ) { //20ms passed usb_task_state = USB_ATTACHED_SUBSTATE_GET_DEVICE_DESCRIPTOR_SIZE; } } break; case USB_ATTACHED_SUBSTATE_GET_DEVICE_DESCRIPTOR_SIZE: // toggle( BPNT_0 ); deviceTable[0].control.maxPacketSize = 8; rcode = usb_getDeviceDescriptor(&deviceTable[0], &deviceDescriptor); if (rcode == 0) { deviceTable[0].control.maxPacketSize = deviceDescriptor.bMaxPacketSize0; usb_task_state = USB_STATE_ADDRESSING; } else { usb_error = USB_ATTACHED_SUBSTATE_GET_DEVICE_DESCRIPTOR_SIZE; usb_task_state = USB_STATE_ERROR; } break; case USB_STATE_ADDRESSING: // Look for an empty spot for (i = 1; i < USB_NUMDEVICES; i++) { if (!deviceTable[i].active) { // Set correct MaxPktSize // deviceTable[i].epinfo = deviceTable[0].epinfo; deviceTable[i].address = i; deviceTable[i].active = true; usb_initEndPoint(&(deviceTable[i].control), 0); //temporary record //until plugged with real device endpoint structure rcode = usb_setAddress(&deviceTable[0], i); if (rcode == 0) { usb_fireEvent(&deviceTable[i], USB_CONNECT); // usb_task_state = USB_STATE_CONFIGURING; // NB: I've bypassed the configuring state, because configuration should be handled // in the usb event handler. usb_task_state = USB_STATE_RUNNING; } else { usb_fireEvent(&deviceTable[i], USB_ADRESSING_ERROR); // TODO remove usb_error at some point? usb_error = USB_STATE_ADDRESSING; usb_task_state = USB_STATE_ERROR; } break; //break if address assigned or error occurred during address assignment attempt } } // If no vacant spot was found in the device table, fire an error. if (usb_task_state == USB_STATE_ADDRESSING) { usb_fireEvent(&deviceTable[i], USB_ADRESSING_ERROR); // No vacant place in devtable usb_error = 0xfe; usb_task_state = USB_STATE_ERROR; } break; case USB_STATE_CONFIGURING: break; case USB_STATE_RUNNING: break; case USB_STATE_ERROR: break; } // xSerialPrintf_P(PSTR("\r\nusb_poll usb_task_state: %u @ Tick: %u"), usb_task_state, xTaskGetTickCount()); // FIXME remove this debugging }