Hello world on the Apimote!
[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   while (!(UC1IFG&UCA1RXIFG));               // USCI_A1 TX buffer ready?
30   c = UCA1RXBUF;
31   UC1IFG&=~UCA1RXIFG;
32
33   return c;
34 }
35
36 //! Transmit a byte.
37 void serial0_tx(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 //! Transmit a byte on the second UART.
43 void serial1_tx(unsigned char x){
44   while ((UC1IFG & UCA1TXIFG) == 0); //loop until buffer is free
45   UCA1TXBUF = x;        /* send the character */
46   while(!(UC1IFG & UCA1TXIFG));
47 }
48
49 //! Transmit a byte.
50 void serial_tx_old(unsigned char x){
51   while ((IFG2 & UCA0TXIFG) == 0); //loop until buffer is free
52   UCA0TXBUF = x;        /* send the character */
53   while(!(IFG2 & UCA0TXIFG));
54 }
55
56
57 //! Set the baud rate.
58 void setbaud0(unsigned char rate){
59
60   //Table 15-4, page 481 of 2xx Family Guide
61   switch(rate){
62   case 1://9600 baud
63     UCA0BR1 = 0x06;
64     UCA0BR0 = 0x82;
65     break;
66   case 2://19200 baud
67     UCA0BR1 = 0x03;
68     UCA0BR0 = 0x41;
69     break;
70   case 3://38400 baud
71     UCA0BR1 = 0xa0;
72     UCA0BR0 = 0x01;
73     break;
74   case 4://57600 baud
75     UCA0BR1 = 0x1d;
76     UCA0BR0 = 0x01;
77     break;
78   default:
79   case 5://115200 baud
80     UCA0BR0 = 0x8a;
81     UCA0BR1 = 0x00;
82     break;
83   }
84 }
85
86 //! Set the baud rate of the second uart.
87 void setbaud1(unsigned char rate){
88   //Table 15-4, page 481 of 2xx Family Guide
89   switch(rate){
90   case 1://9600 baud
91     UCA1BR1 = 0x06;
92     UCA1BR0 = 0x82;
93     break;
94   case 2://19200 baud
95     UCA1BR1 = 0x03;
96     UCA1BR0 = 0x41;
97     break;
98   case 3://38400 baud
99     UCA1BR1 = 0xa0;
100     UCA1BR0 = 0x01;
101     break;
102   case 4://57600 baud
103     UCA1BR1 = 0x1d;
104     UCA1BR0 = 0x01;
105     break;
106   default:
107   case 5://115200 baud
108     UCA1BR0 = 0x8a;
109     UCA1BR1 = 0x00;
110     break;
111   }
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   // Serial 1 on P3.6, 3.7
143   P3SEL    |=  0xC0;
144   //UCA1CTL0 = 0x00;
145   UCA1CTL1 |=  UCSSEL_2;                     // SMCLK
146   setbaud1(5); //115200
147   UCA1MCTL  =  0;
148   UCA1CTL1 &= ~UCSWRST;                      // Initialize USCI state machine
149   
150 }
151
152
153 //This must be in .noinit.
154 __attribute__ ((section (".noinit"))) char dcochoice;
155
156 //! Initialization is correct.
157 void msp430_init_dco_done(){
158   //char *dcochoice=(char *) DCOCHOICEAT; //First word of RAM.
159   dcochoice--;
160 }
161
162 //! Initialize the MSP430 clock.
163 void msp430_init_dco() {
164   int i=1000;
165   //char *dcochoice=(char *) DCOCHOICEAT; //First word of RAM.
166   
167   #ifdef __MSP430_HAS_PORT8__
168   P8SEL = 0; // disable XT2 on P8.7/8
169   #endif
170   
171   //Set P2.6 mode for MSP430F2274
172   #ifndef __MSP430_HAS_PORT5__
173   P2SEL = 0; //disable XIN on 2274
174   #endif
175   
176   
177   #ifdef STATICDCO
178   BCSCTL1 = (STATICDCO>>8);
179   DCOCTL  = (STATICDCO&0xFF);
180   #else
181   if(CALBC1_16MHZ!=0xFF){
182     //Info is intact, use it.
183     BCSCTL1 = CALBC1_16MHZ;
184     DCOCTL = CALDCO_16MHZ;
185   }else{
186     /*
187       Info is missing, guess at a good value.
188
189       A list of correct calibrations in included as dco_calib.c,
190       generated by script.
191     */
192     DCOCTL = 0x00; //clear DCO
193
194     BCSCTL1 =  dco_calibrations[2*dcochoice+1];
195     DCOCTL  =  dco_calibrations[2*dcochoice];
196     dcochoice++;
197     dcochoice%=dco_calibrations_count;
198   }
199   #endif
200
201   //Minor delay.
202   while(i--);
203
204
205
206   return;
207 }
208