From 57f5d0bc19a6407ebc778df5ff2ed7a6adf7dde7 Mon Sep 17 00:00:00 2001 From: dmohamed Date: Sun, 2 Jul 2017 18:54:11 +0200 Subject: [PATCH] FIX - Oublie de modification du fichier USBgadget.c --- lufa/USB_gadget.c | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------- 1 file changed, 104 insertions(+), 94 deletions(-) diff --git a/lufa/USB_gadget.c b/lufa/USB_gadget.c index e561309..0e6ffe1 100644 --- a/lufa/USB_gadget.c +++ b/lufa/USB_gadget.c @@ -1,13 +1,13 @@ /* LUFA Library - Copyright (C) Dean Camera, 2017. + Copyright (C) Dean Camera, 2010. dean [at] fourwalledcubicle [dot] com - www.lufa-lib.org + www.fourwalledcubicle.com */ /* - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted @@ -18,7 +18,7 @@ advertising or publicity pertaining to distribution of the software without specific, written prior permission. - The author disclaims all warranties with regard to this + The author disclaim all warranties with regard to this software, including all implied warranties of merchantability and fitness. In no event shall the author be liable for any special, indirect or consequential damages or any damages @@ -30,11 +30,17 @@ /** \file * - * Main source file for the VirtualSerial demo. This file contains the main tasks of + * Main source file for the USBtoSerial project. This file contains the main tasks of * the demo and is responsible for the initial application hardware configuration. */ -#include "USB_Gadget.h" +#include "USBtoSerial.h" + +/** Circular buffer to hold data from the host before it is sent to the device via the serial port. */ +RingBuff_t USBtoUSART_Buffer; + +/** Circular buffer to hold data from the serial port before it is sent to the host. */ +RingBuff_t USARTtoUSB_Buffer; /** LUFA CDC Class driver interface configuration and state information. This structure is * passed to all CDC Class driver functions, so that multiple instances of the same class @@ -44,33 +50,21 @@ 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, - }, - }, - }; + .ControlInterfaceNumber = 0, -/** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be - * used like any regular character stream in the C APIs. - */ -static FILE USBSerialStream; + .DataINEndpointNumber = CDC_TX_EPNUM, + .DataINEndpointSize = CDC_TXRX_EPSIZE, + .DataINEndpointDoubleBank = false, + + .DataOUTEndpointNumber = CDC_RX_EPNUM, + .DataOUTEndpointSize = CDC_TXRX_EPSIZE, + .DataOUTEndpointDoubleBank = false, + .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM, + .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE, + .NotificationEndpointDoubleBank = false, + }, + }; /** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. @@ -79,18 +73,33 @@ int main(void) { SetupHardware(); - /* Create a regular character stream for the interface so that it can be used with the stdio.h functions */ - CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream); + RingBuffer_InitBuffer(&USBtoUSART_Buffer); + RingBuffer_InitBuffer(&USARTtoUSB_Buffer); LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); - GlobalInterruptEnable(); + sei(); for (;;) { - //CheckJoystickMovement(); - - /* Must throw away unused bytes from the host, or it will lock up while waiting for the device */ - CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); + /* Read bytes from the USB OUT endpoint into the USART transmit buffer */ + int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); + if (!(ReceivedByte < 0) && !(RingBuffer_IsFull(&USBtoUSART_Buffer))) + RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte); + + /* Check if the UART receive buffer flush timer has expired or the buffer is nearly full */ + RingBuff_Count_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer); + if ((TIFR0 & (1 << TOV0)) || (BufferCount > 200)) + { + TIFR0 |= (1 << TOV0); + + /* Read bytes from the USART receive buffer into the USB IN endpoint */ + while (BufferCount--) + CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Remove(&USARTtoUSB_Buffer)); + } + + /* Load the next byte from the USART transmit buffer into the USART */ + if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer))) + Serial_TxByte(RingBuffer_Remove(&USBtoUSART_Buffer)); CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); @@ -100,61 +109,19 @@ int main(void) /** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { -#if (ARCH == ARCH_AVR8) /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); -#elif (ARCH == ARCH_XMEGA) - /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */ - XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU); - XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL); - - /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */ - XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ); - XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB); - - PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm; -#endif /* Hardware Initialization */ - Joystick_Init(); LEDs_Init(); USB_Init(); -} - -/** Checks for changes in the position of the board joystick, sending strings to the host upon each change. */ -void CheckJoystickMovement(void) -{ - uint8_t JoyStatus_LCL = Joystick_GetStatus(); - char* ReportString = NULL; - static bool ActionSent = false; - - if (JoyStatus_LCL & JOY_UP) - ReportString = "Joystick Up\r\n"; - else if (JoyStatus_LCL & JOY_DOWN) - ReportString = "Joystick Down\r\n"; - else if (JoyStatus_LCL & JOY_LEFT) - ReportString = "Joystick Left\r\n"; - else if (JoyStatus_LCL & JOY_RIGHT) - ReportString = "Joystick Right\r\n"; - else if (JoyStatus_LCL & JOY_PRESS) - ReportString = "Joystick Pressed\r\n"; - else - ActionSent = false; - - if ((ReportString != NULL) && (ActionSent == false)) - { - ActionSent = true; - - /* Write the string to the virtual COM port via the created character stream */ - fputs(ReportString, &USBSerialStream); - /* Alternatively, without the stream: */ - // CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString); - } + /* Start the flush timer so that overflows occur rapidly to push received bytes to the USB interface */ + TCCR0B = (1 << CS02); } /** Event handler for the library USB Connection event. */ @@ -179,24 +146,67 @@ void EVENT_USB_Device_ConfigurationChanged(void) LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR); } -/** Event handler for the library USB Control Request reception event. */ -void EVENT_USB_Device_ControlRequest(void) +/** Event handler for the library USB Unhandled Control Request event. */ +void EVENT_USB_Device_UnhandledControlRequest(void) { CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface); } -/** CDC class driver callback function the processing of changes to the virtual - * control lines sent from the host.. +/** ISR to manage the reception of data from the serial port, placing received bytes into a circular buffer + * for later transmission to the host. + */ +ISR(USART1_RX_vect, ISR_BLOCK) +{ + uint8_t ReceivedByte = UDR1; + + if (USB_DeviceState == DEVICE_STATE_Configured) + RingBuffer_Insert(&USARTtoUSB_Buffer, ReceivedByte); +} + +/** Event handler for the CDC Class driver Line Encoding Changed event. * * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced */ -void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo) +void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) { - /* You can get changes to the virtual CDC lines in this callback; a common - use-case is to use the Data Terminal Ready (DTR) flag to enable and - disable CDC communications in your application when set to avoid the - application blocking while waiting for a host to become ready and read - in the pending data from the USB endpoints. - */ - bool HostReady = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) != 0; + uint8_t ConfigMask = 0; + + 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->State.LineEncoding.CharFormat == CDC_LINEENCODING_TwoStopBits) + ConfigMask |= (1 << USBS1); + + 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; + } + + /* Must turn off USART before reconfiguring it, otherwise incorrect operation may occur */ + UCSR1B = 0; + UCSR1A = 0; + UCSR1C = 0; + + /* Set the new baud rate before configuring the USART */ + UBRR1 = SERIAL_2X_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS); + + /* Reconfigure the USART in double speed mode for a wider baud rate range at the expense of accuracy */ + UCSR1C = ConfigMask; + UCSR1A = (1 << U2X1); + UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1)); } -- libgit2 0.21.2