Skip to content

Commit

Permalink
Merge pull request #1315 from stlink-org/testing
Browse files Browse the repository at this point in the history
Merge branch `testing` into `develop`
  • Loading branch information
Nightwalker-87 committed Jun 19, 2023
2 parents 67ae7a1 + b72f5b5 commit d4b53b0
Show file tree
Hide file tree
Showing 52 changed files with 1,093 additions and 916 deletions.
25 changes: 23 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ include(${CMAKE_MODULE_PATH}/get_version.cmake) # Determine project version

include(GNUInstallDirs) # Define GNU standard installation directories

# Define install directory /usr/local/share
# Define install directory for st-link shared files
cmake_host_system_information(RESULT OS_NAME QUERY OS_NAME)
message(STATUS "Checking for OS_NAME: ${OS_NAME}")

Expand Down Expand Up @@ -154,10 +154,17 @@ include_directories(${PROJECT_BINARY_DIR}/inc) # contains version.h

include_directories(src)
include_directories(src/st-flash)
include_directories(src/st-info)
include_directories(src/st-trace)
include_directories(src/st-util)
include_directories(src/stlink-lib)

## Set installation directory for header files
if (WIN32)
set(STLINK_INCLUDE_PATH ${CMAKE_INSTALL_INCLUDEDIR} CACHE PATH "Main include install directory")
else ()
set(STLINK_INCLUDE_PATH ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME} CACHE PATH "Main include install directory")
endif ()

