starting OpenOCD app and client changes for JTAG work that didn't go in earlier.
[goodfet] / firmware / apps / jtag / openocd.c
index 6a0e4c1..feb8e57 100644 (file)
@@ -6,6 +6,8 @@
 
 #include "platform.h"
 #include "command.h"
 
 #include "platform.h"
 #include "command.h"
+#include "openocd.h"
+#include "jtag.h"
 
 #define OPENOCD_APP
 
 
 #define OPENOCD_APP
 
@@ -27,9 +29,115 @@ app_t const openocd_app = {
        "OpenOCD",
 
        /* desc */
        "OpenOCD",
 
        /* desc */
-       "\tThe OpenOCD app handles the OpenOCD protocol.\n"
+       "\tThe OpenOCD app handles the OpenOCD bitbang protocol.\n"
 };
 
 };
 
+//! Clock the JTAG clock line
+static void openocd_tcktock() 
+{
+       CLRTCK; 
+       SETTCK; 
+}
+
+//! reset the cpu
+static void openocd_reset_cpu(void)
+{
+       SETRST;
+       msdelay(100);
+       CLRRST;
+       msdelay(100);
+       SETRST;
+       msdelay(100);
+}
+
+//! reset the tap logic
+static void openocd_reset_test_logic(void)
+{
+       CLRMOSI;
+       SETTMS;
+       openocd_tcktock();
+       openocd_tcktock();
+       openocd_tcktock();
+       openocd_tcktock();
+       openocd_tcktock(); // now in reset-test-logic
+       CLRTMS;
+       openocd_tcktock();  // now in run-test-idle state
+}
+
+//! sets the LED value
+void openocd_led(int led)
+{
+       if (led)
+               /* turn the LED on */
+               PLEDDIR |= PLEDPIN;
+       else
+               /* turn the LED off */
+               PLEDOUT &= ~PLEDPIN;
+}
+
+//! resets the device/JTAG logic
+void openocd_reset(int trst, int srst)
+{
+       if(srst && trst)
+       {
+               // we need to drive TST from low to high at
+               // the same time as the RST
+               SETTST;
+               SETRST;
+               msdelay(100);
+               CLRTST;
+               CLRRST;
+               msdelay(100);
+               SETTST;
+               SETRST;
+               msdelay(100);
+       }
+       else if (!srst && trst)
+       {
+               openocd_reset_test_logic();
+       }
+       else if(srst && !trst)
+       {
+               openocd_reset_cpu();
+       }
+}
+
+//! updates the tck, tms, and tdi values
+void openocd_write(int tck, int tms, int tdi)
+{
+       if(tms)
+               SETTMS;
+       else
+               CLRTMS;
+
+       if(tdi)
+               SETMOSI;
+       else
+               CLRMOSI;
+
+       if(tck)
+               SETTCK;
+       else
+               CLRTCK;
+}
+
+//! Stop JTAG, release pins
+void openocd_stop()
+{
+       P5OUT=0;
+       P4OUT=0;
+}
+
+//! Set up the pins for JTAG mode.
+void openocd_setup()
+{
+       P5DIR|=MOSI+SCK+TMS;
+       P5DIR&=~MISO;
+       P4DIR|=TST;
+       P2DIR|=RST;
+       msdelay(100);
+}
+
 //! handles OpenOCD commands
 void openocd_handle_fn(uint8_t const app,
                                           uint8_t const verb,
 //! handles OpenOCD commands
 void openocd_handle_fn(uint8_t const app,
                                           uint8_t const verb,
@@ -38,15 +146,38 @@ void openocd_handle_fn(uint8_t const app,
        switch(verb)
        {
                case START:
        switch(verb)
        {
                case START:
-                       txdata(app,verb,0);
+                       /* do nothing...*/
+                       txdata(app,OK,0);
                        break;
 
                case STOP:
                        break;
 
                case STOP:
-                       txdata(app,verb,0);
+                       openocd_stop();
+                       txdata(app,OK,0);
                        break;
 
                case SETUP:
                        break;
 
                case SETUP:
-                       txdata(app,verb,0);
+                       openocd_setup();
+                       txdata(app,OK,0);
+                       break;
+
+               case OPENOCD_RESET:
+                       openocd_reset(cmddata[0], cmddata[1]);
+                       txdata(app,OK,0);
+                       break;
+
+               case OPENOCD_READ:
+                       cmddata[0] = READMISO;
+                       txdata(app,OK,1);
+                       break;
+
+               case OPENOCD_WRITE:
+                       openocd_write(cmddata[0], cmddata[1], cmddata[2]);
+                       txdata(app,OK,0);
+                       break;
+
+               case OPENOCD_LED:
+                       openocd_led(cmddata[0]);
+                       txdata(app,OK,0);
                        break;
 
                default:
                        break;
 
                default: