From 3adf5e0393b3b6b711f0858ba52472aaee14614d Mon Sep 17 00:00:00 2001 From: HalfSweet Date: Tue, 8 Apr 2025 23:43:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0JTAG=2020P=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- projects/HSLink-Pro/src/HID_COMM/hid_comm.cpp | 16 ++-- projects/HSLink-Pro/src/USB2UART/usb2uart.c | 73 +++++++++++++++---- projects/HSLink-Pro/src/USB2UART/usb2uart.h | 2 + projects/HSLink-Pro/src/setting.cpp | 71 +++++++++++++----- projects/HSLink-Pro/src/setting.h | 20 +++++ 5 files changed, 139 insertions(+), 43 deletions(-) diff --git a/projects/HSLink-Pro/src/HID_COMM/hid_comm.cpp b/projects/HSLink-Pro/src/HID_COMM/hid_comm.cpp index 854870d..0db12c1 100644 --- a/projects/HSLink-Pro/src/HID_COMM/hid_comm.cpp +++ b/projects/HSLink-Pro/src/HID_COMM/hid_comm.cpp @@ -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")) { @@ -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(); @@ -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); @@ -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()); diff --git a/projects/HSLink-Pro/src/USB2UART/usb2uart.c b/projects/HSLink-Pro/src/USB2UART/usb2uart.c index 613cc4e..ba869b1 100644 --- a/projects/HSLink-Pro/src/USB2UART/usb2uart.c +++ b/projects/HSLink-Pro/src/USB2UART/usb2uart.c @@ -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; @@ -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]; @@ -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 = @@ -115,8 +118,8 @@ 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; @@ -124,8 +127,25 @@ void uartx_preinit(void) { 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); @@ -133,12 +153,24 @@ void uartx_preinit(void) { 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); @@ -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; @@ -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) { @@ -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; @@ -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); + } } diff --git a/projects/HSLink-Pro/src/USB2UART/usb2uart.h b/projects/HSLink-Pro/src/USB2UART/usb2uart.h index cb84618..d20cf42 100644 --- a/projects/HSLink-Pro/src/USB2UART/usb2uart.h +++ b/projects/HSLink-Pro/src/USB2UART/usb2uart.h @@ -9,6 +9,8 @@ extern "C" { #endif +void uartx_io_init(void); + void uartx_preinit(void); void usb2uart_handler(void); diff --git a/projects/HSLink-Pro/src/setting.cpp b/projects/HSLink-Pro/src/setting.cpp index 23805e9..d2a366e 100644 --- a/projects/HSLink-Pro/src/setting.cpp +++ b/projects/HSLink-Pro/src/setting.cpp @@ -12,6 +12,7 @@ #include "rapidjson/document.h" #include "rapidjson/stringbuffer.h" #include "rapidjson/writer.h" +#include "usb2uart.h" using namespace rapidjson; @@ -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(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(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(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(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 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() { @@ -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()}; } @@ -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(); @@ -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) { @@ -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) diff --git a/projects/HSLink-Pro/src/setting.h b/projects/HSLink-Pro/src/setting.h index 8afa611..2ce06c0 100644 --- a/projects/HSLink-Pro/src/setting.h +++ b/projects/HSLink-Pro/src/setting.h @@ -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; @@ -47,6 +48,25 @@ extern HSLink_Setting_t HSLink_Setting; extern BL_Setting_t bl_setting; #ifdef __cplusplus +#include "rapidjson/document.h" + +template +auto get_json_value(const rapidjson::Value &val, const char *key, T value) -> T; + +template<> +auto get_json_value(const rapidjson::Value &val, const char *key, bool value) -> bool; + +template<> +auto get_json_value(const rapidjson::Value &val, const char *key, int value) -> int; + +template<> +auto get_json_value(const rapidjson::Value &val, const char *key, unsigned int value) -> unsigned int; + +template<> +auto get_json_value(const rapidjson::Value &val, const char *key, double value) -> double; + +template<> +auto get_json_value(const rapidjson::Value &val, const char *key, const char* value) -> const char*; extern "C" { #endif