## Subordinate CMakeLists for version config & header installation
add_subdirectory(inc)
Expand All @@ -172,10 +179,10 @@ set(STLINK_HEADERS
src/stlink-lib/chipid.h
src/stlink-lib/commands.h
src/stlink-lib/common_flash.h
src/stlink-lib/common.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/map_file.h
src/stlink-lib/md5.h
Expand All @@ -194,6 +201,7 @@ set(STLINK_SOURCE
src/stlink-lib/helper.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
Expand Down Expand Up @@ -271,6 +279,15 @@ install(TARGETS ${STLINK_LIB_SHARED}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

# Copy libusb DLL-library to binary output folder
if (WIN32)
file(COPY ${LIBUSB_WIN_OUTPUT_FOLDER}/MinGW64/dll/libusb-1.0.dll
DESTINATION ${CMAKE_INSTALL_BINDIR})
file(COPY ${LIBUSB_WIN_OUTPUT_FOLDER}/MinGW64/dll/libusb-1.0.dll
DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR})
endif ()


###
# Static library
###
Expand Down Expand Up @@ -365,7 +382,11 @@ endif ()
###

# MCU configuration files
if (WIN32)
set(CMAKE_CHIPS_DIR ${CMAKE_INSTALL_PREFIX}/config/chips)
else ()
set(CMAKE_CHIPS_DIR ${CMAKE_INSTALL_SHAREDIR}/${PROJECT_NAME}/chips)
endif ()
add_definitions( -DSTLINK_CHIPS_DIR="${CMAKE_CHIPS_DIR}" )
file(GLOB CHIP_FILES ${CMAKE_SOURCE_DIR}/config/chips/*.chip)
install(FILES ${CHIP_FILES} DESTINATION ${CMAKE_CHIPS_DIR})
Expand Down
2 changes: 1 addition & 1 deletion config/chips/H5xx.chip
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
dev_type STM32H5xx
ref_manual_id 0481
chip_id 0x484 // STM32_CHIPID_H5xx
flash_type L5_U5 // ?
flash_type L5_U5_H5
flash_size_reg 0x08fff80c
flash_pagesize 0x2000 // 8 KB
sram_size 0xa0000 // 640 KB
Expand Down
2 changes: 1 addition & 1 deletion config/chips/L5x5xx.chip
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
dev_type STM32L5x2xx
ref_manual_id 0438
chip_id 0x472 // STM32_CHIPID_L5x2xx
flash_type L5_U5
flash_type L5_U5_H5
flash_size_reg 0x0bfa05e0
flash_pagesize 0x1000 // 4 KB
sram_size 0x40000 // 256 KB
Expand Down
2 changes: 1 addition & 1 deletion config/chips/U5x5.chip
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
dev_type STM32U5x5
ref_manual_id 0456
chip_id 0x482 // STM32_CHIPID_U5x5
flash_type L5_U5
flash_type L5_U5_H5
flash_size_reg 0x0bfa07a0
flash_pagesize 0x2000 // 8 KB
sram_size 0xc4800 // 786 KB
Expand Down
16 changes: 9 additions & 7 deletions doc/compiling.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ On Windows users should ensure that the following software is installed:

- `git` (_optional, but recommended_)
- `cmake`
- `MinGW-w64` (7.0.0 or later) with GCC toolchain 8.1.0
- `7-zip`
- `MinGW-w64`

### Installation

Expand All @@ -17,13 +18,13 @@ On Windows users should ensure that the following software is installed:
Ensure that you add cmake to the $PATH system variable when following the instructions by the setup assistant.
3. Install

- _EITHER_: **MinGW-w64** from <https://sourceforge.net/projects/mingw-w64> (mingw-w64-install.exe)<br />
- _OR_: **MSVC toolchain** from Visual Studio Build Tools 2019
- _EITHER_: Download **MinGW-w64** from <hhttp://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-win32/sjlj/x86_64-8.1.0-release-win32-sjlj-rt_v6-rev0.7z>. Extract content to `C:\mingw-w64\` and add `C:\mingw-w64\bin\` to PATH-Variable.<br />
- _OR_: **MSVC toolchain** from Visual Studio Build Tools 2019

4. Create a new destination folder at a place of your choice
5. Open the command-line (cmd.exe) and execute `cd C:\$Path-to-your-destination-folder$\`
6. Fetch the project sourcefiles by running `git clone https://github.com/stlink-org/stlink.git`from the command-line (cmd.exe)<br />
or download the stlink zip-sourcefolder from the Release page on GitHub
or download and extract the stlink zip-sourcefolder from the Release page on GitHub.

#### MSVC toolchain - minimal installation

Expand All @@ -48,11 +49,12 @@ Visual Studio IDE is not necessary, only Windows SDK & build tools are required

#### MinGW-w64

1. Use the command-line to move to the `scripts` directory within the source-folder: `cd stlink\scripts\`
2. Execute `./mingw64-build.bat`
1. Open command-line with administrator privileges
2. Move to the `stlink` directory
3. Execute `mingw64-build.bat`

NOTE:<br />
Per default the build script (currently) uses `C:\Program Files\mingw-w64\x86_64-8.1.0-release-win32-sjlj-rt_v6-rev0\mingw64\bin`.<br />
Per default the build script (currently) uses `C:\mingw-w64\x86_64-8.1.0-release-win32-sjlj-rt_v6-rev0\mingw64\bin`.<br />
When installing different toolchains make sure to update the path in the `mingw64-build.bat`.<br />
This can be achieved by opening the .bat file with a common text editor.

Expand Down
2 changes: 1 addition & 1 deletion inc/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
int32_t (*set_swdclk) (stlink_t * stl, int32_t freq_khz);
int32_t (*trace_enable) (stlink_t * sl, uint32_t frequency);
int32_t (*trace_disable) (stlink_t * sl);
int32_t (*trace_read) (stlink_t * sl, uint8_t* buf, size_t size);
int32_t (*trace_read) (stlink_t * sl, uint8_t* buf, uint32_t size);
} stlink_backend_t;

#endif // BACKEND_H
43 changes: 13 additions & 30 deletions inc/stlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include <stddef.h>
#include <stdbool.h>

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

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -210,22 +210,22 @@ struct _stlink {
// stlink_chipid_params.flash_type, set by stlink_load_device_params(), values: STM32_FLASH_TYPE_xx

stm32_addr_t flash_base; // STM32_FLASH_BASE, set by stlink_load_device_params()
size_t flash_size; // calculated by stlink_load_device_params()
size_t flash_pgsz; // stlink_chipid_params.flash_pagesize, set by stlink_load_device_params()
uint32_t flash_size; // calculated by stlink_load_device_params()
uint32_t flash_pgsz; // stlink_chipid_params.flash_pagesize, set by stlink_load_device_params()

/* sram settings */
stm32_addr_t sram_base; // STM32_SRAM_BASE, set by stlink_load_device_params()
size_t sram_size; // stlink_chipid_params.sram_size, set by stlink_load_device_params()
uint32_t sram_size; // stlink_chipid_params.sram_size, set by stlink_load_device_params()

/* option settings */
stm32_addr_t option_base;
size_t option_size;
uint32_t option_size;

// 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)
stm32_addr_t sys_base; // stlink_chipid_params.bootrom_base, set by stlink_load_device_params()
size_t sys_size; // stlink_chipid_params.bootrom_size, set by stlink_load_device_params()
uint32_t sys_size; // stlink_chipid_params.bootrom_size, set by stlink_load_device_params()

struct stlink_version_ version;

Expand All @@ -234,6 +234,8 @@ struct _stlink {
uint32_t max_trace_freq; // set by stlink_open_usb()
};

/* Functions defined in common.c */

int32_t stlink_enter_swd_mode(stlink_t *sl);
int32_t stlink_enter_jtag_mode(stlink_t *sl);
int32_t stlink_exit_debug_mode(stlink_t *sl);
Expand Down Expand Up @@ -262,25 +264,14 @@ int32_t stlink_target_voltage(stlink_t *sl);
int32_t stlink_set_swdclk(stlink_t *sl, int32_t freq_khz);
int32_t stlink_trace_enable(stlink_t* sl, uint32_t frequency);
int32_t stlink_trace_disable(stlink_t* sl);
int32_t stlink_trace_read(stlink_t* sl, uint8_t* buf, size_t size);
int32_t stlink_erase_flash_mass(stlink_t* sl);
int32_t stlink_erase_flash_section(stlink_t *sl, stm32_addr_t base_addr, size_t size, bool align_size);
int32_t stlink_write_flash(stlink_t* sl, stm32_addr_t address, uint8_t* data, uint32_t length, uint8_t eraseonly);
int32_t stlink_parse_ihex(const char* path, uint8_t erased_pattern, uint8_t * * mem, size_t * size, uint32_t * begin);
int32_t stlink_trace_read(stlink_t* sl, uint8_t* buf, uint32_t size);
int32_t stlink_parse_ihex(const char* path, uint8_t erased_pattern, uint8_t * * mem, uint32_t * size, uint32_t * begin);
uint8_t stlink_get_erased_pattern(stlink_t *sl);
int32_t stlink_mwrite_flash(stlink_t *sl, uint8_t* data, uint32_t length, stm32_addr_t addr);
int32_t stlink_fwrite_flash(stlink_t *sl, const char* path, stm32_addr_t addr);
int32_t stlink_mwrite_sram(stlink_t *sl, uint8_t* data, uint32_t length, stm32_addr_t addr);
int32_t stlink_fwrite_sram(stlink_t *sl, const char* path, stm32_addr_t addr);
int32_t stlink_verify_write_flash(stlink_t *sl, stm32_addr_t address, uint8_t *data, uint32_t length);

//int32_t stlink_chip_id(stlink_t *sl, uint32_t *chip_id);
int32_t stlink_cpu_id(stlink_t *sl, cortex_m3_cpuid_t *cpuid);

int32_t stlink_erase_flash_page(stlink_t* sl, stm32_addr_t flashaddr);
uint32_t stlink_calculate_pagesize(stlink_t *sl, uint32_t flashaddr);
int32_t stlink_check_address_range_validity(stlink_t *sl, stm32_addr_t addr, size_t size);
int32_t stlink_check_address_alignment(stlink_t *sl, stm32_addr_t addr);
uint16_t read_uint16(const unsigned char *c, const int32_t pt);
//void stlink_core_stat(stlink_t *sl);
void stlink_print_data(stlink_t *sl);
Expand All @@ -289,21 +280,13 @@ uint32_t read_uint32(const unsigned char *c, const int32_t pt);
void write_uint32(unsigned char* buf, uint32_t ui);
void write_uint16(unsigned char* buf, uint16_t ui);
bool stlink_is_core_halted(stlink_t *sl);
int32_t write_buffer_to_sram(stlink_t *sl, flash_loader_t* fl, const uint8_t* buf, size_t size);
int32_t write_buffer_to_sram(stlink_t *sl, flash_loader_t* fl, const uint8_t* buf, uint32_t size);
int32_t write_loader_to_sram(stlink_t *sl, stm32_addr_t* addr, size_t* size);
int32_t stlink_fread(stlink_t* sl, const char* path, bool is_ihex, stm32_addr_t addr, size_t size);
int32_t stlink_fread(stlink_t* sl, const char* path, bool is_ihex, stm32_addr_t addr, uint32_t size);
int32_t stlink_load_device_params(stlink_t *sl);

int32_t stlink_target_connect(stlink_t *sl, enum connect_type connect);

#include <sg.h>
#include <usb.h>
#include <register.h>
#include <commands.h>
#include <chipid.h>
#include <flash_loader.h>
#include <version.h>
#include <logging.h>

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion inc/stm32.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ enum stm32_flash_type {
STM32_FLASH_TYPE_H7 = 7,
STM32_FLASH_TYPE_L0_L1 = 8,
STM32_FLASH_TYPE_L4 = 9,
STM32_FLASH_TYPE_L5_U5 = 10,
STM32_FLASH_TYPE_L5_U5_H5 = 10,
STM32_FLASH_TYPE_WB_WL = 11,
};

Expand Down
5 changes: 3 additions & 2 deletions mingw64-build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

mkdir build-mingw
cd build-mingw
set PATH=C:\Program Files (x86)\CMake\bin;C:\Program Files\CMake\bin;C:\Program Files\mingw-w64\x86_64-8.1.0-win32-sjlj-rt_v6-rev0\mingw64\bin;%PATH%
set PATH=C:\Program Files (x86)\CMake\bin;C:\Program Files\CMake\bin;C:\mingw-w64\x86_64-8.1.0-win32-sjlj-rt_v6-rev0\mingw64\bin;%PATH%
cmake -G "MinGW Makefiles" ..
mingw32-make
mingw32-make install DESTDIR=install
mingw32-make install
mingw32-make package
cd ..
34 changes: 22 additions & 12 deletions src/st-flash/flash.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
/* Simple wrapper around the stlink_flash_write function */
/*
* File: flash.c
*
* Tool st-flash - Simple wrapper around the stlink_flash_write function
*/

#include <stdio.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>

#if defined(_WIN32)
#include <win32_socket.h>
#else
#include <unistd.h>
#endif // _WIN32

#include <stm32.h>
#include <stlink.h>
#include "flash.h"
#include "flash_opts.h"

#include <chipid.h>
#include <common_flash.h>
#include <map_file.h>
#include <option_bytes.h>

#include "flash.h"
#include <usb.h>

static stlink_t *connected_stlink = NULL;

Expand Down Expand Up @@ -83,7 +93,7 @@ int32_t main(int32_t ac, char** av) {

if ( o.flash_size != 0u && o.flash_size != sl->flash_size ) {
sl->flash_size = o.flash_size;
printf("Forcing flash size: --flash=0x%08X\n", (uint32_t)sl->flash_size);
printf("Forcing flash size: --flash=0x%08X\n", sl->flash_size);
}

sl->verbose = o.log_level;
Expand All @@ -106,7 +116,7 @@ int32_t main(int32_t ac, char** av) {
}

if (o.cmd == FLASH_CMD_WRITE) {
size_t size = 0;
uint32_t size = 0;

// write
if (o.format == FLASH_FORMAT_IHEX) {
Expand All @@ -119,7 +129,7 @@ int32_t main(int32_t ac, char** av) {
}
if ((o.addr >= sl->flash_base) && (o.addr < sl->flash_base + sl->flash_size)) {
if (o.format == FLASH_FORMAT_IHEX) {
err = stlink_mwrite_flash(sl, mem, (uint32_t)size, o.addr);
err = stlink_mwrite_flash(sl, mem, size, o.addr);
} else {
err = stlink_fwrite_flash(sl, o.filename, o.addr);
}
Expand All @@ -130,7 +140,7 @@ int32_t main(int32_t ac, char** av) {
}
} else if ((o.addr >= sl->sram_base) && (o.addr < sl->sram_base + sl->sram_size)) {
if (o.format == FLASH_FORMAT_IHEX) {
err = stlink_mwrite_sram(sl, mem, (uint32_t)size, o.addr);
err = stlink_mwrite_sram(sl, mem, size, o.addr);
} else {
err = stlink_fwrite_sram(sl, o.filename, o.addr);
}
Expand Down Expand Up @@ -221,10 +231,10 @@ int32_t main(int32_t ac, char** av) {
goto on_error;
}
} else if (o.area == FLASH_OPTION_BYTES) {
size_t remaining_option_length = sl->option_size / 4;
uint32_t remaining_option_length = sl->option_size / 4;
DLOG("@@@@ Read %u (%#x) option bytes from %#10x\n",
(uint32_t)remaining_option_length,
(uint32_t)remaining_option_length,
remaining_option_length,
remaining_option_length,
sl->option_base);

uint32_t option_byte = 0;
Expand Down

0 comments on commit d4b53b0

Please sign in to comment.