BPv4 support

OpenOCD JTAG debugging with the Bus Pirate.

BPv4 support

Postby notch » Thu May 03, 2012 5:37 am

Hi, I started work on adding OpenOCD support for BPv4. I'm quite new to microcontrollers, so probably there are some bugs. My aim is to eventualy add support for urjatg and tjtag using this protocol. As I don't have any hardware compatible with OpenOCD I didn't test if my changes are compatible with it. For now I'm just trying to make it work, beauty will come later (I hope;).

My simple approach is to get all the data before running binOpenOCDTapShiftFast, rather that using interrupts inside this function. It will be slower, but not so complicated (also a lot of stuff is removed from inside the function). Maybe in the future there will be a version using USB interrupts.

The other problem I got into was inability to use #ifdef BUSPIRATEV4 inside the OpenOCD_asm.S so my version only supports BPv4. Maybe someone know how to fix it? Do we need two separate asm files for different hardware?

And last thing: to setup open drain it is using something like:
Code: Select all
OOCD_TMS_ODC=1
but the OOCD_TMS_ODC is pointing to BP_CS which is PORTbits and not ODCbits. Is it on purpose or is it a bug?

Testing and comments greatly appreciated


code follows:
Code: Select all
Index: Bus_Pirate/Firmware/hardwarev4a.h
===================================================================
--- Bus_Pirate/Firmware/hardwarev4a.h   (revision 1857)
+++ Bus_Pirate/Firmware/hardwarev4a.h   (working copy)
@@ -135,6 +135,11 @@
#define BP_CS_ODC              ODCDbits.ODD4
#define BP_AUX_ODC             ODCDbits.ODD5

+//new in v4
+#define BP_AUX1_ODC            ODCDbits.ODCD8
+#define BP_AUX2_ODC            ODCDbits.ODCD0
+
+
//Change notice assignment
#define BP_MOSI_CN             CNEN4bits.CN50IE
#define BP_CLK_CN              CNEN4bits.CN51IE

