Skip to content

Commit

Permalink
Merge pull request #2 from texane/master
Browse files Browse the repository at this point in the history
Update fork with recent upstream changes.
  • Loading branch information
WRansohoff authored Jul 2, 2019
2 parents 2117226 + 84f63d2 commit 91cda2b
Show file tree
Hide file tree
Showing 20 changed files with 199 additions and 73 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ set(STLINK_SOURCE

if (WIN32 OR MSYS OR MINGW)
set (STLINK_SOURCE "${STLINK_SOURCE};src/mmap.c;src/mingw/mingw.c")
set (STLINK_HEADERS "${STLINK_HEADERS};src/mingw/mingw.h")
endif ()

include_directories(${LIBUSB_INCLUDE_DIR})
Expand Down
25 changes: 25 additions & 0 deletions cmake/linux-mingw32.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Sample toolchain file for building for Windows from an Debian/Ubuntu Linux system.
#
# Typical usage:
# *) install cross compiler: `sudo apt-get install mingw-w64`
# *) cd build
# *) cmake -DCMAKE_TOOLCHAIN_FILE=./cmake/linux-mingw32.cmake ..

set(CMAKE_SYSTEM_NAME Windows)
set(TOOLCHAIN_PREFIX i686-w64-mingw32)

# cross compilers to use for C and C++
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)

# target environment on the build host system
# set 1st to dir with the cross compiler's C/C++ headers/libs
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX})

# modify default behavior of FIND_XXX() commands to
# search for headers/libs in the target environment and
# search for programs in the build host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
25 changes: 25 additions & 0 deletions cmake/linux-mingw64.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Sample toolchain file for building for Windows from an Ubuntu Linux system.
#
# Typical usage:
# *) install cross compiler: `sudo apt-get install mingw-w64`
# *) cd build
# *) cmake -DCMAKE_TOOLCHAIN_FILE=./cmake/linux-mingw64.cmake ..

set(CMAKE_SYSTEM_NAME Windows)
set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)

# cross compilers to use for C and C++
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)

# target environment on the build host system
# set 1st to dir with the cross compiler's C/C++ headers/libs
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX})

# modify default behavior of FIND_XXX() commands to
# search for headers/libs in the target environment and
# search for programs in the build host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
2 changes: 1 addition & 1 deletion cmake/modules/Find7Zip.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
find_program(ZIP_EXECUTABLE NAMES 7z.exe
find_program(ZIP_EXECUTABLE NAMES 7z.exe p7zip
HINTS
"C:\\Program Files\\7-Zip\\"
"C:\\Program Files (x86)\\7-Zip\\"
Expand Down
29 changes: 24 additions & 5 deletions cmake/modules/FindLibUSB.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,47 @@ if(NOT LIBUSB_FOUND)
)
endif()
file(MAKE_DIRECTORY ${LIBUSB_WIN_OUTPUT_FOLDER})
execute_process(COMMAND ${ZIP_EXECUTABLE} x -y ${LIBUSB_WIN_ARCHIVE_PATH} -o${LIBUSB_WIN_OUTPUT_FOLDER})

if(${ZIP_EXECUTABLE} MATCHES "p7zip")
execute_process(COMMAND ${ZIP_EXECUTABLE} -d --keep -f ${LIBUSB_WIN_ARCHIVE_PATH} WORKING_DIRECTORY ${LIBUSB_WIN_OUTPUT_FOLDER})
else()
execute_process(COMMAND ${ZIP_EXECUTABLE} x -y ${LIBUSB_WIN_ARCHIVE_PATH} -o${LIBUSB_WIN_OUTPUT_FOLDER})
endif()

FIND_PATH(LIBUSB_INCLUDE_DIR NAMES libusb.h
HINTS ${LIBUSB_WIN_OUTPUT_FOLDER}/include
PATH_SUFFIXES libusb-1.0
NO_DEFAULT_PATH
NO_CMAKE_FIND_ROOT_PATH
)

if (MSYS OR MINGW)
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
find_library(LIBUSB_LIBRARY NAMES ${LIBUSB_NAME}
HINTS ${LIBUSB_WIN_OUTPUT_FOLDER}/MinGW64/static)
HINTS ${LIBUSB_WIN_OUTPUT_FOLDER}/MinGW64/static
NO_DEFAULT_PATH
NO_CMAKE_FIND_ROOT_PATH
)
else ()
find_library(LIBUSB_LIBRARY NAMES ${LIBUSB_NAME}
HINTS ${LIBUSB_WIN_OUTPUT_FOLDER}/MinGW32/static)
HINTS ${LIBUSB_WIN_OUTPUT_FOLDER}/MinGW32/static
NO_DEFAULT_PATH
NO_CMAKE_FIND_ROOT_PATH
)
endif ()
elseif(MSVC)
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
find_library(LIBUSB_LIBRARY NAMES ${LIBUSB_NAME}
HINTS ${LIBUSB_WIN_OUTPUT_FOLDER}/MS64/dll)
HINTS ${LIBUSB_WIN_OUTPUT_FOLDER}/MS64/dll
NO_DEFAULT_PATH
NO_CMAKE_FIND_ROOT_PATH
)
else ()
find_library(LIBUSB_LIBRARY NAMES ${LIBUSB_NAME}
HINTS ${LIBUSB_WIN_OUTPUT_FOLDER}/MS32/dll)
HINTS ${LIBUSB_WIN_OUTPUT_FOLDER}/MS32/dll
NO_DEFAULT_PATH
NO_CMAKE_FIND_ROOT_PATH
)
endif ()
endif ()
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Libusb DEFAULT_MSG LIBUSB_LIBRARY LIBUSB_INCLUDE_DIR)
Expand Down
21 changes: 21 additions & 0 deletions doc/compiling.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,24 @@ NOTES: This solution will link to the dll version of libusb-1.0. To debug or ru
be either on the path, or in the same folder as the executable. It can be copied from here:
`build\3thparty\libusb-1.0.21\MS32\dll\libusb-1.0.dll`.

