Removed Werror, so launchpad code compiles again.
[goodfet] / firmware / lib / msp430f2618.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 <msp430.h>
10 #include <sys/crtld.h>
11
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   char c;
28
29 #ifdef UC1IFG
30   while (!(UC1IFG&UCA1RXIFG));               // USCI_A1 TX buffer ready?
31   c = UCA1RXBUF;
32   UC1IFG&=~UCA1RXIFG;
33 #endif
34   
35   return c;
36 }
37
38 //! Transmit a byte.
39 void serial0_tx(unsigned char x){
40   while ((IFG2 & UCA0TXIFG) == 0); //loop until buffer is free
41   UCA0TXBUF = x;        /* send the character */
42   while(!(IFG2 & UCA0TXIFG));
43 }
44 //! Transmit a byte on the second UART.
45 void serial1_tx(unsigned char x){
46 #ifdef UC1IFG
47   while ((UC1IFG & UCA1TXIFG) == 0); //loop until buffer is free
48   UCA1TXBUF = x;        /* send the character */
49   while(!(UC1IFG & UCA1TXIFG));
50 #endif
51 }
52
53
54 //! Set the baud rate.
55 void setbaud0(unsigned char rate){
56
57   //Table 15-4, page 481 of 2xx Family Guide
58   switch(rate){
59   case 1://9600 baud
60     UCA0BR1 = 0x06;
61     UCA0BR0 = 0x82;
62     break;
63   case 2://19200 baud
64     UCA0BR1 = 0x03;
65     UCA0BR0 = 0x41;
66     break;
67   case 3://38400 baud
68     UCA0BR1 = 0xa0;
69     UCA0BR0 = 0x01;
70     break;
71   case 4://57600 baud
72     UCA0BR1 = 0x1d;
73     UCA0BR0 = 0x01;
74     break;
75   default:
76   case 5://115200 baud
77     UCA0BR0 = 0x8a;
78     UCA0BR1 = 0x00;
79     break;
80   }
81 }
82
83 //! Set the baud rate of the second uart.
84 void setbaud1(unsigned char rate){
85 #ifdef UC1IFG
86   //Table 15-4, page 481 of 2xx Family Guide
87   switch(rate){
88   case 1://9600 baud
89     UCA1BR1 = 0x06;
90     UCA1BR0 = 0x82;
91     break;
92   case 2://19200 baud
93     UCA1BR1 = 0x03;
94     UCA1BR0 = 0x41;
95     break;
96   case 3://38400 baud
97     UCA1BR1 = 0xa0;
98     UCA1BR0 = 0x01;
99     break;
100   case 4://57600 baud
101     UCA1BR1 = 0x1d;
102     UCA1BR0 = 0x01;
103     break;
104   default:
105   case 5://115200 baud
106     UCA1BR0 = 0x8a;
107     UCA1BR1 = 0x00;
108     break;
109   }
110 #endif
111 }
112
113 #define BAUD0EN 0x41
114 #define BAUD1EN 0x03
115
116 void msp430_init_uart(){
117
118   // Serial0 on P3.4, P3.5
119   P3SEL |= BIT4 + BIT5;
120   P3DIR |= BIT4;
121
122   //UCA0CTL1 |= UCSWRST;                    /* disable UART */
123
124   UCA0CTL0 = 0x00;
125   //UCA0CTL0 |= UCMSB ;
126
127   UCA0CTL1 |= UCSSEL_2;                     // SMCLK
128
129   //UCA0BR0 = BAUD0EN;                        // 115200
130   //UCA0BR1 = BAUD1EN;
131   setbaud(5);//default baud, 115200
132
133   UCA0MCTL = 0;                             // Modulation UCBRSx = 5
134   UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
135
136
137   //Leave this commented!
138   //Interrupt is handled by target code, not by bootloader.
139   //IE2 |= UCA0RXIE; //DO NOT UNCOMMENT
140   
141   
142 #ifdef UC1IFG
143   // Serial 1 on P3.6, 3.7
144   P3SEL    |=  0xC0;
145   //UCA1CTL0 = 0x00;
146   UCA1CTL1 |=  UCSSEL_2;                     // SMCLK
147   setbaud1(5); //115200
148   UCA1MCTL  =  0;
149   UCA1CTL1 &= ~UCSWRST;                      // Initialize USCI state machine
150 #endif
151 }
152
153
154 //This must be in .noinit.
155 __attribute__ ((section (".noinit"))) char dcochoice;
156
157 //! Initialization is correct.
158 void msp430_init_dco_done(){
159   //char *dcochoice=(char *) DCOCHOICEAT; //First word of RAM.
160   dcochoice--;
161 }
162
163 //! Initialize the MSP430 clock.
164 void msp430_init_dco() {
165   int i=1000;
166   //char *dcochoice=(char *) DCOCHOICEAT; //First word of RAM.
167   
168   #ifdef __MSP430_HAS_PORT8__
169   P8SEL = 0; // disable XT2 on P8.7/8
170   #endif
171   
172   //Set P2.6 mode for MSP430F2274
173   #ifndef __MSP430_HAS_PORT5__
174   P2SEL = 0; //disable XIN on 2274
175   #endif
176   
177   
178   #ifdef STATICDCO
179   BCSCTL1 = (STATICDCO>>8);
180   DCOCTL  = (STATICDCO&0xFF);
181   #else
182   if(CALBC1_16MHZ!=0xFF){
183     //Info is intact, use it.
184     BCSCTL1 = CALBC1_16MHZ;
185     DCOCTL = CALDCO_16MHZ;
186   }else{
187     /*
188       Info is missing, guess at a good value.
189
190       A list of correct calibrations in included as dco_calib.c,
191       generated by script.
192     */
193     DCOCTL = 0x00; //clear DCO
194
195     BCSCTL1 =  dco_calibrations[2*dcochoice+1];
196     DCOCTL  =  dco_calibrations[2*dcochoice];
197     dcochoice++;
198     dcochoice%=dco_calibrations_count;
199   }
200   #endif
201
202   //Minor delay.
203   while(i--);
204
205
206
207   return;
208 }
209