02ee1c010588a3eb8930816d4e06e0416518aa75
[goodfet] / firmware / lib / msp430x2618.c
1 //! MSP430F2618 clock and I/O definitions
2
3 // Ought to be portable to other 2xx chips.
4 // 2274 looks particularly appealing.
5
6 #include "platform.h"
7
8 #include <signal.h>
9 #include <io.h>
10 #include <iomacros.h>
11
12
13 //! Receive a byte.
14 unsigned char serial_rx(){
15   char c;
16   
17   while(!(IFG2&UCA0RXIFG));//wait for a byte
18   c = UCA0RXBUF;
19   IFG2&=~UCA0RXIFG;
20   
21   //UCA0CTL1 &= ~UCA0RXSE;
22   return c;
23 }
24
25 //! Receive a byte.
26 unsigned char serial1_rx(){
27   //TODO
28 }
29
30
31 //! Transmit a byte.
32 void serial_tx(unsigned char x){
33   while ((IFG2 & UCA0TXIFG) == 0); //loop unti lbuffer is free
34   UCA0TXBUF = x;        /* send the character */
35   while(!(IFG2 & UCA0TXIFG));
36 }
37
38 //! Transmit a byte on the second UART.
39 void serial1_tx(unsigned char x){
40 }
41
42 //! Set the baud rate.
43 void setbaud(unsigned char rate){
44   
45   //http://mspgcc.sourceforge.net/baudrate.html
46   switch(rate){
47   case 1://9600 baud
48     
49     break;
50   case 2://19200 baud
51     
52     break;
53   case 3://38400 baud
54     
55     break;
56   case 4://57600 baud
57     
58     break;
59   default:
60   case 5://115200 baud
61     
62     break;
63   }
64 }
65
66 //! Set the baud rate of the second uart.
67 void setbaud1(unsigned char rate){
68   
69   //http://mspgcc.sourceforge.net/baudrate.html
70   switch(rate){
71   case 1://9600 baud
72     
73     break;
74   case 2://19200 baud
75     
76     break;
77   case 3://38400 baud
78     
79     break;
80   case 4://57600 baud
81     
82     break;
83   default:
84   case 5://115200 baud
85     
86     break;
87   }
88 }
89
90
91 //host.h says 0x2B for DCO=4.9MHZ
92 //Divide by four for accuracy.
93
94 //19200
95 #define BAUD0EN 0x41
96 #define BAUD1EN 0x03
97
98
99 void msp430_init_uart(){
100   // Serial on P3.4, P3.5
101   P3SEL |= BIT4 + BIT5;
102   P3DIR |= BIT4;
103   
104   //UCA0CTL1 |= UCSWRST;                    /* disable UART */
105
106   UCA0CTL0 = 0x00;
107   //UCA0CTL0 |= UCMSB ;
108   UCA0CTL1 |= UCSSEL_2;                     // SMCLK
109   UCA0BR0 = BAUD0EN;                        // 115200
110   UCA0BR1 = BAUD1EN;
111   UCA0MCTL = 0;                             // Modulation UCBRSx = 5
112   UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
113   
114 }
115
116
117 void msp430_init_dco() {
118   BCSCTL1 = CALBC1_16MHZ;
119   DCOCTL = CALDCO_16MHZ;
120   
121   return;
122 }
123