Skip to content

Commit

Permalink
!temporary_commit! hosted: hacky wchlink inplementation
Browse files Browse the repository at this point in the history
  • Loading branch information
perigoso committed Feb 23, 2023
1 parent e5a320a commit 49700af
Show file tree
Hide file tree
Showing 10 changed files with 712 additions and 2 deletions.
45 changes: 45 additions & 0 deletions src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ static bool cmd_help(target_s *t, int argc, const char **argv);

static bool cmd_jtag_scan(target_s *t, int argc, const char **argv);
static bool cmd_swdp_scan(target_s *t, int argc, const char **argv);
static bool cmd_rvswd_scan(target_s *t, int argc, const char **argv);
static bool cmd_auto_scan(target_s *t, int argc, const char **argv);
static bool cmd_frequency(target_s *t, int argc, const char **argv);
static bool cmd_targets(target_s *t, int argc, const char **argv);
Expand Down Expand Up @@ -84,6 +85,7 @@ const command_s cmd_list[] = {
{"help", cmd_help, "Display help for monitor commands"},
{"jtag_scan", cmd_jtag_scan, "Scan JTAG chain for devices"},
{"swdp_scan", cmd_swdp_scan, "Scan SW-DP for devices"},
{"rvswd_scan", cmd_rvswd_scan, "Scan RVSWD for devices"},
{"auto_scan", cmd_auto_scan, "Automatically scan all chain types for devices"},
{"frequency", cmd_frequency, "set minimum high and low times"},
{"targets", cmd_targets, "Display list of available targets"},
Expand Down Expand Up @@ -286,6 +288,49 @@ bool cmd_swdp_scan(target_s *t, int argc, const char **argv)
return true;
}

bool cmd_rvswd_scan(target_s *t, int argc, const char **argv)
{
(void)t;
(void)argc;
(void)argv;

if (platform_target_voltage())
gdb_outf("Target voltage: %s\n", platform_target_voltage());

if (connect_assert_nrst)
platform_nrst_set_val(true); /* will be deasserted after attach */

uint32_t devs = 0;
volatile exception_s e;
TRY_CATCH (e, EXCEPTION_ALL) {
#if PC_HOSTED == 1
devs = platform_rvswd_scan();
#else
devs = 0;
#endif
}
switch (e.type) {
case EXCEPTION_TIMEOUT:
gdb_outf("Timeout during scan.\n");
break;
case EXCEPTION_ERROR:
gdb_outf("Exception: %s\n", e.msg);
break;
}

if (devs == 0) {
platform_target_clk_output_enable(false);
platform_nrst_set_val(false);
gdb_out("RVSWD scan failed!\n");
return false;
}

cmd_targets(NULL, 0, NULL);
platform_target_clk_output_enable(false);
morse(NULL, false);
return true;
}

bool cmd_auto_scan(target_s *t, int argc, const char **argv)
{
(void)t;
Expand Down
1 change: 1 addition & 0 deletions src/include/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ typedef struct target_controller target_controller_s;
#if PC_HOSTED == 1
uint32_t platform_adiv5_swdp_scan(uint32_t targetid);
uint32_t platform_jtag_scan(const uint8_t *lrlens);
uint32_t platform_rvswd_scan();
#endif
uint32_t adiv5_swdp_scan(uint32_t targetid);
uint32_t jtag_scan(const uint8_t *lrlens);
Expand Down
1 change: 1 addition & 0 deletions src/platforms/hosted/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ ifneq ($(HOSTED_BMP_ONLY), 1)
SRC += bmp_libusb.c stlinkv2.c
SRC += ftdi_bmp.c libftdi_swdptap.c libftdi_jtagtap.c
SRC += jlink.c jlink_adiv5_swdp.c jlink_jtagtap.c
SRC += wchlink.c riscv_dtm_wchlink.c
else
SRC += bmp_serial.c
endif
Expand Down
4 changes: 3 additions & 1 deletion src/platforms/hosted/bmp_libusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
void bmp_ident(bmp_info_s *info)
{
PRINT_INFO("Black Magic Debug App %s\n for Black Magic Probe, ST-Link v2 and v3, CMSIS-DAP,"
" JLink and libftdi/MPSSE\n",
" JLink, libftdi/MPSSE and WCH-Link\n",
FIRMWARE_VERSION);
if (info && info->vid && info->pid) {
PRINT_INFO("Using %04x:%04x %s %s\n %s\n", info->vid, info->pid,
Expand Down Expand Up @@ -261,6 +261,8 @@ int find_debuggers(bmda_cli_options_s *cl_opts, bmp_info_s *info)
}
} else if (desc.idVendor == VENDOR_ID_SEGGER)
type = BMP_TYPE_JLINK;
else if (desc.idVendor == VENDOR_ID_WCH)
type = BMP_TYPE_WCHLINK;
else {
const cable_desc_s *cable = cable_desc;
for (; cable->name; ++cable) {
Expand Down
31 changes: 31 additions & 0 deletions src/platforms/hosted/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "stlinkv2.h"
#include "ftdi_bmp.h"
#include "jlink.h"
#include "wchlink.h"
#include "cmsis_dap.h"

bmp_info_s info;
Expand Down Expand Up @@ -121,6 +122,11 @@ void platform_init(int argc, char **argv)
exit(-1);
break;

case BMP_TYPE_WCHLINK:
if (!wchlink_init(&info))
exit(-1);
break;

default:
exit(-1);
}
Expand Down Expand Up @@ -230,6 +236,19 @@ bool platform_jtagtap_init(void)
}
}

uint32_t platform_rvswd_scan()
{
info.is_jtag = false;

switch (info.bmp_type) {
case BMP_TYPE_WCHLINK:
return wchlink_rvswd_scan(&info);

default:
return 0;
}
}

void platform_adiv5_dp_defaults(adiv5_debug_port_s *dp)
{
switch (info.bmp_type) {
Expand Down Expand Up @@ -286,6 +305,9 @@ char *platform_ident(void)
case BMP_TYPE_JLINK:
return "J-Link";

case BMP_TYPE_WCHLINK:
return "WCH-Link";

default:
return NULL;
}
Expand All @@ -306,6 +328,9 @@ const char *platform_target_voltage(void)
case BMP_TYPE_JLINK:
return jlink_target_voltage(&info);

case BMP_TYPE_WCHLINK:
return wchlink_target_voltage(&info);

default:
return NULL;
}
Expand All @@ -329,6 +354,9 @@ void platform_nrst_set_val(bool assert)
case BMP_TYPE_CMSIS_DAP:
return dap_nrst_set_val(assert);

case BMP_TYPE_WCHLINK:
return wchlink_nrst_set_val(&info, assert);

default:
break;
}
Expand All @@ -349,6 +377,9 @@ bool platform_nrst_get_val(void)
case BMP_TYPE_LIBFTDI:
return libftdi_nrst_get_val();

