SPI application transfers correctly; need only !SScommand.
authortravisutk <travisutk@12e2690d-a6be-4b82-a7b7-67c4a43b65c8>
Wed, 3 Jun 2009 21:32:14 +0000 (21:32 +0000)
committertravisutk <travisutk@12e2690d-a6be-4b82-a7b7-67c4a43b65c8>
Wed, 3 Jun 2009 21:32:14 +0000 (21:32 +0000)
git-svn-id: https://svn.code.sf.net/p/goodfet/code/trunk@27 12e2690d-a6be-4b82-a7b7-67c4a43b65c8

client/goodfet
firmware/apps/Makefile
firmware/apps/goodfet.c
firmware/apps/spi/spi.c [new file with mode: 0644]
firmware/include/command.h
firmware/lib/command.c

index 7e93f6a..74c1655 100755 (executable)
@@ -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);
index 83f3125..b3aa926 100644 (file)
@@ -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
 
index b6d8a06..9462020 100644 (file)
@@ -1,5 +1,5 @@
-//GOODFET Echo test.\r
-\r
+//GOODFET Main File\r
+//Includes several applications.\r
 \r
 #include "platform.h"\r
 #include "command.h"\r
@@ -36,6 +36,9 @@ void handle(unsigned char app,
   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
@@ -64,15 +67,5 @@ int main(void)
     }\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
diff --git a/firmware/apps/spi/spi.c b/firmware/apps/spi/spi.c
new file mode 100644 (file)
index 0000000..b0b5323
--- /dev/null
@@ -0,0 +1,82 @@
+//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;
+  }
+}
index 5d7813b..2df7778 100644 (file)
@@ -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);
index c9c1b1c..44a853d 100644 (file)
@@ -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--);
+}