Cleaning MSP430X2 client.
[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 "dco_calib.h"
8
9 #include <signal.h>
10 #include <io.h>
11 #include <iomacros.h>
12
13 //! Receive a byte.
14 unsigned char serial0_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 serial0_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 setbaud0(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 }
82
83 #define BAUD0EN 0x41
84 #define BAUD1EN 0x03
85
86 void msp430_init_uart(){
87   
88   // Serial on P3.4, P3.5
89   P3SEL |= BIT4 + BIT5;
90   P3DIR |= BIT4;
91   
92   //UCA0CTL1 |= UCSWRST;                    /* disable UART */
93   
94   UCA0CTL0 = 0x00;
95   //UCA0CTL0 |= UCMSB ;
96   
97   UCA0CTL1 |= UCSSEL_2;                     // SMCLK
98   
99   //UCA0BR0 = BAUD0EN;                        // 115200
100   //UCA0BR1 = BAUD1EN;
101   setbaud(5);//default baud, 115200
102   
103   UCA0MCTL = 0;                             // Modulation UCBRSx = 5
104   UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
105   
106   
107   //Leave this commented!
108   //Interrupt is handled by target code, not by bootloader.
109   //IE2 |= UCA0RXIE;
110 }
111
112
113 //! Initialization is correct.
114 void msp430_init_dco_done(){
115   char *choice=(char *) 0x200; //First word of RAM.
116   choice[0]--;
117 }
118
119 //! Initialize the MSP430 clock.
120 void msp430_init_dco() {
121   int i=1000;
122   char *choice=(char *) 0x200; //First word of RAM.
123   
124   #ifdef __MSP430_HAS_PORT8__
125   P8SEL = 0; // disable XT2 on P8.7/8
126   #endif
127   
128   //Set P2.6 mode for MSP430F2274
129   #ifndef __MSP430_HAS_PORT5__
130   P2SEL = 0; //disable XIN on 2274
131   #endif
132   
133   
134   if(CALBC1_16MHZ!=0xFF){
135     //Info is intact, use it.
136     BCSCTL1 = CALBC1_16MHZ;
137     DCOCTL = CALDCO_16MHZ;
138   }else{
139     /*
140       Info is missing, guess at a good value.
141       
142       A list of correct calibrations in included as dco_calib.c,
143       generated by script.
144     */
145     DCOCTL = 0x00; //clear DCO
146     
147     BCSCTL1 =  dco_calibrations[2*choice[0]+1];
148     DCOCTL  =  dco_calibrations[2*choice[0]];
149     choice[0]++;
150     choice[0]%=dco_calibrations_count;
151   }
152   
153   //Minor delay.
154   while(i--);
155   
156   
157   
158   return;
159 }
160