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 8654988
Show file tree
Hide file tree
Showing 24 changed files with 86 additions and 65 deletions.
17 changes: 9 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,41 +169,42 @@ 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
)

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
16 changes: 8 additions & 8 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 @@ -184,8 +184,8 @@ enum run_type {

typedef struct _stlink stlink_t;

#include <stm32.h>
#include <backend.h>
#include "stm32.h"
#include "stlink_backend.h"

struct _stlink {
struct _stlink_backend *backend;
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
12 changes: 9 additions & 3 deletions inc/backend.h → inc/stlink_backend.h
Original file line number Diff line number Diff line change
@@ -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 <stdint.h>

Expand Down Expand Up @@ -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
8 changes: 4 additions & 4 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_CMD_H
#define STLINK_CMD_H

enum stlink_commands {
STLINK_GET_VERSION = 0xF1,
Expand Down Expand Up @@ -60,4 +60,4 @@ enum stlink_dfu_commands {
STLINK_DFU_EXIT = 0x07
};

#endif // COMMANDS_H
#endif // STLINK_CMD_H
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
6 changes: 3 additions & 3 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 Expand Up @@ -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
8 changes: 4 additions & 4 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 Expand Up @@ -129,4 +129,4 @@
#define STLINK_REG_CM7_ICIALLU 0xE000EF50
#define STLINK_REG_CM7_CCSIDR 0xE000ED80

#endif // REGISTER_H
#endif // STM32_REGISTER_H
3 changes: 2 additions & 1 deletion src/st-trace/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
#include <unistd.h>

#include <stlink.h>
#include <stlink_backend.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
5 changes: 3 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,8 @@
// #include <sys/types.h> // TODO: Check use

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

#include "calculate.h"
#include "chipid.h"
Expand All @@ -26,7 +28,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
1 change: 1 addition & 0 deletions src/stlink-lib/read_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string.h>

#include <stlink.h>
#include <stlink_backend.h>
#include "read_write.h"

#include "logging.h"
Expand Down
10 changes: 6 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,14 @@
#include <assert.h>
// #include <sys/types.h> // TODO: Check use

#include "sg.h"
#include <stlink.h>
#include <stlink_backend.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
8 changes: 4 additions & 4 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 Expand Up @@ -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
Loading

0 comments on commit 8654988

Please sign in to comment.