USBaspLoader 18-byte code reduction

General discussions about V-USB, our firmware-only implementation of a low speed USB device on Atmel's AVR microcontrollers
Post Reply
blargg
Rank 3
Rank 3
Posts: 102
Joined: Thu Nov 14, 2013 10:01 pm

USBaspLoader 18-byte code reduction

Post by blargg » Sat Nov 16, 2013 4:27 am

I shaved 18 bytes off USBaspLoader without removing functionality (at least for avr-gcc 4.5.3). This is small but given that out of the box it's around 2028 bytes, very close to the 2048 limit, this might be critical to someone.

One change involved eliminating the wdt_reset() call, which seems fine since 1) the wdt is disabled at the beginning of main(), b) it's not re-enabled anywhere I can see in this code or the usbdrv/ code, and c) it's not reset anywhere else in the code, so if say a fuse bit had it locked on all the time, the code wouldn't have worked to begin with.

(I'm sending this to the maintainers too)

Code: Select all

--- USBaspLoader.2012-12-08/firmware/main.c
+++ USBaspLoader.2012-12-08-opt/firmware/main.c
@@ -265,7 +265,7 @@
     if(len > bytesRemaining)
         len = bytesRemaining;
     bytesRemaining -= len;
-    for(i = 0; i < len; i++){
+    for(i = len; i > 0; i--){
         if(currentRequest >= USBASP_FUNC_READEEPROM){
             *data = eeprom_read_byte((void *)currentAddress.w[0]);
         }else{
@@ -281,20 +281,15 @@
 
 static void initForUsbConnectivity(void)
 {
-uchar   i = 0;
-
     usbInit();
     /* enforce USB re-enumerate: */
     usbDeviceDisconnect();  /* do this while interrupts are disabled */
-    while(--i){         /* fake USB disconnect for > 250 ms */
-        wdt_reset();
-        _delay_ms(1);
-    }
+    _delay_ms(251); /* fake USB disconnect for > 250 ms */
     usbDeviceConnect();
     sei();
 }
 
-int __attribute__((noreturn)) main(void)
+int __attribute__((noreturn,OS_main)) main(void)
 {
     /* initialize  */
     wdt_disable();      /* main app may have enabled watchdog */
@@ -306,14 +301,13 @@
     GICR = (1 << IVSEL); /* move interrupts to boot flash section */
 #endif
     if(bootLoaderCondition()){
-        uchar i = 0, j = 0;
+        unsigned i = 0;
         initForUsbConnectivity();
         do{
             usbPoll();
 #if BOOTLOADER_CAN_EXIT
             if(requestBootLoaderExit){
                 if(--i == 0){
-                    if(--j == 0)
                         break;
                 }
             }

matrixstorm
Posts: 16
Joined: Tue Sep 18, 2012 2:30 pm

Re: USBaspLoader 18-byte code reduction

Post by matrixstorm » Sat Nov 16, 2013 11:38 am

Hi blargg.

Perhaps you might want to try the revised USBaspLoader (https://github.com/baerwolf/USBaspLoader) instead.

With this USBaspLoader you have many new features (with individual enable/disable switches) and code-optimizaions.

There are also many bugs fixed.

If you want to try the most recent version of revised USBaspLoader please use the testing-branch (https://github.com/baerwolf/USBaspLoader/tree/testing).

Best regards,
matrixstorm

Post Reply