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