Skip to content

feat: 添加JTAG 20P兼容模式 #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions projects/HSLink-Pro/src/HID_COMM/hid_comm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,6 @@ static void settings(Document &root, char *res)
return;
}

auto get_value_bool = [&](const Value &val, const char *key, bool default_value) -> bool
{
if (val.HasMember(key)) {
printf("key: %s, value: %d\n", key, val[key].GetBool());
return val[key].GetBool();
}
return default_value;
};

const Value &data = root["data"].GetObject();

if (!data.HasMember("boost")) {
Expand All @@ -243,7 +234,7 @@ static void settings(Document &root, char *res)
};
HSLink_Setting.swd_port_mode = mode(data["swd_port_mode"].GetString());
HSLink_Setting.jtag_port_mode = mode(data["jtag_port_mode"].GetString());
HSLink_Setting.jtag_single_bit_mode = get_value_bool(data, "jtag_single_bit_mode", false);
HSLink_Setting.jtag_single_bit_mode = get_json_value(data, "jtag_single_bit_mode", false);

const Value &power = data["power"];
HSLink_Setting.power.vref = power["vref"].GetDouble();
Expand All @@ -264,6 +255,8 @@ static void settings(Document &root, char *res)
HSLink_Setting.led = data["led"].GetBool();
HSLink_Setting.led_brightness = data["led_brightness"].GetUint();

HSLink_Setting.jtag_20pin_compatible = get_json_value(data, "jtag_20pin_compatible", false);

Setting_Save();

FillStatus(HID_RESPONSE_SUCCESS, res);
Expand Down Expand Up @@ -385,6 +378,9 @@ static void get_setting(Document &root, char *res)
writer.Key("led_brightness");
writer.Uint(HSLink_Setting.led_brightness);

writer.Key("jtag_20pin_compatible");
writer.Bool(HSLink_Setting.jtag_20pin_compatible);

writer.EndObject();

std::strcpy(res, buffer.GetString());
Expand Down
73 changes: 57 additions & 16 deletions projects/HSLink-Pro/src/USB2UART/usb2uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ static hpm_stat_t board_uart_dma_config(void);
static uint32_t PIN_UART_DTR = 0;
static uint32_t PIN_UART_RTS = 0;

static void dma_channel_tc_callback(DMA_Type *ptr, uint32_t channel, void *user_data) {
static void dma_channel_tc_callback(DMA_Type *ptr, uint32_t channel, void *user_data)
{
(void) ptr;
(void) channel;
(void) user_data;
Expand All @@ -61,7 +62,8 @@ static void dma_channel_tc_callback(DMA_Type *ptr, uint32_t channel, void *user_
}
}

void uart_isr(void) {
void uart_isr(void)
{
uint32_t uart_received_data_count;
uint32_t i;
dma_resource_t *rx_resource = &dma_resource_pools[UART_RX_DMA_RESOURCE_INDEX];
Expand All @@ -86,7 +88,8 @@ void uart_isr(void) {

SDK_DECLARE_EXT_ISR_M(UART_IRQ, uart_isr)

void usb2uart_handler(void) {
void usb2uart_handler(void)
{
dma_resource_t *rx_resource = &dma_resource_pools[UART_RX_DMA_RESOURCE_INDEX];
const uint32_t rx_desc_size = (sizeof(rx_descriptors) / sizeof(dma_linked_descriptor_t));
uint32_t uart_received_data_count =
Expand Down Expand Up @@ -115,30 +118,59 @@ void usb2uart_handler(void) {
}
}

void uartx_preinit(void) {
// board_init_uart(UART_BASE);
void uartx_io_init(void)
{
if (CheckHardwareVersion(1, 2, 0xFF)) {
PIN_UART_DTR = IOC_PAD_PA06;
PIN_UART_RTS = IOC_PAD_PA05;
} else {
PIN_UART_DTR = IOC_PAD_PA06;
PIN_UART_RTS = IOC_PAD_PA07;
}
HPM_IOC->PAD[PIN_UART_RX].FUNC_CTL = IOC_PA09_FUNC_CTL_UART2_RXD;
HPM_IOC->PAD[PIN_UART_TX].FUNC_CTL = IOC_PA08_FUNC_CTL_UART2_TXD;

// 开启20p兼容模式之后,将会把串口设置为GPIO模式,并输出低电平
if (!HSLink_Setting.jtag_20pin_compatible) {
HPM_IOC->PAD[PIN_UART_RX].FUNC_CTL = IOC_PA09_FUNC_CTL_UART2_RXD;
HPM_IOC->PAD[PIN_UART_TX].FUNC_CTL = IOC_PA08_FUNC_CTL_UART2_TXD;

} else {
HPM_IOC->PAD[PIN_UART_RX].FUNC_CTL = IOC_PAD_FUNC_CTL_ALT_SELECT_SET(0);;
HPM_IOC->PAD[PIN_UART_TX].FUNC_CTL = IOC_PAD_FUNC_CTL_ALT_SELECT_SET(0);
gpiom_set_pin_controller(HPM_GPIOM, GPIO_GET_PORT_INDEX(PIN_UART_RX), GPIO_GET_PIN_INDEX(PIN_UART_RX),
gpiom_soc_gpio0);
gpio_set_pin_input(HPM_GPIO0, GPIO_GET_PORT_INDEX(PIN_UART_RX), GPIO_GET_PIN_INDEX(PIN_UART_RX)); // RX 默认输入

gpiom_set_pin_controller(HPM_GPIOM, GPIO_GET_PORT_INDEX(PIN_UART_TX), GPIO_GET_PIN_INDEX(PIN_UART_TX),
gpiom_soc_gpio0);
gpio_set_pin_output(HPM_GPIO0, GPIO_GET_PORT_INDEX(PIN_UART_TX), GPIO_GET_PIN_INDEX(PIN_UART_TX));

gpio_write_pin(HPM_GPIO0, GPIO_GET_PORT_INDEX(PIN_UART_TX), GPIO_GET_PIN_INDEX(PIN_UART_TX), 0);
}

HPM_IOC->PAD[PIN_UART_DTR].FUNC_CTL = IOC_PAD_FUNC_CTL_ALT_SELECT_SET(0);
HPM_IOC->PAD[PIN_UART_RTS].FUNC_CTL = IOC_PAD_FUNC_CTL_ALT_SELECT_SET(0);

gpiom_set_pin_controller(HPM_GPIOM, GPIO_GET_PORT_INDEX(PIN_UART_DTR), GPIO_GET_PIN_INDEX(PIN_UART_DTR),
gpiom_soc_gpio0);
gpio_set_pin_output(HPM_GPIO0, GPIO_GET_PORT_INDEX(PIN_UART_DTR), GPIO_GET_PIN_INDEX(PIN_UART_DTR));
gpio_write_pin(HPM_GPIO0, GPIO_GET_PORT_INDEX(PIN_UART_DTR), GPIO_GET_PIN_INDEX(PIN_UART_DTR), 1); // 默认输出高电平


gpiom_set_pin_controller(HPM_GPIOM, GPIO_GET_PORT_INDEX(PIN_UART_RTS), GPIO_GET_PIN_INDEX(PIN_UART_RTS),
gpiom_soc_gpio0);
gpio_set_pin_output(HPM_GPIO0, GPIO_GET_PORT_INDEX(PIN_UART_RTS), GPIO_GET_PIN_INDEX(PIN_UART_RTS));
gpio_write_pin(HPM_GPIO0, GPIO_GET_PORT_INDEX(PIN_UART_RTS), GPIO_GET_PIN_INDEX(PIN_UART_RTS), 1); // 默认输出高电平

if (!HSLink_Setting.jtag_20pin_compatible) {
gpio_write_pin(HPM_GPIO0, GPIO_GET_PORT_INDEX(PIN_UART_DTR), GPIO_GET_PIN_INDEX(PIN_UART_DTR), 1); // 默认输出高电平
gpio_write_pin(HPM_GPIO0, GPIO_GET_PORT_INDEX(PIN_UART_RTS), GPIO_GET_PIN_INDEX(PIN_UART_RTS), 1);
} else {
gpio_write_pin(HPM_GPIO0, GPIO_GET_PORT_INDEX(PIN_UART_DTR), GPIO_GET_PIN_INDEX(PIN_UART_DTR), 0); // 默认输出低电平
gpio_write_pin(HPM_GPIO0, GPIO_GET_PORT_INDEX(PIN_UART_RTS), GPIO_GET_PIN_INDEX(PIN_UART_RTS), 0);
}
}

void uartx_preinit(void)
{
uartx_io_init();

clock_set_source_divider(UART_CLK_NAME, clk_src_pll1_clk0, 8);
clock_add_to_group(UART_CLK_NAME, 0);
Expand All @@ -149,7 +181,8 @@ void uartx_preinit(void) {
}
}

void chry_dap_usb2uart_uart_config_callback(struct cdc_line_coding *line_coding) {
void chry_dap_usb2uart_uart_config_callback(struct cdc_line_coding *line_coding)
{
uart_config_t config = {0};
uart_default_config(UART_BASE, &config);
config.baudrate = line_coding->dwDTERate;
Expand All @@ -171,7 +204,8 @@ void chry_dap_usb2uart_uart_config_callback(struct cdc_line_coding *line_coding)
uart_reset_tx_fifo(UART_BASE);
}

void chry_dap_usb2uart_uart_send_bydma(uint8_t *data, uint16_t len) {
void chry_dap_usb2uart_uart_send_bydma(uint8_t *data, uint16_t len)
{
uint32_t buf_addr;
dma_resource_t *tx_resource = &dma_resource_pools[UART_TX_DMA_RESOURCE_INDEX];
if (len <= 0) {
Expand All @@ -184,7 +218,8 @@ void chry_dap_usb2uart_uart_send_bydma(uint8_t *data, uint16_t len) {
dma_mgr_enable_channel(tx_resource);
}

static hpm_stat_t board_uart_dma_config(void) {
static hpm_stat_t board_uart_dma_config(void)
{
dma_mgr_chn_conf_t chg_config;
dma_resource_t *resource = NULL;
uint32_t i = 0;
Expand Down Expand Up @@ -249,14 +284,20 @@ static hpm_stat_t board_uart_dma_config(void) {
return status_success;
}

void usbd_cdc_acm_set_dtr(uint8_t busid, uint8_t intf, bool dtr) {
void usbd_cdc_acm_set_dtr(uint8_t busid, uint8_t intf, bool dtr)
{
(void) busid;
(void) intf;
gpio_write_pin(HPM_GPIO0, GPIO_GET_PORT_INDEX(PIN_UART_DTR), GPIO_GET_PIN_INDEX(PIN_UART_DTR), !dtr);
if (!HSLink_Setting.jtag_20pin_compatible) {
gpio_write_pin(HPM_GPIO0, GPIO_GET_PORT_INDEX(PIN_UART_DTR), GPIO_GET_PIN_INDEX(PIN_UART_DTR), !dtr);
}
}

void usbd_cdc_acm_set_rts(uint8_t busid, uint8_t intf, bool rts) {
void usbd_cdc_acm_set_rts(uint8_t busid, uint8_t intf, bool rts)
{
(void) busid;
(void) intf;
gpio_write_pin(HPM_GPIO0, GPIO_GET_PORT_INDEX(PIN_UART_RTS), GPIO_GET_PIN_INDEX(PIN_UART_RTS), !rts);
if (!HSLink_Setting.jtag_20pin_compatible) {
gpio_write_pin(HPM_GPIO0, GPIO_GET_PORT_INDEX(PIN_UART_RTS), GPIO_GET_PIN_INDEX(PIN_UART_RTS), !rts);
}
}
2 changes: 2 additions & 0 deletions projects/HSLink-Pro/src/USB2UART/usb2uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ extern "C"
{
#endif

void uartx_io_init(void);

void uartx_preinit(void);

void usb2uart_handler(void);
Expand Down
71 changes: 54 additions & 17 deletions projects/HSLink-Pro/src/setting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "usb2uart.h"

using namespace rapidjson;

Expand Down Expand Up @@ -46,11 +47,51 @@ HSLink_Setting_t HSLink_Setting = {
ATTR_PLACE_AT(".bl_setting")
BL_Setting_t bl_setting;

static void store_settings(void);
template<>
auto get_json_value<bool>(const rapidjson::Value &val, const char *key, bool value) -> bool {
if (val.HasMember(key)) {
return val[key].GetBool();
}
return value;
}

static void load_settings(void);
template<>
auto get_json_value<int>(const rapidjson::Value &val, const char *key, int value) -> int {
if (val.HasMember(key)) {
return val[key].GetInt();
}
return value;
}

static void update_settings(void);
template<>
auto get_json_value<unsigned int>(const rapidjson::Value &val, const char *key, unsigned int value) -> unsigned int {
if (val.HasMember(key)) {
return val[key].GetUint();
}
return value;
}

template<>
auto get_json_value<double>(const rapidjson::Value &val, const char *key, double value) -> double {
if (val.HasMember(key)) {
return val[key].GetDouble();
}
return value;
}

template<>
auto get_json_value<const char*>(const rapidjson::Value &val, const char *key, const char* value) -> const char* {
if (val.HasMember(key)) {
return val[key].GetString();
}
return value;
}

static void store_settings();

static void load_settings();

static void update_settings();

static std::string stringify_settings()
{
Expand Down Expand Up @@ -103,6 +144,9 @@ static std::string stringify_settings()
writer.Key("led_brightness");
writer.Uint(HSLink_Setting.led_brightness);

writer.Key("jtag_20pin_compatible");
writer.Bool(HSLink_Setting.jtag_20pin_compatible);

writer.EndObject();
return std::string{buffer.GetString(), buffer.GetSize()};
}
Expand All @@ -120,19 +164,9 @@ static void parse_settings(std::string_view json)
return PORT_MODE_GPIO;
};

// 判断是否存在某个键,如果有返回那个键,没有的话返回默认值,bool类型
auto get_value_bool = [&](const Value &val, const char *key, bool default_value) -> bool
{
if (val.HasMember(key)) {
printf("key: %s, value: %d\n", key, val[key].GetBool());
return val[key].GetBool();
}
return default_value;
};

HSLink_Setting.swd_port_mode = mode(root["swd_port_mode"].GetString());
HSLink_Setting.jtag_port_mode = mode(root["jtag_port_mode"].GetString());
HSLink_Setting.jtag_single_bit_mode = get_value_bool(root, "jtag_single_bit_mode", false);
HSLink_Setting.jtag_single_bit_mode = get_json_value(root, "jtag_single_bit_mode", false);

const Value &power = root["power"];
HSLink_Setting.power.vref = power["vref"].GetDouble();
Expand All @@ -152,9 +186,10 @@ static void parse_settings(std::string_view json)

HSLink_Setting.led = root["led"].GetBool();
HSLink_Setting.led_brightness = root["led_brightness"].GetUint();
HSLink_Setting.jtag_20pin_compatible = get_json_value(root, "jtag_20pin_compatible", false);
}

static void load_settings(void)
static void load_settings()
{
char *settings_json_c_str = fdb_kv_get(&env_db, "settings");
if (settings_json_c_str == NULL) {
Expand All @@ -168,18 +203,20 @@ static void load_settings(void)
log_d("settings loaded %s", settings_json_str.c_str());
}

static void store_settings(void)
static void store_settings()
{
auto settings_json_str = stringify_settings();
fdb_kv_set(&env_db, "settings", settings_json_str.c_str());
log_d("settings stored %s", settings_json_str.c_str());
}

static void update_settings(void)
static void update_settings()
{
LED_SetBrightness(HSLink_Setting.led_brightness);
LED_SetBoost(HSLink_Setting.boost);
LED_SetEnable(HSLink_Setting.led);

uartx_io_init();
}

void Setting_Init(void)
Expand Down
20 changes: 20 additions & 0 deletions projects/HSLink-Pro/src/setting.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ typedef struct {
uint8_t reset; //这是一个Bitmap,用来存储多种设置,每一位的功能见Setting_ResetBit_t
bool led;
uint8_t led_brightness;
bool jtag_20pin_compatible;

char nickname[128];
} HSLink_Setting_t;
Expand All @@ -47,6 +48,25 @@ extern HSLink_Setting_t HSLink_Setting;
extern BL_Setting_t bl_setting;

#ifdef __cplusplus
#include "rapidjson/document.h"

template<typename T>
auto get_json_value(const rapidjson::Value &val, const char *key, T value) -> T;

template<>
auto get_json_value<bool>(const rapidjson::Value &val, const char *key, bool value) -> bool;

template<>
auto get_json_value<int>(const rapidjson::Value &val, const char *key, int value) -> int;

template<>
auto get_json_value<unsigned int>(const rapidjson::Value &val, const char *key, unsigned int value) -> unsigned int;

template<>
auto get_json_value<double>(const rapidjson::Value &val, const char *key, double value) -> double;

template<>
auto get_json_value<const char*>(const rapidjson::Value &val, const char *key, const char* value) -> const char*;
extern "C"
{
#endif
Expand Down