Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean THOMAS committed Oct 18, 2017
0 parents commit d1035ac
Show file tree
Hide file tree
Showing 15 changed files with 1,088 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
src/*.bin
src/*.elf
src/*.d
src/*.o
src/*.map
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "unicore-mx"]
path = unicore-mx
url = https://github.com/insane-adding-machines/unicore-mx
61 changes: 61 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
OBJS := src/dirtyjtag.o src/jtag.o src/usb.o src/delay.o src/cmd.o

PREFIX ?= arm-none-eabi
TARGETS := stm32/f1
DEFS += -DSTM32F1
ARCH_FLAGS := -mthumb -mcpu=cortex-m3 -msoft-float -mfix-cortex-m3-ldrd
LD_SCRIPT := dirtyjtag.ld

UCMX_DIR := $(realpath unicore-mx)
UCMX_INCLUDE_DIR := $(UCMX_DIR)/include
UCMX_LIB_DIR := $(UCMX_DIR)/lib

CFLAGS = -g
CFLAGS += -Wall -Wextra -Werror
CFLAGS += -fno-common -ffunction-sections -fdata-sections
CFLAGS += -std=gnu11

CPPFLAGS = -MD -g
CPPFLAGS += -Wall -Wundef
CPPFLAGS += -I$(UCMX_INCLUDE_DIR) $(DEFS)

LDFLAGS += --static -nostartfiles
LDFLAGS += -L"$(UCMX_LIB_DIR)"
LDFLAGS += -T$(LD_SCRIPT)
LDFLAGS += -Wl,-Map=$(*).map
LDFLAGS += -Wl,--gc-sections # Remove deadcode

LDLIBS += -lucmx_stm32f1
LDLIBS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group

CC := $(PREFIX)-gcc
LD := $(PREFIX)-gcc
AR := $(PREFIX)-ar
AS := $(PREFIX)-as
OBJCOPY := $(PREFIX)-objcopy

all: dirtyjtag

clean: dirtyjtag-clean ucmx-clean

dirtyjtag: ucmx src/dirtyjtag.elf src/dirtyjtag.bin

dirtyjtag-clean:
$(Q)$(RM) src/*.d src/*.o src/*.map src/*.bin src/*.elf

ucmx:
$(Q)$(MAKE) -C $(UCMX_DIR)

ucmx-clean:
$(Q)$(MAKE) -C $(UCMX_DIR) clean

%.bin: %.elf
$(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin

%.elf %.map: $(OBJS) $(LD_SCRIPT)
$(Q)$(LD) $(LDFLAGS) $(ARCH_FLAGS) $(OBJS) $(LDLIBS) -o $(*).elf

%.o: %.c
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(ARCH_FLAGS) -o $@ -c $<

.PHONY: clean dirtyjtag dirtyjtag-clean ucmx ucmx-clean
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# DirtyJTAG

DirtyJTAG is a JTAG adapter firmware for $2 STM32 development boards ("blue pill"/"black pill" STM32F103-based ARM boards). The DirtyJTAG project was created to find an alternative to the obsolete (but cheap) LPT Wiggler cables, and expensive USB JTAG probes.

DirtyJTAG is dirty and dirt cheap, but is not fast nor a perfect implementation of the JTAG protocol. Yet it is a bit less than 500 sloccount lines, therefore it is easily understandable and hackable.

DirtyJTAG is currently only supported by urjtag through custom patches (I hope getting mainline support some time soon though).

If you prefer OpenOCD to UrJTAG, I suggest using Zoobab's fork of Versaloon firmware, which is available [on his GitHub repository](https://github.com/zoobab/versaloon).

## How to flash DirtyJTAG on a "Blue Pill" board

Download a pre-built version of the firmware (available [on the release page](https://github.com/jeanthomas/dirtyjtag/releases)) or build the firmware yourself (instructions provided below). Install [stlink](https://github.com/texane/stlink), then use this command :

```
st-flash write /path/to/dirtyjtag.bin 0x8000000
```

## Pinout

| STM32 | JTAG |
|-------|------|
| PA0 | TDI |
| PA1 | TDO |
| PA2 | TCK |
| PA3 | TMS |
| PA4 | TRST |
| PA5 | SRST |

Pin definition can be modified in `src/jtag.c`.

## How to patch urjtag for DirtyJTAG support

See [urjtag4dirtyjtag](https://github.com/jeanthom/urjtag4dirtyjtag) which is a patched version of urjtag with DirtyJTAG support. I'll try to push those changes upstream some time soon.

## DirtyJTAG compilation

In order to compile DirtyJTAG, you will need the following software :

* git
* ARM toolchain (I'm using Fedora's `arm-none-eabi-gcc-cs`/`arm-none-eabi-newline` packages)
* make

Clone this repository :

```
git clone https://github.com/jeanthom/dirtyjtag
cd dirtyjtag
```

Then download unicore-mx :

```
git submodule init
git submodule update
```

Then you can build the firmware :

```
make bin
```

Your freshly compiled firmware will appear as `src/dirtyjtag.bin`.

## Inspiration

* [opendous-jtag](https://github.com/vfonov/opendous-jtag)
* [neroJtag](https://github.com/makestuff/neroJtag)
* [clujtag-avr](https://github.com/ClusterM/clujtag-avr)
7 changes: 7 additions & 0 deletions dirtyjtag.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* Generic linker script for STM32F103RBT6/STM32F103R8T6 */
MEMORY {
rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
}

