Browse Source

Fix baudrate error calcs; get sign right, allow "U" trailer for F_CPU and
BAUD_RATE variables.
https://github.com/Optiboot/optiboot/issues/151

WestfW 9 years ago
parent
commit
87a0cdb5d2
2 changed files with 18 additions and 12 deletions
  1. 4 2
      optiboot/bootloaders/optiboot/baudcheck.c
  2. 14 10
      optiboot/bootloaders/optiboot/optiboot.c

+ 4 - 2
optiboot/bootloaders/optiboot/baudcheck.c

@@ -23,8 +23,10 @@
  */
 bpsx=BAUD_RATE
 bps=${bpsx/L/}
+bps=${bps/U/}
 fcpux=F_CPU
 fcpu=${fcpux/L/}
+fcpu=${fcpu/U/}
 
 // echo f_cpu = $fcpu, baud = $bps
 /*
@@ -39,8 +41,8 @@ BAUD_SETTING=$(( ( ($fcpu + $bps * 4) / (($bps * 8))) - 1 ))
  * the tenths part of the error separately.
  */
 BAUD_ACTUAL=$(( ($fcpu/(8 * (($BAUD_SETTING)+1))) ))
-BAUD_ERROR=$(( (( 100*($bps - $BAUD_ACTUAL) ) / $bps) ))
-ERR_TS=$(( ((( 1000*($bps - $BAUD_ACTUAL) ) / $bps) - $BAUD_ERROR * 10) ))
+BAUD_ERROR=$(( (( 100*($BAUD_ACTUAL - $bps) ) / $bps) ))
+ERR_TS=$(( ((( 1000*($BAUD_ACTUAL - $bps) ) / $bps) - $BAUD_ERROR * 10) ))
 ERR_TENTHS=$(( ERR_TS > 0 ? ERR_TS: -ERR_TS ))
 
 /*

+ 14 - 10
optiboot/bootloaders/optiboot/optiboot.c

@@ -290,16 +290,20 @@ optiboot_version = 256*(OPTIBOOT_MAJVER + OPTIBOOT_CUSTOMVER) + OPTIBOOT_MINVER;
 
 #define BAUD_SETTING (( (F_CPU + BAUD_RATE * 4L) / ((BAUD_RATE * 8L))) - 1 )
 #define BAUD_ACTUAL (F_CPU/(8 * ((BAUD_SETTING)+1)))
-#define BAUD_ERROR (( 100*(BAUD_RATE - BAUD_ACTUAL) ) / BAUD_RATE)
-
-#if BAUD_ERROR >= 5
-#error BAUD_RATE error greater than 5%
-#elif BAUD_ERROR <= -5
-#error BAUD_RATE error greater than -5%
-#elif BAUD_ERROR >= 2
-#warning BAUD_RATE error greater than 2%
-#elif BAUD_ERROR <= -2
-#warning BAUD_RATE error greater than -2%
+#if BAUD_ACTUAL <= BAUD_RATE
+  #define BAUD_ERROR (( 100*(BAUD_RATE - BAUD_ACTUAL) ) / BAUD_RATE)
+  #if BAUD_ERROR >= 5
+    #error BAUD_RATE error greater than -5%
+  #elif BAUD_ERROR >= 2
+    #warning BAUD_RATE error greater than -2%
+  #endif
+#else
+  #define BAUD_ERROR (( 100*(BAUD_ACTUAL - BAUD_RATE) ) / BAUD_RATE)
+  #if BAUD_ERROR >= 5
+    #error BAUD_RATE error greater than 5%
+  #elif BAUD_ERROR >= 2
+    #warning BAUD_RATE error greater than 2%
+  #endif
 #endif
 
 #if (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 > 250