123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- #include "XPLAINBridge.h"
- bool CurrentFirmwareMode = MODE_USART_BRIDGE;
- USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
- {
- .Config =
- {
- .ControlInterfaceNumber = INTERFACE_ID_CDC_CCI,
- .DataINEndpoint =
- {
- .Address = CDC_TX_EPADDR,
- .Size = CDC_TXRX_EPSIZE,
- .Banks = 1,
- },
- .DataOUTEndpoint =
- {
- .Address = CDC_RX_EPADDR,
- .Size = CDC_TXRX_EPSIZE,
- .Banks = 1,
- },
- .NotificationEndpoint =
- {
- .Address = CDC_NOTIFICATION_EPADDR,
- .Size = CDC_NOTIFICATION_EPSIZE,
- .Banks = 1,
- },
- },
- };
- RingBuffer_t USBtoUART_Buffer;
- static uint8_t USBtoUART_Buffer_Data[128];
- RingBuffer_t UARTtoUSB_Buffer;
- static uint8_t UARTtoUSB_Buffer_Data[128];
- int main(void)
- {
- SetupHardware();
- LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
- GlobalInterruptEnable();
- for (;;)
- {
- if (CurrentFirmwareMode == MODE_USART_BRIDGE)
- UARTBridge_Task();
- else
- AVRISP_Task();
- USB_USBTask();
- }
- }
- void AVRISP_Task(void)
- {
-
- if (USB_DeviceState != DEVICE_STATE_Configured)
- return;
- V2Params_UpdateParamValues();
- Endpoint_SelectEndpoint(AVRISP_DATA_OUT_EPADDR);
-
- if (Endpoint_IsOUTReceived())
- {
- LEDs_SetAllLEDs(LEDMASK_BUSY);
-
- V2Protocol_ProcessCommand();
- LEDs_SetAllLEDs(LEDMASK_USB_READY);
- }
- }
- void UARTBridge_Task(void)
- {
-
- if (USB_DeviceState != DEVICE_STATE_Configured)
- return;
-
- if (!(RingBuffer_IsFull(&USBtoUART_Buffer)))
- {
- int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
-
- if (!(ReceivedByte < 0))
- RingBuffer_Insert(&USBtoUART_Buffer, ReceivedByte);
- }
-
- uint16_t BufferCount = RingBuffer_GetCount(&UARTtoUSB_Buffer);
- if ((TIFR0 & (1 << TOV0)) || (BufferCount > 200))
- {
-
- TIFR0 |= (1 << TOV0);
-
- while (BufferCount--)
- {
-
- if (CDC_Device_SendByte(&VirtualSerial_CDC_Interface,
- RingBuffer_Peek(&UARTtoUSB_Buffer)) != ENDPOINT_READYWAIT_NoError)
- {
- break;
- }
-
- RingBuffer_Remove(&UARTtoUSB_Buffer);
- }
- }
- CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
- }
- void SetupHardware(void)
- {
- #if (ARCH == ARCH_AVR8)
-
- MCUSR &= ~(1 << WDRF);
- wdt_disable();
-
- clock_prescale_set(clock_div_1);
- #endif
-
- MCUCR |= (1 << JTD);
- MCUCR |= (1 << JTD);
-
- PORTF |= (1 << 7);
- Delay_MS(10);
-
- CurrentFirmwareMode = (PINF & (1 << 7)) ? MODE_USART_BRIDGE : MODE_PDI_PROGRAMMER;
-
- MCUCR &= ~(1 << JTD);
- MCUCR &= ~(1 << JTD);
-
- SoftUART_Init();
- LEDs_Init();
- #if defined(RESET_TOGGLES_LIBUSB_COMPAT)
- UpdateCurrentCompatibilityMode();
- #endif
-
- USB_Init();
- }
- void EVENT_USB_Device_ConfigurationChanged(void)
- {
- bool ConfigSuccess = true;
-
- if (CurrentFirmwareMode == MODE_USART_BRIDGE)
- {
- ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
-
- TCCR0B = ((1 << CS02) | (1 << CS00));
-
- RingBuffer_InitBuffer(&USBtoUART_Buffer, USBtoUART_Buffer_Data, sizeof(USBtoUART_Buffer_Data));
- RingBuffer_InitBuffer(&UARTtoUSB_Buffer, UARTtoUSB_Buffer_Data, sizeof(UARTtoUSB_Buffer_Data));
-
- SoftUART_Init();
- }
- else
- {
- ConfigSuccess &= Endpoint_ConfigureEndpoint(AVRISP_DATA_OUT_EPADDR, EP_TYPE_BULK, AVRISP_DATA_EPSIZE, 1);
- if ((AVRISP_DATA_IN_EPADDR & ENDPOINT_EPNUM_MASK) != (AVRISP_DATA_OUT_EPADDR & ENDPOINT_EPNUM_MASK))
- ConfigSuccess &= Endpoint_ConfigureEndpoint(AVRISP_DATA_IN_EPADDR, EP_TYPE_BULK, AVRISP_DATA_EPSIZE, 1);
-
- V2Protocol_Init();
- }
- LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
- }
- void EVENT_USB_Device_ControlRequest(void)
- {
- if (CurrentFirmwareMode == MODE_USART_BRIDGE)
- CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
- }
- void EVENT_USB_Device_Connect(void)
- {
- LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
- }
- void EVENT_USB_Device_Disconnect(void)
- {
- LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
- }
- void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
- {
-
- SoftUART_SetBaud(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS);
- }
- uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
- const uint16_t wIndex,
- const void** const DescriptorAddress)
- {
-
- if (CurrentFirmwareMode == MODE_USART_BRIDGE)
- return USART_GetDescriptor(wValue, wIndex, DescriptorAddress);
- else
- return AVRISP_GetDescriptor(wValue, wIndex, DescriptorAddress);
- }
|