Skip to content
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

Fix: BMDA J-Link set frequency regression #1575

Merged
merged 4 commits into from
Jul 31, 2023
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
34 changes: 23 additions & 11 deletions src/platforms/hosted/jlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ bool jlink_simple_request_32(
* In SWD mode, the `tms` buffer represents direction states and
* the `tdi` buffer represents SWDIO data to send to the device
*
* RM08001 Reference manual for J-Link USB Protocol
* §5.5.12 EMU_CMD_HW_JTAG3
* RM08001 Reference manual for J-Link USB Protocol
* §5.5.12 EMU_CMD_HW_JTAG3
*/
bool jlink_transfer(const uint16_t clock_cycles, const uint8_t *const tms, const uint8_t *const tdi, uint8_t *const tdo)
{
Expand Down Expand Up @@ -304,7 +304,7 @@ static bool jlink_get_version(void)

jlink.hw_version = read_le4(buffer, 0);

DEBUG_INFO("Hardware version: %s v%u.%u.%u\n",
DEBUG_INFO("Hardware version: %s v%" PRIu32 ".%" PRIu32 ".%" PRIu32 "\n",
jlink_hw_type_to_string(JLINK_HARDWARE_VERSION_TYPE(jlink.hw_version)),
JLINK_HARDWARE_VERSION_MAJOR(jlink.hw_version), JLINK_HARDWARE_VERSION_MINOR(jlink.hw_version),
JLINK_HARDWARE_VERSION_REVISION(jlink.hw_version));
Expand Down Expand Up @@ -367,7 +367,7 @@ static bool jlink_get_interfaces(void)
if (!jlink_simple_request_8(JLINK_CMD_INTERFACE_GET, JLINK_INTERFACE_GET_AVAILABLE, buffer, sizeof(buffer)))
return false;

/* available_interfaces is a 32bit bitfield/mask */
/* Available_interfaces is a 32bit bitfield/mask */
jlink.available_interfaces = read_le4(buffer, 0);

/* Print the available interfaces, marking the selected one, and unsuported ones */
Expand Down Expand Up @@ -397,15 +397,21 @@ static bool jlink_get_interface_frequency(const uint8_t interface)
if (!jlink_interface_available(interface))
return false;

/* If the base frequency is non-zero, we've already read the frequency info for the requested interface and can skip the query */
/*
* If the base frequency is non-zero, we've already read the frequency info
* for the requested interface and can skip the query
*/
if (jlink.interface_frequency[interface].base != 0U)
return true;

/* Get the selected interface */
const uint8_t selected_interface = jlink_selected_interface();

if (selected_interface != interface) {
/* If the selected interface doesn't match the requested interface, select it, let's hope this doesn't mess something up elsewhere */
/*
* If the selected interface doesn't match the requested interface, select it,
* let's hope this doesn't mess something up elsewhere
*/
DEBUG_WARN("Trying to get frequency for interface %s but it is not selected, selecting it\n",
jlink_interface_to_string(interface));

Expand All @@ -424,10 +430,13 @@ static bool jlink_get_interface_frequency(const uint8_t interface)
interface_frequency->base = read_le4(buffer, JLINK_INTERFACE_BASE_FREQUENCY_OFFSET);
interface_frequency->min_divisor = read_le2(buffer, JLINK_INTERFACE_MIN_DIV_OFFSET);

/* This is an assumption, if the J-Link was configured before we started, this may not be true, but we have no way to know */
/*
* This is an assumption, if the J-Link was configured before we started,
* this may not be true, but we have no way to know
*/
interface_frequency->current_divisor = interface_frequency->min_divisor;

DEBUG_INFO("%s interface frequency:\n\tBase frequency: %uHz\n\tMinimum divisor: %u\n",
DEBUG_INFO("%s interface frequency:\n\tBase frequency: %" PRIu32 "Hz\n\tMinimum divisor: %u\n",
jlink_interface_to_string(interface), interface_frequency->base, interface_frequency->min_divisor);

#if 0
Expand Down Expand Up @@ -459,7 +468,10 @@ static bool jlink_set_interface_frequency(const uint8_t interface, const uint32_
const uint8_t selected_interface = jlink_selected_interface();

if (selected_interface != interface) {
/* If the selected interface doesn't match the requested interface, select it, let's hope this doesn't mess something up elsewhere */
/*
* If the selected interface doesn't match the requested interface, select it,
* let's hope this doesn't mess something up elsewhere
*/
DEBUG_WARN("Trying to set frequency for interface %s but it is not selected, selecting it\n",
jlink_interface_to_string(interface));

Expand All @@ -482,7 +494,7 @@ static bool jlink_set_interface_frequency(const uint8_t interface, const uint32_
divisor = interface_frequency->min_divisor;

/* Get the approximate frequency we'll actually be running at, convert to kHz in the process */
const uint16_t frequency_khz = (interface_frequency->base / interface_frequency->current_divisor) / 1000U;
const uint16_t frequency_khz = (interface_frequency->base / divisor) / 1000U;

if (!jlink_simple_request_16(JLINK_CMD_INTERFACE_SET_FREQUENCY_KHZ, frequency_khz, NULL, 0))
return false;
Expand Down Expand Up @@ -537,7 +549,7 @@ static bool jlink_kickstart_power(void)

static bool jlink_set_kickstart_power(const bool enable)
{
/*
/*
* Kickstart power is a 5V 300mA supply that can be used to power targets
* Exposed on pin 19 of the J-Link 20 pin connector
*/
Expand Down
8 changes: 4 additions & 4 deletions src/platforms/hosted/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ bool platform_target_get_power(void)

uint32_t platform_target_voltage_sense(void)
{
uint32_t targetVoltage = 0;
uint32_t target_voltage = 0;

switch (info.bmp_type) {
case BMP_TYPE_BMP: {
Expand All @@ -513,22 +513,22 @@ uint32_t platform_target_voltage_sense(void)
uint32_t units = 0;
uint32_t tenths = 0;
sscanf(result, "%" PRIu32 ".%" PRIu32, &units, &tenths);
targetVoltage = (units * 10U) + tenths;
target_voltage = (units * 10U) + tenths;
}
break;
}

#if HOSTED_BMP_ONLY == 0
case BMP_TYPE_JLINK:
targetVoltage = jlink_target_voltage_sense();
target_voltage = jlink_target_voltage_sense();
break;
#endif

default:
break;
}

return targetVoltage;
return target_voltage;
}

void platform_buffer_flush(void)
Expand Down