Skip to content

Commit

Permalink
Merge pull request #28 from ZhiyuanYuanNJ/master
Browse files Browse the repository at this point in the history
Add Support CH339
  • Loading branch information
WCHSoftGroup authored Feb 20, 2024
2 parents e4461f8 + c590648 commit 28f72ea
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 57 deletions.
Binary file not shown.
Binary file added CH347FPGATool/OpenOCD_CH347/bin/libusb-1.0.dll
Binary file not shown.
Binary file modified CH347FPGATool/OpenOCD_CH347/bin/openocd.exe
Binary file not shown.
Binary file modified OpenOCD_CH347/bin/openocd.exe
Binary file not shown.
88 changes: 31 additions & 57 deletions OpenOCD_SourceCode_CH347/src/jtag/drivers/ch347.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ typedef struct _CH347_SWD_CONTEXT {
uint8_t *ch347_cmd_buf;
} CH347_SWD_CONTEXT;
static CH347_SWD_CONTEXT ch347_swd_context;
static bool swd_mode;
static bool swd_mode = false;
#pragma pack()

#ifdef _WIN32
Expand Down Expand Up @@ -234,8 +234,8 @@ bool ugOpen;
unsigned long ugIndex;
struct libusb_device_handle *ch347_handle;

static const uint16_t ch347_vids[] = {0x1a86, 0x1a86};
static const uint16_t ch347_pids[] = {0x55dd, 0x55de};
static const uint16_t ch347_vids[] = {0x1a86, 0x1a86, 0x1a86};
static const uint16_t ch347_pids[] = {0x55dd, 0x55de, 0x55e7};

