new feature "HAVE_UNPRECISEWAIT" for save some flash
authorStephan Baerwolf <stephan.baerwolf@tu-ilmenau.de>
Tue, 13 Nov 2012 17:25:48 +0000 (18:25 +0100)
committerStephan Baerwolf <stephan.baerwolf@tu-ilmenau.de>
Wed, 14 Nov 2012 20:54:33 +0000 (20:54 +0000)
Signed-off-by: Stephan Baerwolf <stephan.baerwolf@tu-ilmenau.de>
firmware/bootloaderconfig.h
firmware/main.c

index a54adab..c9676a6 100644 (file)
@@ -179,6 +179,17 @@ these macros are defined, the boot loader usees them.
  * If the used MCU is fused not to enable watchdog after reset (WDTON is 1 - safty level 1)
  * then "NEED_WATCHDOG" may be deactivated in order to save some memory.
  */
+#ifndef CONFIG_NO__PRECISESLEEP
+  #define HAVE_UNPRECISEWAIT   0
+#else
+  #define HAVE_UNPRECISEWAIT   1
+#endif
+/* This macro enables hand-optimized assembler code
+ * instead to use _sleep_ms for delaying USB enumeration.
+ * Because normally these timings do not need to be exact,
+ * the optimized assembler code does not need to be precise.
+ * Therefore it is very small, which saves some PROGMEM bytes!
+ */
 //#define SIGNATURE_BYTES             0x1e, 0x93, 0x07, 0     /* ATMega8 */
 /* This macro defines the signature bytes returned by the emulated USBasp to
  * the programmer software. They should match the actual device at least in
index fda58a2..8012e20 100644 (file)
@@ -386,10 +386,33 @@ uchar   i;
 
 static void initForUsbConnectivity(void)
 {
+#if HAVE_UNPRECISEWAIT
+    /* (0.25s*F_CPU)/(4 cycles per loop) ~ (65536*waitloopcnt)
+     * F_CPU/(16*65536) ~ waitloopcnt
+     * F_CPU / 1048576 ~ waitloopcnt
+     */
+    uint8_t waitloopcnt = 1 + (F_CPU/1048576);
+#endif
     usbInit();
     /* enforce USB re-enumerate: */
     usbDeviceDisconnect();  /* do this while interrupts are disabled */
+#if HAVE_UNPRECISEWAIT
+    asm volatile (
+      /*we really don't care what value Z has...
+       * ...if we loop 65536/F_CPU more or less...
+       * ...unimportant - just save some opcodes
+       */
+"initForUsbConnectivity_sleeploop:                     \n\t"
+      "sbiw    r30,    1                               \n\t"
+      "sbci    %0,     0                               \n\t"
+      "brne    initForUsbConnectivity_sleeploop        \n\t"
+      : "+d" (waitloopcnt)
+      :
+      : "r30","r31"
+    );
+#else
     _delay_ms(260);         /* fake USB disconnect for > 250 ms */
+#endif
     usbDeviceConnect();
     sei();
 }