From f01bfaf8fa82467725d3ad27d859b30590d9f26b Mon Sep 17 00:00:00 2001
From: Micah Elizabeth Scott <micah@scanlime.org>
Date: Sun, 6 Oct 2013 10:44:21 -0700
Subject: [PATCH] More progress on DFU, still not passing dfu-util download yet

---
 bootloader/bootloader.c |  6 ++---
 bootloader/dfu.c        | 59 +++++++++++++++++++++++++++++++++++++----
 bootloader/usb_desc.c   |  2 +-
 bootloader/usb_desc.h   |  1 +
 bootloader/usb_dev.c    |  7 +----
 5 files changed, 60 insertions(+), 15 deletions(-)

diff --git a/bootloader/bootloader.c b/bootloader/bootloader.c
index e83f4d0..0438949 100644
--- a/bootloader/bootloader.c
+++ b/bootloader/bootloader.c
@@ -66,7 +66,7 @@ bool test_banner_echo()
 		}
 	}
 
-	serial_end();
+	//serial_end();
 	return matched == bannerLength;
 }
 
@@ -75,9 +75,9 @@ int main()
 {
 	// Say hello!
 
-	if (test_banner_echo()) {
+//	if (test_banner_echo()) {
 		led_on();
-	}
+//	}
 
     usb_init();
 
diff --git a/bootloader/dfu.c b/bootloader/dfu.c
index 604e1b5..2b8b866 100644
--- a/bootloader/dfu.c
+++ b/bootloader/dfu.c
@@ -24,27 +24,76 @@
 #include "usb_dev.h"
 #include "serial.h"
 
+typedef enum {
+	appIDLE = 0,
+	appDETACH,
+	dfuIDLE,
+	dfuDNLOAD_SYNC,
+	dfuDNBUSY,
+	dfuDNLOAD_IDLE,
+	dfuMANIFEST_SYNC,
+	dfuMANIFEST,
+	dfuMANIFEST_WAIT_RESET,
+	dfuUPLOAD_IDLE,
+	dfuERROR
+} dfu_state_t;
+
+typedef enum {
+	OK = 0,
+	errTARGET,
+	errFILE,
+	errWRITE,
+	errERASE,
+	errCHECK_ERASED,
+	errPROG,
+	errVERIFY,
+	errADDRESS,
+	errNOTDONE,
+	errFIRMWARE,
+	errVENDOR,
+	errUSBR,
+	errPOR,
+	errUNKNOWN,
+	errSTALLEDPKT,
+} dfu_status_t;
+
+
+static dfu_state_t dfu_state = dfuIDLE;
+static dfu_status_t dfu_status = OK;
+static unsigned dfu_poll_timeout = 1;
+
+
 void dfu_download(unsigned blockNum, unsigned length, const uint8_t *data)
 {
-
+	serial_phex32(blockNum);
+	serial_putchar(' ');
+	serial_phex32(length);
+	serial_putchar('\n');
 }
 
 void dfu_getstatus(uint8_t *status)
 {
-
+	status[0] = dfu_status;
+	status[1] = dfu_poll_timeout >> 16;
+	status[2] = dfu_poll_timeout >> 8;
+	status[3] = dfu_poll_timeout;
+	status[4] = dfu_state;
+	status[5] = 0;  // iString
 }
 
 void dfu_clrstatus()
 {
-
+	if (dfu_state == dfuERROR) {
+		dfu_state = dfuIDLE;
+	}
 }
 
 uint8_t dfu_getstate()
 {
-	return 0;
+	return dfu_state;
 }
 
 void dfu_abort()
 {
-	
+	dfu_state = dfuIDLE;
 }
diff --git a/bootloader/usb_desc.c b/bootloader/usb_desc.c
index aa29eb6..1da9530 100644
--- a/bootloader/usb_desc.c
+++ b/bootloader/usb_desc.c
@@ -107,7 +107,7 @@ static uint8_t config_descriptor[CONFIG_DESC_SIZE] = {
         0,                                      // bNumEndpoints
         DEVICE_CLASS,                           // bInterfaceClass
         DEVICE_SUBCLASS,                        // bInterfaceSubClass
-        0x00,                                   // bInterfaceProtocol
+        DFU_INTERFACE_PROTOCOL,                 // bInterfaceProtocol
         2,                                      // iInterface
 
         // DFU Functional Descriptor (DFU spec TAble 4.2)
diff --git a/bootloader/usb_desc.h b/bootloader/usb_desc.h
index 2d6d604..e2e9d2e 100644
--- a/bootloader/usb_desc.h
+++ b/bootloader/usb_desc.h
@@ -47,6 +47,7 @@
 #define EP0_SIZE                  64
 #define NUM_INTERFACE             1
 #define DFU_INTERFACE             0
+#define DFU_INTERFACE_PROTOCOL	  0x02		// DFU Mode
 #define DFU_DETACH_TIMEOUT		  10000		// 10 second timer
 #define DFU_TRANSFER_SIZE		  1024		// Equal to flash erase block size
 #define CONFIG_DESC_SIZE          (9+9+7)
diff --git a/bootloader/usb_dev.c b/bootloader/usb_dev.c
index 9ea9a1c..c85681d 100644
--- a/bootloader/usb_dev.c
+++ b/bootloader/usb_dev.c
@@ -299,8 +299,6 @@ static void usb_control(uint32_t stat)
 
         // clear any leftover pending IN transactions
         ep0_tx_ptr = NULL;
-        if (ep0_tx_data_toggle) {
-        }
         table[index(0, TX, EVEN)].desc = 0;
         table[index(0, TX, ODD)].desc = 0;
 
@@ -309,9 +307,6 @@ static void usb_control(uint32_t stat)
 
         // actually "do" the setup request
         usb_setup();
-
-        // unfreeze the USB, now that we're ready
-        USB0_CTL = USB_CTL_USBENSOFEN; // clear TXSUSPENDTOKENBUSY bit
         break;
 
     case 0x01:  // OUT transaction received from host
@@ -319,7 +314,7 @@ static void usb_control(uint32_t stat)
 
         if (setup.wRequestAndType == 0x0141 && setup.wIndex == 0) {
             // DFU_DNLOAD
-            dfu_download(setup.wValue, setup.wLength, (uint8_t*) b->addr);
+            dfu_download(setup.wValue, setup.wLength, buf);
         }
 
         // give the buffer back
-- 
GitLab