static uint32_t CH347OpenDevice(uint64_t iIndex)
{
Expand Down Expand Up @@ -447,17 +447,16 @@ static void CH347_Read_Scan(UCHAR *pBuffer, uint32_t length)
} else if (read_buf[index] == CH347_CMD_JTAG_BIT_OP_RD) {
dataLen = read_buf[++index] & 0xFF;
dataLen += (read_buf[++index] & 0xFF) << 8;

for (i = 0; i < dataLen; i++) {
if (read_buf[index + 1 + i] & 1)
if (read_buf[index + 1 + i] == 0x01)
*(pBuffer + read_buf_index) |= (1 << i);
else
*(pBuffer + read_buf_index) &= ~(1 << i);
}
read_buf_index += 1;
index += dataLen + 1;
} else {
LOG_ERROR("readbuf read_commend error");
// LOG_ERROR("readbuf read_commend error");
*(pBuffer + read_buf_index) = read_buf[index];
read_buf_index++;
index++;
Expand Down Expand Up @@ -649,58 +648,31 @@ static void CH347_TMS(struct tms_command *cmd)
static int ch347_reset(int trst, int srst)
{
LOG_DEBUG_IO("reset trst: %i srst %i", trst, srst);
#if 1
unsigned char BitBang[512] = "", BII, i;
unsigned long TxLen;

BII = CH347_CMD_HEADER;
for (i = 0; i < 7; i++) {
if (!swd_mode){
BII = CH347_CMD_HEADER;
for (i = 0; i < 7; i++) {
BitBang[BII++] = TMS_H | TDI_L | TCK_L;
BitBang[BII++] = TMS_H | TDI_L | TCK_H;
}
BitBang[BII++] = TMS_H | TDI_L | TCK_L;
BitBang[BII++] = TMS_H | TDI_L | TCK_H;
}
BitBang[BII++] = TMS_H | TDI_L | TCK_L;

ch347.TCK = TCK_L;
ch347.TDI = TDI_L;
ch347.TMS = 0;

BitBang[0] = CH347_CMD_JTAG_BIT_OP;
BitBang[1] = BII - CH347_CMD_HEADER;
BitBang[2] = 0;

TxLen = BII;

if (!CH347_Write(BitBang, &TxLen) && (TxLen != BII)) {
LOG_ERROR("JTAG_Init send usb data failure.");
return false;
}
#else
if (!swd_mode && trst == 0) {

unsigned long int BI = 0;

CH347_In_Buffer(CH347_CMD_JTAG_BIT_OP);
CH347_In_Buffer(0x01);
CH347_In_Buffer(0);

ch347.TRST = 0;
CH347_IdleClock(BI);

CH347_Flush_Buffer();

Sleep(50);
ch347.TCK = TCK_L;
ch347.TDI = TDI_L;
ch347.TMS = 0;

CH347_In_Buffer(CH347_CMD_JTAG_BIT_OP);
CH347_In_Buffer(0x01);
CH347_In_Buffer(0);
BitBang[0] = CH347_CMD_JTAG_BIT_OP;
BitBang[1] = BII - CH347_CMD_HEADER;
BitBang[2] = 0;

ch347.TRST = 1;
CH347_IdleClock(BI);
TxLen = BII;

CH347_Flush_Buffer();
return ERROR_OK;
if (!CH347_Write(BitBang, &TxLen) && (TxLen != BII)) {
LOG_ERROR("JTAG_Init send usb data failure.");
return false;
}
}
#endif
return ERROR_OK;
}

Expand Down Expand Up @@ -781,7 +753,7 @@ static void CH347_WriteRead(struct scan_command *cmd, uint8_t *bits,
unsigned long BI = 0, DI, DII, PktDataLen, DLen = 0, tempIndex,
totalReadLength = 0, tempLength = 0;
if (ch347.pack_size == LARGER_PACK) {
if ((ch347.read_count >= (USBC_PACKET_USBHS_SINGLE * 1)))
if ((ch347.read_count >= (510 * 1)))
CH347_Flush_Buffer();
} else {
CH347_Flush_Buffer();
Expand Down Expand Up @@ -890,14 +862,15 @@ static void CH347_WriteRead(struct scan_command *cmd, uint8_t *bits,
CH347_Flush_Buffer();
CH347_Read_Scan(readData, readLen);
}

for (i = 0; i < cmd->num_fields; i++) {
/* if neither in_value nor in_handler
* are specified we don't have to examine this field
*/
LOG_DEBUG("fields[%i].in_value[%i], offset: %d",
i, cmd->fields[i].num_bits, offset);
num_bits = cmd->fields[i].num_bits;
if (ch347.pack_size == LARGER_PACK)
bit_count += num_bits;
if (cmd->fields[i].in_value) {
if (ch347.pack_size == LARGER_PACK) {
bit_copy_queued(
Expand All @@ -906,7 +879,10 @@ static void CH347_WriteRead(struct scan_command *cmd, uint8_t *bits,
0,
&ch347.read_buffer[ch347.read_idx],
offset, num_bits);
if (num_bits > 7)
ch347.read_idx += DIV_ROUND_UP(bit_count, 8);
} else {
num_bits = cmd->fields[i].num_bits;
uint8_t *captured = buf_set_buf(
readData, bit_count,
malloc(DIV_ROUND_UP(num_bits,
Expand All @@ -930,11 +906,10 @@ static void CH347_WriteRead(struct scan_command *cmd, uint8_t *bits,
}else {
LOG_DEBUG_IO("cmd->fields with no data");
}
bit_count += cmd->fields[i].num_bits;
if (ch347.pack_size == LARGER_PACK) {
if (num_bits > 7)
ch347.read_idx += DIV_ROUND_UP(bit_count, 8);
offset += num_bits;
}else{
bit_count += cmd->fields[i].num_bits;
}
}
}
Expand Down Expand Up @@ -1207,7 +1182,6 @@ static bool CH347Jtag_INIT(uint64_t iIndex, uint8_t iClockRate)
else
return Check_Speed(iIndex, iClockRate - 2);
}

return Check_Speed(iIndex, iClockRate);
}

Expand Down Expand Up @@ -1831,4 +1805,4 @@ struct adapter_driver ch347_adapter_driver = {

.jtag_ops = &ch347_interface,
.swd_ops = &ch347_swd,
};
};

0 comments on commit 28f72ea

Please sign in to comment.