7823f06c8e8cb6961745f38e1c94cfa896f23fa0
[goodfet] / firmware / lib / msp430x2618.c
1 //! MSP430F2618 clock and I/O definitions
2
3 // Included by other 2xx ports, such as the 2274.
4
5 #include "platform.h"
6
7 #include <signal.h>
8 #include <io.h>
9 #include <iomacros.h>
10
11
12 //! Receive a byte.
13 unsigned char serial_rx(){
14   char c;
15   
16   while(!(IFG2&UCA0RXIFG));//wait for a byte
17   c = UCA0RXBUF;
18   IFG2&=~UCA0RXIFG;
19   
20   //UCA0CTL1 &= ~UCA0RXSE;
21   return c;
22 }
23
24 //! Receive a byte.
25 unsigned char serial1_rx(){
26   //TODO
27   return 00;
28 }
29
30 //! Transmit a byte.
31 void serial_tx(unsigned char x){
32   while ((IFG2 & UCA0TXIFG) == 0); //loop until buffer is free
33   UCA0TXBUF = x;        /* send the character */
34   while(!(IFG2 & UCA0TXIFG));
35 }
36 //! Transmit a byte.
37 void serial_tx_old(unsigned char x){
38   while ((IFG2 & UCA0TXIFG) == 0); //loop until buffer is free
39   UCA0TXBUF = x;        /* send the character */
40   while(!(IFG2 & UCA0TXIFG));
41 }
42
43 //! Transmit a byte on the second UART.
44 void serial1_tx(unsigned char x){
45
46 }
47
48 //! Set the baud rate.
49 void setbaud(unsigned char rate){
50   
51   //Table 15-4, page 481 of 2xx Family Guide
52   switch(rate){
53   case 1://9600 baud
54     UCA0BR1 = 0x06;
55     UCA0BR0 = 0x82;
56     break;
57   case 2://19200 baud
58     UCA0BR1 = 0x03;
59     UCA0BR0 = 0x41;
60     break;
61   case 3://38400 baud
62     UCA0BR1 = 0xa0;
63     UCA0BR0 = 0x01;
64     break;
65   case 4://57600 baud
66     UCA0BR1 = 0x1d;
67     UCA0BR0 = 0x01;
68     break;
69   default:
70   case 5://115200 baud
71     UCA0BR0 = 0x8a;
72     UCA0BR1 = 0x00;
73     break;
74   }
75 }
76
77 //! Set the baud rate of the second uart.
78 void setbaud1(unsigned char rate){
79   
80   //http://mspgcc.sourceforge.net/baudrate.html
81   switch(rate){
82   case 1://9600 baud
83     
84     break;
85   case 2://19200 baud
86     break;
87   case 3://38400 baud
88     
89     break;
90   case 4://57600 baud
91     
92     break;
93   default:
94   case 5://115200 baud
95     
96     break;
97   }
98 }
99
100 #define BAUD0EN 0x41
101 #define BAUD1EN 0x03
102
103 void msp430_init_uart(){
104
105   // Serial on P3.4, P3.5                                                                                                                                                 
106   P3SEL |= BIT4 + BIT5;
107   P3DIR |= BIT4;
108
109   //UCA0CTL1 |= UCSWRST;                    /* disable UART */                                                                                                            
110
111   UCA0CTL0 = 0x00;
112   //UCA0CTL0 |= UCMSB ;                                                                                                                                                   
113   UCA0CTL1 |= UCSSEL_2;                     // SMCLK                                                                                                                      
114
115   //UCA0BR0 = BAUD0EN;                        // 115200                                                                                                                     
116   //UCA0BR1 = BAUD1EN;
117   setbaud(5);//default baud, 115200
118
119   UCA0MCTL = 0;                             // Modulation UCBRSx = 5                                                                                                      
120   UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**                                                                                          
121
122
123   //Leave this commented!                                                                                                                                                 
124   //Interrupt is handled by target code, not by bootloader.                                                                                                               
125   //IE2 |= UCA0RXIE;         
126
127 }
128
129 //! Initialize the MSP430 clock.
130 void msp430_init_dco() {
131   if(CALBC1_16MHZ!=0xFF && CALDCO_16MHZ!=0xFF){
132     //Info is intact, use it.
133     BCSCTL1 = CALBC1_16MHZ;
134     DCOCTL = CALDCO_16MHZ;
135   }else{
136     //Info is missing, guess at a good value.
137     switch(*((int*)0xff0)){
138     default:
139     case 0x6ff2:        //f26f, the MSP430F2618
140       BCSCTL1 = 0x8f;   //CALBC1_16MHZ at 0x10f9
141       DCOCTL = 0x7f;    //CALDCO_16MHZ at 0x10f8
142       break;
143     }
144   }
145   
146   return;
147 }
148