## Linux (MinGW64)

### Prequistes

* 7Zip
* CMake 2.8 or higher
* MinGW64 GCC toolchain (5.3.0)

### Installation (Debian / Ubuntu)

sudo apt install p7zip mingw-w64

### Building

These instructions are for a 32bit version.

```sh
cd <source-dir>
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=./cmake/linux-mingw32.cmake -S . -B ./build/linux-mingw32
cmake --build ./build/linux-mingw32 --target all
```
4 changes: 3 additions & 1 deletion include/stlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ extern "C" {
#define STLINK_SWDCLK_15KHZ_DIVISOR 265
#define STLINK_SWDCLK_5KHZ_DIVISOR 798

#define STLINK_SERIAL_MAX_SIZE 64

/* Enough space to hold both a V2 command or a V1 command packaged as generic scsi*/
#define C_BUF_LEN 32

Expand Down Expand Up @@ -144,7 +146,7 @@ typedef struct flash_loader {
uint32_t chip_id;
int core_stat;

char serial[16];
char serial[STLINK_SERIAL_MAX_SIZE];
int serial_size;

enum stlink_flash_type flash_type;
Expand Down
2 changes: 1 addition & 1 deletion include/stlink/tools/flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct flash_opts
{
enum flash_cmd cmd;
const char* devname;
uint8_t serial[16];
uint8_t serial[STLINK_SERIAL_MAX_SIZE];
const char* filename;
stm32_addr_t addr;
size_t size;
Expand Down
2 changes: 1 addition & 1 deletion include/stlink/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extern "C" {
* @retval NULL Error while opening the stlink
* @retval !NULL Stlink found and ready to use
*/
stlink_t *stlink_open_usb(enum ugly_loglevel verbose, bool reset, char serial[16]);
stlink_t *stlink_open_usb(enum ugly_loglevel verbose, bool reset, char serial[STLINK_SERIAL_MAX_SIZE]);
size_t stlink_probe_usb(stlink_t **stdevs[]);
void stlink_probe_usb_free(stlink_t **stdevs[], size_t size);

Expand Down
14 changes: 14 additions & 0 deletions src/chipid.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,20 @@ static const struct stlink_chipid_params devices[] = {
.bootrom_base = 0x1fff0000, // 3.3.1, pg 99
.bootrom_size = 0x7000 // 28k (per bank), same source as base (pg 99)
},
{
// STLINK_CHIPID_STM32_L41X
// From RM0394 Rev 4 and DS12469 Rev 5
.chip_id = STLINK_CHIPID_STM32_L41X,
.description = "L41x device",
.flash_type = STLINK_FLASH_TYPE_L4,
.flash_size_reg = 0x1fff75e0, // "Flash size data register" (RM0394, sec 47.2, page 1586)
.flash_pagesize = 0x800, // 2K (DS12469, sec 3.4, page 17)
// SRAM1 is 32k at 0x20000000
// SRAM2 is 8k at 0x10000000 and 0x20008000 (DS12469, sec 3.5, page 18)
.sram_size = 0xa000, // 40K (DS12469, sec 3.5, page 18)
.bootrom_base = 0x1fff0000, // System Memory (RM0394, sec 3.3.1, table 8)
.bootrom_size = 0x7000 // 28k, same source as base
},
{
// STLINK_CHIPID_STM32_L43X
// From RM0392.
Expand Down
14 changes: 7 additions & 7 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ static inline unsigned int is_flash_locked(stlink_t *sl) {
else
cr_lock_shift = FLASH_CR_LOCK;

return cr & (1 << cr_lock_shift);
return cr & (1u << cr_lock_shift);
}

static void unlock_flash(stlink_t *sl) {
Expand Down Expand Up @@ -352,11 +352,11 @@ static void lock_flash(stlink_t *sl) {
cr_lock_shift = FLASH_CR_LOCK;
}

n = read_flash_cr(sl) | (1 << cr_lock_shift);
n = read_flash_cr(sl) | (1u << cr_lock_shift);
stlink_write_debug32(sl, cr_reg, n);

if (sl->flash_type == STLINK_FLASH_TYPE_F1_XL) {
n = read_flash_cr2(sl) | (1 << cr_lock_shift);
n = read_flash_cr2(sl) | (1u << cr_lock_shift);
stlink_write_debug32(sl, FLASH_CR2, n);
}
}
Expand Down Expand Up @@ -2524,15 +2524,15 @@ int stlink_write_option_bytes(stlink_t *sl, stm32_addr_t addr, uint8_t* base, ui

/* Unlock flash if necessary (ref manuel page 52) */
stlink_read_debug32(sl, STM32G0_FLASH_CR, &val);
if ((val & (1 << STM32G0_FLASH_CR_LOCK))) {
if ((val & (1u << STM32G0_FLASH_CR_LOCK))) {

/* disable flash write protection. */
stlink_write_debug32(sl, STM32G0_FLASH_KEYR, 0x45670123);
stlink_write_debug32(sl, STM32G0_FLASH_KEYR, 0xCDEF89AB);

// check that the lock is no longer set.
stlink_read_debug32(sl, STM32G0_FLASH_CR, &val);
if ((val & (1 << STM32G0_FLASH_CR_LOCK))) {
if ((val & (1u << STM32G0_FLASH_CR_LOCK))) {
ELOG("Flash unlock failed! System reset required to be able to unlock it again!\n");
return -1;
}
Expand Down Expand Up @@ -2578,11 +2578,11 @@ int stlink_write_option_bytes(stlink_t *sl, stm32_addr_t addr, uint8_t* base, ui

/* Re-lock option bytes */
stlink_read_debug32(sl, STM32G0_FLASH_CR, &val);
val |= (1 << STM32G0_FLASH_CR_OPTLOCK);
val |= (1u << STM32G0_FLASH_CR_OPTLOCK);
stlink_write_debug32(sl, STM32G0_FLASH_CR, val);
/* Re-lock flash. */
stlink_read_debug32(sl, STM32G0_FLASH_CR, &val);
val |= (1 << STM32G0_FLASH_CR_LOCK);
val |= (1u << STM32G0_FLASH_CR_LOCK);
stlink_write_debug32(sl, STM32G0_FLASH_CR, val);

return 0;
Expand Down
2 changes: 2 additions & 0 deletions src/flash_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ int stlink_flash_loader_write_to_sram(stlink_t *sl, stm32_addr_t* addr, size_t*
loader_code = loader_code_stm32l;
loader_size = sizeof(loader_code_stm32l);
} else if (sl->core_id == STM32VL_CORE_ID
|| sl->chip_id == STLINK_CHIPID_STM32_F1_MEDIUM
|| sl->chip_id == STLINK_CHIPID_STM32_F3
|| sl->chip_id == STLINK_CHIPID_STM32_F3_SMALL
|| sl->chip_id == STLINK_CHIPID_STM32_F303_HIGH
Expand Down Expand Up @@ -306,6 +307,7 @@ int stlink_flash_loader_write_to_sram(stlink_t *sl, stm32_addr_t* addr, size_t*
loader_code = loader_code_stm32f0;
loader_size = sizeof(loader_code_stm32f0);
} else if ((sl->chip_id == STLINK_CHIPID_STM32_L4) ||
(sl->chip_id == STLINK_CHIPID_STM32_L41X) ||
(sl->chip_id == STLINK_CHIPID_STM32_L43X) ||
(sl->chip_id == STLINK_CHIPID_STM32_L46X) ||
(sl->chip_id == STLINK_CHIPID_STM32_L4RX) ||
Expand Down
15 changes: 14 additions & 1 deletion src/gdbserver/gdb-remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ int gdb_recv_packet(int fd, char** buffer) {
char* packet_buffer = malloc(packet_size);
unsigned state;

if(packet_buffer == NULL)
return -2;

start:
state = 0;
packet_idx = 0;
/*
* 0: waiting $
* 1: data, waiting #
Expand All @@ -77,6 +81,7 @@ int gdb_recv_packet(int fd, char** buffer) {
char c;
while(state != 4) {
if(read(fd, &c, 1) != 1) {
free(packet_buffer);
return -2;
}

Expand All @@ -98,7 +103,13 @@ int gdb_recv_packet(int fd, char** buffer) {

if(packet_idx == packet_size) {
packet_size += ALLOC_STEP;
packet_buffer = realloc(packet_buffer, packet_size);
void* p = realloc(packet_buffer, packet_size);
if(p != NULL)
packet_buffer = p;
else {
free(packet_buffer);
return -2;
}
}
}
break;
Expand All @@ -119,13 +130,15 @@ int gdb_recv_packet(int fd, char** buffer) {
if(recv_cksum_int != cksum) {
char nack = '-';
if(write(fd, &nack, 1) != 1) {
free(packet_buffer);
return -2;
}

goto start;
} else {
char ack = '+';
if(write(fd, &ack, 1) != 1) {
free(packet_buffer);
return -2;
}
}
Expand Down
Loading

0 comments on commit 91cda2b

Please sign in to comment.