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