case BMP_TYPE_WCHLINK:
return wchlink_nrst_get_val(&info);

default:
return false;
}
Expand Down
5 changes: 4 additions & 1 deletion src/platforms/hosted/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ void platform_buffer_flush(void);

#define VENDOR_ID_SEGGER 0x1366U

#define VENDOR_ID_WCH 0x1a86U

typedef enum bmp_type_e {
BMP_TYPE_NONE = 0,
BMP_TYPE_BMP,
BMP_TYPE_STLINKV2,
BMP_TYPE_LIBFTDI,
BMP_TYPE_CMSIS_DAP,
BMP_TYPE_JLINK
BMP_TYPE_JLINK,
BMP_TYPE_WCHLINK
} bmp_type_t;

void gdb_ident(char *p, int count);
Expand Down
77 changes: 77 additions & 0 deletions src/platforms/hosted/riscv_dtm_wchlink.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* This file is part of the Black Magic Debug project.
*
* Copyright (C) 2023 1BitSquared <[email protected]>
* Written by Rafael Silva <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "general.h"
#include "bmp_hosted.h"
#include "riscv_debug.h"
#include "target_probe.h"
#include "target.h"
#include "wchlink.h"

void riscv_dtm_wchlink_init(riscv_dtm_s *dtm);

static bool riscv_dtm_wchlink_dmi_read(riscv_dtm_s *const dtm, const uint32_t address, uint32_t *const value)
{
(void)dtm;
return wchlink_riscv_dmi_read(&info, address, value);
}

static bool riscv_dtm_wchlink_dmi_write(riscv_dtm_s *const dtm, const uint32_t address, const uint32_t value)
{
(void)dtm;
return wchlink_riscv_dmi_write(&info, address, value);
}

uint8_t riscv_dtm_wchlink_handler()
{
riscv_dtm_s *dtm = calloc(1, sizeof(*dtm));
if (!dtm) { /* calloc failed: heap exhaustion */
DEBUG_WARN("calloc: failed in %s\n", __func__);
return 0;
}

dtm->dmi_read = riscv_dtm_wchlink_dmi_read;
dtm->dmi_write = riscv_dtm_wchlink_dmi_write;

dtm->version = RISCV_DEBUG_0_13;
// dtm->designer_code = 0xaa55; /* WCHLINK deosn't seem to have a jep106 code assigned */

/* Call higher level code to discover the DMI bus */
riscv_dtm_init(dtm);

/* If we failed to find any DMs or Harts, free the structure */
if (!dtm->ref_count)
free(dtm);

return dtm->ref_count;
}
39 changes: 39 additions & 0 deletions src/platforms/hosted/riscv_dtm_wchlink.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* This file is part of the Black Magic Debug project.
*
* Copyright (C) 2023 1BitSquared <[email protected]>
* Written by Rafael Silva <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef PLATFORM_HOSTED_RISCV_DTM_WCHLINK_H
#define PLATFORM_HOSTED_RISCV_DTM_WCHLINK_H

uint8_t riscv_dtm_wchlink_handler();

#endif /* PLATFORM_HOSTED_RISCV_DTM_WCHLINK_H */
Loading

0 comments on commit 49700af

Please sign in to comment.