MSP430X2 client connects but reads garbage from ram.
[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   return 00;
29 }
30
31 //! Transmit a byte.
32 void serial_tx(unsigned char x){
33   while ((IFG2 & UCA0TXIFG) == 0); //loop until buffer is free
34   UCA0TXBUF = x;        /* send the character */
35   while(!(IFG2 & UCA0TXIFG));
36 }
37 //! Transmit a byte.
38 void serial_tx_old(unsigned char x){
39   while ((IFG2 & UCA0TXIFG) == 0); //loop until buffer is free
40   UCA0TXBUF = x;        /* send the character */
41   while(!(IFG2 & UCA0TXIFG));
42 }
43
44 //! Transmit a byte on the second UART.
45 void serial1_tx(unsigned char x){
46
47 }
48
49 //! Set the baud rate.
50 void setbaud(unsigned char rate){
51   
52   //Table 15-4, page 481 of 2xx Family Guide
53   switch(rate){
54   case 1://9600 baud
55     UCA0BR1 = 0x06;
56     UCA0BR0 = 0x82;
57     break;
58   case 2://19200 baud
59     UCA0BR1 = 0x03;
60     UCA0BR0 = 0x41;
61     break;
62   case 3://38400 baud
63     UCA0BR1 = 0xa0;
64     UCA0BR0 = 0x01;
65     break;
66   case 4://57600 baud
67     UCA0BR1 = 0x1d;
68     UCA0BR0 = 0x01;
69     break;
70   default:
71   case 5://115200 baud
72     UCA0BR0 = 0x8a;
73     UCA0BR1 = 0x00;
74     break;
75   }
76 }
77
78 //! Set the baud rate of the second uart.
79 void setbaud1(unsigned char rate){
80   
81   //http://mspgcc.sourceforge.net/baudrate.html
82   switch(rate){
83   case 1://9600 baud
84     
85     break;
86   case 2://19200 baud
87     break;
88   case 3://38400 baud
89     
90     break;
91   case 4://57600 baud
92     
93     break;
94   default:
95   case 5://115200 baud
96     
97     break;
98   }
99 }
100
101 #define BAUD0EN 0x41
102 #define BAUD1EN 0x03
103
104 void msp430_init_uart(){
105
106   // Serial on P3.4, P3.5                                                                                                                                                 
107   P3SEL |= BIT4 + BIT5;
108   P3DIR |= BIT4;
109
110   //UCA0CTL1 |= UCSWRST;                    /* disable UART */                                                                                                            
111
112   UCA0CTL0 = 0x00;
113   //UCA0CTL0 |= UCMSB ;                                                                                                                                                   
114   UCA0CTL1 |= UCSSEL_2;                     // SMCLK                                                                                                                      
115
116   //UCA0BR0 = BAUD0EN;                        // 115200                                                                                                                     
117   //UCA0BR1 = BAUD1EN;
118   setbaud(5);//default baud, 115200
119
120   UCA0MCTL = 0;                             // Modulation UCBRSx = 5                                                                                                      
121   UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**                                                                                          
122
123
124   //Leave this commented!                                                                                                                                                 
125   //Interrupt is handled by target code, not by bootloader.                                                                                                               
126   //IE2 |= UCA0RXIE;         
127
128 }
129
130 //! Initialize the MSP430 clock.
131 void msp430_init_dco() {
132   //LED lights when init_dco begins,
133   //then dims when successful.
134   PLEDOUT|=PLEDPIN;
135   
136   if(CALBC1_16MHZ!=0xFF && CALDCO_16MHZ!=0xFF){
137     //Info is intact, use it.
138     BCSCTL1 = CALBC1_16MHZ;
139     DCOCTL = CALDCO_16MHZ;
140   }else{
141     //Info is missing, guess at a good value.
142     switch(*((int*)0xff0)){
143     default:
144     case 0x6ff2:        //f26f, the MSP430F2618
145       BCSCTL1 = 0x8f;   //CALBC1_16MHZ at 0x10f9
146       DCOCTL = 0x7f;    //CALDCO_16MHZ at 0x10f8
147       break;
148     }
149   }
150   
151   //Hopefully by here the clock is meaningful.
152   PLEDOUT&=~PLEDPIN;
153   return;
154 }
155