diff options
-rw-r--r-- | lib/dualusbio.c | 129 | ||||
-rw-r--r-- | lib/dualusbio.h | 37 | ||||
-rw-r--r-- | usb-led2/Makefile | 45 | ||||
-rw-r--r-- | usb-led2/usb-led2.c | 87 |
4 files changed, 298 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; +} diff --git a/lib/dualusbio.h b/lib/dualusbio.h new file mode 100644 index 0000000..0f07ea6 --- /dev/null +++ b/lib/dualusbio.h @@ -0,0 +1,37 @@ +/* + * 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/>. + */ + +#ifndef SPREADAVR_dualusbio_h_INCLUDED +#define SPREADAVR_dualusbio_h_INCLUDED + +#include <stdio.h> +#include <stdint.h> + +extern FILE usbio_port1; +extern FILE usbio_port2; + +void dualusbio_init(void); +void dualusbio_task(void); +void dualusbio_make_stdio(uint8_t port); +int16_t dualusbio_bytes_received(uint8_t port); + +#endif diff --git a/usb-led2/Makefile b/usb-led2/Makefile new file mode 100644 index 0000000..616bf62 --- /dev/null +++ b/usb-led2/Makefile @@ -0,0 +1,45 @@ +## +## spreadspace avr utils +## +## +## Copyright (C) 2013-2014 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/>. +## + +NAME := usb-led2 +BOARD_TYPE := teensy2 +OBJ := $(NAME).o +LIBS := util led lufa-descriptor-usbdualserial dualusbio +EXTERNAL_LIBS := lufa +SPREADAVR_PATH := .. + +LUFA_PATH := $(SPREADAVR_PATH)/contrib/LUFA-120219 +LUFA_OPTS = -D USB_DEVICE_ONLY +LUFA_OPTS += -D DEVICE_STATE_AS_GPIOR=0 +LUFA_OPTS += -D ORDERED_EP_CONFIG +LUFA_OPTS += -D FIXED_CONTROL_ENDPOINT_SIZE=8 +LUFA_OPTS += -D FIXED_NUM_CONFIGURATIONS=1 +LUFA_OPTS += -D USE_FLASH_DESCRIPTORS +LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)" +LUFA_OPTS += -D INTERRUPT_CONTROL_ENDPOINT + +LUFA_OPTS += -D USB_MANUFACTURER="L\"equinox\"" -D USB_MANUFACTURER_LEN=7 +LUFA_OPTS += -D USB_PRODUCT="L\"spreadspace usb-led example\"" -D USB_PRODUCT_LEN=27 + +LUFA_COMPONENTS := USB USBCLASS + +include $(SPREADAVR_PATH)/include.mk diff --git a/usb-led2/usb-led2.c b/usb-led2/usb-led2.c new file mode 100644 index 0000000..345f076 --- /dev/null +++ b/usb-led2/usb-led2.c @@ -0,0 +1,87 @@ +/* + * spreadspace avr utils + * + * + * Copyright (C) 2013-2014 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 <avr/io.h> +#include <avr/wdt.h> +#include <avr/interrupt.h> +#include <avr/power.h> +#include <stdio.h> + +#include "util.h" +#include "led.h" +#include "dualusbio.h" + +void handle_cmd1(uint8_t cmd) +{ + switch(cmd) { + case '0': led_off(); break; + case '1': led_on(); break; + case 't': led_toggle(); break; + default: fprintf(&usbio_port1, "1: error\r\n"); return; + } + fprintf(&usbio_port1, "1: ok\r\n"); +} + +void handle_cmd2(uint8_t cmd) +{ + switch(cmd) { + case '0': led_on(); break; + case '1': led_off(); break; + case 't': led_toggle(); break; + default: fprintf(&usbio_port2, "2: error\r\n"); return; + } + fprintf(&usbio_port2, "2: ok\r\n"); +} + +int main(void) +{ + MCUSR &= ~(1 << WDRF); + wdt_disable(); + + cpu_init(); + led_init(); + dualusbio_init(); + sei(); + + for(;;) { + int16_t BytesReceived = dualusbio_bytes_received(1); + while(BytesReceived > 0) { + int ReceivedByte = fgetc(&usbio_port1); + if(ReceivedByte != EOF) { + handle_cmd1(ReceivedByte); + } + BytesReceived--; + } + + BytesReceived = dualusbio_bytes_received(2); + while(BytesReceived > 0) { + int ReceivedByte = fgetc(&usbio_port2); + if(ReceivedByte != EOF) { + handle_cmd2(ReceivedByte); + } + BytesReceived--; + } + + dualusbio_task(); + } +} |