Dropped ugly 'blocks' kludge for a 16-bit length field.
authortravisutk <travisutk@12e2690d-a6be-4b82-a7b7-67c4a43b65c8>
Mon, 5 Oct 2009 11:34:09 +0000 (11:34 +0000)
committertravisutk <travisutk@12e2690d-a6be-4b82-a7b7-67c4a43b65c8>
Mon, 5 Oct 2009 11:34:09 +0000 (11:34 +0000)
git-svn-id: https://svn.code.sf.net/p/goodfet/code/trunk@177 12e2690d-a6be-4b82-a7b7-67c4a43b65c8

15 files changed:
client/GoodFET.py
client/GoodFETMSP430.py
client/GoodFETSPI.py
client/goodfet.msp430
client/goodfet.spiflash
firmware/apps/chipcon/chipcon.c
firmware/apps/i2c/i2c.c
firmware/apps/jtag/jtag.c
firmware/apps/jtag/jtag430.c
firmware/apps/jtag/jtag430x2.c
firmware/apps/monitor/monitor.c
firmware/apps/spi/spi.c
firmware/goodfet.c
firmware/include/command.h
firmware/lib/command.c

index a31676c..18a3eb1 100755 (executable)
@@ -50,11 +50,16 @@ class GoodFET:
     def getbuffer(self,size=0x1c00):
         writecmd(0,0xC2,[size&0xFF,(size>>16)&0xFF]);
         print "Got %02x%02x buffer size." % (self.data[1],self.data[0]);
-    def writecmd(self, app, verb, count=0, data=[], blocks=1):
+    def writecmd(self, app, verb, count=0, 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));
+        
+            
+        #little endian 16-bit length
+        self.serialport.write(chr(count&0xFF));
+        self.serialport.write(chr(count>>8));
+        
         #print "count=%02x, len(data)=%04x" % (count,len(data));
         if count!=0:
             for d in data:
@@ -65,7 +70,7 @@ class GoodFET:
         
         
         if not self.besilent:
-            self.readcmd(blocks);
+            self.readcmd();
         
     besilent=0;
     app=0;
@@ -73,7 +78,7 @@ class GoodFET:
     count=0;
     data="";
 
-    def readcmd(self,blocks=1):
+    def readcmd(self):
         """Read a reply from the GoodFET."""
         while 1:
             #print "Reading...";
@@ -81,16 +86,16 @@ class GoodFET:
             #print "APP=%2x" % self.app;
             self.verb=ord(self.serialport.read(1));
             #print "VERB=%02x" % self.verb;
-            self.count=ord(self.serialport.read(1));
-            #print "Waiting for %i bytes." % self.count;
-            
-            #print "READ %02x %02x %02x " % (self.app, self.verb, self.count);
+            self.count=(
+                ord(self.serialport.read(1))
+                +(ord(self.serialport.read(1))<<8)
+                );
             
             #Debugging string; print, but wait.
             if self.app==0xFF and self.verb==0xFF:
                 print "DEBUG %s" % self.serialport.read(self.count);
             else:
-                self.data=self.serialport.read(self.count*blocks);
+                self.data=self.serialport.read(self.count);
                 return self.data;
     
     #Monitor stuff
index 155ce2a..4ee6e4f 100644 (file)
@@ -39,16 +39,16 @@ class GoodFETMSP430(GoodFET):
         """Read a word at an address."""
         self.data=[adr&0xff, (adr&0xff00)>>8,
                    (adr&0xff0000)>>16,(adr&0xff000000)>>24,
-                   ]; 
-        self.writecmd(self.MSP430APP,0x02,4,self.data,1);
+                   ];
+        self.writecmd(self.MSP430APP,0x02,4,self.data);
+        
         return ord(self.data[0])+(ord(self.data[1])<<8);
