fixed serial speed to 115200
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 8 Aug 2013 13:38:56 +0000 (15:38 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 8 Aug 2013 13:38:56 +0000 (15:38 +0200)
client/GoodFET.py
firmware/lib/atmega328.c

index 4a7f644..f8af36e 100755 (executable)
@@ -177,7 +177,7 @@ class GoodFET:
         
         baud=115200;
         if(os.environ.get("platform")=='arduino' or os.environ.get("board")=='arduino'):
-            baud=38400; #Slower, for now.
+            baud=115200; # fixed for maximum speed
         self.serialport = serial.Serial(
             port,
             #9600,
index 896b36f..6127e83 100644 (file)
@@ -28,37 +28,39 @@ void serial1_tx(unsigned char x){
 
 //! Set the baud rate.
 void setbaud0(unsigned char rate){
-  //TODO support multiple rates.
-  #define SPEED 9600
-  
-  
+  /* disable everything briefly */
+  UCSR0B = 0;
+
+  int32_t r;
   switch(rate){
   case 1://9600 baud
-    
+    r = 9600;
     break;
   case 2://19200 baud
-    
+    r = 19200;
     break;
   case 3://38400 baud
-    
+    r = 38400;
     break;
   case 4://57600 baud
-    
+    r = 57600;
     break;
+
   default:
   case 5://115200 baud
-    
+    r = 115200;
     break;
   }
-  
-  //#define BAUD 115200L
-#define BAUD 9600L
-#include <util/setbaud.h>
-  UBRR0H = UBRRH_VALUE;
-  UBRR0L = UBRRL_VALUE;
-  
-  UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
-  UCSR0B = _BV(RXEN0) | _BV(TXEN0);
+
+  /* enabling rx/tx must be done before frame/baud setup */
+  UCSR0B = ((1 << TXEN0) | (1 << RXEN0));
+
+  UCSR0A = (1 << U2X0);   /* double the baud rate */
+  UCSR0C = (3 << UCSZ00); /* 8N1 */
+
+  UBRR0L = (int8_t) (F_CPU/(r*8L)-1);
+  UBRR0H = (F_CPU/(r*8L)-1) >> 8;
+
   return;
   
 }