diff --git a/CMakeLists.txt b/CMakeLists.txt index 9262859d1..58c64d327 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -169,24 +169,25 @@ add_subdirectory(inc) ## Define source- and headerfiles for stlink library set(STLINK_HEADERS - inc/backend.h inc/stlink.h + inc/stlink_backend.h + inc/stlink_cmd.h inc/stm32.h - inc/stm32flash.h + inc/stm32_flash.h src/stlink-lib/calculate.h src/stlink-lib/chipid.h - src/stlink-lib/commands.h src/stlink-lib/common_flash.h src/stlink-lib/flash_loader.h src/stlink-lib/helper.h src/stlink-lib/libusb_settings.h src/stlink-lib/lib_md5.h src/stlink-lib/logging.h + src/stlink-lib/logging_spdlog_wr.h src/stlink-lib/map_file.h src/stlink-lib/md5.h src/stlink-lib/option_bytes.h - src/stlink-lib/register.h - src/stlink-lib/sg.h + src/stlink-lib/read_write.h + src/stlink-lib/sg_legacy.h src/stlink-lib/usb.h ) @@ -194,16 +195,16 @@ set(STLINK_SOURCE src/stlink-lib/calculate.c src/stlink-lib/chipid.c src/stlink-lib/common_flash.c - src/stlink-lib/common.c + src/stlink-lib/common_legacy.c src/stlink-lib/flash_loader.c src/stlink-lib/helper.c + src/stlink-lib/lib_md5.c src/stlink-lib/logging.c src/stlink-lib/map_file.c - src/stlink-lib/lib_md5.c src/stlink-lib/md5.c src/stlink-lib/option_bytes.c src/stlink-lib/read_write.c - src/stlink-lib/sg.c + src/stlink-lib/sg_legacy.c src/stlink-lib/usb.c ) diff --git a/doc/flashloaders.md b/doc/flashloaders.md index b1208b6f5..035b80f0c 100644 --- a/doc/flashloaders.md +++ b/doc/flashloaders.md @@ -9,7 +9,7 @@ As SRAM is usually less in size than FLASH, `stlink` only flashes one page (may ## The flashing process 1. `st-flash` loads compiled binary of corresponding flashloader to SRAM by calling `stlink_flash_loader_init` in `src/flash_loader.c` -2. `st-flash` erases corresponding flash page by calling `stlink_erase_flash_page` in `common.c`. +2. `st-flash` erases corresponding flash page by calling `stlink_erase_flash_page` in `common_legacy.c`. 3. `st-flash` calls `stlink_flash_loader_run` in `flash_loader.c`. In this function + buffer of one flash page is written to SRAM following the flashloader + the buffer start address (in SRAM) is written to register `r0` diff --git a/doc/tutorial.md b/doc/tutorial.md index 2a858ac39..5bbd7869b 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -115,12 +115,12 @@ Check your hardware and try to identify what you have in front of you before ass Please let us know, if you come across any further websites or tutorials that help to identify STM32 fake chips so we can list them here to help others. -### c) Appearance of the warning message `WARN src/common.c: unknown chip id!` +### c) Appearance of the warning message `WARN src/common_legacy.c: unknown chip id!` The chip ID is the main identifier for STM32 MCU and their specific type and provides primary information on flash and SRAM architecture. This so called `DBGMCU_IDCODE` register is allocated either at memory address `0xE0042000` or `0x40015800`. -A failure of chip identification results in the error `WARN src/common.c: unknown chip id!`. +A failure of chip identification results in the error `WARN src/common_legacy.c: unknown chip id!`. There are different variants of this message that refer to different issues: - `unknown chip id! 0` --> Target chip (board) is unknown. diff --git a/inc/stlink.h b/inc/stlink.h index d516e5c4f..927530abd 100644 --- a/inc/stlink.h +++ b/inc/stlink.h @@ -12,7 +12,7 @@ #include #include -#include +#include #ifdef __cplusplus extern "C" { @@ -184,8 +184,8 @@ enum run_type { typedef struct _stlink stlink_t; -#include -#include +#include "stm32.h" +#include "stlink_backend.h" struct _stlink { struct _stlink_backend *backend; @@ -224,7 +224,7 @@ struct _stlink { // bootloader // sys_base and sys_size are not used by the tools, but are only there to download the bootloader code - // (see tests/sg.c) + // (see tests/sg_legacy.c) stm32_addr_t sys_base; // stlink_chipid_params.bootrom_base, set by stlink_load_device_params() uint32_t sys_size; // stlink_chipid_params.bootrom_size, set by stlink_load_device_params() @@ -238,7 +238,7 @@ struct _stlink { uint32_t otp_size; }; -/* Functions defined in common.c */ +/* Functions defined in common_legacy.c */ int32_t stlink_enter_swd_mode(stlink_t *sl); // int32_t stlink_enter_jtag_mode(stlink_t *sl); @@ -271,13 +271,13 @@ int32_t stlink_fread(stlink_t* sl, const char* path, bool is_ihex, stm32_addr_t int32_t stlink_load_device_params(stlink_t *sl); int32_t stlink_target_connect(stlink_t *sl, enum connect_type connect); +#include #include -#include #include -#include +#include +#include #include #include -#include #ifdef __cplusplus } diff --git a/inc/backend.h b/inc/stlink_backend.h similarity index 93% rename from inc/backend.h rename to inc/stlink_backend.h index 6b9c9c0a8..f4302769e 100644 --- a/inc/backend.h +++ b/inc/stlink_backend.h @@ -1,5 +1,11 @@ -#ifndef BACKEND_H -#define BACKEND_H +/* + * File: stlink_backend.h + * + * stlink backend + */ + +#ifndef STLINK_BACKEND_H +#define STLINK_BACKEND_H #include @@ -36,4 +42,4 @@ int32_t (*trace_read) (stlink_t * sl, uint8_t* buf, uint32_t size); } stlink_backend_t; -#endif // BACKEND_H +#endif // STLINK_BACKEND_H diff --git a/src/stlink-lib/commands.h b/inc/stlink_cmd.h similarity index 96% rename from src/stlink-lib/commands.h rename to inc/stlink_cmd.h index 64cecce16..5d41777d9 100644 --- a/src/stlink-lib/commands.h +++ b/inc/stlink_cmd.h @@ -1,11 +1,11 @@ /* - * File: commands.h + * File: stlink_cmd.h * * stlink commands */ -#ifndef COMMANDS_H -#define COMMANDS_H +#ifndef STLINK_CMD_H +#define STLINK_CMD_H enum stlink_commands { STLINK_GET_VERSION = 0xF1, @@ -60,4 +60,4 @@ enum stlink_dfu_commands { STLINK_DFU_EXIT = 0x07 }; -#endif // COMMANDS_H +#endif // STLINK_CMD_H diff --git a/inc/stm32.h b/inc/stm32.h index a7f33cd4e..87c94c9ff 100644 --- a/inc/stm32.h +++ b/inc/stm32.h @@ -169,7 +169,7 @@ enum stm32_chipids { #define STM32_G4_OPTION_BYTES_BASE ((uint32_t) 0x1ffff800) /* ============ */ -/* Old defines from common.c are below */ +/* Old defines from common_legacy.c are below */ /* ============ */ /* Constant STM32 memory address */ diff --git a/inc/stm32flash.h b/inc/stm32_flash.h similarity index 99% rename from inc/stm32flash.h rename to inc/stm32_flash.h index fb10f0e19..39d9301dd 100644 --- a/inc/stm32flash.h +++ b/inc/stm32_flash.h @@ -1,5 +1,5 @@ -#ifndef STM32FLASH_H -#define STM32FLASH_H +#ifndef STM32_FLASH_H +#define STM32_FLASH_H #include @@ -434,4 +434,4 @@ #define FLASH_WB_SR_PGAERR (5) /* Programming error */ #define FLASH_WB_SR_BSY (16) /* Busy */ -#endif // STM32FLASH_H +#endif // STM32_FLASH_H diff --git a/src/stlink-lib/register.h b/inc/stm32_register.h similarity index 98% rename from src/stlink-lib/register.h rename to inc/stm32_register.h index f1e9574cd..257baa8ef 100644 --- a/src/stlink-lib/register.h +++ b/inc/stm32_register.h @@ -1,11 +1,11 @@ /* - * File: register.h + * File: stm32_register.h * * Common STM32 registers */ -#ifndef REGISTER_H -#define REGISTER_H +#ifndef STM32_REGISTER_H +#define STM32_REGISTER_H #define STLINK_REG_CM3_CPUID 0xE000ED00 @@ -129,4 +129,4 @@ #define STLINK_REG_CM7_ICIALLU 0xE000EF50 #define STLINK_REG_CM7_CCSIDR 0xE000ED80 -#endif // REGISTER_H +#endif // STM32_REGISTER_H diff --git a/src/st-trace/trace.c b/src/st-trace/trace.c index 83c03a17e..d6d15ef66 100644 --- a/src/st-trace/trace.c +++ b/src/st-trace/trace.c @@ -11,11 +11,12 @@ #include #include +#include +#include #include #include #include -#include #include #define DEFAULT_LOGGING_LEVEL 50 diff --git a/src/st-util/gdb-server.c b/src/st-util/gdb-server.c index d2af44cd3..28f2e5d29 100644 --- a/src/st-util/gdb-server.c +++ b/src/st-util/gdb-server.c @@ -27,6 +27,8 @@ #endif #include +#include + #include "gdb-server.h" #include "gdb-remote.h" #include "memory-map.h" @@ -38,7 +40,6 @@ #include #include #include -#include #include #define FLASH_BASE 0x08000000 diff --git a/src/stlink-lib/common.c b/src/stlink-lib/common_legacy.c similarity index 99% rename from src/stlink-lib/common.c rename to src/stlink-lib/common_legacy.c index 83b9a46dd..371b052fe 100644 --- a/src/stlink-lib/common.c +++ b/src/stlink-lib/common_legacy.c @@ -2,7 +2,7 @@ /* TODO: This file should be split up into new or existing modules. */ /* - * File: common.c + * File: common_legacy.c * * */ @@ -17,6 +17,8 @@ // #include // TODO: Check use #include +#include +#include #include "calculate.h" #include "chipid.h" @@ -26,7 +28,6 @@ #include "map_file.h" #include "md5.h" #include "read_write.h" -#include "register.h" #include "usb.h" #ifndef O_BINARY diff --git a/src/stlink-lib/flash_loader.c b/src/stlink-lib/flash_loader.c index f3a400713..48d6c9abb 100644 --- a/src/stlink-lib/flash_loader.c +++ b/src/stlink-lib/flash_loader.c @@ -10,14 +10,14 @@ #include #include +#include #include -#include "flash_loader.h" +#include "flash_loader.h" #include "common_flash.h" #include "helper.h" #include "logging.h" #include "read_write.h" -#include "register.h" #define FLASH_REGS_BANK2_OFS 0x40 #define FLASH_BANK2_START_ADDR 0x08080000 diff --git a/src/stlink-lib/logging.c b/src/stlink-lib/logging.c index 92092f3d0..29573768e 100644 --- a/src/stlink-lib/logging.c +++ b/src/stlink-lib/logging.c @@ -2,6 +2,7 @@ * File: logging.c * * UglyLogging: Slow, yet another wheel reinvented, but enough to make the rest of our code pretty enough. + * Ugly, low performance, configurable level, logging "framework" */ #define __STDC_WANT_LIB_EXT1__ 1 diff --git a/src/stlink-lib/logging.h b/src/stlink-lib/logging.h index 560e20eca..eef660428 100644 --- a/src/stlink-lib/logging.h +++ b/src/stlink-lib/logging.h @@ -9,13 +9,13 @@ #define LOGGING_H #include -#include "spdlog_wrapper.h" +#include "logging_spdlog_wr.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus -/* Optional: Enable interface for SPDLOG to replace UglyLogging */ +/* Optional: Enable interface for spdlog to replace UglyLogging */ // #define SPDLOG_LOGGING enum ugly_loglevel { diff --git a/src/stlink-lib/spdlog_wrapper.h b/src/stlink-lib/logging_spdlog_wr.h similarity index 59% rename from src/stlink-lib/spdlog_wrapper.h rename to src/stlink-lib/logging_spdlog_wr.h index 26f34c8b5..4a1379295 100644 --- a/src/stlink-lib/spdlog_wrapper.h +++ b/src/stlink-lib/logging_spdlog_wr.h @@ -1,3 +1,10 @@ +/* + * File: logging_spdlog_wr.h + * + * spdlog: Very fast, header-only/compiled, C++ logging library. + * This is a wrapper file for spdlog. + */ + #ifndef _SPDLOG_WRAPPER_ #define _SPDLOG_WRAPPER_ diff --git a/src/stlink-lib/read_write.c b/src/stlink-lib/read_write.c index 877ab3b48..749a66e03 100644 --- a/src/stlink-lib/read_write.c +++ b/src/stlink-lib/read_write.c @@ -9,6 +9,7 @@ #include #include +#include #include "read_write.h" #include "logging.h" diff --git a/src/stlink-lib/sg.c b/src/stlink-lib/sg_legacy.c similarity index 99% rename from src/stlink-lib/sg.c rename to src/stlink-lib/sg_legacy.c index 16501db1f..c37e93cad 100644 --- a/src/stlink-lib/sg.c +++ b/src/stlink-lib/sg_legacy.c @@ -77,7 +77,7 @@ */ /* - * File: sg.c + * File: sg_legacy.c * * */ @@ -91,12 +91,14 @@ #include // #include // TODO: Check use -#include "sg.h" +#include +#include +#include +#include -#include "commands.h" +#include "sg_legacy.h" #include "logging.h" #include "read_write.h" -#include "register.h" #include "usb.h" // #include // TODO: Check use diff --git a/src/stlink-lib/sg.h b/src/stlink-lib/sg_legacy.h similarity index 97% rename from src/stlink-lib/sg.h rename to src/stlink-lib/sg_legacy.h index b10d0f6f0..8e07d213c 100644 --- a/src/stlink-lib/sg.h +++ b/src/stlink-lib/sg_legacy.h @@ -1,13 +1,13 @@ /* == nightwalker-87: TODO: CONTENT AND USE OF THIS SOURCE FILE IS TO BE VERIFIED (07.06.2023) == */ /* - * File: sg.h + * File: sg_legacy.h * * */ -#ifndef SG_H -#define SG_H +#ifndef SG_LEGACY_H +#define SG_LEGACY_H #include @@ -102,4 +102,4 @@ int32_t _stlink_sg_exit_debug_mode(stlink_t *stl); stlink_t* stlink_v1_open_inner(const int32_t verbose); stlink_t* stlink_v1_open(const int32_t verbose, int32_t reset); -#endif // SG_H +#endif // SG_LEGACY_H diff --git a/src/stlink-lib/usb.c b/src/stlink-lib/usb.c index 32580b2ba..66597349a 100644 --- a/src/stlink-lib/usb.c +++ b/src/stlink-lib/usb.c @@ -23,12 +23,13 @@ #include #include -#include "usb.h" +#include +#include +#include -#include "commands.h" #include "logging.h" #include "read_write.h" -#include "register.h" +#include "usb.h" static inline uint32_t le_to_h_u32(const uint8_t* buf) { return ((uint32_t) ((uint32_t) buf[0] | (uint32_t) buf[1] << 8 | (uint32_t) buf[2] << 16 | (uint32_t) buf[3] << 24)); @@ -85,7 +86,6 @@ void _stlink_usb_close(stlink_t* sl) { // maybe we couldn't even get the usb device? if (handle != NULL) { if (handle->usb_handle != NULL) { libusb_close(handle->usb_handle); } - libusb_exit(handle->libusb_ctx); free(handle); } @@ -116,13 +116,13 @@ ssize_t send_recv(struct stlink_libusb* handle, int32_t terminate, unsigned char } /* Checking the command execution status stored in the first byte of the response */ - if (handle->protocoll != 1 && check_error >= CMD_CHECK_STATUS && + if (handle->protocol != 1 && check_error >= CMD_CHECK_STATUS && rxbuf[0] != STLINK_DEBUG_ERR_OK) { switch(rxbuf[0]) { case STLINK_DEBUG_ERR_AP_WAIT: case STLINK_DEBUG_ERR_DP_WAIT: if (check_error == CMD_CHECK_RETRY && retry < 3) { - uint32_t delay_us = (1<protocoll == 1) && terminate) { + if ((handle->protocol == 1) && terminate) { // read the SG reply unsigned char sg_buf[13]; t = libusb_bulk_transfer(handle->usb_handle, handle->ep_rep, sg_buf, 13, &res, 3000); @@ -179,7 +179,7 @@ static int32_t fill_command(stlink_t * sl, enum SCSI_Generic_Direction dir, uint int32_t i = 0; memset(cmd, 0, sizeof(sl->c_buf)); - if (slu->protocoll == 1) { + if (slu->protocol == 1) { cmd[i++] = 'U'; cmd[i++] = 'S'; cmd[i++] = 'B'; @@ -1181,7 +1181,7 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, enum connect_type connect, // if no serial provided, or if serial match device, fixup version and protocol if (((serial == NULL) || (*serial == 0)) || (memcmp(serial, &sl->serial, STLINK_SERIAL_LENGTH) == 0)) { if (STLINK_V1_USB_PID(desc.idProduct)) { - slu->protocoll = 1; + slu->protocol = 1; sl->version.stlink_v = 1; } else if (STLINK_V2_USB_PID(desc.idProduct) || STLINK_V2_1_USB_PID(desc.idProduct)) { sl->version.stlink_v = 2; @@ -1266,7 +1266,7 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, enum connect_type connect, } slu->sg_transfer_idx = 0; - slu->cmd_len = (slu->protocoll == 1) ? STLINK_SG_SIZE : STLINK_CMD_SIZE; + slu->cmd_len = (slu->protocol == 1) ? STLINK_SG_SIZE : STLINK_CMD_SIZE; // initialize stlink version (sl->version) stlink_version(sl); @@ -1292,7 +1292,7 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, enum connect_type connect, sl->freq = freq; // set the speed before entering the mode as the chip discovery phase // should be done at this speed too - // set the stlink clock speed (default is 1800kHz) + // set the stlink clock speed (default is 1800 kHz) DLOG("JTAG/SWD freq set to %d\n", freq); _stlink_usb_set_swdclk(sl, freq); diff --git a/src/stlink-lib/usb.h b/src/stlink-lib/usb.h index 2a76182b0..49a8f1b49 100644 --- a/src/stlink-lib/usb.h +++ b/src/stlink-lib/usb.h @@ -56,7 +56,7 @@ struct stlink_libusb { uint32_t ep_req; uint32_t ep_rep; uint32_t ep_trace; - int32_t protocoll; + int32_t protocol; uint32_t sg_transfer_idx; uint32_t cmd_len; }; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 03c9912d3..ce49a8a32 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,7 +2,7 @@ # Build test executables ### -set(TESTEXEC usb sg) +set(TESTEXEC usb sg_legacy) set(TEST_DEPENDENCY ${STLINK_LIB_SHARED}) if (WIN32) diff --git a/tests/sg.c b/tests/sg_legacy.c similarity index 99% rename from tests/sg.c rename to tests/sg_legacy.c index 1284bb15b..da56da1b4 100644 --- a/tests/sg.c +++ b/tests/sg_legacy.c @@ -9,7 +9,7 @@ #include #include -#include +#include #if defined(_MSC_VER) #define __attribute__(x) diff --git a/tests/usb.c b/tests/usb.c index f8b60c306..a4b90d9e7 100644 --- a/tests/usb.c +++ b/tests/usb.c @@ -5,9 +5,9 @@ #include #include +#include #include -#include #include static void usage(void) {