Added goodthopter11 to config.mk.
[goodfet] / firmware / lib / msp430f1611.c
1 //! MSP430F1612/1611 clock and I/O definitions
2
3 #include "platform.h"
4
5 #ifdef __MSPGCC__
6 #include <msp430.h>
7 #else
8 #include <signal.h>
9 #include <msp430.h>
10 #include <iomacros.h>
11 #endif
12
13 //! Receive a byte.
14 unsigned char serial0_rx(){
15   char c;
16
17   while(!(IFG1&URXIFG0));//wait for a byte
18   c = RXBUF0;
19   IFG1&=~URXIFG0;
20   U0TCTL &= ~URXSE;
21
22   return c;
23 }
24
25 //! Receive a byte.
26 unsigned char serial1_rx(){
27   char c;
28
29   while(!(IFG2&URXIFG1));//wait for a byte
30   c = RXBUF1;
31   IFG2&=~URXIFG1;
32   U1TCTL &= ~URXSE;
33
34   return c;
35 }
36
37 //! Transmit a byte.
38 void serial0_tx(unsigned char x){
39   while ((IFG1 & UTXIFG0) == 0); //loop until buffer is free
40   TXBUF0 = x;
41 }
42
43 //! Transmit a byte on the second UART.
44 void serial1_tx(unsigned char x){
45   while ((IFG2 & UTXIFG1) == 0); //loop until buffer is free
46   TXBUF1 = x;
47 }
48
49 /** Later, add support for the EZ430/FETUIF with 12MHz crystal
50     UBR00=0xE2; UBR10=0x04; UMCTL0=0x00; // uart0 12000000Hz 9600bps
51     UBR00=0x71; UBR10=0x02; UMCTL0=0x00; // uart0 12000000Hz 19200bps
52     UBR00=0x38; UBR10=0x01; UMCTL0=0x55; // uart0 12000000Hz 38400bps
53     UBR00=0xD0; UBR10=0x00; UMCTL0=0x4A; // uart0 12000000Hz 57581bps
54     UBR00=0x68; UBR10=0x00; UMCTL0=0x04; // uart0 12000000Hz 115273bps
55  */
56
57 //! Set the baud rate.
58 void setbaud0(unsigned char rate){
59
60   //http://mspgcc.sourceforge.net/baudrate.html
61   switch(rate){
62   case 1://9600 baud
63     UBR00=0x7F; UBR10=0x01; UMCTL0=0x5B; /* uart0 3683400Hz 9599bps */
64     break;
65   case 2://19200 baud
66     UBR00=0xBF; UBR10=0x00; UMCTL0=0xF7; /* uart0 3683400Hz 19194bps */
67     break;
68   case 3://38400 baud
69     UBR00=0x5F; UBR10=0x00; UMCTL0=0xBF; /* uart0 3683400Hz 38408bps */
70     break;
71   case 4://57600 baud
72     UBR00=0x40; UBR10=0x00; UMCTL0=0x00; /* uart0 3683400Hz 57553bps */
73     break;
74   default:
75   case 5://115200 baud
76     UBR00=0x20; UBR10=0x00; UMCTL0=0x00; /* uart0 3683400Hz 115106bps */
77     break;
78   }
79 }
80
81 //! Set the baud rate of the second uart.
82 void setbaud1(unsigned char rate){
83   //http://mspgcc.sourceforge.net/baudrate.html
84   switch(rate){
85   case 1://9600 baud
86     UBR01=0x7F; UBR11=0x01; UMCTL1=0x5B; /* uart0 3683400Hz 9599bps */
87     break;
88   case 2://19200 baud
89     UBR01=0xBF; UBR11=0x00; UMCTL1=0xF7; /* uart0 3683400Hz 19194bps */
90     break;
91   case 3://38400 baud
92     UBR01=0x5F; UBR11=0x00; UMCTL1=0xBF; /* uart0 3683400Hz 38408bps */
93     break;
94   case 4://57600 baud
95     UBR01=0x40; UBR11=0x00; UMCTL1=0x00; /* uart0 3683400Hz 57553bps */
96     break;
97   default:
98   case 5://115200 baud
99     UBR01=0x20; UBR11=0x00; UMCTL1=0x00; /* uart0 3683400Hz 115106bps */
100     break;
101   }
102 }
103
104
105 void msp430_init_uart0(){
106   /* RS232 */
107
108   P3SEL |= BIT4|BIT5;                        // P3.4,5 = USART0 TXD/RXD
109   P3DIR |= BIT4;
110
111   UCTL0 = SWRST | CHAR;                 /* 8-bit character, UART mode */
112   UTCTL0 = SSEL1;                       /* UCLK = MCLK */
113
114   setbaud0(0);
115
116   ME1 &= ~USPIE0;                       /* USART1 SPI module disable */
117   ME1 |= (UTXE0 | URXE0);               /* Enable USART1 TXD/RXD */
118
119   UCTL0 &= ~SWRST;
120
121   /* XXX Clear pending interrupts before enable!!! */
122   U0TCTL |= URXSE;
123
124
125   //IE1 |= URXIE1;                        /* Enable USART1 RX interrupt  */
126 }
127
128
129 void msp430_init_uart1(){
130
131   /* RS232 */
132   P3DIR &= ~0x80;                       /* Select P37 for input (UART1RX) */
133   P3DIR |= 0x40;                        /* Select P36 for output (UART1TX) */
134   P3SEL |= 0xC0;                        /* Select P36,P37 for UART1{TX,RX} */
135
136   UCTL1 = SWRST | CHAR;                 /* 8-bit character, UART mode */
137   UTCTL1 = SSEL1;                       /* UCLK = MCLK */
138
139   setbaud1(0);
140
141   ME2 &= ~USPIE1;                       /* USART1 SPI module disable */
142   ME2 |= (UTXE1 | URXE1);               /* Enable USART1 TXD/RXD */
143
144   UCTL1 &= ~SWRST;
145
146   /* XXX Clear pending interrupts before enable!!! */
147   U1TCTL |= URXSE;
148
149   //IE2 |= URXIE1;                        /* Enable USART1 RX interrupt  */
150 }
151
152
153 /** For EZ430/FETUIF
154  void msp430_init_dco() {
155   WDTCTL = WDTPW + WDTHOLD; //stop WDT
156
157   BCSCTL1 = 0;
158
159   do {
160     int i;
161     IFG1 &= ~OFIFG;
162     for (i=0; i<1000; i++);
163
164   } while (IFG1 & OFIFG);
165
166   BCSCTL2 = SELM1 | DIVM1 | SELS;
167
168 }
169  */
170
171
172 //! Initialization is correct.
173 void msp430_init_dco_done(){
174   //Nothing to do for the 1612.
175 }
176
177
178 void msp430_init_dco() {
179 /* This code taken from the FU Berlin sources and reformatted. */
180   //
181
182 //Works well.
183 //#define MSP430_CPU_SPEED 2457600UL
184
185 //Too fast for internal resistor.
186 //#define MSP430_CPU_SPEED 4915200UL
187
188 //Max speed.
189 //#define MSP430_CPU_SPEED 4500000UL
190
191 //baud rate speed
192 #define MSP430_CPU_SPEED 3683400UL
193 #define DELTA    ((MSP430_CPU_SPEED) / (32768 / 8))
194   unsigned int compare, oldcapture = 0;
195   unsigned int i;
196
197   WDTCTL = WDTPW + WDTHOLD; //stop WDT
198
199
200   DCOCTL=0xF0;
201   //a4
202   //1100
203
204   /* ACLK is devided by 4. RSEL=6 no division for MCLK
205      and SSMCLK. XT2 is off. */
206   //BCSCTL1 = 0xa8;
207
208   BCSCTL2 = 0x00; /* Init FLL to desired frequency using the 32762Hz
209                      crystal DCO frquenzy = 2,4576 MHz  */
210
211   PLEDOUT|=PLEDPIN;
212
213   BCSCTL1 |= DIVA1 + DIVA0;             /* ACLK = LFXT1CLK/8 */
214   for(i = 0xffff; i > 0; i--) {         /* Delay for XTAL to settle */
215     asm("nop");
216   }
217
218   CCTL2 = CCIS0 + CM0 + CAP;            // Define CCR2, CAP, ACLK
219   TACTL = TASSEL1 + TACLR + MC1;        // SMCLK, continous mode
220
221
222   while(1) {
223
224     while((CCTL2 & CCIFG) != CCIFG);    /* Wait until capture occured! */
225     CCTL2 &= ~CCIFG;                    /* Capture occured, clear flag */
226     compare = CCR2;                     /* Get current captured SMCLK */
227     compare = compare - oldcapture;     /* SMCLK difference */
228     oldcapture = CCR2;                  /* Save current captured SMCLK */
229
230     if(DELTA == compare) {
231       break;                            /* if equal, leave "while(1)" */
232     } else if(DELTA < compare) {        /* DCO is too fast, slow it down */
233       DCOCTL--;
234       if(DCOCTL == 0xFF) {              /* Did DCO role under? */
235         BCSCTL1--;
236       }
237     } else {                            /* -> Select next lower RSEL */
238       DCOCTL++;
239       if(DCOCTL == 0x00) {              /* Did DCO role over? */
240         BCSCTL1++;
241       }
242                                         /* -> Select next higher RSEL  */
243     }
244   }
245
246   CCTL2 = 0;                            /* Stop CCR2 function */
247   TACTL = 0;                            /* Stop Timer_A */
248
249   BCSCTL1 &= ~(DIVA1 + DIVA0);          /* remove /8 divisor from ACLK again */
250
251   PLEDOUT=~PLEDPIN;
252
253 }
254