Echo works.
[goodfet] / firmware / lib / msp430f1612.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   PLEDOUT^=PLEDPIN;
20   
21   return c;
22 }
23 //! Transmit a byte.
24 void serial_tx(unsigned char x){
25   while ((IFG1 & UTXIFG0) == 0); //loop until buffer is free
26   TXBUF0 = x;
27 }
28
29
30 void msp430_init_uart(){
31   
32   /* RS232 */
33   
34   P3SEL |= BIT4|BIT5;                        // P3.4,5 = USART0 TXD/RXD
35   P3DIR |= BIT4;
36   
37   
38   UCTL0 = SWRST | CHAR;                 /* 8-bit character, UART mode */
39   
40   
41   UTCTL0 = SSEL1;                       /* UCLK = MCLK */
42
43   //http://mspgcc.sourceforge.net/baudrate.html
44   UBR00=0x00; UBR10=0x01; UMCTL0=0x00;
45   
46   ME1 &= ~USPIE0;                       /* USART1 SPI module disable */
47   ME1 |= (UTXE0 | URXE0);               /* Enable USART1 TXD/RXD */
48
49   UCTL0 &= ~SWRST;
50
51   /* XXX Clear pending interrupts before enable!!! */
52   U0TCTL |= URXSE;
53
54   //IE1 |= URXIE1;                        /* Enable USART1 RX interrupt  */
55 }
56
57
58 void msp430_init_dco() {
59     /* This code taken from the FU Berlin sources and reformatted. */
60 #define MSP430_CPU_SPEED 2457600UL
61 #define DELTA    ((MSP430_CPU_SPEED) / (32768 / 8))
62   unsigned int compare, oldcapture = 0;
63   unsigned int i;
64   
65   WDTCTL = WDTPW + WDTHOLD; //stop WDT
66
67   BCSCTL1 = 0xa4; /* ACLK is devided by 4. RSEL=6 no division for MCLK
68                      and SSMCLK. XT2 is off. */
69
70   BCSCTL2 = 0x00; /* Init FLL to desired frequency using the 32762Hz
71                      crystal DCO frquenzy = 2,4576 MHz  */
72
73   BCSCTL1 |= DIVA1 + DIVA0;             /* ACLK = LFXT1CLK/8 */
74   for(i = 0xffff; i > 0; i--) {         /* Delay for XTAL to settle */
75     asm("nop");
76   }
77
78   CCTL2 = CCIS0 + CM0 + CAP;            // Define CCR2, CAP, ACLK
79   TACTL = TASSEL1 + TACLR + MC1;        // SMCLK, continous mode
80
81
82   while(1) {
83
84     while((CCTL2 & CCIFG) != CCIFG);    /* Wait until capture occured! */
85     CCTL2 &= ~CCIFG;                    /* Capture occured, clear flag */
86     compare = CCR2;                     /* Get current captured SMCLK */
87     compare = compare - oldcapture;     /* SMCLK difference */
88     oldcapture = CCR2;                  /* Save current captured SMCLK */
89
90     if(DELTA == compare) {
91       break;                            /* if equal, leave "while(1)" */
92     } else if(DELTA < compare) {        /* DCO is too fast, slow it down */
93       DCOCTL--;
94       if(DCOCTL == 0xFF) {              /* Did DCO role under? */
95         BCSCTL1--;
96       }
97     } else {                            /* -> Select next lower RSEL */
98       DCOCTL++;
99       if(DCOCTL == 0x00) {              /* Did DCO role over? */
100         BCSCTL1++;
101       }
102                                         /* -> Select next higher RSEL  */
103     }
104   }
105
106   CCTL2 = 0;                            /* Stop CCR2 function */
107   TACTL = 0;                            /* Stop Timer_A */
108
109   BCSCTL1 &= ~(DIVA1 + DIVA0);          /* remove /8 divisor from ACLK again */
110 }
111