X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/60fd0ff418566d85224f9841037ba01b77acc0d8..a9e0935a90346beb0c981924becc1f55d969a08b:/Projects/XPLAINBridge/XPLAINBridge.c?ds=sidebyside diff --git a/Projects/XPLAINBridge/XPLAINBridge.c b/Projects/XPLAINBridge/XPLAINBridge.c index 2de545f95..5457a2283 100644 --- a/Projects/XPLAINBridge/XPLAINBridge.c +++ b/Projects/XPLAINBridge/XPLAINBridge.c @@ -51,11 +51,11 @@ USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface = .DataINEndpointNumber = CDC_TX_EPNUM, .DataINEndpointSize = CDC_TXRX_EPSIZE, - .DataINEndpointDoubleBank = false, + .DataINEndpointDoubleBank = true, .DataOUTEndpointNumber = CDC_RX_EPNUM, .DataOUTEndpointSize = CDC_TXRX_EPSIZE, - .DataOUTEndpointDoubleBank = false, + .DataOUTEndpointDoubleBank = true, .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM, .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE, @@ -80,23 +80,27 @@ int main(void) Buffer_Initialize(&USBtoUART_Buffer); Buffer_Initialize(&UARTtoUSB_Buffer); + LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); + sei(); + for (;;) { - if (USB_DeviceState == DEVICE_STATE_Configured) - { - if (CurrentFirmwareMode == MODE_USART_BRIDGE) - USARTBridge_Task(); - else - AVRISP_Task(); - } - + if (CurrentFirmwareMode == MODE_USART_BRIDGE) + USARTBridge_Task(); + else + AVRISP_Task(); + USB_USBTask(); } } void AVRISP_Task(void) { - Endpoint_SelectEndpoint(AVRISP_DATA_EPNUM); + /* Must be in the configured state for the AVRISP code to process data */ + if (USB_DeviceState != DEVICE_STATE_Configured) + return; + + Endpoint_SelectEndpoint(AVRISP_DATA_OUT_EPNUM); /* Check to see if a V2 Protocol command has been received */ if (Endpoint_IsOUTReceived()) @@ -112,6 +116,10 @@ void AVRISP_Task(void) void USARTBridge_Task(void) { + /* Must be in the configured state for the USART Bridge code to process data */ + if (USB_DeviceState != DEVICE_STATE_Configured) + return; + /* Read bytes from the USB OUT endpoint into the UART transmit buffer */ for (uint8_t DataBytesRem = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface); DataBytesRem != 0; DataBytesRem--) { @@ -130,8 +138,8 @@ void USARTBridge_Task(void) SoftUART_TxByte(Buffer_GetElement(&USBtoUART_Buffer)); /* Load bytes from the UART into the UART receive buffer */ - if(SoftUART_IsReceived()) - Buffer_StoreElement(&UARTtoUSB_Buffer, SoftUART_RxByte()); + if (SoftUART_IsReceived()) + Buffer_StoreElement(&UARTtoUSB_Buffer, SoftUART_RxByte()); CDC_Device_USBTask(&VirtualSerial_CDC_Interface); } @@ -162,22 +170,33 @@ void SetupHardware(void) /* Select the firmware mode based on the JTD pin's value */ CurrentFirmwareMode = (PINF & (1 << 7)) ? MODE_USART_BRIDGE : MODE_PDI_PROGRAMMER; + + /* Re-enable JTAG debugging */ + MCUCR &= ~(1 << JTD); + MCUCR &= ~(1 << JTD); } /** Event handler for the library USB Configuration Changed event. */ void EVENT_USB_Device_ConfigurationChanged(void) { - bool EndpointConfigSuccess; + bool EndpointConfigSuccess = true; + /* Configure the device endpoints according to the selected mode */ if (CurrentFirmwareMode == MODE_USART_BRIDGE) { - EndpointConfigSuccess = CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface); + EndpointConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface); } else { - EndpointConfigSuccess = Endpoint_ConfigureEndpoint(AVRISP_DATA_EPNUM, EP_TYPE_BULK, - ENDPOINT_DIR_OUT, AVRISP_DATA_EPSIZE, - ENDPOINT_BANK_SINGLE); + EndpointConfigSuccess &= Endpoint_ConfigureEndpoint(AVRISP_DATA_OUT_EPNUM, EP_TYPE_BULK, + ENDPOINT_DIR_OUT, AVRISP_DATA_EPSIZE, + ENDPOINT_BANK_SINGLE); + + #if defined(LIBUSB_FILTERDRV_COMPAT) + EndpointConfigSuccess &= Endpoint_ConfigureEndpoint(AVRISP_DATA_IN_EPNUM, EP_TYPE_BULK, + ENDPOINT_DIR_IN, AVRISP_DATA_EPSIZE, + ENDPOINT_BANK_SINGLE); + #endif } if (EndpointConfigSuccess) @@ -193,6 +212,18 @@ void EVENT_USB_Device_UnhandledControlRequest(void) CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface); } +/** Event handler for the library USB Connection event. */ +void EVENT_USB_Device_Connect(void) +{ + LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING); +} + +/** Event handler for the library USB Disconnection event. */ +void EVENT_USB_Device_Disconnect(void) +{ + LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); +} + /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors" * documentation) by the application code so that the address and size of a requested descriptor can be given * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function @@ -201,6 +232,7 @@ void EVENT_USB_Device_UnhandledControlRequest(void) */ uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, const uint8_t wIndex, void** const DescriptorAddress) { + /* Return the correct descriptors based on the selected mode */ if (CurrentFirmwareMode == MODE_USART_BRIDGE) return USART_GetDescriptor(wValue, wIndex, DescriptorAddress); else