From 2d7ee914305cbde76e9fd1cd712476ba36b72f19 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Fri, 10 Jun 2011 23:26:36 -0300 Subject: [PATCH] atusb/fw/: added transition from runtime to DFU mode according to DFU spec - descr.c (config_descriptor): added DFU interface descriptor - ep0.c (my_dfu_setup, my_set_interface, ep0_init): added DFU interface switching - ep0.c (my_reset, ep0_init): CPU-reset (into boot loader) on USB bus reset when in appDETACH - Makefile (OBJS): added dfu_common.o --- atusb/fw/Makefile | 3 ++- atusb/fw/descr.c | 9 +++++++-- atusb/fw/ep0.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/atusb/fw/Makefile b/atusb/fw/Makefile index 3deb830..3989294 100644 --- a/atusb/fw/Makefile +++ b/atusb/fw/Makefile @@ -31,7 +31,8 @@ SIZE = $(AVR_PREFIX)size USB_ID = 20b7:1540 USB_OBJS = usb.o atu2.o -OBJS = atusb.o board.o board_app.o sernum.o spi.o descr.o ep0.o $(USB_OBJS) +OBJS = atusb.o board.o board_app.o sernum.o spi.o descr.o ep0.o dfu_common.o \ + $(USB_OBJS) BOOT_OBJS = boot.o board.o sernum.o spi.o flash.o dfu.o dfu_common.o \ $(USB_OBJS) diff --git a/atusb/fw/descr.c b/atusb/fw/descr.c index f3fb0c0..d32d455 100644 --- a/atusb/fw/descr.c +++ b/atusb/fw/descr.c @@ -12,6 +12,7 @@ #include "usb.h" +#include "dfu.h" #include "board.h" @@ -55,9 +56,9 @@ const uint8_t config_descriptor[] = { #if 0 LE(9+9+7+7), /* wTotalLength */ #else - LE(9+9), /* wTotalLength */ + LE(9+9+9), /* wTotalLength */ #endif - 1, /* bNumInterfaces */ + 2, /* bNumInterfaces */ 1, /* bConfigurationValue (> 0 !) */ 0, /* iConfiguration */ USB_ATTR_BUS_POWERED, /* bmAttributes */ @@ -98,4 +99,8 @@ const uint8_t config_descriptor[] = { LE(EP1_SIZE), /* wMaxPacketSize */ 0, /* bInterval */ #endif + + /* Interface #1 */ + + DFU_ITF_DESCR(1, dfu_proto_runtime) }; diff --git a/atusb/fw/ep0.c b/atusb/fw/ep0.c index 81c2a3f..95f112f 100644 --- a/atusb/fw/ep0.c +++ b/atusb/fw/ep0.c @@ -21,11 +21,13 @@ #endif #include "usb.h" +#include "dfu.h" #include "at86rf230.h" #include "atusb/ep0.h" #include "version.h" #include "board.h" +#include "sernum.h" #include "spi.h" @@ -207,7 +209,43 @@ static int my_setup(const struct setup_request *setup) } +static int my_dfu_setup(const struct setup_request *setup) +{ + switch (setup->bmRequestType | setup->bRequest << 8) { + case DFU_TO_DEV(DFU_DETACH): + /* @@@ should use wTimeout */ + dfu.state = appDETACH; + return 1; + default: + return dfu_setup_common(setup); + } +} + + +static void my_set_interface(int nth) +{ + if (nth) { + user_setup = my_dfu_setup; + user_get_descriptor = dfu_my_descr; + dfu.state = appIDLE; + } else { + user_setup = my_setup; + user_get_descriptor = sernum_get_descr; + } +} + + +static void my_reset(void) +{ + if (dfu.state == appDETACH) + reset_cpu(); +} + + void ep0_init(void) { user_setup = my_setup; + user_set_interface = my_set_interface; + my_set_interface(0); + user_reset = my_reset; }