commit 26d914ab21da0c28445869bacc928dcd948bd90a
parent b490b31fa0f496487c9775cfe7fb882a4b90eabd
Author: Brian Swetland <swetland@frotz.net>
Date: Sun, 28 Sep 2014 21:16:05 -0700
dap: api-ize and tidy up a bit, move test code to its own file
Diffstat:
M | Makefile | | | 12 | ++++++------ |
A | dap-registers.h | | | 75 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | dap-test.c | | | 66 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | dap.c | | | 65 | +++++++++++++++-------------------------------------------------- |
M | dap.h | | | 77 | +++++++++++++++-------------------------------------------------------------- |
5 files changed, 177 insertions(+), 118 deletions(-)
diff --git a/Makefile b/Makefile
@@ -4,17 +4,17 @@ CFLAGS := -Wall -O0 -g
LIBS := -lusb-1.0 -lrt
-all: jtag dap
+all: jtag dap-test
JTAG_OBJS := jtag-mpsse-driver.o jtag-core.o jtag.o
$(JTAG_OBJS): jtag.h jtag-driver.h
jtag: $(JTAG_OBJS)
$(CC) -o jtag $(JTAG_OBJS) $(LIBS)
-DAP_OBJS := dap.o jtag-core.o jtag-mpsse-driver.o
-$(DAP_OBJS): jtag.h jtag-driver.h dap.h
-dap: $(DAP_OBJS)
- $(CC) -o dap $(DAP_OBJS) $(LIBS)
+DAP_OBJS := dap-test.o dap.o jtag-core.o jtag-mpsse-driver.o
+$(DAP_OBJS): jtag.h jtag-driver.h dap.h dap-registers.h
+dap-test: $(DAP_OBJS)
+ $(CC) -o dap-test $(DAP_OBJS) $(LIBS)
clean:
- rm -f *.o jtag dap
+ rm -f *.o jtag dap-test
diff --git a/dap-registers.h b/dap-registers.h
@@ -0,0 +1,75 @@
+
+#ifndef _DAP_REGISTERS_H_
+#define _DAP_REGISTERS_H_
+
+/* ARM DAP Controller (4bit IR) */
+#define DAP_IR_SIZE 4
+
+#define DAP_IR_ABORT 0x08
+#define DAP_IR_DPACC 0x0A
+#define DAP_IR_APACC 0x0B
+#define DAP_IR_IDCODE 0x0E
+#define DAP_IR_BYPASS 0x0F
+
+/* DPACC/APACC DR bits */
+#define XPACC_STATUS(n) ((n) & 0x3)
+#define XPACC_WAIT 0x1
+#define XPACC_OK 0x2
+#define XPACC_RD(a) (0x1 | (((a) >> 1) & 6))
+#define XPACC_WR(a,v) ((((u64)(v)) << 3) | (0x0 | (((a) >> 1) & 6)))
+
+/* DP addresses */
+#define DPACC_RESERVED 0x0
+#define DPACC_CSW 0x4
+#define DPACC_SELECT 0x8
+#define DPACC_RDBUFF 0xC
+
+#define DPCSW_CSYSPWRUPACK (1 << 31)
+#define DPCSW_CSYSPWRUPREQ (1 << 30)
+#define DPCSW_CDBGPWRUPACK (1 << 29)
+#define DPCSW_CDBGPWRUPREQ (1 << 28)
+#define DPCSW_CDBGRSTACK (1 << 27)
+#define DPCSW_CDBGRSTREQ (1 << 26)
+#define DPCSW_TRNCNT(n) (((n) & 0x3FF) << 12)
+#define DPCSW_MASKLANE(n) (((n) & 0xF) << 8) // pushed verify or compare
+#define DPCSW_WDATAERR (1 << 7) // reserved on jtag
+#define DPCSW_READOK (1 << 6) // reserved on jtag
+#define DPCSW_STICKYERR (1 << 5)
+#define DPCSW_STICKYCMP (1 << 4)
+#define DPCSW_TRNMODE_NORMAL (0 << 2)
+#define DPCSW_TRNMODE_PUSH_VRFY (1 << 2)
+#define DPCSW_TRNMODE_PUSH_CMP (2 << 2)
+#define DPCSW_STICKYORUN (1 << 1)
+#define DPCSW_ORUNDETECT (1 << 0)
+
+#define DPSEL_APSEL(n) (((n) & 0xFF) << 24)
+#define DPSEL_APBANKSEL(a) ((a) & 0xF0)
+#define DPSEL_CTRLSEL (1 << 0) // reserved on jtag
+
+/* Reading RDBUFF returns 0, has no side effects */
+/* Can be used to obtain final read result and ack values at end of seq */
+
+/* AP addresses */
+#define APACC_CSW 0x00
+#define APACC_TAR 0x04
+#define APACC_DRW 0x0C
+#define APACC_BD0 0x10
+#define APACC_BD1 0x14
+#define APACC_BD2 0x18
+#define APACC_BD3 0x1C
+#define APACC_CFG 0xF4
+#define APACC_BASE 0xF8
+#define APACC_IDR 0xFC
+
+#define APCSW_DBGSWEN (1 << 31)
+#define APCSW_SPIDEN (1 << 23) // ro
+#define APCSW_TRBUSY (1 << 7) // ro
+#define APCSW_DEVICEEN (1 << 6) // ro
+#define APCSW_INCR_NONE (0 << 4)
+#define APCSW_INCR_SINGLE (1 << 4)
+#define APCSW_INCR_PACKED (2 << 4) // may not be supported
+#define APCSW_SIZE8 (0 << 0) // may not be supported
+#define APCSW_SIZE16 (1 << 0) // may not be supported
+#define APCSW_SIZE32 (2 << 0)
+
+#endif
diff --git a/dap-test.c b/dap-test.c
@@ -0,0 +1,66 @@
+// Copyright 2014 Brian Swetland <swetland@frotz.net>
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "dap.h"
+
+u32 DATA[1024*1024];
+
+int dap_probe(DAP *dap);
+
+int main(int argc, char **argv) {
+ JTAG *jtag;
+ DAP *dap;
+ unsigned n;
+ u32 x;
+
+ if (jtag_mpsse_open(&jtag)) return -1;
+
+ dap = dap_init(jtag, 0x4ba00477);
+ if (dap_attach(dap))
+ return -1;
+
+// dap_dp_rd(dap, DPACC_CSW, &x);
+// printf("DPCSW=%08x\n", x);
+
+ dap_probe(dap);
+
+#if 1
+ for (n = 0; n < 8; n++) {
+ x = 0xefefefef;
+ dap_mem_rd32(dap, 0, 0x00000000 + n*4, &x);
+ printf("%08x: %08x\n", n*4, x);
+ }
+#endif
+
+#if 0
+ for (n = 0; n < 1024*1024; n++) DATA[n] = n;
+ for (n = 0; n < 10; n++)
+ dap_mem_write(dap, 0, 0, DATA, 192*1024);
+#endif
+
+#if 0
+ if (dap_mem_read(dap, 0, 0, DATA, 4096) == 0) {
+ for (n = 0; n < 16; n++) printf("%08x ",DATA[n]);
+ printf("\n");
+ }
+ //dap_mem_wr32(dap, 0, 0x10, 0x805038a1);
+ dap_dp_rd(dap, DPACC_CSW, &x);
+ printf("DPCSW=%08x\n", x);
+#endif
+ return 0;
+}
diff --git a/dap.c b/dap.c
@@ -16,8 +16,8 @@
#include <stdlib.h>
#include <string.h>
-#include "jtag.h"
#include "dap.h"
+#include "dap-registers.h"
#define CSW_ERRORS (DPCSW_STICKYERR | DPCSW_STICKYCMP | DPCSW_STICKYORUN)
#define CSW_ENABLES (DPCSW_CSYSPWRUPREQ | DPCSW_CDBGPWRUPREQ | DPCSW_ORUNDETECT)
@@ -29,10 +29,11 @@ static u64 NOW(void) {
return (((u64) ts.tv_sec) * ((u64)1000000000)) + ((u64) ts.tv_nsec);
}
-typedef struct {
+struct DAP {
JTAG *jtag;
+ u32 device_id;
u32 cached_ir;
-} DAP;
+};
static void q_dap_ir_wr(DAP *dap, u32 ir) {
if (dap->cached_ir != ir) {
@@ -240,11 +241,12 @@ int dap_mem_write(DAP *dap, u32 apnum, u32 addr, void *data, u32 len) {
return 0;
}
-DAP *dap_init(JTAG *jtag) {
+DAP *dap_init(JTAG *jtag, u32 id) {
DAP *dap = malloc(sizeof(DAP));
memset(dap, 0, sizeof(DAP));
dap->jtag = jtag;
dap->cached_ir = 0xFFFFFFFF;
+ dap->device_id = id;
return dap;
}
@@ -252,6 +254,15 @@ int dap_attach(DAP *dap) {
unsigned n;
u32 x;
+ if (jtag_enumerate(dap->jtag) < 0) {
+ fprintf(stderr, "dap: jtag enumeration failed\n");
+ return -1;
+ }
+ if (jtag_select_device(dap->jtag, dap->device_id)) {
+ fprintf(stderr, "dap: cannot find device on chain\n");
+ return -1;
+ }
+
// make sure we abort any ongoing transactions first
q_dap_abort(dap);
@@ -346,49 +357,3 @@ int dap_probe(DAP *dap) {
return 0;
}
-u32 DATA[1024*1024];
-
-int main(int argc, char **argv) {
- JTAG *jtag;
- DAP *dap;
- unsigned n;
- u32 x;
-
- if (jtag_mpsse_open(&jtag)) return -1;
- if (jtag_enumerate(jtag) < 0) return -1;
- if (jtag_select_device(jtag, 0x4ba00477)) return -1;
-
- dap = dap_init(jtag);
- if (dap_attach(dap))
- return -1;
-
- dap_dp_rd(dap, DPACC_CSW, &x);
- printf("DPCSW=%08x\n", x);
-
- dap_probe(dap);
-
-#if 1
- for (n = 0; n < 8; n++) {
- x = 0xefefefef;
- dap_mem_rd32(dap, 0, 0x00000000 + n*4, &x);
- printf("%08x: %08x\n", n*4, x);
- }
-#endif
-
-#if 0
- for (n = 0; n < 1024*1024; n++) DATA[n] = n;
- for (n = 0; n < 10; n++)
- dap_mem_write(dap, 0, 0, DATA, 192*1024);
-#endif
-
-#if 0
- if (dap_mem_read(dap, 0, 0, DATA, 4096) == 0) {
- for (n = 0; n < 16; n++) printf("%08x ",DATA[n]);
- printf("\n");
- }
- //dap_mem_wr32(dap, 0, 0x10, 0x805038a1);
- dap_dp_rd(dap, DPACC_CSW, &x);
- printf("DPCSW=%08x\n", x);
-#endif
- return 0;
-}
diff --git a/dap.h b/dap.h
@@ -1,71 +1,24 @@
-/* ARM DAP Controller (4bit IR) */
-#define DAP_IR_SIZE 4
+#ifndef _DAP_H_
+#define _DAP_H_
-#define DAP_IR_ABORT 0x08
-#define DAP_IR_DPACC 0x0A
-#define DAP_IR_APACC 0x0B
-#define DAP_IR_IDCODE 0x0E
-#define DAP_IR_BYPASS 0x0F
+#include "jtag.h"
-/* DPACC/APACC DR bits */
-#define XPACC_STATUS(n) ((n) & 0x3)
-#define XPACC_WAIT 0x1
-#define XPACC_OK 0x2
-#define XPACC_RD(a) (0x1 | (((a) >> 1) & 6))
-#define XPACC_WR(a,v) ((((u64)(v)) << 3) | (0x0 | (((a) >> 1) & 6)))
+typedef struct DAP DAP;
-/* DP addresses */
-#define DPACC_RESERVED 0x0
-#define DPACC_CSW 0x4
-#define DPACC_SELECT 0x8
-#define DPACC_RDBUFF 0xC
+// single word io -- must be 32bit aligned
+int dap_mem_wr32(DAP *dap, u32 n, u32 addr, u32 val);
+int dap_mem_rd32(DAP *dap, u32 n, u32 addr, u32 *val);
-#define DPCSW_CSYSPWRUPACK (1 << 31)
-#define DPCSW_CSYSPWRUPREQ (1 << 30)
-#define DPCSW_CDBGPWRUPACK (1 << 29)
-#define DPCSW_CDBGPWRUPREQ (1 << 28)
-#define DPCSW_CDBGRSTACK (1 << 27)
-#define DPCSW_CDBGRSTREQ (1 << 26)
-#define DPCSW_TRNCNT(n) (((n) & 0x3FF) << 12)
-#define DPCSW_MASKLANE(n) (((n) & 0xF) << 8) // pushed verify or compare
-#define DPCSW_WDATAERR (1 << 7) // reserved on jtag
-#define DPCSW_READOK (1 << 6) // reserved on jtag
-#define DPCSW_STICKYERR (1 << 5)
-#define DPCSW_STICKYCMP (1 << 4)
-#define DPCSW_TRNMODE_NORMAL (0 << 2)
-#define DPCSW_TRNMODE_PUSH_VRFY (1 << 2)
-#define DPCSW_TRNMODE_PUSH_CMP (2 << 2)
-#define DPCSW_STICKYORUN (1 << 1)
-#define DPCSW_ORUNDETECT (1 << 0)
+// multi-word io -- must be 32bit aligned
+// len in bytes, must be 32bit aligned
+int dap_mem_read(DAP *dap, u32 apnum, u32 addr, void *data, u32 len);
+int dap_mem_write(DAP *dap, u32 apnum, u32 addr, void *data, u32 len);
-#define DPSEL_APSEL(n) (((n) & 0xFF) << 24)
-#define DPSEL_APBANKSEL(a) ((a) & 0xF0)
-#define DPSEL_CTRLSEL (1 << 0) // reserved on jtag
+int dap_attach(DAP *dap);
-/* Reading RDBUFF returns 0, has no side effects */
-/* Can be used to obtain final read result and ack values at end of seq */
+DAP *dap_init(JTAG *jtag, u32 jtag_device_id);
-/* AP addresses */
-#define APACC_CSW 0x00
-#define APACC_TAR 0x04
-#define APACC_DRW 0x0C
-#define APACC_BD0 0x10
-#define APACC_BD1 0x14
-#define APACC_BD2 0x18
-#define APACC_BD3 0x1C
-#define APACC_CFG 0xF4
-#define APACC_BASE 0xF8
-#define APACC_IDR 0xFC
-
-#define APCSW_DBGSWEN (1 << 31)
-#define APCSW_SPIDEN (1 << 23) // ro
-#define APCSW_TRBUSY (1 << 7) // ro
-#define APCSW_DEVICEEN (1 << 6) // ro
-#define APCSW_INCR_NONE (0 << 4)
-#define APCSW_INCR_SINGLE (1 << 4)
-#define APCSW_INCR_PACKED (2 << 4) // may not be supported
-#define APCSW_SIZE8 (0 << 0) // may not be supported
-#define APCSW_SIZE16 (1 << 0) // may not be supported
-#define APCSW_SIZE32 (2 << 0)
+int dap_attach(DAP *dap);
+#endif