-    def MSP430peekblock(self,adr,blocks=1):
+    def MSP430peekblock(self,adr):
         """Grab a few block from an SPI Flash ROM.  Block size is unknown"""
         data=[adr&0xff, (adr&0xff00)>>8,
               (adr&0xff0000)>>16,(adr&0xff000000)>>24,
-              blocks];
-        
-        self.writecmd(self.MSP430APP,0x02,5,data,blocks);
+              0x00,0x04];
+        self.writecmd(self.MSP430APP,0x02,6,data);
         return self.data;
     
     def MSP430poke(self,adr,val):
@@ -139,8 +139,10 @@ class GoodFETMSP430(GoodFET):
         }
     def MSP430test(self):
         """Test MSP430 JTAG.  Requires that a chip be attached."""
+        
         if self.MSP430ident()==0xffff:
-            print "Is anything connected?";
+            print "ERROR Is anything connected?";
+        print "Testing %s." % self.MSP430identstr();
         print "Testing RAM from 200 to 210.";
         for a in range(0x200,0x210):
             self.MSP430poke(a,0);
index 5d42354..044aea2 100644 (file)
@@ -79,14 +79,13 @@ class GoodFETSPIFlash(GoodFETSPI):
               0];
         self.SPItrans(data);
         return ord(self.data[4]);
-    def SPIpeekblock(self,adr,blocks=1):
+    def SPIpeekblock(self,adr):
         """Grab a few block from an SPI Flash ROM.  Block size is unknown"""
         data=[(adr&0xFF0000)>>16,
               (adr&0xFF00)>>8,
-              adr&0xFF,
-              blocks];
+              adr&0xFF];
         
-        self.writecmd(0x01,0x02,4,data,blocks);
+        self.writecmd(0x01,0x02,3,data);
         return self.data;
     
     def SPIpokebyte(self,adr,val):
@@ -102,7 +101,7 @@ class GoodFETSPIFlash(GoodFETSPI):
         
     def SPIchiperase(self):
         """Mass erase an SPI Flash ROM."""
-        self.writecmd(0x01,0x81,0,[]);
+        self.writecmd(0x01,0x81);
     def SPIwriteenable(self):
         """SPI Flash Write Enable"""
         data=[0x06];
index 20ee81e..f616fba 100755 (executable)
@@ -53,7 +53,7 @@ if(sys.argv[1]=="dump"):
     h = IntelHex(None);
     i=start;
     while i<=stop:
-        data=client.MSP430peekblock(i,0x20);
+        data=client.MSP430peekblock(i);
         print "Dumped %06x."%i;
         for j in data:
             if i<=stop: h[i]=ord(j);
index 88635b0..55a7b12 100755 (executable)
@@ -53,7 +53,20 @@ if(sys.argv[1]=="test"):
         print "Some success, some failures.  Is a wire loose?";
     else:
         print "All reads succeeded.  Wiring is probably good.";
