This repository has been archived by the owner on Apr 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for WinChipHead WCH-Link RISC-V
WCH's RISC-V MCUs, such as CH32V103, have a proprietary 2-wire debug interface. The vendor provided a custom debug adapter called WCH-Link, with a closed-source fork of OpenOCD(violates GPL) embedded in their IDE software called MounRiver(`http://www.mounriver.com/download`). The vendor's OpenOCD communicates with WCH-Link using a minimal protocol, payloading RISC-V DM(Debug Module) commands directly, shadowing the transport layer completely, which also makes the port quite ugly. This is an initial attempt to add support for WCH-Link by reverse-engineering the vendor's OpenOCD. Actual debugging isn't possible yet as I'm currently leaving out some (weird?) initial command sequences. Flash operations aren't supported yet, either.
- Loading branch information
Showing
7 changed files
with
184 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#ifdef HAVE_CONFIG_H | ||
#include "config.h" | ||
#endif | ||
|
||
#include <stdint.h> | ||
|
||
#include <jtag/interface.h> | ||
#include <jtag/commands.h> | ||
|
||
#include "wlink.h" | ||
|
||
#include "libusb_helper.h" | ||
|
||
struct libusb_device_handle *wfd; | ||
|
||
int pReadData(int index, int channel, uint8_t *data, int *size) | ||
{ | ||
int transferred; | ||
|
||
if (channel == 1) | ||
jtag_libusb_bulk_read(wfd, BULK_EP_IN_1, (char *)data, *size, USB_TIMEOUT, &transferred); | ||
else | ||
jtag_libusb_bulk_read(wfd, BULK_EP_IN_2, (char *)data, *size, USB_TIMEOUT, &transferred); | ||
return 1; | ||
} | ||
|
||
int pWriteData(int index, int endpoint, uint8_t *data, int *size) | ||
{ | ||
int transferred; | ||
|
||
jtag_libusb_bulk_write(wfd, endpoint, (char *)data, *size, USB_TIMEOUT, &transferred); | ||
return 1; | ||
} | ||
|
||
static int wlink_init(void) | ||
{ | ||
uint8_t wdata[9] = {0x81, 0xD, 0x1, 0x1}, rdata[9]; | ||
int length; | ||
|
||
const uint16_t vids[] = { VID, 0 }; | ||
const uint16_t pids[] = { PID, 0 }; | ||
|
||
if (jtag_libusb_open(vids, pids, &wfd, NULL) != ERROR_OK) { | ||
LOG_ERROR("Failed to open or find the device"); | ||
goto err; | ||
} | ||
jtag_libusb_set_configuration(wfd, 0); | ||
if (libusb_claim_interface(wfd, 0) != ERROR_OK) { | ||
LOG_ERROR("Failed to claim interface"); | ||
goto err; | ||
} | ||
|
||
length = 4; | ||
pWriteData(0, 1, wdata, &length); | ||
length = 6; | ||
pReadData(0, 1, rdata, &length); | ||
LOG_INFO("WCH-Link version %d.%d", rdata[3], rdata[4]); | ||
|
||
length = 4; | ||
wdata[3] = 0x2; | ||
pWriteData(0, 1, wdata, &length); | ||
length = 6; | ||
pReadData(0, 1, rdata, &length); | ||
if (rdata[0] == 0x81 && rdata[1] == 0x55 && rdata[2] == 1 && rdata[3] == 1) { | ||
LOG_ERROR("Failed to detect chip"); | ||
goto err; | ||
} | ||
|
||
return ERROR_OK; | ||
err: | ||
if (wfd) | ||
jtag_libusb_close(wfd); | ||
return ERROR_FAIL; | ||
} | ||
|
||
static int wlink_execute_queue(void) | ||
{ | ||
return ERROR_OK; | ||
} | ||
|
||
static int wlink_quit(void) | ||
{ | ||
jtag_libusb_close(wfd); | ||
return ERROR_OK; | ||
} | ||
|
||
static const struct command_registration wlink_command_handlers[] = { | ||
//TODO | ||
COMMAND_REGISTRATION_DONE | ||
}; | ||
|
||
static struct jtag_interface wlink_interface = { | ||
.execute_queue = wlink_execute_queue, | ||
}; | ||
|
||
struct adapter_driver wlink_adapter_driver = { | ||
.name = "wlink", | ||
.transports = jtag_only, | ||
.commands = wlink_command_handlers, | ||
|
||
.init = wlink_init, | ||
.quit = wlink_quit, | ||
|
||
.jtag_ops = &wlink_interface, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/*************************************************************************** | ||
* Copyright (C) 2008 Lou Deluxe * | ||
* [email protected] * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
* This program is distributed in the hope that it will be useful, * | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
* GNU General Public License for more details. * | ||
* * | ||
* You should have received a copy of the GNU General Public License * | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. * | ||
***************************************************************************/ | ||
|
||
#ifndef OPENOCD_JTAG_DRIVERS_WLINK_H | ||
#define OPENOCD_JTAG_DRIVERS_WLINK_H | ||
|
||
#define VID 0x1a86 | ||
#define PID 0x8010 | ||
|
||
#define BULK_EP_IN_1 0x81 | ||
#define BULK_EP_IN_2 0x82 | ||
|
||
#define USB_TIMEOUT 3000 | ||
|
||
int pReadData(int index, int channel, uint8_t *data, int *size); | ||
int pWriteData(int index, int endpoint, uint8_t *data, int *size); | ||
|
||
#endif /* OPENOCD_JTAG_DRIVERS_WLINK_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters