From d05aa81927af8672656b0b3f0ccdefb6e6b42f94 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Fri, 28 Sep 2018 21:09:19 -0400 Subject: avr: Use generic usb_cdc code for usb serial support Use the generic usb_cdc driver code instead of the "pjrc" usb driver code. Signed-off-by: Kevin O'Connor --- src/avr/Makefile | 4 +- src/avr/usbserial.c | 269 +++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 226 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/avr/Makefile b/src/avr/Makefile index 9f814a44..c3f3ab34 100644 --- a/src/avr/Makefile +++ b/src/avr/Makefile @@ -3,14 +3,14 @@ # Use the avr toolchain CROSS_PREFIX=avr- -dirs-y += src/avr src/generic lib/pjrc_usb_serial +dirs-y += src/avr src/generic CFLAGS += -mmcu=$(CONFIG_MCU) # Add avr source files src-y += avr/main.c avr/timer.c avr/gpio.c src-$(CONFIG_AVR_WATCHDOG) += avr/watchdog.c -src-$(CONFIG_AVR_USBSERIAL) += avr/usbserial.c ../lib/pjrc_usb_serial/usb_serial.c +src-$(CONFIG_AVR_USBSERIAL) += avr/usbserial.c generic/usb_cdc.c src-$(CONFIG_AVR_SERIAL) += avr/serial.c generic/serial_irq.c # Suppress broken "misspelled signal handler" warnings on gcc 4.8.1 diff --git a/src/avr/usbserial.c b/src/avr/usbserial.c index 2a16160d..188a1b14 100644 --- a/src/avr/usbserial.c +++ b/src/avr/usbserial.c @@ -1,71 +1,250 @@ -// Wrappers for AVR usb serial. +// Hardware interface to USB on AVR at90usb // -// Copyright (C) 2016,2017 Kevin O'Connor +// Copyright (C) 2018 Kevin O'Connor // // This file may be distributed under the terms of the GNU GPLv3 license. -#include // memmove -#include "../lib/pjrc_usb_serial/usb_serial.h" -#include "board/misc.h" // console_sendf -#include "command.h" // command_find_and_dispatch +#include // USB_COM_vect +#include // NULL +#include "autoconf.h" // CONFIG_MACH_at90usb1286 +#include "board/usb_cdc.h" // usb_notify_ep0 +#include "board/usb_cdc_ep.h" // USB_CDC_EP_BULK_IN +#include "pgm.h" // READP #include "sched.h" // DECL_INIT -static uint8_t receive_buf[MESSAGE_MAX], receive_pos; +// EPCFG0X definitions +#define EP_TYPE_CONTROL 0x00 +#define EP_TYPE_BULK_IN 0x81 +#define EP_TYPE_BULK_OUT 0x80 +#define EP_TYPE_INTERRUPT_IN 0xC1 -void -usbserial_init(void) +// EPCFG1X definitions +#define EP_SINGLE_BUFFER 0x02 +#define EP_DOUBLE_BUFFER 0x06 +#define EP_SIZE(s) ((s)==64 ? 0x30 : ((s)==32 ? 0x20 : ((s)==16 ? 0x10 : 0x00))) + +static void +usb_write_packet(const uint8_t *data, uint8_t len) { - usb_init(); + while (len--) + UEDATX = *data++; } -DECL_INIT(usbserial_init); -// Check for new incoming data static void -console_check_input(void) -{ - for (;;) { - if (receive_pos >= sizeof(receive_buf)) - break; - int16_t ret = usb_serial_getchar(); - if (ret == -1) - break; - receive_buf[receive_pos++] = ret; - } +usb_write_packet_progmem(const uint8_t *data, uint8_t len) +{ + while (len--) + UEDATX = READP(*data++); } -// Remove from the receive buffer the given number of bytes static void -console_pop_input(uint8_t len) +usb_read_packet(uint8_t *data, uint8_t len) +{ + while (len--) + *data++ = UEDATX; +} + +int_fast8_t +usb_read_bulk_out(void *data, uint_fast8_t max_len) +{ + UENUM = USB_CDC_EP_BULK_OUT; + if (!(UEINTX & (1<