From 72ed993e982d1a2484ef9d54d93757f2f9094547 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Fri, 10 Apr 2009 01:16:50 +0200 Subject: * digital/dev2: - added usb_serial_isp, no isp yet. --- digital/dev2/README | 32 ++++ digital/dev2/src/common/select.c | 66 +++++++++ digital/dev2/src/common/select.h | 40 +++++ digital/dev2/src/common/serial.c | 77 ++++++++++ digital/dev2/src/common/serial.h | 37 +++++ digital/dev2/src/usb_serial_isp/Makefile | 14 ++ digital/dev2/src/usb_serial_isp/avrconfig.h | 79 ++++++++++ digital/dev2/src/usb_serial_isp/descriptors.c | 202 ++++++++++++++++++++++++++ digital/dev2/src/usb_serial_isp/descriptors.h | 38 +++++ digital/dev2/src/usb_serial_isp/dev2ctl.py | 66 +++++++++ digital/dev2/src/usb_serial_isp/main.c | 137 +++++++++++++++++ 11 files changed, 788 insertions(+) create mode 100644 digital/dev2/README create mode 100644 digital/dev2/src/common/select.c create mode 100644 digital/dev2/src/common/select.h create mode 100644 digital/dev2/src/common/serial.c create mode 100644 digital/dev2/src/common/serial.h create mode 100644 digital/dev2/src/usb_serial_isp/Makefile create mode 100644 digital/dev2/src/usb_serial_isp/avrconfig.h create mode 100644 digital/dev2/src/usb_serial_isp/descriptors.c create mode 100644 digital/dev2/src/usb_serial_isp/descriptors.h create mode 100755 digital/dev2/src/usb_serial_isp/dev2ctl.py create mode 100644 digital/dev2/src/usb_serial_isp/main.c (limited to 'digital') diff --git a/digital/dev2/README b/digital/dev2/README new file mode 100644 index 00000000..b7bc8d88 --- /dev/null +++ b/digital/dev2/README @@ -0,0 +1,32 @@ +dev2 - Multi-purpose development board using USB and Ethernet. + +This development board can be connected to four different boards to: + + - use a serial port, + - program an AVR using its ISP interface, + - program a CPLD using XSVF JTAG files, + - do anything else you dare to implement. + +Only one out of the four boards can be used at a given time. The dev2 can be +connected using USB and/or Ethernet. + + +Copyright (C) 2009 Nicolas Schodet + +APBTeam: + Web: http://apbteam.org/ + Email: team AT apbteam DOT org + +This program 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 2 of the License, or +(at your option) any later version. + +This program 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 this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. diff --git a/digital/dev2/src/common/select.c b/digital/dev2/src/common/select.c new file mode 100644 index 00000000..d4e7bb30 --- /dev/null +++ b/digital/dev2/src/common/select.c @@ -0,0 +1,66 @@ +/* select.c - Output selection mechanism. */ +/* dev2 - Multi-purpose development board using USB and Ethernet. {{{ + * + * Copyright (C) 2009 Nicolas Schodet + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT org + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * }}} */ +#include "common.h" + +#include "io.h" + +#include "select.h" + +#define ENABLE _BV (2) +#define SELA _BV (5) +#define SELB _BV (4) + +/* Initialise selector. */ +void +select_init (void) +{ + PORTC &= ~(ENABLE | SELA | SELB); + DDRC |= ENABLE | SELA | SELB; +} + +/* Select an output (1-4) or switch off (0, anything else). */ +void +select_out (uint8_t num) +{ + uint8_t p = PORTC & ~(ENABLE | SELA | SELB); + switch (num) + { + case 1: + PORTC = p | ENABLE; + break; + case 2: + PORTC = p | ENABLE | SELA; + break; + case 3: + PORTC = p | ENABLE | SELB; + break; + case 4: + PORTC = p | ENABLE | SELA | SELB; + break; + default: + PORTC = p; + break; + } +} diff --git a/digital/dev2/src/common/select.h b/digital/dev2/src/common/select.h new file mode 100644 index 00000000..30b760bd --- /dev/null +++ b/digital/dev2/src/common/select.h @@ -0,0 +1,40 @@ +#ifndef select_h +#define select_h +/* select.h */ +/* dev2 - Multi-purpose development board using USB and Ethernet. {{{ + * + * Copyright (C) 2009 Nicolas Schodet + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT org + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * }}} */ + +static inline uint8_t +select_active (uint8_t num) +{ + return num >= 1 && num <= 4; +} + +void +select_init (void); + +void +select_out (uint8_t num); + +#endif /* select_h */ diff --git a/digital/dev2/src/common/serial.c b/digital/dev2/src/common/serial.c new file mode 100644 index 00000000..f7f3fe06 --- /dev/null +++ b/digital/dev2/src/common/serial.c @@ -0,0 +1,77 @@ +/* serial.c */ +/* dev2 - Multi-purpose development board using USB and Ethernet. {{{ + * + * Copyright (C) 2009 Nicolas Schodet + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT org + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * }}} */ +#include "common.h" + +#include "io.h" + +#include "serial.h" +#include "descriptors.h" + +#include "modules/uart/uart.h" +#include "modules/usb/usb.h" + +#define RX D, 2 + +void +serial_init (void) +{ + /* Use Pull-up on RX pin. */ + IO_DDR (RX) &= ~IO_BV (RX); + IO_PORT (RX) |= IO_BV (RX); + uart0_init (); +} + +void +serial_uninit (void) +{ + /* Disable UART. */ + UCSR1B = 0; + IO_PORT (RX) &= ~IO_BV (RX); +} + +void +serial_task (void) +{ + Endpoint_SelectEndpoint (SERIAL_RX_EPNUM); + /* If data is available from USB: */ + if (Endpoint_ReadWriteAllowed ()) + { + /* Read as much as possible, and clear endpoint. */ + do { + uart0_putc (Endpoint_Read_Byte ()); + } while (Endpoint_ReadWriteAllowed ()); + Endpoint_ClearCurrentBank (); + } + /* If data is available from uart and there is room in the TX endpoint: */ + Endpoint_SelectEndpoint (SERIAL_TX_EPNUM); + if (uart0_poll () && Endpoint_ReadWriteAllowed ()) + { + do { + Endpoint_Write_Byte (uart0_getc ()); + } while (uart0_poll () && Endpoint_ReadWriteAllowed ()); + Endpoint_ClearCurrentBank (); + } +} + diff --git a/digital/dev2/src/common/serial.h b/digital/dev2/src/common/serial.h new file mode 100644 index 00000000..5b55e681 --- /dev/null +++ b/digital/dev2/src/common/serial.h @@ -0,0 +1,37 @@ +#ifndef serial_h +#define serial_h +/* serial.h */ +/* dev2 - Multi-purpose development board using USB and Ethernet. {{{ + * + * Copyright (C) 2009 Nicolas Schodet + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT org + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * }}} */ + +void +serial_init (void); + +void +serial_uninit (void); + +void +serial_task (void); + +#endif /* serial_h */ diff --git a/digital/dev2/src/usb_serial_isp/Makefile b/digital/dev2/src/usb_serial_isp/Makefile new file mode 100644 index 00000000..29230cb0 --- /dev/null +++ b/digital/dev2/src/usb_serial_isp/Makefile @@ -0,0 +1,14 @@ +BASE = ../../../avr +AVR_PROGS = dev2_serial_isp +dev2_serial_isp_SOURCES = main.c descriptors.c serial.c select.c +MODULES = usb uart +CONFIGFILE = avrconfig.h +AVR_MCU = at90usb162 +# -O2 : speed +# -Os : size +OPTIMIZE = -Os + +INCLUDES = -I.. -I. +vpath %.c ../common + +include $(BASE)/make/Makefile.gen diff --git a/digital/dev2/src/usb_serial_isp/avrconfig.h b/digital/dev2/src/usb_serial_isp/avrconfig.h new file mode 100644 index 00000000..085aed8b --- /dev/null +++ b/digital/dev2/src/usb_serial_isp/avrconfig.h @@ -0,0 +1,79 @@ +#ifndef avrconfig_h +#define avrconfig_h +/* avrconfig.h */ +/* usb - USB device module using LUFA. {{{ + * + * Copyright (C) 2009 Nicolas Schodet + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT org + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * }}} */ + +/* utils */ +/** AVR Frequency : 1000000, 1843200, 2000000, 3686400, 4000000, 7372800, + * 8000000, 11059200, 14745600, 16000000, 18432000, 20000000. */ +#define AC_FREQ 8000000 + +/* uart - UART module. */ +/** Select hardware uart for primary uart: 0, 1 or -1 to disable. */ +#define AC_UART0_PORT 1 +/** Baudrate: 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, + * 115200, 230400, 250000, 500000, 1000000. */ +#define AC_UART0_BAUDRATE 38400 +/** Send mode: + * - POLLING: no interrupts. + * - RING: interrupts, ring buffer. */ +#define AC_UART0_SEND_MODE RING +/** Recv mode, same as send mode. */ +#define AC_UART0_RECV_MODE RING +/** Character size: 5, 6, 7, 8, 9 (only 8 implemented). */ +#define AC_UART0_CHAR_SIZE 8 +/** Parity : ODD, EVEN, NONE. */ +#define AC_UART0_PARITY EVEN +/** Stop bits : 1, 2. */ +#define AC_UART0_STOP_BITS 1 +/** Send buffer size, should be power of 2 for RING mode. */ +#define AC_UART0_SEND_BUFFER_SIZE 16 +/** Recv buffer size, should be power of 2 for RING mode. */ +#define AC_UART0_RECV_BUFFER_SIZE 32 +/** If the send buffer is full when putc: + * - DROP: drop the new byte. + * - WAIT: wait until there is room in the send buffer. */ +#define AC_UART0_SEND_BUFFER_FULL WAIT +/** In HOST compilation: + * - STDIO: use stdin/out. + * - PTS: use pseudo terminal. */ +#define AC_UART0_HOST_DRIVER STDIO +/** Same thing for secondary port. */ +#define AC_UART1_PORT -1 +#define AC_UART1_BAUDRATE 115200 +#define AC_UART1_SEND_MODE RING +#define AC_UART1_RECV_MODE RING +#define AC_UART1_CHAR_SIZE 8 +#define AC_UART1_PARITY EVEN +#define AC_UART1_STOP_BITS 1 +#define AC_UART1_SEND_BUFFER_SIZE 32 +#define AC_UART1_RECV_BUFFER_SIZE 32 +#define AC_UART1_SEND_BUFFER_FULL WAIT +#define AC_UART1_HOST_DRIVER PTS + +/* usb */ +#include "modules/usb/lufaconfig.h" + +#endif /* avrconfig_h */ diff --git a/digital/dev2/src/usb_serial_isp/descriptors.c b/digital/dev2/src/usb_serial_isp/descriptors.c new file mode 100644 index 00000000..684b1004 --- /dev/null +++ b/digital/dev2/src/usb_serial_isp/descriptors.c @@ -0,0 +1,202 @@ +/* descriptors.c */ +/* dev2 - Multi-purpose development board using USB and Ethernet. {{{ + * + * Copyright (C) 2009 Nicolas Schodet + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT org + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * }}} */ +#include "common.h" + +#include "modules/usb/usb.h" + +#include + +#include "descriptors.h" + +/* Configuration structure. */ +typedef struct +{ + USB_Descriptor_Configuration_Header_t Config; + USB_Descriptor_Interface_t TS1_Interface; + USB_Descriptor_Endpoint_t TS1_DataOutEndpoint; + USB_Descriptor_Endpoint_t TS1_DataInEndpoint; + USB_Descriptor_Interface_t TS2_Interface; + USB_Descriptor_Endpoint_t TS2_DataOutEndpoint; + USB_Descriptor_Endpoint_t TS2_DataInEndpoint; +} USB_Descriptor_Configuration_t; + +USB_Descriptor_Device_t PROGMEM DeviceDescriptor = +{ + Header: { Size: sizeof (USB_Descriptor_Device_t), Type: DTYPE_Device }, + USBSpecification: VERSION_BCD (01.10), + Class: 0xFF, + SubClass: 0x00, + Protocol: 0x00, + Endpoint0Size: 8, + /* Taken from LUFA IDs. */ + VendorID: 0x03EB, + ProductID: 0x204E, + ReleaseNumber: 0x0000, + ManufacturerStrIndex: 0x01, + ProductStrIndex: 0x02, + SerialNumStrIndex: NO_DESCRIPTOR, + NumberOfConfigurations: 1 +}; + +USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor = +{ + Config: + { + Header: { Size: sizeof (USB_Descriptor_Configuration_Header_t), + Type: DTYPE_Configuration }, + TotalConfigurationSize: sizeof (USB_Descriptor_Configuration_t), + TotalInterfaces: 2, + ConfigurationNumber: 1, + ConfigurationStrIndex: NO_DESCRIPTOR, + ConfigAttributes: (USB_CONFIG_ATTR_BUSPOWERED | + USB_CONFIG_ATTR_SELFPOWERED), + MaxPowerConsumption: USB_CONFIG_POWER_MA (100) + }, + + TS1_Interface: + { + Header: { Size: sizeof (USB_Descriptor_Interface_t), + Type: DTYPE_Interface }, + InterfaceNumber: 0, + AlternateSetting: 0, + TotalEndpoints: 2, + Class: 0xFF, + SubClass: 0x00, + Protocol: 0x00, + InterfaceStrIndex: NO_DESCRIPTOR + }, + + TS1_DataOutEndpoint: + { + Header: { Size: sizeof (USB_Descriptor_Endpoint_t), + Type: DTYPE_Endpoint }, + EndpointAddress: (ENDPOINT_DESCRIPTOR_DIR_OUT | SERIAL_RX_EPNUM), + Attributes: EP_TYPE_BULK, + EndpointSize: SERIAL_RX_EPSIZE, + PollingIntervalMS: 0x00 + }, + + TS1_DataInEndpoint: + { + Header: { Size: sizeof (USB_Descriptor_Endpoint_t), + Type: DTYPE_Endpoint }, + EndpointAddress: (ENDPOINT_DESCRIPTOR_DIR_IN | SERIAL_TX_EPNUM), + Attributes: EP_TYPE_BULK, + EndpointSize: SERIAL_TX_EPSIZE, + PollingIntervalMS: 0x00 + }, + + TS2_Interface: + { + Header: { Size: sizeof (USB_Descriptor_Interface_t), + Type: DTYPE_Interface }, + InterfaceNumber: 1, + AlternateSetting: 0, + TotalEndpoints: 2, + Class: 0xFF, + SubClass: 0x00, + Protocol: 0x00, + InterfaceStrIndex: NO_DESCRIPTOR + }, + + TS2_DataOutEndpoint: + { + Header: { Size: sizeof (USB_Descriptor_Endpoint_t), + Type: DTYPE_Endpoint }, + EndpointAddress: (ENDPOINT_DESCRIPTOR_DIR_OUT | ISP_RX_EPNUM), + Attributes: EP_TYPE_BULK, + EndpointSize: ISP_RX_EPSIZE, + PollingIntervalMS: 0x00 + }, + + TS2_DataInEndpoint: + { + Header: { Size: sizeof (USB_Descriptor_Endpoint_t), + Type: DTYPE_Endpoint }, + EndpointAddress: (ENDPOINT_DESCRIPTOR_DIR_IN | ISP_TX_EPNUM), + Attributes: EP_TYPE_BULK, + EndpointSize: ISP_TX_EPSIZE, + PollingIntervalMS: 0x00 + } +}; + +USB_Descriptor_String_t PROGMEM LanguageString = +{ + Header: { Size: USB_STRING_LEN (1), Type: DTYPE_String }, + UnicodeString: {LANGUAGE_ID_ENG} +}; + +USB_Descriptor_String_t PROGMEM ManufacturerString = +{ + Header: { Size: USB_STRING_LEN (7), Type: DTYPE_String }, + UnicodeString: L"APBTeam" +}; + +USB_Descriptor_String_t PROGMEM ProductString = +{ + Header: { Size: USB_STRING_LEN (15), Type: DTYPE_String }, + UnicodeString: L"dev2 serial isp" +}; + +uint16_t +USB_GetDescriptor (const uint16_t wValue, const uint8_t wIndex, + void ** const DescriptorAddress) +{ + const uint8_t DescriptorType = wValue >> 8; + const uint8_t DescriptorNumber = wValue & 0xFF; + void * Address = NULL; + uint16_t Size = NO_DESCRIPTOR; + switch (DescriptorType) + { + case DTYPE_Device: + Address = DESCRIPTOR_ADDRESS (DeviceDescriptor); + Size = sizeof (USB_Descriptor_Device_t); + break; + case DTYPE_Configuration: + Address = DESCRIPTOR_ADDRESS (ConfigurationDescriptor); + Size = sizeof (USB_Descriptor_Configuration_t); + break; + case DTYPE_String: + switch (DescriptorNumber) + { + case 0x00: + Address = DESCRIPTOR_ADDRESS (LanguageString); + Size = pgm_read_byte (&LanguageString.Header.Size); + break; + case 0x01: + Address = DESCRIPTOR_ADDRESS (ManufacturerString); + Size = pgm_read_byte (&ManufacturerString.Header.Size); + break; + case 0x02: + Address = DESCRIPTOR_ADDRESS (ProductString); + Size = pgm_read_byte (&ProductString.Header.Size); + break; + } + break; + } + *DescriptorAddress = Address; + return Size; +} + diff --git a/digital/dev2/src/usb_serial_isp/descriptors.h b/digital/dev2/src/usb_serial_isp/descriptors.h new file mode 100644 index 00000000..e2dba02f --- /dev/null +++ b/digital/dev2/src/usb_serial_isp/descriptors.h @@ -0,0 +1,38 @@ +#ifndef descriptors_h +#define descriptors_h +/* descriptors.h */ +/* dev2 - Multi-purpose development board using USB and Ethernet. {{{ + * + * Copyright (C) 2009 Nicolas Schodet + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT org + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * }}} */ + +/* Endpoints. */ +#define SERIAL_TX_EPNUM 1 +#define SERIAL_TX_EPSIZE 16 +#define SERIAL_RX_EPNUM 2 +#define SERIAL_RX_EPSIZE 16 +#define ISP_TX_EPNUM 3 +#define ISP_TX_EPSIZE 16 +#define ISP_RX_EPNUM 4 +#define ISP_RX_EPSIZE 16 + +#endif /* descriptors_h */ diff --git a/digital/dev2/src/usb_serial_isp/dev2ctl.py b/digital/dev2/src/usb_serial_isp/dev2ctl.py new file mode 100755 index 00000000..00097521 --- /dev/null +++ b/digital/dev2/src/usb_serial_isp/dev2ctl.py @@ -0,0 +1,66 @@ +#!/usr/bin/python +# dev2 - Multi-purpose development board using USB and Ethernet. {{{ +# +# Copyright (C) 2009 Nicolas Schodet +# +# APBTeam: +# Web: http://apbteam.org/ +# Email: team AT apbteam DOT org +# +# This program 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 2 of the License, or +# (at your option) any later version. +# +# This program 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 this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# }}} +"""Control dev2 board using USB.""" +import sys +from optparse import OptionParser +import usb + +# Parse options. +opt = OptionParser (description = __doc__) +opt.add_option ('-s', '--select', type = 'int', + help = 'select specified OUTPUT (1 to 4)', metavar = 'OUTPUT') +opt.add_option ('-u', '--unselect', action = 'store_true', default = False, + help = 'unselect outputs') +opt.add_option ('-d', '--dfu', action = 'store_true', default = False, + help = 'go to DFU boot loader') + +(options, args) = opt.parse_args () +if args: + opt.error ('too many arguments') +if (options.select is not None) + options.unselect + options.dfu != 1: + opt.error ('choose one of available options') +if options.select is not None and (options.select < 1 or options.select > 4): + opt.error ('output out of bound') +if options.unselect: + options.select = 0 + +# Open device. +d = None +for bus in usb.busses (): + for dev in bus.devices: + if dev.idVendor == 0x03eb and dev.idProduct == 0x204e: + d = dev.open () +if d is None: + print >> sys.stderr, 'device not found' + sys.exit (1) + +# Send control message. +if options.dfu: + d.controlMsg (usb.TYPE_VENDOR | usb.RECIP_DEVICE, 0, 0) +elif options.select is not None: + d.controlMsg (usb.TYPE_VENDOR | usb.RECIP_DEVICE, 1, 0, + value = options.select) +else: + assert 0 diff --git a/digital/dev2/src/usb_serial_isp/main.c b/digital/dev2/src/usb_serial_isp/main.c new file mode 100644 index 00000000..d8694fe6 --- /dev/null +++ b/digital/dev2/src/usb_serial_isp/main.c @@ -0,0 +1,137 @@ +/* main.c */ +/* dev2 - Multi-purpose development board using USB and Ethernet. {{{ + * + * Copyright (C) 2009 Nicolas Schodet + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT org + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * }}} */ +#include "common.h" + +#include "modules/usb/usb.h" +#include "io.h" + +#include + +#include "descriptors.h" +#include "common/serial.h" +#include "common/select.h" + +HANDLES_EVENT (USB_Connect); +HANDLES_EVENT (USB_Disconnect); +HANDLES_EVENT (USB_ConfigurationChanged); +HANDLES_EVENT (USB_UnhandledControlPacket); + +volatile uint8_t usb_connected, usb_configured; + +int +main (void) +{ + /* Disable watchdog if enabled by bootloader/fuses. */ + MCUSR &= ~(1 << WDRF); + wdt_disable (); + /* Disable Clock Division. */ + SetSystemClockPrescaler (0); + /* Initialise hardware. */ + select_init (); + /* Initialize USB Subsystem. */ + USB_Init (); + /* Main loop. */ + while (1) + { + if (usb_connected) + USB_USBTask (); + if (usb_configured) + { + serial_task (); + } + } +} + +EVENT_HANDLER (USB_Connect) +{ + usb_connected = 1; +} + +EVENT_HANDLER (USB_Disconnect) +{ + usb_connected = 0; + usb_configured = 0; +} + +EVENT_HANDLER (USB_ConfigurationChanged) +{ + /* Setup Rx and Tx Endpoints for the first port. */ + Endpoint_ConfigureEndpoint (SERIAL_TX_EPNUM, EP_TYPE_BULK, + ENDPOINT_DIR_IN, SERIAL_TX_EPSIZE, + ENDPOINT_BANK_SINGLE); + Endpoint_ConfigureEndpoint (SERIAL_RX_EPNUM, EP_TYPE_BULK, + ENDPOINT_DIR_OUT, SERIAL_RX_EPSIZE, + ENDPOINT_BANK_SINGLE); + /* Setup Rx and Tx Endpoints for the second port. */ + Endpoint_ConfigureEndpoint (ISP_TX_EPNUM, EP_TYPE_BULK, + ENDPOINT_DIR_IN, ISP_TX_EPSIZE, + ENDPOINT_BANK_SINGLE); + Endpoint_ConfigureEndpoint (ISP_RX_EPNUM, EP_TYPE_BULK, + ENDPOINT_DIR_OUT, ISP_RX_EPSIZE, + ENDPOINT_BANK_SINGLE); + /* Start tasks. */ + usb_configured = 1; +} + +EVENT_HANDLER (USB_UnhandledControlPacket) +{ + /* Process TS specific control requests. */ + switch (bRequest) + { + /* Switch back to DFU mode. */ + case 0: + if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_VENDOR | + REQREC_DEVICE)) + { + Endpoint_ClearSetupReceived (); + /* Send acknowledgement and wait for it to be sent. */ + Endpoint_ClearSetupIN (); + while (!Endpoint_IsSetupINReady ()) + ; + USB_ShutDown (); + /* Jump to bootloader. */ + ((void (*) (void)) 0x3000) (); + } + break; + /* Select output. */ + case 1: + if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_VENDOR | + REQREC_DEVICE)) + { + /* Selector parameter. */ + uint8_t output = Endpoint_Read_Byte (); + Endpoint_ClearSetupReceived (); + /* Select output. */ + serial_uninit (); + select_out (output); + if (select_active (output)) + serial_init (); + /* Send acknowledgement. */ + Endpoint_ClearSetupIN (); + } + break; + } +} + -- cgit v1.2.3