From: travisutk Date: Tue, 2 Jun 2009 16:04:50 +0000 (+0000) Subject: Echo works. X-Git-Url: http://git.rot13.org/?p=goodfet;a=commitdiff_plain;h=48d828330be887dcd02ffcf0f7e326d5626f5845 Echo works. git-svn-id: https://svn.code.sf.net/p/goodfet/code/trunk@19 12e2690d-a6be-4b82-a7b7-67c4a43b65c8 --- diff --git a/firmware/include/platform.h b/firmware/include/platform.h index fe42d4f..abc9ef8 100644 --- a/firmware/include/platform.h +++ b/firmware/include/platform.h @@ -6,5 +6,5 @@ #define PLEDDIR P1DIR #define PLEDPIN 0x1 -// + diff --git a/firmware/lib/msp430f1612.c b/firmware/lib/msp430f1612.c new file mode 100644 index 0000000..c003213 --- /dev/null +++ b/firmware/lib/msp430f1612.c @@ -0,0 +1,111 @@ +//! MSP430F1612/1611 clock and I/O definitions + +#include "platform.h" + +#include +#include +#include + + +//! Receive a byte. +unsigned char serial_rx(){ + char c; + + while(!(IFG1&URXIFG0));//wait for a byte + c = RXBUF0; + IFG1&=~URXIFG0; + U0TCTL &= ~URXSE; + + PLEDOUT^=PLEDPIN; + + return c; +} +//! Transmit a byte. +void serial_tx(unsigned char x){ + while ((IFG1 & UTXIFG0) == 0); //loop until buffer is free + TXBUF0 = x; +} + + +void msp430_init_uart(){ + + /* RS232 */ + + P3SEL |= BIT4|BIT5; // P3.4,5 = USART0 TXD/RXD + P3DIR |= BIT4; + + + UCTL0 = SWRST | CHAR; /* 8-bit character, UART mode */ + + + UTCTL0 = SSEL1; /* UCLK = MCLK */ + + //http://mspgcc.sourceforge.net/baudrate.html + UBR00=0x00; UBR10=0x01; UMCTL0=0x00; + + ME1 &= ~USPIE0; /* USART1 SPI module disable */ + ME1 |= (UTXE0 | URXE0); /* Enable USART1 TXD/RXD */ + + UCTL0 &= ~SWRST; + + /* XXX Clear pending interrupts before enable!!! */ + U0TCTL |= URXSE; + + //IE1 |= URXIE1; /* Enable USART1 RX interrupt */ +} + + +void msp430_init_dco() { + /* This code taken from the FU Berlin sources and reformatted. */ +#define MSP430_CPU_SPEED 2457600UL +#define DELTA ((MSP430_CPU_SPEED) / (32768 / 8)) + unsigned int compare, oldcapture = 0; + unsigned int i; + + WDTCTL = WDTPW + WDTHOLD; //stop WDT + + BCSCTL1 = 0xa4; /* ACLK is devided by 4. RSEL=6 no division for MCLK + and SSMCLK. XT2 is off. */ + + BCSCTL2 = 0x00; /* Init FLL to desired frequency using the 32762Hz + crystal DCO frquenzy = 2,4576 MHz */ + + BCSCTL1 |= DIVA1 + DIVA0; /* ACLK = LFXT1CLK/8 */ + for(i = 0xffff; i > 0; i--) { /* Delay for XTAL to settle */ + asm("nop"); + } + + CCTL2 = CCIS0 + CM0 + CAP; // Define CCR2, CAP, ACLK + TACTL = TASSEL1 + TACLR + MC1; // SMCLK, continous mode + + + while(1) { + + while((CCTL2 & CCIFG) != CCIFG); /* Wait until capture occured! */ + CCTL2 &= ~CCIFG; /* Capture occured, clear flag */ + compare = CCR2; /* Get current captured SMCLK */ + compare = compare - oldcapture; /* SMCLK difference */ + oldcapture = CCR2; /* Save current captured SMCLK */ + + if(DELTA == compare) { + break; /* if equal, leave "while(1)" */ + } else if(DELTA < compare) { /* DCO is too fast, slow it down */ + DCOCTL--; + if(DCOCTL == 0xFF) { /* Did DCO role under? */ + BCSCTL1--; + } + } else { /* -> Select next lower RSEL */ + DCOCTL++; + if(DCOCTL == 0x00) { /* Did DCO role over? */ + BCSCTL1++; + } + /* -> Select next higher RSEL */ + } + } + + CCTL2 = 0; /* Stop CCR2 function */ + TACTL = 0; /* Stop Timer_A */ + + BCSCTL1 &= ~(DIVA1 + DIVA0); /* remove /8 divisor from ACLK again */ +} + diff --git a/firmware/tests/blink/blink.c b/firmware/tests/blink/blink.c index fabe89c..c841bec 100644 --- a/firmware/tests/blink/blink.c +++ b/firmware/tests/blink/blink.c @@ -1,4 +1,4 @@ -//GOODFET Echo test. +//GOODFET Blink test. #include "platform.h" diff --git a/firmware/tests/echo/Makefile b/firmware/tests/echo/Makefile index a8b398b..e8efa32 100644 --- a/firmware/tests/echo/Makefile +++ b/firmware/tests/echo/Makefile @@ -5,17 +5,20 @@ BSL=tos-bsl --invert-reset --invert-test -c $(PORT) #mcu=msp430x1611 mcu=msp430x1612 +#ldscript is wonky GCCINC=-T ../../ldscripts/161x.x -CC=msp430-gcc -g -mmcu=$(mcu) -DGCC $(GCCINC) -I ../../include +CC=msp430-gcc -g -mmcu=$(mcu) -DGCC $(GCCINC) -I ../../include +libs=../../lib/msp430f1612.c app=echo install: $(app) $(BSL) -e -p $(app) #$(BSL) -P $(app) -g 0x2500 $(BSL) -P $(app) -r +$(app): $(app).c $(libs) erase: $(BSL) -e clean: - rm $(app) + rm -f $(app) diff --git a/firmware/tests/echo/echo.c b/firmware/tests/echo/echo.c index fabe89c..1f55288 100644 --- a/firmware/tests/echo/echo.c +++ b/firmware/tests/echo/echo.c @@ -7,16 +7,30 @@ #include #include + //LED on P1.0 //IO on P5 //! Initialize registers and all that jazz. void init(){ + volatile unsigned int i; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer //LED and TX OUT PLEDDIR |= PLEDPIN; + msp430_init_dco(); + + msp430_init_uart(); + + while(1){ + i = 10000; + while(i--); + + serial_tx(0x80); + PLEDOUT^=PLEDPIN; // Blink + } + //Enable Interrupts. //eint(); } @@ -27,6 +41,12 @@ int main(void) volatile unsigned int i; init(); + + PLEDOUT^=PLEDPIN; // Blink + + //while(1) serial_tx(serial_rx()); + while(1) serial_tx(0x80); + while(1){ i = 10000; while(i--);