INCLUDE libucmx_stm32f1.ld
229 changes: 229 additions & 0 deletions src/cmd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/*
Copyright (c) 2017 Jean THOMAS.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <stdint.h>
#include <string.h>
#include <unicore-mx/usbd/usbd.h>
#include <unicore-mx/stm32/gpio.h>

#include "jtag.h"
#include "usb.h"
#include "cmd.h"

#define CMD_STOP 0x00
#define CMD_INFO 0x01
#define CMD_FREQ 0x02
#define CMD_XFER 0x03
#define CMD_SETSIG 0x04
#define CMD_GETSIG 0x05
#define CMD_CLK 0x06

#define SIG_TCK (1 << 1)
#define SIG_TDI (1 << 2)
#define SIG_TDO (1 << 3)
#define SIG_TMS (1 << 4)
#define SIG_TRST (1 << 5)
#define SIG_SRST (1 << 6)

/**
* @brief Handle CMD_INFO command
*
* CMD_INFO returns a string to the host software. This
* could be used to check DirtyJTAG firmware version
* or supported commands. As of now it is implemented
* but not usefull.
*
* @param usbd_dev USB device
*/
static void cmd_info(usbd_device *usbd_dev);

/**
* @brief Handle CMD_FREQ command
*
* CMD_FREQ sets the clock frequency on the probe.
* Currently this does not changes anything.
*
* @param commands Command data
*/
static void cmd_freq(const uint8_t *commands);

/**
* @brief Handle CMD_XFER command
*
* CMD_XFER reads and writes data simultaneously.
*
* @param usbd_dev USB device
* @param commands Command data
*/
static void cmd_xfer(usbd_device *usbd_dev, const uint8_t *commands);

/**
* @brief Handle CMD_SETSIG command
*
* CMD_SETSIG set the logic state of the JTAG signals.
*
* @param commands Command data
*/
static void cmd_setsig(const uint8_t *commands);

/**
* @brief Handle CMD_GETSIG command
*
* CMD_GETSIG gets the current signal state.
*
* @param usbd_dev USB device
*/
static void cmd_getsig(usbd_device *usbd_dev);

/**
* @brief Handle CMD_CLK command
*
* CMD_CLK sends clock pulses with specific TMS and TDI state.
*
* @param commands Command data
*/
static void cmd_clk(const uint8_t *commands);

uint8_t cmd_handle(usbd_device *usbd_dev, const usbd_transfer *transfer) {
uint8_t *commands;

commands = (uint8_t*)transfer->buffer;

while (*commands != CMD_STOP) {
switch (*commands) {
case CMD_INFO:
cmd_info(usbd_dev);
break;

case CMD_FREQ:
cmd_freq(commands);
commands += 2;
break;

case CMD_XFER:
cmd_xfer(usbd_dev, commands);
return 0;
break;

case CMD_SETSIG:
cmd_setsig(commands);
commands += 2;
break;

case CMD_GETSIG:
cmd_getsig(usbd_dev);
return 0;
break;

case CMD_CLK:
cmd_clk(commands);
commands += 2;
break;

default:
return 1; /* Unsupported command, halt */
break;
}

commands++;
}

return 1;
}

static void cmd_info(usbd_device *usbd_dev) {
char info_string[64] = "DJTAG1\n";

usb_send(usbd_dev, (uint8_t*)info_string, 64);
}

static void cmd_freq(const uint8_t *commands) {
jtag_set_frequency((commands[1] << 8) | commands[2]);
}

static void cmd_xfer(usbd_device *usbd_dev, const uint8_t *commands) {
uint8_t transferred_bits;
uint8_t output_buffer[32];

/* Fill the output buffer with zeroes */
memset(output_buffer, 0, 32);

/* This is the number of transfered bits in one transfer command */
transferred_bits = commands[1];

jtag_transfer(transferred_bits, commands+2, output_buffer);

/* Send the transfer response back to host */
usb_send(usbd_dev, output_buffer, 32);
}

static void cmd_setsig(const uint8_t *commands) {
uint8_t signal_mask, signal_status;

signal_mask = commands[1];
signal_status = commands[2];

if (signal_mask & SIG_TCK) {
jtag_set_tck(signal_status & SIG_TCK);
}

if (signal_mask & SIG_TDI) {
jtag_set_tdi(signal_status & SIG_TDI);
}

if (signal_mask & SIG_TMS) {
jtag_set_tms(signal_status & SIG_TMS);
}

if (signal_mask & SIG_TRST) {
jtag_set_trst(signal_status & SIG_TRST);
}

if (signal_mask & SIG_SRST) {
jtag_set_srst(signal_status & SIG_SRST);
}
}

static void cmd_getsig(usbd_device *usbd_dev) {
uint8_t signal_status = 0;

if (jtag_get_tdo()) {
signal_status |= SIG_TDO;
}

usb_send(usbd_dev, &signal_status, 1);
}

static void cmd_clk(const uint8_t *commands) {
uint8_t signals, clk_pulses, i;

signals = commands[1];
clk_pulses = commands[2];

/* Set TDI & TMS signals */
jtag_set_tms(signals & SIG_TMS);
jtag_set_tdi(signals & SIG_TDI);

/* Send clock pulses */
for (i = 0; i < clk_pulses; i++) {
jtag_clock();
}
}
Loading

0 comments on commit d1035ac

Please sign in to comment.