From 41ccbbc6b4bbeeae177cc39dfa930018d6a46702 Mon Sep 17 00:00:00 2001
From: Stephan Baerwolf <stephan.baerwolf@tu-ilmenau.de>
Date: Fri, 23 Nov 2012 18:00:46 +0100
Subject: [PATCH 1/3] tinyUSBboard Version v0.95

Signed-off-by: Stephan Baerwolf <stephan.baerwolf@tu-ilmenau.de>
---
 Makefile.inc                |    2 +-
 firmware/bootloaderconfig.h |    2 +-
 firmware/spminterface.h     |    5 +++--
 updater/updater.c           |   11 ++++++++---
 4 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/Makefile.inc b/Makefile.inc
index 3efdc6d..e5d5f12 100644
--- a/Makefile.inc
+++ b/Makefile.inc
@@ -9,7 +9,7 @@ F_CPU = 16000000
 DEVICE = atmega8
 
 # where the updating firmware should be located (starting address)
-FLASHADDRESS = 0x0000
+FLASHADDRESS = 0xC00
 
 # some MCU independend defines...
 #...will be extended within MCU dependend configuration below...
diff --git a/firmware/bootloaderconfig.h b/firmware/bootloaderconfig.h
index af67d6e..6c288c2 100644
--- a/firmware/bootloaderconfig.h
+++ b/firmware/bootloaderconfig.h
@@ -153,7 +153,7 @@ these macros are defined, the boot loader usees them.
  */
 
 /* all boards should use a magic to make it safe to confuse updatefiles :-)  */
-#define HAVE_SPMINTEREFACE_MAGICVALUE    0
+#define HAVE_SPMINTEREFACE_MAGICVALUE    0xfe65a980
 /* If this feature is enabled (value != 0), the configured 32bit value is 
  * used as a magic value within spminterface. "bootloader__do_spm" will check
  * additional four (4) registers for this value and only proceed, if they contain
diff --git a/firmware/spminterface.h b/firmware/spminterface.h
index 8026aca..54a4649 100644
--- a/firmware/spminterface.h
+++ b/firmware/spminterface.h
@@ -294,7 +294,7 @@ void do_spm(const uint32_t flash_byteaddress, const uint8_t spmcrval, const uint
 
 //assume  SPMCR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6
 #if HAVE_SPMINTEREFACE_MAGICVALUE
-const uint16_t bootloader__do_spm[23] BOOTLIBLINK = {
+const uint16_t bootloader__do_spm[23+8] BOOTLIBLINK = {
   (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
   bootloader__do_spm_magic_exitstrategy(0xf4a1), // brne +20
   (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
@@ -307,7 +307,8 @@ const uint16_t bootloader__do_spm[23] BOOTLIBLINK = {
 const uint16_t bootloader__do_spm[15] BOOTLIBLINK = {
 #endif
   0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
-  0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508
+  0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508,
+  0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
 };
 
 /*
diff --git a/updater/updater.c b/updater/updater.c
index 75f0996..6edc305 100644
--- a/updater/updater.c
+++ b/updater/updater.c
@@ -7,11 +7,11 @@
 
 // activate updaters full set of features
 #ifndef CONFIG_UPDATER_REDUCEWRITES
-  #define CONFIG_UPDATER_REDUCEWRITES
+//   #define CONFIG_UPDATER_REDUCEWRITES
 #endif
 
 #ifndef CONFIG_UPDATER_CLEANMEMCLEAR
-  #define CONFIG_UPDATER_CLEANMEMCLEAR
+//   #define CONFIG_UPDATER_CLEANMEMCLEAR
 #endif
 
 
@@ -276,6 +276,10 @@ int main(void)
     
     wdt_disable();
     cli();
+    DDRD = (1<<PD5) | (1<<PD3);
+    DDRB = (1<<PB0) | (1<<PB1);
+    
+    PORTD = (1<<PD5);
 
     // check if firmware would change...
     buffer[0]=0;
@@ -332,7 +336,8 @@ int main(void)
       }
 
 
-
+      PORTB = (1<<PB1);
     }
 
+    PORTB |= (1<<PB0);
 }
-- 
1.7.8.6


From 77797ba6576d9b3bcc0c0bc51cb42d5109d80bac Mon Sep 17 00:00:00 2001
From: Stephan Baerwolf <stephan.baerwolf@tu-ilmenau.de>
Date: Fri, 23 Nov 2012 18:02:53 +0100
Subject: [PATCH 2/3] some space optimizations - tinyUSBboard v0.95.1

Signed-off-by: Stephan Baerwolf <stephan.baerwolf@tu-ilmenau.de>
---
 firmware/Makefile       |    2 +-
 firmware/main.c         |   18 ++++++++++--------
 firmware/spminterface.h |    5 +++--
 3 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/firmware/Makefile b/firmware/Makefile
index ba588dd..2d581a2 100644
--- a/firmware/Makefile
+++ b/firmware/Makefile
@@ -19,7 +19,7 @@ include ../Makefile.inc
 
 # Remove the -fno-* options when you use gcc 3, it does not understand them
 CFLAGS = -Wall -Os -fno-move-loop-invariants -fno-tree-scev-cprop -fno-inline-small-functions -I. -mmcu=$(DEVICE) -DBOOTLOADER_ADDRESS=$(BOOTLOADER_ADDRESS) -DF_CPU=$(F_CPU) $(DEFINES)
-LDFLAGS = -Wl,--relax,--gc-sections -Wl,--section-start=.text=$(BOOTLOADER_ADDRESS)
+LDFLAGS = -Wl,--relax,--gc-sections -Wl,--section-start=.text=$(BOOTLOADER_ADDRESS) -Wl,--defsym=nullVector=0
 
 OBJECTS =  usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o
 
diff --git a/firmware/main.c b/firmware/main.c
index 7ce88db..f7c37ec 100644
--- a/firmware/main.c
+++ b/firmware/main.c
@@ -182,21 +182,20 @@ static const uchar  signatureBytes[4] = {
 static void (*nullVector)(void) __attribute__((__noreturn__));
 #endif
 
-static void __attribute__((__noreturn__)) leaveBootloader()
-{
 #if (USE_EXCESSIVE_ASSEMBLER) && (defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__))
+static void __attribute__((naked)) __attribute__((__noreturn__)) leaveBootloader(void);
+static void leaveBootloader(void)
+{
 asm  volatile  (
   "cli\n\t"
-  "clr		r30\n\t"
   "sbi		%[usbddr],	%[usbminus]\n\t"  
   "cbi		%[port],	%[bit]\n\t"
-  "out		%[usbintrenab],	r30\n\t"
-  "out		%[usbintrcfg],	r30\n\t"
+  "out		%[usbintrenab],	__zero_reg__\n\t"
+  "out		%[usbintrcfg],	__zero_reg__\n\t"
   "ldi		r31,		%[ivce]\n\t"
   "out		%[mygicr],	r31\n\t"
-  "out		%[mygicr],	r30\n\t"  
-  "clr		r31\n\t"
-  "icall\n\t"
+  "out		%[mygicr],	__zero_reg__\n\t"  
+  "rjmp		nullVector\n\t"
   :
   : [port]        "I" (_SFR_IO_ADDR(PIN_PORT(JUMPER_PORT))),
     [bit]         "I" (PIN(JUMPER_PORT, JUMPER_BIT)),
@@ -209,6 +208,9 @@ asm  volatile  (
 );
 /* TODO: compiler will put an unnecessary "ret" at this end ! */
 #else
