X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/b9b03aadb219d06fbad9d110e508db93e45461af..2a0c28e6e47c8a173f32fc99cd8666a2633c5c12:/Demos/Device/ClassDriver/USBtoSerial/USBtoSerial.c?ds=sidebyside diff --git a/Demos/Device/ClassDriver/USBtoSerial/USBtoSerial.c b/Demos/Device/ClassDriver/USBtoSerial/USBtoSerial.c index 85826c2b3..637071a7e 100644 --- a/Demos/Device/ClassDriver/USBtoSerial/USBtoSerial.c +++ b/Demos/Device/ClassDriver/USBtoSerial/USBtoSerial.c @@ -46,18 +46,26 @@ RingBuff_t Tx_Buffer; * passed to all CDC Class driver functions, so that multiple instances of the same class * within a device can be differentiated from one another. */ -USB_ClassInfo_CDC_t VirtualSerial_CDC_Interface = +USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface = { - .ControlInterfaceNumber = 0, + .Config = + { + .ControlInterfaceNumber = 0, - .DataINEndpointNumber = CDC_TX_EPNUM, - .DataINEndpointSize = CDC_TXRX_EPSIZE, + .DataINEndpointNumber = CDC_TX_EPNUM, + .DataINEndpointSize = CDC_TXRX_EPSIZE, - .DataOUTEndpointNumber = CDC_RX_EPNUM, - .DataOUTEndpointSize = CDC_TXRX_EPSIZE, + .DataOUTEndpointNumber = CDC_RX_EPNUM, + .DataOUTEndpointSize = CDC_TXRX_EPSIZE, - .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM, - .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE, + .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM, + .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE, + }, + + .State = + { + // Leave all state values to their defaults + } }; /** Main program entry point. This routine contains the overall program flow, including initial @@ -74,21 +82,24 @@ int main(void) for (;;) { - for (uint8_t DataBytesRem = USB_CDC_BytesReceived(&VirtualSerial_CDC_Interface); DataBytesRem != 0; DataBytesRem--) + /* Read bytes from the USB OUT endpoint into the USART transmit buffer */ + for (uint8_t DataBytesRem = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface); DataBytesRem != 0; DataBytesRem--) { if (!(BUFF_STATICSIZE - Rx_Buffer.Elements)) break; - Buffer_StoreElement(&Rx_Buffer, USB_CDC_ReceiveByte(&VirtualSerial_CDC_Interface)); + Buffer_StoreElement(&Rx_Buffer, CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface)); } + /* Read bytes from the USART receive buffer into the USB IN endpoint */ if (Tx_Buffer.Elements) - USB_CDC_SendByte(&VirtualSerial_CDC_Interface, Buffer_GetElement(&Rx_Buffer)); - + CDC_Device_SendByte(&VirtualSerial_CDC_Interface, Buffer_GetElement(&Tx_Buffer)); + + /* Load bytes from the USART transmit buffer into the USART */ if (Rx_Buffer.Elements) Serial_TxByte(Buffer_GetElement(&Rx_Buffer)); - USB_CDC_USBTask(&VirtualSerial_CDC_Interface); + CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); } } @@ -126,14 +137,14 @@ void EVENT_USB_ConfigurationChanged(void) { LEDs_SetAllLEDs(LEDMASK_USB_READY); - if (!(USB_CDC_ConfigureEndpoints(&VirtualSerial_CDC_Interface))) + if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface))) LEDs_SetAllLEDs(LEDMASK_USB_ERROR); } /** Event handler for the library USB Unhandled Control Packet event. */ void EVENT_USB_UnhandledControlPacket(void) { - USB_CDC_ProcessControlPacket(&VirtualSerial_CDC_Interface); + CDC_Device_ProcessControlPacket(&VirtualSerial_CDC_Interface); } /** ISR to manage the reception of data from the serial port, placing received bytes into a circular buffer @@ -141,35 +152,46 @@ void EVENT_USB_UnhandledControlPacket(void) */ ISR(USART1_RX_vect, ISR_BLOCK) { - if (USB_IsConnected) + if (USB_DeviceState == DEVICE_STATE_Configured) Buffer_StoreElement(&Tx_Buffer, UDR1); } /** Event handler for the CDC Class driver Line Encoding Changed event. * - * \param CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced + * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced */ -void EVENT_USB_CDC_LineEncodingChanged(USB_ClassInfo_CDC_t* CDCInterfaceInfo) +void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) { uint8_t ConfigMask = 0; - if (CDCInterfaceInfo->LineEncoding.ParityType == CDC_PARITY_Odd) - ConfigMask = ((1 << UPM11) | (1 << UPM10)); - else if (CDCInterfaceInfo->LineEncoding.ParityType == CDC_PARITY_Even) - ConfigMask = (1 << UPM11); + switch (CDCInterfaceInfo->State.LineEncoding.ParityType) + { + case CDC_PARITY_Odd: + ConfigMask = ((1 << UPM11) | (1 << UPM10)); + break; + case CDC_PARITY_Even: + ConfigMask = (1 << UPM11); + break; + } - if (CDCInterfaceInfo->LineEncoding.CharFormat == CDC_LINEENCODING_TwoStopBits) + if (CDCInterfaceInfo->State.LineEncoding.CharFormat == CDC_LINEENCODING_TwoStopBits) ConfigMask |= (1 << USBS1); - if (CDCInterfaceInfo->LineEncoding.DataBits == 6) - ConfigMask |= (1 << UCSZ10); - else if (CDCInterfaceInfo->LineEncoding.DataBits == 7) - ConfigMask |= (1 << UCSZ11); - else if (CDCInterfaceInfo->LineEncoding.DataBits == 8) - ConfigMask |= ((1 << UCSZ11) | (1 << UCSZ10)); + switch (CDCInterfaceInfo->State.LineEncoding.DataBits) + { + case 6: + ConfigMask |= (1 << UCSZ10); + break; + case 7: + ConfigMask |= (1 << UCSZ11); + break; + case 8: + ConfigMask |= ((1 << UCSZ11) | (1 << UCSZ10)); + break; + } UCSR1A = (1 << U2X1); UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1)); UCSR1C = ConfigMask; - UBRR1 = SERIAL_2X_UBBRVAL((uint16_t)CDCInterfaceInfo->LineEncoding.BaudRateBPS); + UBRR1 = SERIAL_2X_UBBRVAL((uint16_t)CDCInterfaceInfo->State.LineEncoding.BaudRateBPS); }