Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
phdussud committed Apr 3, 2021
0 parents commit be8f147
Show file tree
Hide file tree
Showing 12 changed files with 1,168 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/

285 changes: 285 additions & 0 deletions cmd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
/*
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 <stdbool.h>
#include <string.h>

#include <pico/stdlib.h>
#include <hardware/clocks.h>
#include <hardware/gpio.h>

#include "jtag.pio.h"
#include "tusb.h"
#include "pio_jtag.h"
#include "cmd.h"


enum CommandIdentifier {
CMD_STOP = 0x00,
CMD_INFO = 0x01,
CMD_FREQ = 0x02,
CMD_XFER = 0x03,
CMD_SETSIG = 0x04,
CMD_GETSIG = 0x05,
CMD_CLK = 0x06,
CMD_SETVOLTAGE = 0x07,
CMD_GOTOBOOTLOADER = 0x08
};

enum CommandModifier {
NO_READ = 0x80,
EXTEND_LENGTH = 0x40
};

enum SignalIdentifier {
SIG_TCK = 1 << 1,
SIG_TDI = 1 << 2,
SIG_TDO = 1 << 3,
SIG_TMS = 1 << 4,
SIG_TRST = 1 << 5,
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();

/**
* @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(pio_jtag_inst_t* jtag, 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(pio_jtag_inst_t* jtag, const uint8_t *commands, bool extend_length, bool no_read, uint8_t* tx_buf);

/**
* @brief Handle CMD_SETSIG command
*
* CMD_SETSIG set the logic state of the JTAG signals.
*
* @param commands Command data
*/
static void cmd_setsig(pio_jtag_inst_t* jtag, 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(pio_jtag_inst_t* jtag);

/**
* @brief Handle CMD_CLK command
*
* CMD_CLK sends clock pulses with specific TMS and TDI state.
*
* @param commands Command data
*/
static void cmd_clk(pio_jtag_inst_t* jtag, const uint8_t *commands);
/**
* @brief Handle CMD_SETVOLTAGE command
*
* CMD_SETVOLTAGE sets the I/O voltage for devices that support this feature.
*
* @param commands Command data
*/
static void cmd_setvoltage(const uint8_t *commands);

/**
* @brief Handle CMD_GOTOBOOTLOADER command
*
* CMD_GOTOBOOTLOADER resets the MCU and enters its bootloader (if installed)
*/
static void cmd_gotobootloader(void);

uint8_t cmd_handle(pio_jtag_inst_t* jtag, uint8_t* rxbuf, uint32_t count, uint8_t* tx_buf) {
uint8_t *commands= (uint8_t*)rxbuf;

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

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

case CMD_XFER:
case CMD_XFER|NO_READ:
case CMD_XFER|EXTEND_LENGTH:
case CMD_XFER|NO_READ|EXTEND_LENGTH:
cmd_xfer(jtag, commands, *commands & EXTEND_LENGTH, *commands & NO_READ, tx_buf);
return !!(*commands & NO_READ);
break;

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

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

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

case CMD_SETVOLTAGE:
cmd_setvoltage(commands);
commands += 1;
break;

case CMD_GOTOBOOTLOADER:
cmd_gotobootloader();
break;

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

commands++;
}

return 1;
}

static void cmd_info() {
char info_string[10] = "DJTAG2\n";

tud_vendor_write((uint8_t*)info_string, 10);
}

static void cmd_freq(pio_jtag_inst_t* jtag, const uint8_t *commands) {
jtag_set_clk_freq(jtag, (commands[1] << 8) | commands[2]);
}

//static uint8_t output_buffer[64];

static void cmd_xfer(pio_jtag_inst_t* jtag, const uint8_t *commands, bool extend_length, bool no_read, uint8_t* tx_buf) {
uint16_t transferred_bits;
uint8_t* output_buffer = 0;

/* Fill the output buffer with zeroes */
if (!no_read) {
output_buffer = tx_buf;
memset(output_buffer, 0, 64);
}

/* This is the number of transfered bits in one transfer command */
transferred_bits = commands[1];
if (extend_length) {
transferred_bits += 256;
// Ensure we don't do over-read
if (transferred_bits > 62*8) {
return;
}
}
jtag_transfer(jtag, transferred_bits, commands+2, output_buffer);

/* Send the transfer response back to host */
if (!no_read) {
tud_vendor_write(output_buffer, (transferred_bits + 7)/8);
}
}



static void cmd_setsig(pio_jtag_inst_t* jtag, 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_clk(jtag,signal_status & SIG_TCK);
}

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

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

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

if (signal_mask & SIG_SRST) {
jtag_set_rst(jtag, signal_status & SIG_SRST);
}
}

static void cmd_getsig(pio_jtag_inst_t* jtag) {
uint8_t signal_status = 0;

if (jtag_get_tdo(jtag)) {
signal_status |= SIG_TDO;
}
tud_vendor_write(&signal_status, 1);
}

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

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

jtag_strobe(jtag, clk_pulses, signals & SIG_TMS, signals & SIG_TDI);
}

static void cmd_setvoltage(const uint8_t *commands) {
(void)commands;
}

static void cmd_gotobootloader(void) {

}
29 changes: 29 additions & 0 deletions cmd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
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.
*/

/**
* @brief Handle a DirtyJTAG command
*
* @param usbd_dev USB device
* @param transfer Received packet
* @return Command needs to send data back to host
*/
uint8_t cmd_handle(pio_jtag_inst_t* jtag, uint8_t* rxbuf, uint32_t count, uint8_t* tx_buf);
62 changes: 62 additions & 0 deletions dirtyJtag.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "pio_jtag.h"
#include "bsp/board.h"
#include "get_serial.h"
#include "tusb.h"
#include "cmd.h"

#define PIN_TDI 16
#define PIN_TDO 17
#define PIN_TCK 18
#define PIN_TMS 19
#define PIN_RST 20
#define PIN_TRST 21
#define PIN_SYNC 22

void init_pins()
{
gpio_init(PIN_SYNC);
gpio_set_dir(PIN_SYNC, GPIO_OUT);
}

pio_jtag_inst_t jtag = {
.pio = pio0,
.sm = 0
};

void djtag_init()
{
init_pins();
init_jtag(&jtag, 1000, PIN_TCK, PIN_TDI, PIN_TDO, PIN_TMS, PIN_RST, PIN_TRST);
}

static uint8_t rx_buf[64];
static uint8_t tx_buf[64];

void jtag_task(pio_jtag_inst_t* jtag)
{
if ( tud_vendor_available() ) {
uint count = tud_vendor_read(rx_buf, 64);
if (count == 0) {
return;
}
cmd_handle(jtag, rx_buf, count, tx_buf);
}

}


int main()
{
// stdio_init_all();
// usb_serial_init();
board_init();
tusb_init();
djtag_init();
while (1) {
tud_task(); // tinyusb device task
jtag_task(&jtag);
}
}
Loading

0 comments on commit be8f147

Please sign in to comment.