summaryrefslogtreecommitdiff
path: root/lib/dualusbio.c
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2015-06-22 00:18:41 +0200
committerChristian Pointner <equinox@spreadspace.org>2015-06-22 00:18:41 +0200
commite8c5fbe15f12caa15a6a3f37884129df4ef0ddad (patch)
treeb2d47bbb1322f4bc6eb4f6c215a85a2b43fd0049 /lib/dualusbio.c
parentremoved useless header in usbio (diff)
added dualusbio lib and example for it (not fully working yet..)
Diffstat (limited to 'lib/dualusbio.c')
-rw-r--r--lib/dualusbio.c129
1 files changed, 129 insertions, 0 deletions
diff --git a/lib/dualusbio.c b/lib/dualusbio.c
new file mode 100644
index 0000000..d774d6a
--- /dev/null
+++ b/lib/dualusbio.c
@@ -0,0 +1,129 @@
+/*
+ * spreadspace avr utils
+ *
+ *
+ * Copyright (C) 2013-2015 Christian Pointner <equinox@spreadspace.org>
+ *
+ * This file is part of spreadspace avr utils.
+ *
+ * spreadspace avr utils is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * any later version.
+ *
+ * spreadspace avr utils is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+
+#include "dualusbio.h"
+
+/*
+ LUFA Library
+ Copyright (C) Dean Camera, 2012.
+
+ dean [at] fourwalledcubicle [dot] com
+ www.lufa-lib.org
+*/
+#include <LUFA/Drivers/USB/USB.h>
+#include "lufa-descriptor-usbdualserial.h"
+
+USB_ClassInfo_CDC_Device_t VirtualSerial1_CDC_Interface =
+ {
+ .Config =
+ {
+ .ControlInterfaceNumber = 0,
+
+ .DataINEndpointNumber = CDC1_TX_EPNUM,
+ .DataINEndpointSize = CDC_TXRX_EPSIZE,
+ .DataINEndpointDoubleBank = false,
+
+ .DataOUTEndpointNumber = CDC1_RX_EPNUM,
+ .DataOUTEndpointSize = CDC_TXRX_EPSIZE,
+ .DataOUTEndpointDoubleBank = false,
+
+ .NotificationEndpointNumber = CDC1_NOTIFICATION_EPNUM,
+ .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE,
+ .NotificationEndpointDoubleBank = false,
+ },
+ };
+
+USB_ClassInfo_CDC_Device_t VirtualSerial2_CDC_Interface =
+ {
+ .Config =
+ {
+ .ControlInterfaceNumber = 2,
+
+ .DataINEndpointNumber = CDC2_TX_EPNUM,
+ .DataINEndpointSize = CDC_TXRX_EPSIZE,
+ .DataINEndpointDoubleBank = false,
+
+ .DataOUTEndpointNumber = CDC2_RX_EPNUM,
+ .DataOUTEndpointSize = CDC_TXRX_EPSIZE,
+ .DataOUTEndpointDoubleBank = false,
+
+ .NotificationEndpointNumber = CDC2_NOTIFICATION_EPNUM,
+ .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE,
+ .NotificationEndpointDoubleBank = false,
+ },
+ };
+
+void EVENT_USB_Device_ConfigurationChanged(void)
+{
+ CDC_Device_ConfigureEndpoints(&VirtualSerial1_CDC_Interface);
+ CDC_Device_ConfigureEndpoints(&VirtualSerial2_CDC_Interface);
+}
+
+void EVENT_USB_Device_ControlRequest(void)
+{
+ CDC_Device_ProcessControlRequest(&VirtualSerial1_CDC_Interface);
+ CDC_Device_ProcessControlRequest(&VirtualSerial2_CDC_Interface);
+}
+
+FILE usbio_port1;
+FILE usbio_port2;
+
+void dualusbio_init(void)
+{
+ USB_Init();
+ CDC_Device_CreateStream(&VirtualSerial1_CDC_Interface, &usbio_port1);
+ CDC_Device_CreateStream(&VirtualSerial2_CDC_Interface, &usbio_port2);
+}
+
+
+void dualusbio_task(void)
+{
+#if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
+ if(USB_DeviceState == DEVICE_STATE_Unattached && USB_VBUS_GetStatus()) {
+ USB_Disable();
+ USB_Init();
+ }
+#endif
+
+ CDC_Device_USBTask(&VirtualSerial1_CDC_Interface);
+ CDC_Device_USBTask(&VirtualSerial2_CDC_Interface);
+ USB_USBTask();
+}
+
+void dualusbio_make_stdio(uint8_t port)
+{
+ switch(port) {
+ case 1: stdin = stdout = stderr = &usbio_port1; break;
+ case 2: stdin = stdout = stderr = &usbio_port2; break;
+ }
+}
+
+int16_t dualusbio_bytes_received(uint8_t port)
+{
+ switch(port) {
+ case 1: return CDC_Device_BytesReceived(&VirtualSerial1_CDC_Interface);
+ case 2: return CDC_Device_BytesReceived(&VirtualSerial2_CDC_Interface);
+ }
+ return 0;
+}