Skip to content

Commit

Permalink
Restructuring of stlink-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Nightwalker-87 committed Jan 12, 2025
1 parent 2c92415 commit c5367ab
Show file tree
Hide file tree
Showing 22 changed files with 64 additions and 55 deletions.
15 changes: 8 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,39 +171,40 @@ add_subdirectory(inc)
set(STLINK_HEADERS
inc/backend.h
inc/stlink.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
)

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
)

Expand Down
2 changes: 1 addition & 1 deletion doc/flashloaders.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
4 changes: 2 additions & 2 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions inc/stlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <stdbool.h>

#include <stm32.h>
#include <stm32flash.h>
#include <stm32_flash.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -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()

Expand All @@ -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);
Expand Down Expand Up @@ -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 <stlink_cmd.h>
#include <chipid.h>
#include <commands.h>
#include <flash_loader.h>
#include <sg.h>
#include <logging.h>
#include <sg_legacy.h>
#include <usb.h>
#include <version.h>
#include <logging.h>

#ifdef __cplusplus
}
Expand Down
6 changes: 3 additions & 3 deletions src/stlink-lib/commands.h → inc/stlink_cmd.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* File: commands.h
* File: stlink_cmd.h
*
* stlink commands
*/

#ifndef COMMANDS_H
#define COMMANDS_H
#ifndef STLINK_COMMANDS_H
#define STLINK_COMMANDS_H

enum stlink_commands {
STLINK_GET_VERSION = 0xF1,
Expand Down
2 changes: 1 addition & 1 deletion inc/stm32.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions inc/stm32flash.h → inc/stm32_flash.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef STM32FLASH_H
#define STM32FLASH_H
#ifndef STM32_FLASH_H
#define STM32_FLASH_H

#include <stdint.h>

Expand Down
6 changes: 3 additions & 3 deletions src/stlink-lib/register.h → inc/stm32_register.h
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/st-trace/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
#include <unistd.h>

#include <stlink.h>
#include <stm32_register.h>

#include <chipid.h>
#include <logging.h>
#include <read_write.h>
#include <register.h>
#include <usb.h>

#define DEFAULT_LOGGING_LEVEL 50
Expand Down
3 changes: 2 additions & 1 deletion src/st-util/gdb-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#endif

#include <stlink.h>
#include <stm32_register.h>

#include "gdb-server.h"
#include "gdb-remote.h"
#include "memory-map.h"
Expand All @@ -38,7 +40,6 @@
#include <helper.h>
#include <logging.h>
#include <read_write.h>
#include <register.h>
#include <usb.h>

#define FLASH_BASE 0x08000000
Expand Down
4 changes: 2 additions & 2 deletions src/stlink-lib/common.c → src/stlink-lib/common_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* TODO: This file should be split up into new or existing modules. */

/*
* File: common.c
* File: common_legacy.c
*
*
*/
Expand All @@ -17,6 +17,7 @@
// #include <sys/types.h> // TODO: Check use

#include <stlink.h>
#include <stm32_register.h>

#include "calculate.h"
#include "chipid.h"
Expand All @@ -26,7 +27,6 @@
#include "map_file.h"
#include "md5.h"
#include "read_write.h"
#include "register.h"
#include "usb.h"

#ifndef O_BINARY
Expand Down
4 changes: 2 additions & 2 deletions src/stlink-lib/flash_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
#include <unistd.h>

#include <stm32.h>
#include <stm32_register.h>
#include <stlink.h>
#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
Expand Down
1 change: 1 addition & 0 deletions src/stlink-lib/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/stlink-lib/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
#define LOGGING_H

#include <stdint.h>
#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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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_

Expand Down
8 changes: 4 additions & 4 deletions src/stlink-lib/sg.c → src/stlink-lib/sg_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
*/

/*
* File: sg.c
* File: sg_legacy.c
*
*
*/
Expand All @@ -91,12 +91,12 @@
#include <assert.h>
// #include <sys/types.h> // TODO: Check use

#include "sg.h"
#include <stlink_cmd.h>
#include <stm32_register.h>

#include "commands.h"
#include "sg_legacy.h"
#include "logging.h"
#include "read_write.h"
#include "register.h"
#include "usb.h"
// #include <stlink.h> // TODO: Check use

Expand Down
6 changes: 3 additions & 3 deletions src/stlink-lib/sg.h → src/stlink-lib/sg_legacy.h
Original file line number Diff line number Diff line change
@@ -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 <stdint.h>

Expand Down
21 changes: 10 additions & 11 deletions src/stlink-lib/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
#include <unistd.h>

#include <stlink.h>
#include "usb.h"
#include <stlink_cmd.h>
#include <stm32_register.h>

#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));
Expand Down Expand Up @@ -85,7 +85,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);
}
Expand Down Expand Up @@ -116,13 +115,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<<retry) * 1000;
uint32_t delay_us = (1 << retry) * 1000;
DLOG("%s wait error (0x%02X), delaying %u us and retry\n", cmd, rxbuf[0], delay_us);
usleep(delay_us);
retry++;
Expand All @@ -149,7 +148,7 @@ ssize_t send_recv(struct stlink_libusb* handle, int32_t terminate, unsigned char
}
}

if ((handle->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);
Expand Down Expand Up @@ -179,7 +178,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';
Expand Down Expand Up @@ -1181,7 +1180,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;
Expand Down Expand Up @@ -1266,7 +1265,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);
Expand All @@ -1292,7 +1291,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);

Expand Down
Loading

0 comments on commit c5367ab

Please sign in to comment.