Code: Select all
Index: Bus_Pirate/Firmware/binIO.c
===================================================================
--- Bus_Pirate/Firmware/binIO.c (revision 1857)
+++ Bus_Pirate/Firmware/binIO.c (working copy)
@@ -137,9 +137,7 @@
                 binBBversion(); //say name on return
             } else if (inByte == 6) {//goto OpenOCD mode
                 binReset();
-#ifndef BUSPIRATEV4
                 binOpenOCD();
-#endif
                 binReset();
                 binBBversion(); //say name on return
             } else if (inByte == 7) {//goto pic mode

Code: Select all
Index: Bus_Pirate/Firmware/baseIO.c
===================================================================
--- Bus_Pirate/Firmware/baseIO.c        (revision 1857)
+++ Bus_Pirate/Firmware/baseIO.c        (working copy)
@@ -531,6 +531,7 @@
void UARTbufFlush(void) {
     WaitInReady();
     SendCDC_In_ArmNext(CDC_In_count);
+    CDC_In_count = 0;
     fcnt = 0;
}


Code: Select all
Index: Bus_Pirate/Firmware/OpenOCD_asm.S
===================================================================
--- Bus_Pirate/Firmware/OpenOCD_asm.S   (revision 1857)
+++ Bus_Pirate/Firmware/OpenOCD_asm.S   (working copy)
@@ -14,8 +14,8 @@
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;

-   .equ __24FJ64GA002, 1
-   .include "p24FJ64GA002.inc"
+   .equ __24FJ256GB106, 1
+   .include "p24FJ256GB106.inc"



@@ -27,13 +27,12 @@
;  Do we support delay ?
#define HAS_DELAY   1

-;  Bus pirate v3 hardware
-#define IOPOR      PORTB
+#define IOPOR           PORTD
+#define BP_MOSI_BIT   RD1
+#define BP_CLK_BIT   RD2
+#define BP_MISO_BIT   RD3
+#define BP_CS_BIT   RD4

-#define BP_MOSI_BIT   RB9
-#define BP_CLK_BIT   RB8
-#define BP_MISO_BIT   RB7
-#define BP_CS_BIT   RB6

;  Pin usage by OpenOCD
#define OOCD_TDO_BIT   BP_MISO_BIT
@@ -103,13 +102,13 @@
__loop_word:               ; do {

      ;   Wait until there are enough bytes RXed
-      mov.w   _UART1RXToRecv, w0   ;   w0 = UART1RXToRecv;
-      add.w   w5, #4, w5      ;   w5 += 4;
-      cpsgt   w5, w0         ;   w0 = min(w0, w5)
-      mov.w   w5, w0
+      ;mov.w   _UART1RXToRecv, w0   ;   w0 = UART1RXToRecv;
+      ;add.w   w5, #4, w5      ;   w5 += 4;
+      ;cpsgt   w5, w0         ;   w0 = min(w0, w5)
+      ;mov.w   w5, w0
1:
-      cp   _UART1RXRecvd      ;   while (_UART1RXRecvd < w0);
-      bra   lt, 1b
+      ;cp   _UART1RXRecvd      ;   while (_UART1RXRecvd < w0);
+      ;bra   lt, 1b

      ;   Fetch and organize 2x16 bits
      mov.w   [w7++], w2      ;   w2 = *((uint16_t*)w7++);
@@ -181,25 +180,25 @@
      mov.w   w4, [w8++]      ;   *((uint16_t*)w8++) = w4;

      ;   Increment available bytes
-      lsr.w   w0, #3, w0      ;   w0 = w0 >> 3;
-      inc.w   w0, w0         ;   w0 = w0 + 1;
-      add   _UART1TXAvailable   ;   UART1TXAvailable += w0;
+      ;lsr.w   w0, #3, w0      ;   w0 = w0 >> 3;
+      ;inc.w   w0, w0         ;   w0 = w0 + 1;
+      ;add   _UART1TXAvailable   ;   UART1TXAvailable += w0;

      ;   Trigger TX if needed
      ;   (we're in the loop, so use optimized version, also we can't
      ;   loose register content -> no function call)
-      btst   IEC0, #U1TXIE
-      bra   z, 1f
+      ;btst   IEC0, #U1TXIE
+      ;bra   z, 1f

-      mov   _UART1TXSent, w0
-      cp   _UART1TXAvailable
-      bra   z, 1f
+      ;mov   _UART1TXSent, w0
+      ;cp   _UART1TXAvailable
+      ;bra   z, 1f

-      bclr   IFS0, #U1TXIF
-      bset   IEC0, #U1TXIE
-      add   _UART1TXBuf, wreg
-      mov   [w0], w0
-      mov   w0, U1TXREG
+      ;bclr   IFS0, #U1TXIF
+      ;bset   IEC0, #U1TXIE
+      ;add   _UART1TXBuf, wreg
+      ;mov   [w0], w0
+      ;mov   w0, U1TXREG
1:

      ;   Outer (word) loop branch condition
@@ -214,7 +213,7 @@
      pop.d   w8         ; restore w8,w9

      ; If there was anything left
-      goto   _UART1TXInt
+      ;goto   _UART1TXInt

      ; (we used a goto, no need for return :)
-
+                return


Code: Select all
Index: Bus_Pirate/Firmware/OpenOCD.c
===================================================================
--- Bus_Pirate/Firmware/OpenOCD.c   (revision 1857)
+++ Bus_Pirate/Firmware/OpenOCD.c   (working copy)
@@ -1,5 +1,6 @@
#include "base.h"
#include "baseIO.h"
+#include "cdc.h"
#include "busPirateCore.h"
#include "binIOhelpers.h"

@@ -13,6 +14,8 @@
#define OOCD_SRST_TRIS  BP_AUX_DIR
#if defined (BUSPIRATEV3)
#define OOCD_TRST_TRIS  BP_PGD_DIR
+#elif defined (BUSPIRATEV4)
+#define OOCD_TRST_TRIS  BP_AUX2_DIR
#endif

// io ports
@@ -23,6 +26,8 @@
#define OOCD_SRST       BP_AUX
#if defined (BUSPIRATEV3)
#define OOCD_TRST       BP_PGD
+#elif defined (BUSPIRATEV4)
+#define OOCD_TRST       BP_AUX2
#endif

// open-drain control
@@ -33,6 +38,8 @@
#define OOCD_SRST_ODC   BP_AUX
#if defined (BUSPIRATEV3)
#define OOCD_TRST_ODC   BP_PGD
+#elif defined (BUSPIRATEV4)
+#define OOCD_TRST_ODC   BP_AUX2 // Notch FIXME: shouldn't it be BP_AUX2_OCD ??
#endif

#define CMD_UNKNOWN       0x00
@@ -78,9 +85,11 @@

void binOpenOCD(void){
   unsigned char *buf = bpConfig.terminalInput; // for simplicity :)
+        unsigned char *buf2 = buf + 1024;
   unsigned int i,j;
   unsigned char inByte;
   unsigned char inByte2;
+        unsigned int len,count;

   OpenOCDJtagDelay = 1;

@@ -184,7 +193,9 @@
            inByte=UART1RX();
            inByte2=UART1RX();

+#ifndef BUSPIRATEV4
            IFS0bits.U1RXIF = 0; // reset the RX flag
+#endif

            j = (inByte << 8) | inByte2; // number of bit sequences

@@ -193,11 +204,39 @@
               j = 0x2000;

            i = (j+7)/8; // number of bytes used
+
+#if defined (BUSPIRATEV4)
+                                // disable T1 timer interrupt, so baseIO doesn't send our buffers
+                                IEC0bits.T1IE = 0;
            buf[0] = CMD_TAP_SHIFT;
            buf[1] = inByte;
            buf[2] = inByte2;
            binOpenOCDAnswer(buf, 3);
+                                // don't want to use baseIO interface anymore
+                                UARTbufFlush();

+                                // first get all the bytes consumed by baseIO
+                                count = 0;
+                                while (usbbufgetbyte(buf + count) > 0) {
+                                        count++;
+                                }
+                               
+                                while (2*i > count) {
+                                        len = (2*i-count > CDC_BUFFER_SIZE) ? CDC_BUFFER_SIZE : 2*i-count;
+                                        count += getUnsignedCharArrayUsbUart(buf + count, len);
+                                }
+                               
+                                binOpenOCDTapShiftFast(buf, buf2, j, OpenOCDJtagDelay);
+
+                                count = 0;
+                                do {
+                                        len = (i-count > CDC_BUFFER_SIZE) ? CDC_BUFFER_SIZE : i-count;
+                                        count += putUnsignedCharArrayUsbUsart(buf2 + count, len);
+                                } while (i > count);
+
+                                // enable back the T1 timer interrupt
+                                IEC0bits.T1IE = 1;
+#else
            // prepare the interrupt transfer
            UART1RXBuf = (unsigned char*)bpConfig.terminalInput;
            UART1RXToRecv = 2*i;
@@ -209,8 +248,9 @@

            // enable RX interrupt
            IEC0bits.U1RXIE = 1;
-
-            binOpenOCDTapShiftFast(UART1RXBuf, UART1TXBuf, j, OpenOCDJtagDelay);
+                                binOpenOCDTapShiftFast(UART1RXBuf, UART1TXBuf, j, OpenOCDJtagDelay);
+#endif /* BUSPIRATEV4*/
+            
            break;
         default:
            buf[0] = 0x00; // unknown command
@@ -227,7 +267,7 @@
   OOCD_TDI=0;
   OOCD_CLK=0;
   OOCD_SRST=0;
-#if defined (BUSPIRATEV3)
+#if defined (BUSPIRATEV3) || defined (BUSPIRATEV4)
   OOCD_TRST=0;
#endif
   // setup open-drain if necessary
@@ -236,7 +276,7 @@
      OOCD_CLK_ODC=1;
      OOCD_TDI_ODC=1;
      OOCD_SRST_ODC=1;
-#if defined (BUSPIRATEV3)
+#if defined (BUSPIRATEV3) || defined (BUSPIRATEV4)
      OOCD_TRST_ODC=1;
#endif
   } else {
@@ -244,7 +284,7 @@
      OOCD_CLK_ODC=0;
      OOCD_TDI_ODC=0;
      OOCD_SRST_ODC=0;
-#if defined (BUSPIRATEV3)
+#if defined (BUSPIRATEV3) || defined (BUSPIRATEV4)
      OOCD_TRST_ODC=0;
#endif
   }
@@ -254,7 +294,7 @@
      OOCD_TDI_TRIS=0;
      OOCD_CLK_TRIS=0;
      OOCD_SRST_TRIS=0;
-#if defined (BUSPIRATEV3)
+#if defined (BUSPIRATEV3) || defined (BUSPIRATEV4)
      OOCD_TRST_TRIS=0;
#endif
      OOCD_TDO_TRIS=1;
@@ -263,7 +303,7 @@
      OOCD_TDI_TRIS=1;
      OOCD_CLK_TRIS=1;
      OOCD_SRST_TRIS=1;
-#if defined (BUSPIRATEV3)
+#if defined (BUSPIRATEV3) || defined (BUSPIRATEV4)
      OOCD_TRST_TRIS=1;
#endif
      OOCD_TDO_TRIS=1;
@@ -280,8 +320,16 @@
static void binOpenOCDHandleFeature(unsigned char feat, unsigned char action) {
   switch (feat) {
      case FEATURE_LED:
+#if defined (BUSPIRATEV4)
+                        if (action) {
+                                BP_USBLED_ON();
+                        } else {
+                                BP_USBLED_OFF();
+                        }
+#else
         BP_LEDMODE_DIR=0; //LED to output
         BP_LEDMODE=action;
+#endif
         break;
      case FEATURE_VREG:
         if (action) {
@@ -291,7 +339,7 @@
         }
         break;
      case FEATURE_PULLUP:
-#if defined (BUSPIRATEV3)
+#if defined (BUSPIRATEV3) || defined (BUSPIRATEV4)
         if (action) {
            BP_PULLUP_ON();
         } else {
@@ -299,7 +347,7 @@
         }
#endif
         break;
-#if defined (BUSPIRATEV3)
+#if defined (BUSPIRATEV3) || defined (BUSPIRATEV4)
      case FEATURE_TRST:
         OOCD_TRST=action;
         break;
notch
Newbie
Newbie
 
Posts: 11
Joined: Thu May 03, 2012 2:33 am

Re: BPv4 support

Postby robots » Thu May 03, 2012 3:42 pm

Openocd patch is already in works. I just didn't have enough time to test it properly :).

I am having trouble with output pins, as the TDI, TMS or TCK are missbehaving.

I have taken different approach. Receive the data as they come, and process them. If the buffer gets full, usb will stall host, so no lost data. (Probably a bit faster approach than buffering all, and transmitting.)

You also need to know that adding new functionality doesn't mean commenting out BPv3 functionality.

I have also extended the "Features" of the protocol (This is not yet patched in the OpenOCD):
- support for pullup source
- more ADC measurements (VUSB is added for BPv4)

I was aiming for a working C code, and then rewriting it into assembly.
Attachments
openocd-bpv4.txt
patch
(7.93 KiB) Downloaded 481 times
User avatar
robots
Hero Member
Hero Member
 
Posts: 569
Joined: Sat Dec 05, 2009 11:19 am
Location: CZ/Prague

Re: BPv4 support

Postby notch » Thu May 03, 2012 6:47 pm

I suggest using BP_AUX2 instead of BP_PGD for TRST signal, as now there is enough pins on the IO port.

Also could you elaborate on ODC bits definition? For me it looks like setting BP to MODE_JTAG_OD will send bunch of '1' straight to to IO port, rather than changing their ODC settings. Is it intentional? Am I missing something here?
notch
Newbie
Newbie
 
Posts: 11
Joined: Thu May 03, 2012 2:33 am

Re: BPv4 support

Postby robots » Fri May 04, 2012 1:35 am

It seems that all the OOCD_*_OCD defines should have suffix *_ODC, like BP_CS_ODC, but I wonder where that suffix got lost :-) (it used to work some time ago)

I suggest using BP_AUX2 instead of BP_PGD for TRST signal, as now there is enough pins on the IO port.


Sure. I was just lazy :-)
User avatar
robots
Hero Member
Hero Member
 
Posts: 569
Joined: Sat Dec 05, 2009 11:19 am
Location: CZ/Prague

Re: BPv4 support

Postby ginpb » Sun May 12, 2013 8:57 am

Hi robots, I wanted to ask you if there is any news on the openocd support for the bpv4?.
Thanks.
ginpb
Sr. Member
Sr. Member
 
Posts: 282
Joined: Tue Sep 06, 2011 8:16 am

Re: BPv4 support

Postby DavidKarlas » Tue Jan 07, 2014 3:54 pm

I just started hacking with ARM devices and I would like to buy Bus Pirate v4 primarilly for OpenODC JTAG. So I'm wondering does either of this patches work?

Thank you in advance,
David
DavidKarlas
Newbie
Newbie
 
Posts: 1
Joined: Tue Jan 07, 2014 3:27 pm

Re: BPv4 support

Postby turboscrew » Thu Jan 23, 2014 4:47 pm

Same here (as David).
Also interested.

BTW, what is that pull-up supply voltage?
Is it (roughly) the jtag signal voltage?
Is it (usually) taken from the target (reference voltage?) in case of "external"?
turboscrew
Newbie
Newbie
 
Posts: 8
Joined: Thu Jan 23, 2014 4:26 pm

Re: BPv4 support

Postby ian » Thu Mar 20, 2014 2:31 am

Bus blaster is MUCH better for OpenOCD. Bus Pirate support is not nearly as streamlined.
User avatar
ian
Crew
Crew
 
Posts: 10803
Joined: Mon Jul 06, 2009 6:14 am

Re: BPv4 support

Postby gaguilar » Sat Mar 14, 2015 1:24 pm

I tried they don't work.

I added some patches from one place and another and in the end I got:

Open On-Chip Debugger 0.7.0 (2013-10-22-08:31)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.sourceforge.net/doc/doxygen/bugs.html
Warn : Adapter driver 'buspirate' did not declare which transports it allows; assuming legacy JTAG-only
Info : only one transport option; autoselect 'jtag'
srst_only separate srst_gates_jtag srst_open_drain connect_deassert_srst
Info : Buspirate Interface ready!
Info : This adapter doesn't support configurable speed
Warn : There are no enabled taps. AUTO PROBING MIGHT NOT WORK!!
Error: JTAG scan chain interrogation failed: all zeroes
Error: Check JTAG interface, timings, target power, etc.
Error: Trying to use configured scan chain anyway...
Error: IR capture error at bit 0, saw 0x00 not 0x...3
Warn : Bypassing JTAG setup events due to errors
Warn : gdb services need one or more targets defined

So I suppose it's not working. Someone that know what's he doing?
gaguilar
Newbie
Newbie
 
Posts: 7
Joined: Fri Mar 13, 2015 7:39 am


Return to OpenOCD JTAG