Commit 57f5d0bc19a6407ebc778df5ff2ed7a6adf7dde7

Authored by dmohamed
1 parent 1e53d5cc

FIX - Oublie de modification du fichier USBgadget.c

Showing 1 changed file with 104 additions and 94 deletions   Show diff stats
lufa/USB_gadget.c
1 1 /*
2 2 LUFA Library
3   - Copyright (C) Dean Camera, 2017.
  3 + Copyright (C) Dean Camera, 2010.
4 4  
5 5 dean [at] fourwalledcubicle [dot] com
6   - www.lufa-lib.org
  6 + www.fourwalledcubicle.com
7 7 */
8 8  
9 9 /*
10   - Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  10 + Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11 11  
12 12 Permission to use, copy, modify, distribute, and sell this
13 13 software and its documentation for any purpose is hereby granted
... ... @@ -18,7 +18,7 @@
18 18 advertising or publicity pertaining to distribution of the
19 19 software without specific, written prior permission.
20 20  
21   - The author disclaims all warranties with regard to this
  21 + The author disclaim all warranties with regard to this
22 22 software, including all implied warranties of merchantability
23 23 and fitness. In no event shall the author be liable for any
24 24 special, indirect or consequential damages or any damages
... ... @@ -30,11 +30,17 @@
30 30  
31 31 /** \file
32 32 *
33   - * Main source file for the VirtualSerial demo. This file contains the main tasks of
  33 + * Main source file for the USBtoSerial project. This file contains the main tasks of
34 34 * the demo and is responsible for the initial application hardware configuration.
35 35 */
36 36  
37   -#include "USB_Gadget.h"
  37 +#include "USBtoSerial.h"
  38 +
  39 +/** Circular buffer to hold data from the host before it is sent to the device via the serial port. */
  40 +RingBuff_t USBtoUSART_Buffer;
  41 +
  42 +/** Circular buffer to hold data from the serial port before it is sent to the host. */
  43 +RingBuff_t USARTtoUSB_Buffer;
38 44  
39 45 /** LUFA CDC Class driver interface configuration and state information. This structure is
40 46 * 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 =
44 50 {
45 51 .Config =
46 52 {
47   - .ControlInterfaceNumber = INTERFACE_ID_CDC_CCI,
48   - . DataINEndpoint =
49   - {
50   - .Address = CDC_TX_EPADDR,
51   - .Size = CDC_TXRX_EPSIZE,
52   - .Banks = 1,
53   - },
54   - .DataOUTEndpoint =
55   - {
56   - .Address = CDC_RX_EPADDR,
57   - .Size = CDC_TXRX_EPSIZE,
58   - .Banks = 1,
59   - },
60   - .NotificationEndpoint =
61   - {
62   - .Address = CDC_NOTIFICATION_EPADDR,
63   - .Size = CDC_NOTIFICATION_EPSIZE,
64   - .Banks = 1,
65   - },
66   - },
67   - };
  53 + .ControlInterfaceNumber = 0,
68 54  
69   -/** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be
70   - * used like any regular character stream in the C APIs.
71   - */
72   -static FILE USBSerialStream;
  55 + .DataINEndpointNumber = CDC_TX_EPNUM,
  56 + .DataINEndpointSize = CDC_TXRX_EPSIZE,
  57 + .DataINEndpointDoubleBank = false,
  58 +
  59 + .DataOUTEndpointNumber = CDC_RX_EPNUM,
  60 + .DataOUTEndpointSize = CDC_TXRX_EPSIZE,
  61 + .DataOUTEndpointDoubleBank = false,
73 62  
  63 + .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM,
  64 + .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE,
  65 + .NotificationEndpointDoubleBank = false,
  66 + },
  67 + };
74 68  
75 69 /** Main program entry point. This routine contains the overall program flow, including initial
76 70 * setup of all components and the main program loop.
... ... @@ -79,18 +73,33 @@ int main(void)
79 73 {
80 74 SetupHardware();
81 75  
82   - /* Create a regular character stream for the interface so that it can be used with the stdio.h functions */
83   - CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream);
  76 + RingBuffer_InitBuffer(&USBtoUSART_Buffer);
  77 + RingBuffer_InitBuffer(&USARTtoUSB_Buffer);
84 78  
85 79 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
86   - GlobalInterruptEnable();
  80 + sei();
87 81  
88 82 for (;;)
89 83 {
90   - //CheckJoystickMovement();
91   -
92   - /* Must throw away unused bytes from the host, or it will lock up while waiting for the device */
93   - CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
  84 + /* Read bytes from the USB OUT endpoint into the USART transmit buffer */
  85 + int16_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
  86 + if (!(ReceivedByte < 0) && !(RingBuffer_IsFull(&USBtoUSART_Buffer)))
  87 + RingBuffer_Insert(&USBtoUSART_Buffer, ReceivedByte);
  88 +
  89 + /* Check if the UART receive buffer flush timer has expired or the buffer is nearly full */
  90 + RingBuff_Count_t BufferCount = RingBuffer_GetCount(&USARTtoUSB_Buffer);
  91 + if ((TIFR0 & (1 << TOV0)) || (BufferCount > 200))
  92 + {
  93 + TIFR0 |= (1 << TOV0);
  94 +
  95 + /* Read bytes from the USART receive buffer into the USB IN endpoint */
  96 + while (BufferCount--)
  97 + CDC_Device_SendByte(&VirtualSerial_CDC_Interface, RingBuffer_Remove(&USARTtoUSB_Buffer));
  98 + }
  99 +
  100 + /* Load the next byte from the USART transmit buffer into the USART */
  101 + if (!(RingBuffer_IsEmpty(&USBtoUSART_Buffer)))
  102 + Serial_TxByte(RingBuffer_Remove(&USBtoUSART_Buffer));
94 103  
95 104 CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
96 105 USB_USBTask();
... ... @@ -100,61 +109,19 @@ int main(void)
100 109 /** Configures the board hardware and chip peripherals for the demo's functionality. */
101 110 void SetupHardware(void)
102 111 {
103   -#if (ARCH == ARCH_AVR8)
104 112 /* Disable watchdog if enabled by bootloader/fuses */
105 113 MCUSR &= ~(1 << WDRF);
106 114 wdt_disable();
107 115  
108 116 /* Disable clock division */
109 117 clock_prescale_set(clock_div_1);
110   -#elif (ARCH == ARCH_XMEGA)
111   - /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
112   - XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
113   - XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
114   -
115   - /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
116   - XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
117   - XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
118   -
119   - PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
120   -#endif
121 118  
122 119 /* Hardware Initialization */
123   - Joystick_Init();
124 120 LEDs_Init();
125 121 USB_Init();
126   -}
127   -
128   -/** Checks for changes in the position of the board joystick, sending strings to the host upon each change. */
129   -void CheckJoystickMovement(void)
130   -{
131   - uint8_t JoyStatus_LCL = Joystick_GetStatus();
132   - char* ReportString = NULL;
133   - static bool ActionSent = false;
134   -
135   - if (JoyStatus_LCL & JOY_UP)
136   - ReportString = "Joystick Up\r\n";
137   - else if (JoyStatus_LCL & JOY_DOWN)
138   - ReportString = "Joystick Down\r\n";
139   - else if (JoyStatus_LCL & JOY_LEFT)
140   - ReportString = "Joystick Left\r\n";
141   - else if (JoyStatus_LCL & JOY_RIGHT)
142   - ReportString = "Joystick Right\r\n";
143   - else if (JoyStatus_LCL & JOY_PRESS)
144   - ReportString = "Joystick Pressed\r\n";
145   - else
146   - ActionSent = false;
147   -
148   - if ((ReportString != NULL) && (ActionSent == false))
149   - {
150   - ActionSent = true;
151   -
152   - /* Write the string to the virtual COM port via the created character stream */
153   - fputs(ReportString, &USBSerialStream);
154 122  
155   - /* Alternatively, without the stream: */
156   - // CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString);
157   - }
  123 + /* Start the flush timer so that overflows occur rapidly to push received bytes to the USB interface */
  124 + TCCR0B = (1 << CS02);
158 125 }
159 126  
160 127 /** Event handler for the library USB Connection event. */
... ... @@ -179,24 +146,67 @@ void EVENT_USB_Device_ConfigurationChanged(void)
179 146 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
180 147 }
181 148  
182   -/** Event handler for the library USB Control Request reception event. */
183   -void EVENT_USB_Device_ControlRequest(void)
  149 +/** Event handler for the library USB Unhandled Control Request event. */
  150 +void EVENT_USB_Device_UnhandledControlRequest(void)
