hosted/stlink: Naming consistency for 'stlink' global object

This commit is contained in:
dragonmux 2022-06-15 22:13:00 -04:00 committed by Piotr Esden-Tempski
parent 66b2d0659a
commit 2765811bbb
1 changed files with 53 additions and 54 deletions

View File

@ -204,7 +204,7 @@ typedef struct {
bool ap_error; bool ap_error;
} stlink_t; } stlink_t;
stlink_t Stlink; stlink_t stlink;
static int stlink_usb_get_rw_status(bool verbose); static int stlink_usb_get_rw_status(bool verbose);
@ -271,7 +271,7 @@ static int stlink_usb_error_check(uint8_t *data, bool verbose)
* Change in error status when reading outside RAM. * Change in error status when reading outside RAM.
* This fix allows CDT plugin to visualize memory. * This fix allows CDT plugin to visualize memory.
*/ */
Stlink.ap_error = true; stlink.ap_error = true;
if (verbose) if (verbose)
DEBUG_WARN("STLINK_SWD_AP_FAULT\n"); DEBUG_WARN("STLINK_SWD_AP_FAULT\n");
return STLINK_ERROR_AP_FAULT; return STLINK_ERROR_AP_FAULT;
@ -303,7 +303,7 @@ static int stlink_usb_error_check(uint8_t *data, bool verbose)
case STLINK_SWD_AP_STICKY_ERROR: case STLINK_SWD_AP_STICKY_ERROR:
if (verbose) if (verbose)
DEBUG_WARN("STLINK_SWD_AP_STICKY_ERROR\n"); DEBUG_WARN("STLINK_SWD_AP_STICKY_ERROR\n");
Stlink.ap_error = true; stlink.ap_error = true;
return STLINK_ERROR_FAIL; return STLINK_ERROR_FAIL;
case STLINK_SWD_AP_STICKYORUN_ERROR: case STLINK_SWD_AP_STICKYORUN_ERROR:
if (verbose) if (verbose)
@ -344,7 +344,7 @@ static int stlink_send_recv_retry(uint8_t *txbuf, size_t txsize,
* with AP_FAULT and finally with ERROR_OK and the pending result. * with AP_FAULT and finally with ERROR_OK and the pending result.
* Interpret AP_FAULT as AP_WAIT in this case. * Interpret AP_FAULT as AP_WAIT in this case.
*/ */
Stlink.ap_error = false; stlink.ap_error = false;
res = STLINK_ERROR_WAIT; res = STLINK_ERROR_WAIT;
} }
if (first_res == STLINK_ERROR_OK) if (first_res == STLINK_ERROR_OK)
@ -405,21 +405,21 @@ static int write_retry(uint8_t *cmdbuf, size_t cmdsize,
*/ */
static void stlink_version(bmp_info_t *info) static void stlink_version(bmp_info_t *info)
{ {
if (Stlink.ver_hw == 30) { if (stlink.ver_hw == 30) {
uint8_t cmd[16] = {STLINK_APIV3_GET_VERSION_EX}; uint8_t cmd[16] = {STLINK_APIV3_GET_VERSION_EX};
uint8_t data[12]; uint8_t data[12];
int size = send_recv(info->usb_link, cmd, 16, data, 12); int size = send_recv(info->usb_link, cmd, 16, data, 12);
if (size == -1) { if (size == -1) {
DEBUG_WARN("[!] stlink_send_recv STLINK_APIV3_GET_VERSION_EX\n"); DEBUG_WARN("[!] stlink_send_recv STLINK_APIV3_GET_VERSION_EX\n");
} }
Stlink.ver_stlink = data[0]; stlink.ver_stlink = data[0];
Stlink.ver_swim = data[1]; stlink.ver_swim = data[1];
Stlink.ver_jtag = data[2]; stlink.ver_jtag = data[2];
Stlink.ver_mass = data[3]; stlink.ver_mass = data[3];
Stlink.ver_bridge = data[4]; stlink.ver_bridge = data[4];
Stlink.block_size = 512; stlink.block_size = 512;
Stlink.vid = data[3] << 9 | data[8]; stlink.vid = data[3] << 9 | data[8];
Stlink.pid = data[5] << 11 | data[10]; stlink.pid = data[5] << 11 | data[10];
} else { } else {
uint8_t cmd[16] = {STLINK_GET_VERSION}; uint8_t cmd[16] = {STLINK_GET_VERSION};
uint8_t data[6]; uint8_t data[6];
@ -427,27 +427,26 @@ static void stlink_version(bmp_info_t *info)
if (size == -1) { if (size == -1) {
DEBUG_WARN("[!] stlink_send_recv STLINK_GET_VERSION_EX\n"); DEBUG_WARN("[!] stlink_send_recv STLINK_GET_VERSION_EX\n");
} }
Stlink.vid = data[3] << 8 | data[2]; stlink.vid = data[3] << 8 | data[2];
Stlink.pid = data[5] << 8 | data[4]; stlink.pid = data[5] << 8 | data[4];
int version = data[0] << 8 | data[1]; /* Big endian here!*/ int version = data[0] << 8 | data[1]; /* Big endian here!*/
Stlink.block_size = 64; stlink.block_size = 64;
Stlink.ver_stlink = (version >> 12) & 0x0f; stlink.ver_stlink = (version >> 12) & 0x0f;
Stlink.ver_jtag = (version >> 6) & 0x3f; stlink.ver_jtag = (version >> 6) & 0x3f;
if ((Stlink.pid == PRODUCT_ID_STLINKV21_MSD) || if ((stlink.pid == PRODUCT_ID_STLINKV21_MSD) ||
(Stlink.pid == PRODUCT_ID_STLINKV21)) { (stlink.pid == PRODUCT_ID_STLINKV21)) {
Stlink.ver_mass = (version >> 0) & 0x3f; stlink.ver_mass = (version >> 0) & 0x3f;
} else { } else {
Stlink.ver_swim = (version >> 0) & 0x3f; stlink.ver_swim = (version >> 0) & 0x3f;
} }
} }
DEBUG_INFO("STLink firmware version: V%dJ%d",Stlink.ver_stlink, DEBUG_INFO("ST-Link firmware version: V%dJ%d",stlink.ver_stlink, stlink.ver_jtag);
Stlink.ver_jtag); if (stlink.ver_hw == 30) {
if (Stlink.ver_hw == 30) { DEBUG_INFO("M%dB%dS%d", stlink.ver_mass, stlink.ver_bridge, stlink.ver_swim);
DEBUG_INFO("M%dB%dS%d", Stlink.ver_mass, Stlink.ver_bridge, Stlink.ver_swim); } else if (stlink.ver_hw == 20) {
} else if (Stlink.ver_hw == 20) { DEBUG_INFO("S%d", stlink.ver_swim);
DEBUG_INFO("S%d", Stlink.ver_swim); } else if (stlink.ver_hw == 21) {
} else if (Stlink.ver_hw == 21) { DEBUG_INFO("M%d", stlink.ver_mass);
DEBUG_INFO("M%d", Stlink.ver_mass);
} }
DEBUG_INFO("\n"); DEBUG_INFO("\n");
} }
@ -567,23 +566,23 @@ int stlink_init(bmp_info_t *info)
return 0; return 0;
switch (info->pid) { switch (info->pid) {
case PRODUCT_ID_STLINKV2: case PRODUCT_ID_STLINKV2:
Stlink.ver_hw = 20; stlink.ver_hw = 20;
info->usb_link->ep_tx = 2; info->usb_link->ep_tx = 2;
Stlink.ep_tx = 2; stlink.ep_tx = 2;
break; break;
case PRODUCT_ID_STLINKV21 : case PRODUCT_ID_STLINKV21 :
case PRODUCT_ID_STLINKV21_MSD: case PRODUCT_ID_STLINKV21_MSD:
Stlink.ver_hw = 21; stlink.ver_hw = 21;
info->usb_link->ep_tx = 1; info->usb_link->ep_tx = 1;
Stlink.ep_tx = 1; stlink.ep_tx = 1;
break; break;
case PRODUCT_ID_STLINKV3_BL: case PRODUCT_ID_STLINKV3_BL:
case PRODUCT_ID_STLINKV3: case PRODUCT_ID_STLINKV3:
case PRODUCT_ID_STLINKV3E: case PRODUCT_ID_STLINKV3E:
case PRODUCT_ID_STLINKV3_NO_MSD: case PRODUCT_ID_STLINKV3_NO_MSD:
Stlink.ver_hw = 30; stlink.ver_hw = 30;
info->usb_link->ep_tx = 1; info->usb_link->ep_tx = 1;
Stlink.ep_tx = 1; stlink.ep_tx = 1;
break; break;
default: default:
DEBUG_INFO("Unhandled STM32 device\n"); DEBUG_INFO("Unhandled STM32 device\n");
@ -613,8 +612,8 @@ int stlink_init(bmp_info_t *info)
sl->req_trans = libusb_alloc_transfer(0); sl->req_trans = libusb_alloc_transfer(0);
sl->rep_trans = libusb_alloc_transfer(0); sl->rep_trans = libusb_alloc_transfer(0);
stlink_version(info); stlink_version(info);
if ((Stlink.ver_stlink < 3 && Stlink.ver_jtag < 32) || if ((stlink.ver_stlink < 3 && stlink.ver_jtag < 32) ||
(Stlink.ver_stlink == 3 && Stlink.ver_jtag < 3)) { (stlink.ver_stlink == 3 && stlink.ver_jtag < 3)) {
/* Maybe the adapter is in some strange state. Try to reset */ /* Maybe the adapter is in some strange state. Try to reset */
int result = libusb_reset_device(sl->ul_libusb_device_handle); int result = libusb_reset_device(sl->ul_libusb_device_handle);
DEBUG_WARN("INFO: Trying Stlink reset\n"); DEBUG_WARN("INFO: Trying Stlink reset\n");
@ -628,8 +627,8 @@ int stlink_init(bmp_info_t *info)
} }
stlink_version(info); stlink_version(info);
} }
if ((Stlink.ver_stlink < 3 && Stlink.ver_jtag < 32) || if ((stlink.ver_stlink < 3 && stlink.ver_jtag < 32) ||
(Stlink.ver_stlink == 3 && Stlink.ver_jtag < 3)) { (stlink.ver_stlink == 3 && stlink.ver_jtag < 3)) {
DEBUG_WARN("Please update Firmware\n"); DEBUG_WARN("Please update Firmware\n");
return -1; return -1;
} }
@ -648,19 +647,19 @@ void stlink_nrst_set_val(bmp_info_t *info, bool assert)
(assert)? STLINK_DEBUG_APIV2_DRIVE_NRST_LOW (assert)? STLINK_DEBUG_APIV2_DRIVE_NRST_LOW
: STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH}; : STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH};
uint8_t data[2]; uint8_t data[2];
Stlink.nrst = assert; stlink.nrst = assert;
send_recv(info->usb_link, cmd, 16, data, 2); send_recv(info->usb_link, cmd, 16, data, 2);
stlink_usb_error_check(data, true); stlink_usb_error_check(data, true);
} }
bool stlink_nrst_get_val(void) bool stlink_nrst_get_val(void)
{ {
return Stlink.nrst; return stlink.nrst;
} }
int stlink_hwversion(void) int stlink_hwversion(void)
{ {
return Stlink.ver_stlink; return stlink.ver_stlink;
} }
static int stlink_enter_debug_jtag(bmp_info_t *info) static int stlink_enter_debug_jtag(bmp_info_t *info)
@ -734,8 +733,8 @@ uint32_t stlink_dp_error(ADIv5_DP_t *dp)
dp->fault = 0; dp->fault = 0;
if (err) if (err)
DEBUG_WARN("stlink_dp_error %d\n", err); DEBUG_WARN("stlink_dp_error %d\n", err);
err |= Stlink.ap_error; err |= stlink.ap_error;
Stlink.ap_error = false; stlink.ap_error = false;
return err; return err;
} }
@ -750,8 +749,8 @@ static int stlink_read_dp_register(uint16_t port, uint16_t addr, uint32_t *reg)
STLINK_DEBUG_APIV2_READ_DAP_REG, STLINK_DEBUG_APIV2_READ_DAP_REG,
port & 0xff, port >> 8, port & 0xff, port >> 8,
addr & 0xff, addr >> 8}; addr & 0xff, addr >> 8};
if (port == STLINK_DEBUG_PORT_ACCESS && Stlink.dap_select) if (port == STLINK_DEBUG_PORT_ACCESS && stlink.dap_select)
cmd[4] = ((Stlink.dap_select & 0xf) << 4) | (addr & 0xf); cmd[4] = ((stlink.dap_select & 0xf) << 4) | (addr & 0xf);
else else
cmd[4] = addr & 0xff; cmd[4] = addr & 0xff;
uint8_t data[8]; uint8_t data[8];
@ -768,7 +767,7 @@ static int stlink_read_dp_register(uint16_t port, uint16_t addr, uint32_t *reg)
static int stlink_write_dp_register(uint16_t port, uint16_t addr, uint32_t val) static int stlink_write_dp_register(uint16_t port, uint16_t addr, uint32_t val)
{ {
if (port == STLINK_DEBUG_PORT_ACCESS && addr == 8) { if (port == STLINK_DEBUG_PORT_ACCESS && addr == 8) {
Stlink.dap_select = val; stlink.dap_select = val;
DEBUG_PROBE("Caching SELECT 0x%02" PRIx32 "\n", val); DEBUG_PROBE("Caching SELECT 0x%02" PRIx32 "\n", val);
return STLINK_ERROR_OK; return STLINK_ERROR_OK;
} else { } else {
@ -823,7 +822,7 @@ static bool stlink_ap_setup(int ap)
stlink_send_recv_retry(cmd, 16, data, 2); stlink_send_recv_retry(cmd, 16, data, 2);
int res = stlink_usb_error_check(data, true); int res = stlink_usb_error_check(data, true);
if (res) { if (res) {
if (Stlink.ver_hw == 30) { if (stlink.ver_hw == 30) {
DEBUG_WARN("STLINKV3 only connects to STM8/32!\n"); DEBUG_WARN("STLINKV3 only connects to STM8/32!\n");
} }
return false; return false;
@ -862,7 +861,7 @@ static void stlink_readmem(ADIv5_AP_t *ap, void *dest, uint32_t src, size_t len)
uint8_t type; uint8_t type;
if (src & 1 || len & 1) { if (src & 1 || len & 1) {
type = STLINK_DEBUG_READMEM_8BIT; type = STLINK_DEBUG_READMEM_8BIT;
if (len > Stlink.block_size) { if (len > stlink.block_size) {
DEBUG_WARN(" Too large!\n"); DEBUG_WARN(" Too large!\n");
return; return;
} }
@ -902,8 +901,8 @@ static void stlink_writemem8(usb_link_t *link, ADIv5_AP_t *ap, uint32_t addr,
while (len) { while (len) {
size_t length; size_t length;
/* OpenOCD has some note about writemem8*/ /* OpenOCD has some note about writemem8*/
if (len > Stlink.block_size) if (len > stlink.block_size)
length = Stlink.block_size; length = stlink.block_size;
else else
length = len; length = len;
uint8_t cmd[16] = { uint8_t cmd[16] = {
@ -1115,7 +1114,7 @@ static int divisor;
static unsigned int v3_freq[2]; static unsigned int v3_freq[2];
void stlink_max_frequency_set(bmp_info_t *info, uint32_t freq) void stlink_max_frequency_set(bmp_info_t *info, uint32_t freq)
{ {
if (Stlink.ver_hw == 30) { if (stlink.ver_hw == 30) {
uint8_t cmd[16] = {STLINK_DEBUG_COMMAND, uint8_t cmd[16] = {STLINK_DEBUG_COMMAND,
STLINK_APIV3_GET_COM_FREQ, STLINK_APIV3_GET_COM_FREQ,
info->is_jtag ? STLINK_MODE_JTAG : STLINK_MODE_SWD}; info->is_jtag ? STLINK_MODE_JTAG : STLINK_MODE_SWD};
@ -1188,7 +1187,7 @@ void stlink_max_frequency_set(bmp_info_t *info, uint32_t freq)
uint32_t stlink_max_frequency_get(bmp_info_t *info) uint32_t stlink_max_frequency_get(bmp_info_t *info)
{ {
uint32_t ret = 0; uint32_t ret = 0;
if (Stlink.ver_hw == 30) { if (stlink.ver_hw == 30) {
ret = v3_freq[(info->is_jtag) ? STLINK_MODE_JTAG : STLINK_MODE_SWD]; ret = v3_freq[(info->is_jtag) ? STLINK_MODE_JTAG : STLINK_MODE_SWD];
} else { } else {
ret = V2_CLOCK_RATE; ret = V2_CLOCK_RATE;