-
+    print "Erasing.";
+    client.SPIchiperase();
+    print "Testing erase.";
+    data=client.SPIpeekblock(0);
+    for i in data:
+        if ord(i)!=0xFF:
+            print "ERROR not properly erased!";
+    data=range(0,10);
+    client.SPIpokebytes(0,data);
+    print "Testing flash write.";
+    for i in data:
+        if(client.SPIpeek(i)!=i):
+            print "%06x not properly poked to %02x" % (i,i);
+    print "Test complete.";
 if(sys.argv[1]=="info"):
     data=client.SPIjedec();
     print "Ident as %s\nManufacturer: %02x %s\nType: %02x\nCapacity: %02x (%i bytes)" % (
@@ -77,7 +90,7 @@ if(sys.argv[1]=="dump"):
     
     i=start;
     while i<=stop:
-        data=client.SPIpeekblock(i,255);
+        data=client.SPIpeekblock(i);
         #if(i%0x1000==0):
         print "Dumped %06x."%i;
         for j in data:
@@ -128,7 +141,7 @@ if(sys.argv[1]=="flash"):
     chars=list(file.read());
     
     #N.B., chunksize must be an even fraction of 0x100.
-    chunksize=0x80;
+    chunksize=0x100;
     
     #client.silent(1);
     while i<=stop:
index 7b55db8..05349ea 100644 (file)
@@ -124,7 +124,7 @@ void ccread(unsigned char len){
 //! Handles a monitor command.
 void cchandle(unsigned char app,
               unsigned char verb,
-              unsigned char len){
+              unsigned long len){
   switch(verb){
     //CC_PEEK and CC_POKE will come later.
   case READ:  //Write a command and return 1-byte reply.
index 6ee1623..dc26032 100644 (file)
@@ -159,7 +159,7 @@ unsigned char I2C_Read( unsigned char ack )
 //! Handles a monitor command.
 void i2chandle(unsigned char app,
               unsigned char verb,
-              unsigned char len){
+              unsigned long len){
   unsigned char i;
   switch(verb){
     
index d28a02f..73a6750 100644 (file)
@@ -206,7 +206,7 @@ unsigned char jtag_ir_shift8(unsigned char in){
 //! Handles a monitor command.
 void jtaghandle(unsigned char app,
               unsigned char verb,
-              unsigned char len){
+              unsigned long len){
   switch(verb){
     //START handled by specific JTAG
   case STOP:
index e4ee945..d0b83cb 100644 (file)
@@ -275,7 +275,7 @@ void jtag430_setinstrfetch(){
 //! Handles classic MSP430 JTAG commands.  Forwards others to JTAG.
 void jtag430handle(unsigned char app,
                   unsigned char verb,
-                  unsigned char len){
+                  unsigned long len){
   register char blocks;
   unsigned long at;
   unsigned int i, val;
@@ -306,31 +306,24 @@ void jtag430handle(unsigned char app,
     
   case JTAG430_READMEM:
   case PEEK:
-    /*
-    cmddataword[0]=jtag430_readmem(cmddataword[0]);
-    txdata(app,verb,2);
-    */
-    blocks=(len>4?cmddata[4]:1);
     at=cmddatalong[0];
     
     //Fetch large blocks for bulk fetches,
     //small blocks for individual peeks.
-    if(blocks>1)
-      len=0x80;
+    if(len>5)
+      len=(cmddataword[2]);//always even.
+    else
+      len=2;
+    len&=~1;//clue lsbit
     
     txhead(app,verb,len);
-    
-    while(blocks--){
-      for(i=0;i<len;i+=2){
-       jtag430_resettap();
-       //delay(10);
-       
-       val=jtag430_readmem(at);
-       
-       at+=2;
-       serial_tx(val&0xFF);
-       serial_tx((val&0xFF00)>>8);
-      }
+    for(i=0;i<len;i+=2){
+      jtag430_resettap();
+      val=jtag430_readmem(at);
+      
+      at+=2;
+      serial_tx(val&0xFF);
+      serial_tx((val&0xFF00)>>8);
     }
     break;
   case JTAG430_WRITEMEM:
index d0043ac..1acc6e1 100644 (file)
@@ -186,7 +186,7 @@ unsigned int jtag430x2_fusecheck(){
 //! Handles MSP430X2 JTAG commands.  Forwards others to JTAG.
 void jtag430x2handle(unsigned char app,
                     unsigned char verb,
-                    unsigned char len){
+                    unsigned long len){
   register char blocks;
   
   unsigned int i,val;
index a678bbc..3f35ea2 100644 (file)
@@ -10,7 +10,7 @@
 //! Handles a monitor command.
 void monitorhandle(unsigned char app,
                   unsigned char verb,
-                  unsigned char len){
+                  unsigned long len){
   switch(verb){
   case PEEK:
     cmddata[0]=memorybyte[cmddataword[0]];
index d6d5256..ce21cb8 100644 (file)
@@ -153,10 +153,8 @@ void spiflash_pokeblock(unsigned long adr,
 //! Peek some blocks.
 void spiflash_peek(unsigned char app,
                   unsigned char verb,
-                  unsigned char len){
-  register char blocks=(len>3?cmddata[3]:1);
-  unsigned char i;
-  
+                  unsigned long len){
+  unsigned int i;
   P5OUT&=~SS; //Drop !SS to begin transaction.
   spitrans8(0x03);//Flash Read Command
   len=3;//write 3 byte pointer
@@ -164,27 +162,20 @@ void spiflash_peek(unsigned char app,
     spitrans8(cmddata[i]);
   
   //Send reply header
-  len=0x80;//128 byte chunk, repeated for each block
+  len=0x1000;
   txhead(app,verb,len);
   
-  while(blocks--){
-    for(i=0;i<len;i++)
-      serial_tx(spitrans8(0));
-    
-    /* old fashioned
-    for(i=0;i<len;i++)
-      cmddata[i]=spitrans8(0);
-    txdata(app,verb,len);
-    */
-  }
+  while(len--)
+    serial_tx(spitrans8(0));
+  
   P5OUT|=SS;  //Raise !SS to end transaction.
 }
 
 //! Handles a monitor command.
 void spihandle(unsigned char app,
               unsigned char verb,
-              unsigned char len){
-  unsigned char i;
+              unsigned long len){
+  unsigned long i;
   
   //Raise !SS to end transaction, just in case we forgot.
   P5OUT|=SS;
@@ -231,7 +222,6 @@ void spihandle(unsigned char app,
     P5OUT|=SS;          //Raise !SS to end transaction.
     
     
-    
     while(spiflash_status()&0x01)
       P1OUT^=1;
     
@@ -247,7 +237,7 @@ void spihandle(unsigned char app,
     spitrans8(0xC7);//Chip Erase
     P5OUT|=SS;  //Raise !SS to end transaction.
     
-        
+    
     while(spiflash_status()&0x01)//while busy
       P1OUT^=1;
     P1OUT&=~1;
index c310c0c..4cddc19 100644 (file)
@@ -35,7 +35,7 @@ void init(){
 //! Handle a command.\r
 void handle(unsigned char app,\r
            unsigned char verb,\r
-           unsigned char len){\r
+           unsigned long len){\r
   //debugstr("GoodFET");\r
   switch(app){\r
   case MONITOR:\r
@@ -70,7 +70,8 @@ void handle(unsigned char app,
 int main(void)\r
 {\r
   volatile unsigned int i;\r
-  unsigned char app, verb, len;\r
+  unsigned char app, verb;\r
+  unsigned long len;\r
   \r
   init();\r
   \r
@@ -81,13 +82,23 @@ int main(void)
     //Magic 3\r
     app=serial_rx();\r
     verb=serial_rx();\r
-    len=serial_rx();\r
+    //len=serial_rx();\r
+    len=rxword();\r
     \r
-    //Read data, if any\r
-    for(i=0;i<len;i++){\r
-      cmddata[i]=serial_rx();\r
+    //Read data, looking for buffer overflow.y\r
+    if(len<=CMDDATALEN){\r
+      for(i=0;i<len;i++){\r
+       cmddata[i]=serial_rx();\r
+      }\r
+      handle(app,verb,len);\r
+    }else{\r
+      //Listen to the blaberring.\r
+      for(i-0;i<len;i++)\r
+       serial_rx();\r
+      //Reply with an error.\r
+      debugstr("Buffer length exceeded.");\r
+      txdata(MONITOR,NOK,0);\r
     }\r
-    handle(app,verb,len);\r
   }\r
 }\r
 \r
index 51325be..fe3beb1 100644 (file)
@@ -4,7 +4,12 @@
 */
 
 //! Global data buffer.
-extern unsigned char cmddata[0x100];
+
+#ifndef CMDDATALEN
+#define CMDDATALEN 0x200
+#endif
+
+extern unsigned char cmddata[CMDDATALEN];
 extern unsigned char silent;
 
 #define cmddataword ((unsigned int*) cmddata)
@@ -97,19 +102,30 @@ extern unsigned char silent;
 //! Handle a command.  Defined in goodfet.c
 void handle(unsigned char app,
            unsigned char verb,
-           unsigned char len);
+           unsigned long len);
 //! Transmit a header.
 void txhead(unsigned char app,
            unsigned char verb,
-           unsigned int len);
+           unsigned long len);
 //! Transmit data.
 void txdata(unsigned char app,
            unsigned char verb,
-           unsigned int len);
+           unsigned long len);
 //! Transmit a string.
 void txstring(unsigned char app,
              unsigned char verb,
              const char *str);
+
+//! Receive a long.
+unsigned long rxlong();
+//! Receive a word.
+unsigned int rxword();
+
+//! Transmit a long.
+void txlong(unsigned long l);
+//! Transmit a word.
+void txword(unsigned int l);
+
 //! Transmit a debug string.
 void debugstr(const char *str);
 
@@ -119,12 +135,12 @@ void delay(unsigned int count);
 void msdelay(unsigned int ms);
 
 
-void monitorhandle(unsigned char, unsigned char, unsigned char);
-void spihandle(unsigned char, unsigned char, unsigned char);
-void i2chandle(unsigned char, unsigned char, unsigned char);
-void cchandle(unsigned char, unsigned char, unsigned char);
-void jtaghandle(unsigned char, unsigned char, unsigned char);
-void jtag430handle(unsigned char, unsigned char, unsigned char);
+void monitorhandle(unsigned char, unsigned char, unsigned long);
+void spihandle(unsigned char, unsigned char, unsigned long);
+void i2chandle(unsigned char, unsigned char, unsigned long);
+void cchandle(unsigned char, unsigned char, unsigned long);
+void jtaghandle(unsigned char, unsigned char, unsigned long);
+void jtag430handle(unsigned char, unsigned char, unsigned long);
 void jtag430x2handle(unsigned char app, unsigned char verb,
-                    unsigned char len);
+                    unsigned long len);
   
index 8d9eac1..d984fca 100644 (file)
@@ -7,17 +7,15 @@
 #include "platform.h"
 #include <string.h>
 
-unsigned char cmddata[256];
+unsigned char cmddata[CMDDATALEN];
 unsigned char silent=0;
 
 //! Transmit a string.
 void txstring(unsigned char app,
              unsigned char verb,
              const char *str){
-  unsigned char len=strlen(str);
-  serial_tx(app);
-  serial_tx(verb);
-  serial_tx(len);
+  unsigned long len=strlen(str);
+  txhead(app,verb,len);
   while(len--)
     serial_tx(*(str++));
 }
@@ -35,16 +33,17 @@ void debugstr(const char *str){
 //! Transmit a header.
 void txhead(unsigned char app,
            unsigned char verb,
-           unsigned int len){
+           unsigned long len){
   serial_tx(app);
   serial_tx(verb);
-  serial_tx(len);
+  //serial_tx(len);
+  txword(len);
 }
 
 //! Transmit data.
 void txdata(unsigned char app,
            unsigned char verb,
-           unsigned int len){
+           unsigned long len){
   unsigned int i=0;
   if(silent)
     return;
@@ -54,6 +53,41 @@ void txdata(unsigned char app,
   }
 }
 
+//! Receive a long.
+unsigned long rxlong(){
+  unsigned long toret=0;
+  toret=serial_rx();
+  toret|=(((long)serial_rx())<<8);
+  toret|=(((long)serial_rx())<<16);
+  toret|=(((long)serial_rx())<<24);
+  return toret;
+}
+//! Receive a word.
+unsigned int rxword(){
+  unsigned long toret=0;
+  toret=serial_rx();
+  toret|=(((long)serial_rx())<<8);
+  return toret;
+}
+//! Transmit a long.
+void txlong(unsigned long l){
+  serial_tx(l&0xFF);
+  l>>=8;
+  serial_tx(l&0xFF);
+  l>>=8;
+  serial_tx(l&0xFF);
+  l>>=8;
+  serial_tx(l&0xFF);
+  l>>=8;
+}
+//! Transmit a word.
+void txword(unsigned int l){
+  serial_tx(l&0xFF);
+  l>>=8;
+  serial_tx(l&0xFF);
+  l>>=8;
+}
+
 //Be very careful changing delay().
 //It was chosen poorly by trial and error.