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];
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);
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
-//GOODFET Echo test.\r
-\r
+//GOODFET Main File\r
+//Includes several applications.\r
\r
#include "platform.h"\r
#include "command.h"\r
case MONITOR:\r
monitorhandle(app,verb,len);\r
break;\r
+ case SPI:\r
+ spihandle(app,verb,len);\r
+ break;\r
default:\r
txdata(app,NOK,0);\r
}\r
}\r
handle(app,verb,len);\r
}\r
- \r
- //while(1) serial_tx(serial_rx());\r
- while(1) serial_tx(serial_rx());\r
- \r
- while(1){\r
- i = 10000;\r
- while(i--);\r
- \r
- PLEDOUT^=PLEDPIN; // Blink\r
- }\r
}\r
\r
--- /dev/null
+//GoodFET SPI Application
+//Handles basic I/O
+
+//Higher level left to client application.
+
+#include "platform.h"
+#include "command.h"
+
+#include <signal.h>
+#include <io.h>
+#include <iomacros.h>
+
+
+//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;
+ }
+}
-//! 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);
unsigned char cmddata[256];
-//!Transmit data.
+//! Transmit data.
void txdata(unsigned char app,
unsigned char verb,
unsigned char len){
serial_tx(cmddata[i]);
}
}
+
+
+//! Delay for a count.
+void delay(unsigned int count){
+ volatile unsigned int i=count;
+ while(i--);
+}