Added new RNDISHost Host LowLevel demo. Fixed misnamed Pipe_SetPipeToken() macro...
[pub/lufa.git] / Projects / XPLAINBridge / XPLAINBridge.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11 Copyright 2009 John Steggall (steggall.j@gmail.com)
12
13 Permission to use, copy, modify, and distribute this software
14 and its documentation for any purpose and without fee is hereby
15 granted, provided that the above copyright notice appear in all
16 copies and that both that the copyright notice and this
17 permission notice and warranty disclaimer appear in supporting
18 documentation, and that the name of the author not be used in
19 advertising or publicity pertaining to distribution of the
20 software without specific, written prior permission.
21
22 The author disclaim all warranties with regard to this
23 software, including all implied warranties of merchantability
24 and fitness. In no event shall the author be liable for any
25 special, indirect or consequential damages or any damages
26 whatsoever resulting from loss of use, data or profits, whether
27 in an action of contract, negligence or other tortious action,
28 arising out of or in connection with the use or performance of
29 this software.
30 */
31
32 /** \file
33 *
34 * Main source file for the XPLAINBridge project. This file contains the main tasks of
35 * the demo and is responsible for the initial application hardware configuration.
36 */
37
38 #include "XPLAINBridge.h"
39
40 /** Circular buffer to hold data from the host before it is sent to the device via the serial port. */
41 RingBuff_t USBtoUART_Buffer;
42
43 /** Circular buffer to hold data from the serial port before it is sent to the host. */
44 RingBuff_t UARTtoUSB_Buffer;
45
46 /** LUFA CDC Class driver interface configuration and state information. This structure is
47 * passed to all CDC Class driver functions, so that multiple instances of the same class
48 * within a device can be differentiated from one another.
49 */
50 USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
51 {
52 .Config =
53 {
54 .ControlInterfaceNumber = 0,
55
56 .DataINEndpointNumber = CDC_TX_EPNUM,
57 .DataINEndpointSize = CDC_TXRX_EPSIZE,
58 .DataINEndpointDoubleBank = false,
59
60 .DataOUTEndpointNumber = CDC_RX_EPNUM,
61 .DataOUTEndpointSize = CDC_TXRX_EPSIZE,
62 .DataOUTEndpointDoubleBank = false,
63
64 .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM,
65 .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE,
66 .NotificationEndpointDoubleBank = false,
67 },
68 };
69
70 /** Main program entry point. This routine contains the overall program flow, including initial
71 * setup of all components and the main program loop.
72 */
73 int main(void)
74 {
75 SetupHardware();
76
77 Buffer_Initialize(&USBtoUART_Buffer);
78 Buffer_Initialize(&UARTtoUSB_Buffer);
79
80 for (;;)
81 {
82 /* Read bytes from the USB OUT endpoint into the UART transmit buffer */
83 for (uint8_t DataBytesRem = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface); DataBytesRem != 0; DataBytesRem--)
84 {
85 if (!(BUFF_STATICSIZE - USBtoUART_Buffer.Elements))
86 break;
87
88 Buffer_StoreElement(&USBtoUART_Buffer, CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface));
89 }
90
91 /* Read bytes from the UART receive buffer into the USB IN endpoint */
92 if (UARTtoUSB_Buffer.Elements)
93 CDC_Device_SendByte(&VirtualSerial_CDC_Interface, Buffer_GetElement(&UARTtoUSB_Buffer));
94
95 /* Load bytes from the UART transmit buffer into the UART */
96 if ((USBtoUART_Buffer.Elements) && SoftUART_IsReady())
97 SoftUART_TxByte(Buffer_GetElement(&USBtoUART_Buffer));
98
99 /* Load bytes from the UART into the UART receive buffer */
100 if(SoftUART_IsReceived())
101 Buffer_StoreElement(&UARTtoUSB_Buffer, SoftUART_RxByte());
102
103 CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
104 USB_USBTask();
105 }
106 }
107
108 /** Configures the board hardware and chip peripherals for the demo's functionality. */
109 void SetupHardware(void)
110 {
111 /* Disable watchdog if enabled by bootloader/fuses */
112 MCUSR &= ~(1 << WDRF);
113 wdt_disable();
114
115 /* Disable clock division */
116 clock_prescale_set(clock_div_1);
117
118 /* Hardware Initialization */
119 SoftUART_Init();
120 LEDs_Init();
121 USB_Init();
122 }
123
124 /** Event handler for the library USB Configuration Changed event. */
125 void EVENT_USB_Device_ConfigurationChanged(void)
126 {
127 LEDs_SetAllLEDs(LEDS_LED1);
128
129 if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface)))
130 LEDs_SetAllLEDs(LEDS_NO_LEDS);
131 }
132
133 /** Event handler for the library USB Unhandled Control Request event. */
134 void EVENT_USB_Device_UnhandledControlRequest(void)
135 {
136 CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
137 }