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