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