408e3eea76cc5edf0079462322e25962c0dc95c3
[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 #ifdef __MSPGCC__
9 #include <msp430.h>
10 #else
11 #include <signal.h>
12 #include <io.h>
13 #include <iomacros.h>
14 #endif
15
16
17 //! Receive a byte.
18 unsigned char serial0_rx(){
19   char c;
20
21   while(!(IFG2&UCA0RXIFG));//wait for a byte
22   c = UCA0RXBUF;
23   IFG2&=~UCA0RXIFG;
24
25   //UCA0CTL1 &= ~UCA0RXSE;
26   return c;
27 }
28
29 //! Receive a byte.
30 unsigned char serial1_rx(){
31   //TODO
32   return 00;
33 }
34
35 //! Transmit a byte.
36 void serial0_tx(unsigned char x){
37   while ((IFG2 & UCA0TXIFG) == 0); //loop until buffer is free
38   UCA0TXBUF = x;        /* send the character */
39   while(!(IFG2 & UCA0TXIFG));
40 }
41 //! Transmit a byte.
42 void serial_tx_old(unsigned char x){
43   while ((IFG2 & UCA0TXIFG) == 0); //loop until buffer is free
44   UCA0TXBUF = x;        /* send the character */
45   while(!(IFG2 & UCA0TXIFG));
46 }
47
48 //! Transmit a byte on the second UART.
49 void serial1_tx(unsigned char x){
50
51 }
52
53 //! Set the baud rate.
54 void setbaud0(unsigned char rate){
55
56   //Table 15-4, page 481 of 2xx Family Guide
57   switch(rate){
58   case 1://9600 baud
59     UCA0BR1 = 0x06;
60     UCA0BR0 = 0x82;
61     break;
62   case 2://19200 baud
63     UCA0BR1 = 0x03;
64     UCA0BR0 = 0x41;
65     break;
66   case 3://38400 baud
67     UCA0BR1 = 0xa0;
68     UCA0BR0 = 0x01;
69     break;
70   case 4://57600 baud
71     UCA0BR1 = 0x1d;
72     UCA0BR0 = 0x01;
73     break;
74   default:
75   case 5://115200 baud
76     UCA0BR0 = 0x8a;
77     UCA0BR1 = 0x00;
78     break;
79   }
80 }
81
82 //! Set the baud rate of the second uart.
83 void setbaud1(unsigned char rate){
84
85 }
86
87 #define BAUD0EN 0x41
88 #define BAUD1EN 0x03
89
90 void msp430_init_uart(){
91
92   // Serial on P3.4, P3.5
93   P3SEL |= BIT4 + BIT5;
94   P3DIR |= BIT4;
95
96   //UCA0CTL1 |= UCSWRST;                    /* disable UART */
97
98   UCA0CTL0 = 0x00;
99   //UCA0CTL0 |= UCMSB ;
100
101   UCA0CTL1 |= UCSSEL_2;                     // SMCLK
102
103   //UCA0BR0 = BAUD0EN;                        // 115200
104   //UCA0BR1 = BAUD1EN;
105   setbaud(5);//default baud, 115200
106
107   UCA0MCTL = 0;                             // Modulation UCBRSx = 5
108   UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
109
110
111   //Leave this commented!
112   //Interrupt is handled by target code, not by bootloader.
113   //IE2 |= UCA0RXIE;
114 }
115
116
117 //! Initialization is correct.
118 void msp430_init_dco_done(){
119   char *choice=(char *) 0x200; //First word of RAM.
120   choice[0]--;
121 }
122
123 //! Initialize the MSP430 clock.
124 void msp430_init_dco() {
125   int i=1000;
126   char *choice=(char *) 0x200; //First word of RAM.
127
128   #ifdef __MSP430_HAS_PORT8__
129   P8SEL = 0; // disable XT2 on P8.7/8
130   #endif
131
132   //Set P2.6 mode for MSP430F2274
133   #ifndef __MSP430_HAS_PORT5__
134   P2SEL = 0; //disable XIN on 2274
135   #endif
136
137
138   #ifdef STATICDCO
139   BCSCTL1 = (STATICDCO>>8);
140   DCOCTL  = (STATICDCO&0xFF);
141   #else
142   if(CALBC1_16MHZ!=0xFF){
143     //Info is intact, use it.
144     BCSCTL1 = CALBC1_16MHZ;
145     DCOCTL = CALDCO_16MHZ;
146   }else{
147     /*
148       Info is missing, guess at a good value.
149
150       A list of correct calibrations in included as dco_calib.c,
151       generated by script.
152     */
153     DCOCTL = 0x00; //clear DCO
154
155     BCSCTL1 =  dco_calibrations[2*choice[0]+1];
156     DCOCTL  =  dco_calibrations[2*choice[0]];
157     choice[0]++;
158     choice[0]%=dco_calibrations_count;
159   }
160   #endif
161
162   //Minor delay.
163   while(i--);
164
165
166
167   return;
168 }
169