587d6e2eb85998f914cc9152fa4678b4002e4ce1
[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 <signal.h>
8 #include <io.h>
9 #include <iomacros.h>
10
11 //! Receive a byte.
12 unsigned char serial_rx(){
13   char c;
14   
15   while(!(IFG2&UCA0RXIFG));//wait for a byte
16   c = UCA0RXBUF;
17   IFG2&=~UCA0RXIFG;
18   
19   //UCA0CTL1 &= ~UCA0RXSE;
20   return c;
21 }
22
23 //! Receive a byte.
24 unsigned char serial1_rx(){
25   //TODO
26   return 00;
27 }
28
29 //! Transmit a byte.
30 void serial_tx(unsigned char x){
31   while ((IFG2 & UCA0TXIFG) == 0); //loop until buffer is free
32   UCA0TXBUF = x;        /* send the character */
33   while(!(IFG2 & UCA0TXIFG));
34 }
35 //! Transmit a byte.
36 void serial_tx_old(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
42 //! Transmit a byte on the second UART.
43 void serial1_tx(unsigned char x){
44
45 }
46
47 //! Set the baud rate.
48 void setbaud(unsigned char rate){
49   
50   //Table 15-4, page 481 of 2xx Family Guide
51   switch(rate){
52   case 1://9600 baud
53     UCA0BR1 = 0x06;
54     UCA0BR0 = 0x82;
55     break;
56   case 2://19200 baud
57     UCA0BR1 = 0x03;
58     UCA0BR0 = 0x41;
59     break;
60   case 3://38400 baud
61     UCA0BR1 = 0xa0;
62     UCA0BR0 = 0x01;
63     break;
64   case 4://57600 baud
65     UCA0BR1 = 0x1d;
66     UCA0BR0 = 0x01;
67     break;
68   default:
69   case 5://115200 baud
70     UCA0BR0 = 0x8a;
71     UCA0BR1 = 0x00;
72     break;
73   }
74 }
75
76 //! Set the baud rate of the second uart.
77 void setbaud1(unsigned char rate){
78   
79 }
80
81 #define BAUD0EN 0x41
82 #define BAUD1EN 0x03
83
84 void msp430_init_uart(){
85   
86   // Serial on P3.4, P3.5
87   P3SEL |= BIT4 + BIT5;
88   P3DIR |= BIT4;
89   
90   //UCA0CTL1 |= UCSWRST;                    /* disable UART */
91   
92   UCA0CTL0 = 0x00;
93   //UCA0CTL0 |= UCMSB ;
94   
95   UCA0CTL1 |= UCSSEL_2;                     // SMCLK
96   
97   //UCA0BR0 = BAUD0EN;                        // 115200
98   //UCA0BR1 = BAUD1EN;
99   setbaud(5);//default baud, 115200
100   
101   UCA0MCTL = 0;                             // Modulation UCBRSx = 5
102   UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
103   
104   
105   //Leave this commented!
106   //Interrupt is handled by target code, not by bootloader.
107   //IE2 |= UCA0RXIE;
108 }
109
110 /* Info Flash Values
111 CALDCO_16MHZ 0xdc CALBC1_16MHZ 0x8e   2274-000.txt
112 CALDCO_16MHZ 0x74 CALBC1_16MHZ 0x8f   2618-000.txt
113 CALDCO_16MHZ 0x6c CALBC1_16MHZ 0x8f   2618-001.txt
114 CALDCO_16MHZ 0x97 CALBC1_16MHZ 0x8f   2618-002.txt
115 CALDCO_16MHZ 0x6c CALBC1_16MHZ 0x8f   2618-003.txt
116 CALDCO_16MHZ 0x95 CALBC1_16MHZ 0x8f   2618-004.txt
117 CALDCO_16MHZ 0xcc CALBC1_16MHZ 0x8e   2618-005.txt
118 CALDCO_16MHZ 0x87 CALBC1_16MHZ 0x8f   2618-006.txt
119 CALDCO_16MHZ 0x96 CALBC1_16MHZ 0x8f   2619-001.txt
120 */
121
122 //! Initialization is correct.
123 void msp430_init_dco_done(){
124   char *choice=(char *) 0x200; //First word of RAM.
125   choice[0]--;
126 }
127
128 //! Initialize the MSP430 clock.
129 void msp430_init_dco() {
130   int i=1000;
131   char *choice=(char *) 0x200; //First word of RAM.
132   #ifdef __MSP430_HAS_PORT8__
133   P8SEL = 0; // disable XT2 on P8.7/8
134   #endif
135   
136   if(CALBC1_16MHZ!=0xFF){
137     //Info is intact, use it.
138     BCSCTL1 = CALBC1_16MHZ;
139     DCOCTL = CALDCO_16MHZ;
140   }else{
141     /*
142       Info is missing, guess at a good value.
143       
144       For now, the choice doesn't jump backward after a successful
145       connection.  For that reason, keep this list as small as possible.
146       Future revisions will subtract one from choice[0] after a successful
147       connection, keeping choice[1] as the target.
148     */
149       
150     #define CHOICES 4
151     DCOCTL = 0x00; //clear DCO
152     switch(choice[0]++%CHOICES){
153     default:
154     case 0:
155       BCSCTL1 = 0x8f;   //CALBC1_16MHZ at 0x10f9
156       DCOCTL = 0x83;    //CALDCO_16MHZ at 0x10f8
157       break;
158     case 1:
159       BCSCTL1 = 0x8f;
160       DCOCTL = 0x95;
161       break;
162     case 2:
163       BCSCTL1 = 0x8f;
164       DCOCTL = 0x6c;
165       break;
166     case 3:
167       BCSCTL1 = 0x8e;
168       DCOCTL = 0xdc;
169       break;
170     }
171   }
172   
173   //Minor delay.
174   while(i--);
175   
176   return;
177 }
178