184 151 {
185 152 CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
186 153 }
187 154  
188   -/** CDC class driver callback function the processing of changes to the virtual
189   - * control lines sent from the host..
  155 +/** ISR to manage the reception of data from the serial port, placing received bytes into a circular buffer
  156 + * for later transmission to the host.
  157 + */
  158 +ISR(USART1_RX_vect, ISR_BLOCK)
  159 +{
  160 + uint8_t ReceivedByte = UDR1;
  161 +
  162 + if (USB_DeviceState == DEVICE_STATE_Configured)
  163 + RingBuffer_Insert(&USARTtoUSB_Buffer, ReceivedByte);
  164 +}
  165 +
  166 +/** Event handler for the CDC Class driver Line Encoding Changed event.
190 167 *
191 168 * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced
192 169 */
193   -void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t *const CDCInterfaceInfo)
  170 +void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
194 171 {
195   - /* You can get changes to the virtual CDC lines in this callback; a common
196   - use-case is to use the Data Terminal Ready (DTR) flag to enable and
197   - disable CDC communications in your application when set to avoid the
198   - application blocking while waiting for a host to become ready and read
199   - in the pending data from the USB endpoints.
200   - */
201   - bool HostReady = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR) != 0;
  172 + uint8_t ConfigMask = 0;
  173 +
  174 + switch (CDCInterfaceInfo->State.LineEncoding.ParityType)
  175 + {
  176 + case CDC_PARITY_Odd:
  177 + ConfigMask = ((1 << UPM11) | (1 << UPM10));
  178 + break;
  179 + case CDC_PARITY_Even:
  180 + ConfigMask = (1 << UPM11);
  181 + break;
  182 + }
  183 +
  184 + if (CDCInterfaceInfo->State.LineEncoding.CharFormat == CDC_LINEENCODING_TwoStopBits)
  185 + ConfigMask |= (1 << USBS1);
  186 +
  187 + switch (CDCInterfaceInfo->State.LineEncoding.DataBits)
  188 + {
  189 + case 6:
  190 + ConfigMask |= (1 << UCSZ10);
  191 + break;
  192 + case 7:
  193 + ConfigMask |= (1 << UCSZ11);
  194 + break;
  195 + case 8:
  196 + ConfigMask |= ((1 << UCSZ11) | (1 << UCSZ10));
  197 + break;
  198 + }
  199 +
  200 + /* Must turn off USART before reconfiguring it, otherwise incorrect operation may occur */
  201 + UCSR1B = 0;
  202 + UCSR1A = 0;
  203 + UCSR1C = 0;
  204 +
  205 + /* Set the new baud rate before configuring the USART */
  206 + UBRR1 = SERIAL_2X_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS);
  207 +
  208 + /* Reconfigure the USART in double speed mode for a wider baud rate range at the expense of accuracy */
  209 + UCSR1C = ConfigMask;
  210 + UCSR1A = (1 << U2X1);
  211 + UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1));
202 212 }
... ...