fixed serial speed to 115200
[goodfet] / firmware / lib / atmega328.c
1 //! MSP430F1612/1611 clock and I/O definitions
2
3 #include "platform.h"
4
5 #include <avr/io.h>
6 #include <util/delay.h>
7
8 //! Receive a byte.
9 unsigned char serial0_rx(){
10   while( !(UCSR0A & (1 << RXC0)) );
11   return UDR0;
12 }
13
14 //! Receive a byte.
15 unsigned char serial1_rx(){
16   return 0;
17 }
18
19 //! Transmit a byte.
20 void serial0_tx(unsigned char x){
21   while (!(UCSR0A & (1<<UDRE0)) );
22   UDR0 = x;
23 }
24
25 //! Transmit a byte on the second UART.
26 void serial1_tx(unsigned char x){
27 }
28
29 //! Set the baud rate.
30 void setbaud0(unsigned char rate){
31   /* disable everything briefly */
32   UCSR0B = 0;
33
34   int32_t r;
35   switch(rate){
36   case 1://9600 baud
37     r = 9600;
38     break;
39   case 2://19200 baud
40     r = 19200;
41     break;
42   case 3://38400 baud
43     r = 38400;
44     break;
45   case 4://57600 baud
46     r = 57600;
47     break;
48
49   default:
50   case 5://115200 baud
51     r = 115200;
52     break;
53   }
54
55   /* enabling rx/tx must be done before frame/baud setup */
56   UCSR0B = ((1 << TXEN0) | (1 << RXEN0));
57
58   UCSR0A = (1 << U2X0);   /* double the baud rate */
59   UCSR0C = (3 << UCSZ00); /* 8N1 */
60
61   UBRR0L = (int8_t) (F_CPU/(r*8L)-1);
62   UBRR0H = (F_CPU/(r*8L)-1) >> 8;
63
64   return;
65   
66 }
67
68 //! Set the baud rate of the second uart.
69 void setbaud1(unsigned char rate){
70   //http://mspgcc.sourceforge.net/baudrate.html
71   switch(rate){
72   case 1://9600 baud
73     
74     break;
75   case 2://19200 baud
76     
77     break;
78   case 3://38400 baud
79     
80     break;
81   case 4://57600 baud
82     
83     break;
84   default:
85   case 5://115200 baud
86     
87     break;
88   }
89 }
90
91
92 void avr_init_uart0(){
93   PORTD = _BV(PD2);
94   setbaud0(0);
95   _delay_ms(500); //takes a bit to stabilize
96 }
97
98
99 void avr_init_uart1(){
100 }
101
102
103