Skip to content

Commit

Permalink
Add TDO readout functionnality in CMD_CLK (dirtyjtag#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanthom committed Dec 13, 2022
1 parent bef4f6e commit b69bb88
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
26 changes: 19 additions & 7 deletions src/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ enum CommandIdentifier {
};

enum CommandModifier {
// CMD_XFER
NO_READ = 0x80,
EXTEND_LENGTH = 0x40
EXTEND_LENGTH = 0x40,
// CMD_CLK
READOUT = 0x80,
};

enum SignalIdentifier {
Expand Down Expand Up @@ -108,10 +111,11 @@ static void cmd_getsig(usbd_device *usbd_dev);
*
* CMD_CLK sends clock pulses with specific TMS and TDI state.
*
* @param usbd_dev USB device
* @param commands Command data
* @param readout Enable TDO readout
*/
static void cmd_clk(const uint8_t *commands);

static void cmd_clk(usbd_device *usbd_dev, const uint8_t *commands, bool readout);

uint8_t cmd_handle(usbd_device *usbd_dev, const usbd_transfer *transfer) {
uint8_t *commands= (uint8_t*)transfer->buffer;
Expand Down Expand Up @@ -143,8 +147,12 @@ uint8_t cmd_handle(usbd_device *usbd_dev, const usbd_transfer *transfer) {
break;

case CMD_CLK:
cmd_clk(commands);
commands += 2;
cmd_clk(usbd_dev, commands, !!(*commands & READOUT));
if (*commands & READOUT) {
return 0;
} else {
commands += 2;
}
break;

default:
Expand Down Expand Up @@ -231,11 +239,15 @@ static void cmd_getsig(usbd_device *usbd_dev) {
usb_send(usbd_dev, &signal_status, 1);
}

static void cmd_clk(const uint8_t *commands) {
static void cmd_clk(usbd_device *usbd_dev, const uint8_t *commands, bool readout) {
uint8_t signals, clk_pulses;

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

jtag_strobe(clk_pulses, signals & SIG_TMS, signals & SIG_TDI);
uint8_t readout_val = jtag_strobe(clk_pulses, signals & SIG_TMS, signals & SIG_TDI);

if (readout) {
usb_send(usbd_dev, &readout_val, 1);
}
}
40 changes: 28 additions & 12 deletions src/jtag.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,27 +341,34 @@ void jtag_transfer_internal(uint16_t length, const uint8_t *in, uint8_t *out) {

}

void jtag_strobe(uint8_t pulses, bool tms, bool tdi) {
/* Set TMS state */
bool jtag_strobe(uint8_t pulses, bool tms, bool tdi) {
bool ret;

// Set initial TDI/TMS state
jtag_set_tms(tms);
jtag_set_tdi(tdi);
if (max_frequency)
{
while (pulses)
{

if (!pulses) {
ret = jtag_get_tdo();
} else if (max_frequency) {
pulses--;
while (pulses) {
pulses--;
//double the writes to slow down the clock rate
GPIO_BSRR(JTAG_PORT_TCK) = JTAG_PIN_TCK;
GPIO_BSRR(JTAG_PORT_TCK) = JTAG_PIN_TCK;
GPIO_BSRR(JTAG_PORT_TCK) = JTAG_PIN_TCK << 16;
GPIO_BSRR(JTAG_PORT_TCK) = JTAG_PIN_TCK << 16;
pulses--;
}
}
else
{
GPIO_BSRR(JTAG_PORT_TCK) = JTAG_PIN_TCK;
GPIO_BSRR(JTAG_PORT_TCK) = JTAG_PIN_TCK;
ret = jtag_get_tdo();
GPIO_BSRR(JTAG_PORT_TCK) = JTAG_PIN_TCK << 16;
GPIO_BSRR(JTAG_PORT_TCK) = JTAG_PIN_TCK << 16;
} else {
pulses--;
timer_set_counter(TIM2,0);
while (pulses)
{
while (pulses) {
TIM_SR(TIM2) = ~TIM_SR_UIF;
GPIO_BSRR(JTAG_PORT_TCK) = JTAG_PIN_TCK;
while (!(TIM_SR(TIM2) & TIM_SR_UIF));
Expand All @@ -370,7 +377,16 @@ void jtag_strobe(uint8_t pulses, bool tms, bool tdi) {
pulses--;
while (!(TIM_SR(TIM2) & TIM_SR_UIF));
}
TIM_SR(TIM2) = ~TIM_SR_UIF;
GPIO_BSRR(JTAG_PORT_TCK) = JTAG_PIN_TCK;
while (!(TIM_SR(TIM2) & TIM_SR_UIF));
TIM_SR(TIM2) = ~TIM_SR_UIF;
ret = jtag_get_tdo();
GPIO_BSRR(JTAG_PORT_TCK) = JTAG_PIN_TCK << 16;
while (!(TIM_SR(TIM2) & TIM_SR_UIF));
}

return ret;
}

#if USE_SPI1
Expand Down
3 changes: 2 additions & 1 deletion src/jtag.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ void jtag_transfer(uint16_t length, const uint8_t *in,
* @param pulses Number of clock pulses
* @param tms Logic state of TMS
* @param tdi Logic state of TDI
* @return Last TDO state
*/
void jtag_strobe(uint8_t pulses, bool tms, bool tdi);
bool jtag_strobe(uint8_t pulses, bool tms, bool tdi);

#define F_CPU 72000000UL
#define HW_stlinkv2 0
Expand Down

0 comments on commit b69bb88

Please sign in to comment.