From: travisutk Date: Wed, 3 Jun 2009 21:32:14 +0000 (+0000) Subject: SPI application transfers correctly; need only !SScommand. X-Git-Url: http://git.rot13.org/?p=goodfet;a=commitdiff_plain;h=0652c424f95ebdb6c9be17edc0a4d1057432142f SPI application transfers correctly; need only !SScommand. git-svn-id: https://svn.code.sf.net/p/goodfet/code/trunk@27 12e2690d-a6be-4b82-a7b7-67c4a43b65c8 --- diff --git a/client/goodfet b/client/goodfet index 7e93f6a..74c1655 100755 --- a/client/goodfet +++ b/client/goodfet @@ -29,36 +29,32 @@ class Client: self.serialport.flushOutput() #Read and handle the initial command. + time.sleep(1); client.readcmd(); - client.handlecmd(); + if(self.verb!=0x7F): + print "Verb is wrong. Incorrect firmware?"; + - time.sleep(1); def writecmd(self, app, verb, count, data): + """Write a command and some data to the GoodFET.""" self.serialport.write(chr(app)); self.serialport.write(chr(verb)); self.serialport.write(chr(count)); #print "count=%02x, len(data)=%04x" % (count,len(data)); - for d in data: - self.serialport.write(chr(d)); + if count!=0: + for d in data: + self.serialport.write(chr(d)); + #self.readcmd(); #Uncomment this later, to ensure a response. def readcmd(self): + """Read a reply from the GoodFET.""" self.app=ord(self.serialport.read(1)); self.verb=ord(self.serialport.read(1)); self.count=ord(self.serialport.read(1)); if self.count>0: self.data=self.serialport.read(self.count); #print "%02x %02x %02x" % (self.app, self.verb, self.count); - def handlemonitor(self): - if self.verb==0x7E: - print "Monitor: NOK"; - if self.verb==0x7F: - print "Monitor: OK"; - def handlecmd(self): - if self.app==0: - #print "Monitor command." - self.handlemonitor(); - else: - print "Unknown application %02x." % self.app + #Monitor stuff def peekbyte(self,address): """Read a byte of memory from the monitor.""" self.data=[address&0xff,address>>8]; @@ -88,11 +84,28 @@ class Client: self.pokebyte(0x0021,1); #Light LED print "Self-test complete."; + + def spisetup(self): + """Moved the FET into the SPI application.""" + print "Initializing SPI."; + self.writecmd(1,0x10,0,self.data); #SPI/SETUP + self.readcmd(); + def spitrans8(self,byte): + """Read and write 8 bits by SPI.""" + self.data=[byte]; + self.writecmd(1,0,1,self.data); #SPI exchange + self.readcmd(); + + if self.app!=1 or self.verb!=0: + print "Error in SPI transaction; app=%02x, verb=%02x" % (self.app, self.verb); + return ord(self.data[0]); client=Client(); client.serInit("/dev/ttyUSB0") - - client.monitortest(); +client.spisetup(); +while 1: + print "%02x" % client.spitrans8(5); + time.sleep(0.1); diff --git a/firmware/apps/Makefile b/firmware/apps/Makefile index 83f3125..b3aa926 100644 --- a/firmware/apps/Makefile +++ b/firmware/apps/Makefile @@ -10,7 +10,7 @@ GCCINC=-T ../ldscripts/161x.x CC=msp430-gcc -g -mmcu=$(mcu) -DGCC $(GCCINC) -I ../include -apps= monitor/monitor.c +apps= monitor/monitor.c spi/spi.c libs= ../lib/msp430f1612.c ../lib/command.c app=goodfet diff --git a/firmware/apps/goodfet.c b/firmware/apps/goodfet.c index b6d8a06..9462020 100644 --- a/firmware/apps/goodfet.c +++ b/firmware/apps/goodfet.c @@ -1,5 +1,5 @@ -//GOODFET Echo test. - +//GOODFET Main File +//Includes several applications. #include "platform.h" #include "command.h" @@ -36,6 +36,9 @@ void handle(unsigned char app, case MONITOR: monitorhandle(app,verb,len); break; + case SPI: + spihandle(app,verb,len); + break; default: txdata(app,NOK,0); } @@ -64,15 +67,5 @@ int main(void) } handle(app,verb,len); } - - //while(1) serial_tx(serial_rx()); - while(1) serial_tx(serial_rx()); - - while(1){ - i = 10000; - while(i--); - - PLEDOUT^=PLEDPIN; // Blink - } } diff --git a/firmware/apps/spi/spi.c b/firmware/apps/spi/spi.c new file mode 100644 index 0000000..b0b5323 --- /dev/null +++ b/firmware/apps/spi/spi.c @@ -0,0 +1,82 @@ +//GoodFET SPI Application +//Handles basic I/O + +//Higher level left to client application. + +#include "platform.h" +#include "command.h" + +#include +#include +#include + + +//Pins and I/O +#define SS BIT0 +#define MOSI BIT1 +#define MISO BIT2 +#define SCK BIT3 + +//This could be more accurate. +//Does it ever need to be? +#define SPISPEED 0 +#define SPIDELAY(x) delay(x) + +#define SETMOSI P5OUT|=MOSI +#define CLRMOSI P5OUT&=~MOSI +#define SETCLK P5OUT|=SCK +#define CLRCLK P5OUT&=~SCK +#define READMISO (P5IN&MISO?1:0) + +//! Set up the pins for SPI mode. +unsigned char spisetup(){ + P5DIR|=MOSI+SCK+SS; + P5DIR&=~MISO; +} + +//! Read and write an SPI bit. +unsigned char spitrans8(unsigned char byte){ + unsigned int bit; + //This function came from the SPI Wikipedia article. + //Minor alterations. + + for (bit = 0; bit < 8; bit++) { + /* write MOSI on trailing edge of previous clock */ + if (byte & 0x80) + SETMOSI; + else + CLRMOSI; + byte <<= 1; + + /* half a clock cycle before leading/rising edge */ + SPIDELAY(SPISPEED/2); + SETCLK; + + /* half a clock cycle before trailing/falling edge */ + SPIDELAY(SPISPEED/2); + + /* read MISO on trailing edge */ + byte |= READMISO; + CLRCLK; + } + + return byte; +} + +//! Handles a monitor command. +void spihandle(unsigned char app, + unsigned char verb, + unsigned char len){ + switch(verb){ + //PEEK and POKE might come later. + case READ: + case WRITE: + cmddata[0]=spitrans8(cmddata[0]); + txdata(app,verb,1); + break; + case SETUP: + spisetup(); + txdata(app,verb,0); + break; + } +} diff --git a/firmware/include/command.h b/firmware/include/command.h index 5d7813b..2df7778 100644 --- a/firmware/include/command.h +++ b/firmware/include/command.h @@ -1,22 +1,28 @@ -//! Command handling functions. +// Command handling functions. //! Global data buffer. extern unsigned char cmddata[256]; #define cmddataword ((unsigned int*) cmddata) #define memorybyte ((unsigned char*) 0) -// Command prefixes +// Global Commands #define READ 0x00 #define WRITE 0x01 #define PEEK 0x02 #define POKE 0x03 +#define SETUP 0x10 #define NOK 0x7E #define OK 0x7F -//!Handle a command. Defined in goodfet.c -void handle(unsigned char app,unsigned char verb,unsigned char len); +//! Handle a command. Defined in goodfet.c +void handle(unsigned char app, + unsigned char verb, + unsigned char len); -//!Transmit data. +//! Transmit data. void txdata(unsigned char app, unsigned char verb, unsigned char len); + +//! Delay +void delay(unsigned int count); diff --git a/firmware/lib/command.c b/firmware/lib/command.c index c9c1b1c..44a853d 100644 --- a/firmware/lib/command.c +++ b/firmware/lib/command.c @@ -2,7 +2,7 @@ unsigned char cmddata[256]; -//!Transmit data. +//! Transmit data. void txdata(unsigned char app, unsigned char verb, unsigned char len){ @@ -14,3 +14,10 @@ void txdata(unsigned char app, serial_tx(cmddata[i]); } } + + +//! Delay for a count. +void delay(unsigned int count){ + volatile unsigned int i=count; + while(i--); +}