msp430_init_dco on 2xx devices now knows defaults for MSP430F2618 chips with missing...
[goodfet] / firmware / lib / msp430x1612.c
1 //! MSP430F1612/1611 clock and I/O definitions
2
3 #include "platform.h"
4
5 #include <signal.h>
6 #include <io.h>
7 #include <iomacros.h>
8
9
10 //! Receive a byte.
11 unsigned char serial_rx(){
12   char c;
13   
14   while(!(IFG1&URXIFG0));//wait for a byte
15   c = RXBUF0;
16   IFG1&=~URXIFG0;
17   U0TCTL &= ~URXSE;
18   
19   return c;
20 }
21
22 //! Receive a byte.
23 unsigned char serial1_rx(){
24   char c;
25   
26   while(!(IFG2&URXIFG1));//wait for a byte
27   c = RXBUF1;
28   IFG2&=~URXIFG1;
29   U1TCTL &= ~URXSE;
30   
31   return c;
32 }
33
34
35 //! Transmit a byte.
36 void serial_tx(unsigned char x){
37   while ((IFG1 & UTXIFG0) == 0); //loop until buffer is free
38   TXBUF0 = x;
39 }
40
41 //! Transmit a byte on the second UART.
42 void serial1_tx(unsigned char x){
43   while ((IFG2 & UTXIFG1) == 0); //loop until buffer is free
44   TXBUF1 = x;
45 }
46
47 //! Set the baud rate.
48 void setbaud(unsigned char rate){
49   
50   //http://mspgcc.sourceforge.net/baudrate.html
51   switch(rate){
52   case 1://9600 baud
53     UBR00=0x7F; UBR10=0x01; UMCTL0=0x5B; /* uart0 3683400Hz 9599bps */
54     break;
55   case 2://19200 baud
56     UBR00=0xBF; UBR10=0x00; UMCTL0=0xF7; /* uart0 3683400Hz 19194bps */
57     break;
58   case 3://38400 baud
59     UBR00=0x5F; UBR10=0x00; UMCTL0=0xBF; /* uart0 3683400Hz 38408bps */
60     break;
61   case 4://57600 baud
62     UBR00=0x40; UBR10=0x00; UMCTL0=0x00; /* uart0 3683400Hz 57553bps */
63     break;
64   default:
65   case 5://115200 baud
66     UBR00=0x20; UBR10=0x00; UMCTL0=0x00; /* uart0 3683400Hz 115106bps */
67     break;
68   }
69 }
70
71 //! Set the baud rate of the second uart.
72 void setbaud1(unsigned char rate){
73   
74   //http://mspgcc.sourceforge.net/baudrate.html
75   switch(rate){
76   case 1://9600 baud
77     //    UBR01=0x7F; UBR11=0x01; UMCTL1=0x5B; /* uart0 3683400Hz 9599bps */
78     break;
79   case 2://19200 baud
80     //UBR01=0xBF; UBR11=0x00; UMCTL1=0xF7; /* uart0 3683400Hz 19194bps */
81     break;
82   case 3://38400 baud
83     //UBR01=0x5F; UBR11=0x00; UMCTL1=0xBF; /* uart0 3683400Hz 38408bps */
84     break;
85   case 4://57600 baud
86     //UBR01=0x40; UBR11=0x00; UMCTL1=0x00; /* uart0 3683400Hz 57553bps */
87     break;
88   default:
89   case 5://115200 baud
90     //UBR01=0x20; UBR11=0x00; UMCTL1=0x00; /* uart0 3683400Hz 115106bps */
91     break;
92   }
93 }
94
95
96 void msp430_init_uart(){
97   
98   /* RS232 */
99   
100   P3SEL |= BIT4|BIT5;                        // P3.4,5 = USART0 TXD/RXD
101   P3DIR |= BIT4;
102   
103   UCTL0 = SWRST | CHAR;                 /* 8-bit character, UART mode */
104   UTCTL0 = SSEL1;                       /* UCLK = MCLK */
105   
106   setbaud(0);
107   
108   ME1 &= ~USPIE0;                       /* USART1 SPI module disable */
109   ME1 |= (UTXE0 | URXE0);               /* Enable USART1 TXD/RXD */
110
111   UCTL0 &= ~SWRST;
112
113   /* XXX Clear pending interrupts before enable!!! */
114   U0TCTL |= URXSE;
115   
116   
117   //IE1 |= URXIE1;                        /* Enable USART1 RX interrupt  */
118 }
119
120
121 void msp430_init_dco() {
122 /* This code taken from the FU Berlin sources and reformatted. */
123   //
124
125 //Works well.
126 //#define MSP430_CPU_SPEED 2457600UL
127
128 //Too fast for internal resistor.
129 //#define MSP430_CPU_SPEED 4915200UL
130
131 //Max speed.
132 //#deefine MSP430_CPU_SPEED 4500000UL
133
134 //baud rate speed
135 #define MSP430_CPU_SPEED 3683400UL
136 #define DELTA    ((MSP430_CPU_SPEED) / (32768 / 8))
137   unsigned int compare, oldcapture = 0;
138   unsigned int i;
139   
140   WDTCTL = WDTPW + WDTHOLD; //stop WDT
141   
142   
143   DCOCTL=0xF0;
144   //a4
145   //1100
146
147   /* ACLK is devided by 4. RSEL=6 no division for MCLK
148      and SSMCLK. XT2 is off. */
149   //BCSCTL1 = 0xa8;
150   
151   BCSCTL2 = 0x00; /* Init FLL to desired frequency using the 32762Hz
152                      crystal DCO frquenzy = 2,4576 MHz  */
153   
154   P1OUT|=1;
155   
156   BCSCTL1 |= DIVA1 + DIVA0;             /* ACLK = LFXT1CLK/8 */
157   for(i = 0xffff; i > 0; i--) {         /* Delay for XTAL to settle */
158     asm("nop");
159   }
160
161   CCTL2 = CCIS0 + CM0 + CAP;            // Define CCR2, CAP, ACLK
162   TACTL = TASSEL1 + TACLR + MC1;        // SMCLK, continous mode
163
164
165   while(1) {
166
167     while((CCTL2 & CCIFG) != CCIFG);    /* Wait until capture occured! */
168     CCTL2 &= ~CCIFG;                    /* Capture occured, clear flag */
169     compare = CCR2;                     /* Get current captured SMCLK */
170     compare = compare - oldcapture;     /* SMCLK difference */
171     oldcapture = CCR2;                  /* Save current captured SMCLK */
172
173     if(DELTA == compare) {
174       break;                            /* if equal, leave "while(1)" */
175     } else if(DELTA < compare) {        /* DCO is too fast, slow it down */
176       DCOCTL--;
177       if(DCOCTL == 0xFF) {              /* Did DCO role under? */
178         BCSCTL1--;
179       }
180     } else {                            /* -> Select next lower RSEL */
181       DCOCTL++;
182       if(DCOCTL == 0x00) {              /* Did DCO role over? */
183         BCSCTL1++;
184       }
185                                         /* -> Select next higher RSEL  */
186     }
187   }
188   
189   CCTL2 = 0;                            /* Stop CCR2 function */
190   TACTL = 0;                            /* Stop Timer_A */
191
192   BCSCTL1 &= ~(DIVA1 + DIVA0);          /* remove /8 divisor from ACLK again */
193   
194   P1OUT=0;
195
196 }
197