+static void __attribute__((__noreturn__)) leaveBootloader(void);
+static void leaveBootloader(void)
+{
     DBG1(0x01, 0, 0);
     cli();
     usbDeviceDisconnect();
diff --git a/firmware/spminterface.h b/firmware/spminterface.h
index 54a4649..2c91c50 100644
--- a/firmware/spminterface.h
+++ b/firmware/spminterface.h
@@ -294,7 +294,7 @@ void do_spm(const uint32_t flash_byteaddress, const uint8_t spmcrval, const uint
 
 //assume  SPMCR==0x37, SPMEN==0x0, RWWSRE=0x4, RWWSB=0x6
 #if HAVE_SPMINTEREFACE_MAGICVALUE
-const uint16_t bootloader__do_spm[23+8] BOOTLIBLINK = {
+const uint16_t bootloader__do_spm[23+8+4] BOOTLIBLINK = {
   (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 28) & 0xf))<<8) | (0x70 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 24) & 0xf))), // r23
   bootloader__do_spm_magic_exitstrategy(0xf4a1), // brne +20
   (((0x30 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 20) & 0xf))<<8) | (0x60 | ((HAVE_SPMINTEREFACE_MAGICVALUE >> 16) & 0xf))), // r22
@@ -308,7 +308,8 @@ const uint16_t bootloader__do_spm[15] BOOTLIBLINK = {
 #endif
   0x2dec, 0x2dfd, 0xb6b7, 0xfcb0, 0xcffd, 0xbf27, 0x95e8, 0xb6b7,
   0xfcb0, 0xcffd, 0xe121, 0xb6b7, 0xfcb6, 0xcff4, 0x9508,
-  0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
+  0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
+  0xffff, 0xffff, 0xffff, 0xffff
 };
 
 /*
-- 
1.7.8.6


From c3ec9c1ee747ad63fb0428ef7e5f6a73a7ba26b0 Mon Sep 17 00:00:00 2001
From: Stephan Baerwolf <stephan.baerwolf@tu-ilmenau.de>
Date: Fri, 23 Nov 2012 18:11:13 +0100
Subject: [PATCH 3/3] export also raw

Signed-off-by: Stephan Baerwolf <stephan.baerwolf@tu-ilmenau.de>
---
 updater/Makefile |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/updater/Makefile b/updater/Makefile
index 066c95c..0be5d89 100644
--- a/updater/Makefile
+++ b/updater/Makefile
@@ -56,6 +56,7 @@ updater.elf: updater.o usbasploader.o
 
 updater.hex: updater.elf
 	$(OBC) -j .text -j .data -O ihex updater.elf updater.hex
+	$(OBC) -j .text -j .data -O binary updater.elf updater.raw
 	$(ECHO) "."
 	$(ECHO) "."
 	$(SIZ) updater.elf
@@ -73,4 +74,4 @@ clean:
 	$(RM) usbasploader.raw
 	$(RM) updater.hex
 	$(RM) updater.elf
-	$(RM) usbasploader.raw
\ No newline at end of file
+	$(RM) usbasploader.raw
-- 
1.7.8.6

