!GEN is now 1 by default.
[goodfet] / firmware / goodfet.c
index bf63b13..355ae00 100644 (file)
@@ -1,5 +1,6 @@
 /*! \file goodfet.c\r
   \author Travis Goodspeed\r
+  \brief Main module.\r
   \r
   This is the main module of the GoodFET, which calls the initialization\r
   routines and delegates commands to the various applications.\r
 #include "platform.h"\r
 #include "command.h"\r
 #include "apps.h"\r
+#include "glitch.h"\r
 \r
 \r
-\r
-//LED on P1.0\r
-//IO on P5\r
-\r
 //! Initialize registers and all that jazz.\r
 void init(){\r
+  int i;\r
   WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer\r
   \r
   //LED out and on.\r
   PLEDDIR |= PLEDPIN;\r
-  PLEDOUT |= PLEDPIN;\r
+  PLEDOUT &= ~PLEDPIN;\r
   \r
   //Setup clocks, unique to each '430.\r
   msp430_init_dco();\r
   msp430_init_uart();\r
   \r
+  //DAC should be at full voltage if it exists.\r
+#ifdef DAC12IR\r
+  //glitchvoltages(0xfff,0xfff);\r
+  ADC12CTL0 = REF2_5V + REFON;                  // Internal 2.5V ref on\r
+  for(i=0;i!=0xFFFF;i++) asm("nop");\r
+  DAC12_0CTL = DAC12IR + DAC12AMP_5 + DAC12ENC; // Int ref gain 1\r
+  DAC12_0DAT = 0xFFF; //Max voltage 0xfff\r
+  DAC12_1CTL = DAC12IR + DAC12AMP_5 + DAC12ENC; // Int ref gain 1\r
+  DAC12_1DAT = 0x000; //Min voltage 0x000\r
+#endif\r
+  \r
+  /** FIXME\r
+      \r
+      This part is really ugly.  GSEL (P5.7) must be high to select\r
+      normal voltage, but a lot of applications light to swing it low\r
+      to be a nuissance.  To get around this, we assume that anyone\r
+      with a glitching FET will also have a DAC, then we set that DAC\r
+      to a high voltage.\r
+      \r
+      At some point, each target must be sanitized to show that it\r
+      doesn't clear P5OUT or P5DIR.\r
+  */\r
+  P5DIR|=BIT7; P5OUT=BIT7; //Normal Supply\r
+  //P5DIR&=~BIT7; //Glitch Supply\r
+  \r
   //Enable Interrupts.\r
   //eint();\r
 }\r
 \r
+\r
 //! 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
+  P1OUT&=~1;\r
   switch(app){\r
+  case GLITCH:\r
+    glitchhandle(app,verb,len);\r
+    break;\r
   case MONITOR:\r
     monitorhandle(app,verb,len);\r
     break;\r
   case SPI:\r
     spihandle(app,verb,len);\r
     break;\r
+  case AVR:\r
+    avrhandle(app,verb,len);\r
+    break;\r
   case I2CAPP:\r
     i2chandle(app,verb,len);\r
     break;\r
@@ -51,15 +84,20 @@ void handle(unsigned char app,
   case JTAG:\r
     jtaghandle(app,verb,len);\r
     break;\r
+  case EJTAG:\r
+    ejtaghandle(app,verb,len);\r
+    break;\r
   case JTAG430: //Also JTAG430X, JTAG430X2\r
     jtag430x2handle(app,verb,len);\r
     break;\r
   default:\r
-    #ifdef HANDLEOTHER\r
-    HANDLEOTHER(app,verb,len);\r
-    #else\r
-    txdata(app,NOK,0);\r
-    #endif\r
+    if(pluginhandle){\r
+      pluginhandle(app,verb,len);\r
+    }else{\r
+      debugstr("Plugin missing.");\r
+      debughex(app);\r
+      txdata(app,NOK,0);\r
+    }\r
     break;\r
   }\r
 }\r
@@ -68,24 +106,58 @@ 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
+  // MSP reboot count for reset input & reboot function located at 0xFFFE\r
+  volatile unsigned int reset_count = 0;\r
+  void (*reboot_function)(void) = (void *) 0xFFFE;\r
   \r
   init();\r
-  \r
+\r
   txstring(MONITOR,OK,"http://goodfet.sf.net/");\r
   \r
+  \r
   //Command loop.  There's no end!\r
   while(1){\r
     //Magic 3\r
     app=serial_rx();\r
+\r
+       // If the app is the reset byte (0x80) increment and loop\r
+       if (app == 0x80) {\r
+               reset_count++;\r
+\r
+               if (reset_count > 4) {\r
+                       // We could trigger the WDT with either:\r
+                       // WDTCTL = 0;\r
+                       // or\r
+                       // WDTCTL = WDTPW + WDTCNTCL + WDTSSEL + 0x00;\r
+                       // but instead we'll jump to our reboot function pointer\r
+                       (*reboot_function)();\r
+               }\r
+\r
+               continue;\r
+       } else {\r
+               reset_count = 0;\r
+       }\r
+\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