Browse Source

Finish SINGLESPEED option support

WestfW 6 years ago
parent
commit
b30d32e152

+ 8 - 0
optiboot/bootloaders/optiboot/baudcheck.c

@@ -32,7 +32,11 @@ fcpu=${fcpu/U/}
 /*
  * Compute the divisor
  */
+#ifdef SINGLESPEED
+BAUD_SETTING=$(( ( ($fcpu + $bps * 8) / (($bps * 16))) - 1 ))
+#else
 BAUD_SETTING=$(( ( ($fcpu + $bps * 4) / (($bps * 8))) - 1 ))
+#endif
 // echo baud setting = $BAUD_SETTING
 
 /*
@@ -40,7 +44,11 @@ BAUD_SETTING=$(( ( ($fcpu + $bps * 4) / (($bps * 8))) - 1 ))
  * And the error.  Since we're all integers, we have to calculate
  * the tenths part of the error separately.
  */
+#ifdef SINGLESPEED
+BAUD_ACTUAL=$(( ($fcpu/(16 * (($BAUD_SETTING)+1))) ))
+#else
 BAUD_ACTUAL=$(( ($fcpu/(8 * (($BAUD_SETTING)+1))) ))
+#endif
 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 - 3
optiboot/bootloaders/optiboot/optiboot.c

@@ -326,8 +326,15 @@ typedef union {
 #define UART 0
 #endif
 
+#ifdef SINGLESPEED
+/* Single speed option */
+#define BAUD_SETTING (( (F_CPU + BAUD_RATE * 8L) / ((BAUD_RATE * 16L))) - 1 )
+#define BAUD_ACTUAL (F_CPU/(16 * ((BAUD_SETTING)+1)))
+#else
+/* Normal U2X usage */
 #define BAUD_SETTING (( (F_CPU + BAUD_RATE * 4L) / ((BAUD_RATE * 8L))) - 1 )
 #define BAUD_ACTUAL (F_CPU/(8 * ((BAUD_SETTING)+1)))
+#endif
 #if BAUD_ACTUAL <= BAUD_RATE
   #define BAUD_ERROR (( 100*(BAUD_RATE - BAUD_ACTUAL) ) / BAUD_RATE)
   #if BAUD_ERROR >= 5
@@ -344,14 +351,14 @@ typedef union {
   #endif
 #endif
 
-#if (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 > 250
+#if BAUD_SETTING > 250
 #error Unachievable baud rate (too slow) BAUD_RATE 
 #endif // baud rate slow check
-#if (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 < 3
+#if (BAUD_SETTING - 1) < 3
 #if BAUD_ERROR != 0 // permit high bitrates (ie 1Mbps@16MHz) if error is zero
 #error Unachievable baud rate (too fast) BAUD_RATE 
 #endif
-#endif // baud rate fastn check
+#endif // baud rate fast check
 
 /* Watchdog settings */
 #define WATCHDOG_OFF    (0)
@@ -559,12 +566,16 @@ int main(void) {
 
 #ifndef SOFT_UART
 #if defined(__AVR_ATmega8__) || defined (__AVR_ATmega32__) || defined (__AVR_ATmega16__)
+#ifndef SINGLESPEED
   UCSRA = _BV(U2X); //Double speed mode USART
+#endif
   UCSRB = _BV(RXEN) | _BV(TXEN);  // enable Rx & Tx
   UCSRC = _BV(URSEL) | _BV(UCSZ1) | _BV(UCSZ0);  // config USART; 8N1
   UBRRL = (uint8_t)( (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 );
 #else
+#ifndef SINGLESPEED
   UART_SRA = _BV(U2X0); //Double speed mode USART0
+#endif
   UART_SRB = _BV(RXEN0) | _BV(TXEN0);
   UART_SRC = _BV(UCSZ00) | _BV(UCSZ01);
   UART_SRL = (uint8_t)( (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 );