Skip to content

Commit

Permalink
Grouped source files for win32
Browse files Browse the repository at this point in the history
  • Loading branch information
Nightwalker-87 committed Jun 5, 2020
1 parent c7041cc commit e74c6ec
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 73 deletions.
50 changes: 26 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,38 @@ endif ()
find_package(libusb REQUIRED)

## Check for system-specific additional header files and libraries

include(CheckIncludeFile)
include(CheckLibraryExists)

CHECK_LIBRARY_EXISTS(ssp __stack_chk_fail "" _stack_chk_fail_exists)
if (_stack_chk_fail_exists)
if (WIN32)
set(SSP_LIB -static ssp)
else ()
set(SSP_LIB ssp)
endif ()
else ()
set(SSP_LIB "")
endif ()

CHECK_INCLUDE_FILE(sys/mman.h STLINK_HAVE_SYS_MMAN_H)
if (STLINK_HAVE_SYS_MMAN_H)
add_definitions(-DSTLINK_HAVE_SYS_MMAN_H)
endif ()
else ()
include_directories(src/win32/mmap)
set(STLINK_SOURCE "${STLINK_SOURCE};src/win32/mmap/mmap.c")
set(STLINK_HEADERS "${STLINK_HEADERS};src/win32/mmap/mmap.h")
endif()

CHECK_INCLUDE_FILE(unistd.h STLINK_HAVE_UNISTD_H)
if (STLINK_HAVE_UNISTD_H)
add_definitions(-DSTLINK_HAVE_UNISTD_H)
endif ()

include(CheckLibraryExists)

CHECK_LIBRARY_EXISTS(ssp __stack_chk_fail "" _stack_chk_fail_exists)
if (_stack_chk_fail_exists)
if(WIN32)
set(SSP_LIB -static ssp)
else()
set(SSP_LIB ssp)
endif()
else ()
set(SSP_LIB "")
if (MSVC)
# Use string.h rather than strings.h and disable annoying warnings
add_definitions(-DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS /wd4710)
endif ()


Expand All @@ -85,7 +94,6 @@ include_directories(${PROJECT_SOURCE_DIR}/inc) # contains top-level header files
include_directories(${PROJECT_BINARY_DIR}/inc) # contains version.h

include_directories(src)
include_directories(src/mmap)
include_directories(src/st-flash)
include_directories(src/stlink-lib)

Expand All @@ -112,17 +120,10 @@ set(STLINK_SOURCE
src/stlink-lib/usb.c
)

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

if (MSVC)
if (WIN32 OR MINGW OR MSYS OR MSVC) # ToDo: Check if WIN32 is necessary here
include_directories(src/win32)
include_directories(src/getopt)
# Use string.h rather than strings.h and disable annoying warnings
add_definitions(-DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS /wd4710)
set(STLINK_SOURCE "${STLINK_SOURCE};src/win32/mingw.c")
set(STLINK_HEADERS "${STLINK_HEADERS};src/win32/mingw.h")
endif ()

## Include test execution for test-targets for target Debug
Expand Down Expand Up @@ -225,7 +226,8 @@ set(ST-UTIL_SOURCES src/st-util/gdb-remote.c src/st-util/gdb-server.c src/st-uti

if (MSVC)
# Add getopt to sources
set(ST-UTIL_SOURCES "${ST-UTIL_SOURCES};src/getopt/getopt.c")
include_directories(src/win32/getopt)
set(ST-UTIL_SOURCES "${ST-UTIL_SOURCES};src/win32/getopt/getopt.c")
endif ()

add_executable(st-flash ${ST-FLASH_SOURCES})
Expand Down
100 changes: 51 additions & 49 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
#include <sys/stat.h>

#include <stlink.h>
#include <mmap.h>
#include <logging.h>
#include <md5.h>

#ifdef STLINK_HAVE_SYS_MMAN_H
#include <sys/mman.h>
#else
#include <mmap.h>
#endif

#ifndef O_BINARY
#define O_BINARY 0
#endif
Expand Down Expand Up @@ -193,24 +198,26 @@
#define STM32L4_FLASH_CR (FLASH_REGS_ADDR + 0x14)
#define STM32L4_FLASH_OPTR (FLASH_REGS_ADDR + 0x20)

#define STM32L4_FLASH_SR_BSY 16
#define STM32L4_FLASH_SR_ERRMASK 0x3f8 /* SR [9:3] */

#define STM32L4_FLASH_CR_LOCK 31 /* Lock control register */
#define STM32L4_FLASH_CR_OPTLOCK 30 /* Lock option bytes */
#define STM32L4_FLASH_CR_PG 0 /* Program */
#define STM32L4_FLASH_CR_PER 1 /* Page erase */
#define STM32L4_FLASH_CR_MER1 2 /* Bank 1 erase */
#define STM32L4_FLASH_CR_MER2 15 /* Bank 2 erase */
#define STM32L4_FLASH_CR_STRT 16 /* Start command */
#define STM32L4_FLASH_CR_OPTSTRT 17 /* Start writing option bytes */
#define STM32L4_FLASH_CR_BKER 11 /* Bank select for page erase */
#define STM32L4_FLASH_CR_PNB 3 /* Page number (8 bits) */
#define STM32L4_FLASH_CR_OBL_LAUNCH 27 /* Option bytes reload */
#define STM32L4_FLASH_SR_BSY 16
#define STM32L4_FLASH_SR_ERRMASK 0x3f8 /* SR [9:3] */

#define STM32L4_FLASH_CR_LOCK 31 /* Lock control register */
#define STM32L4_FLASH_CR_OPTLOCK 30 /* Lock option bytes */
#define STM32L4_FLASH_CR_PG 0 /* Program */
#define STM32L4_FLASH_CR_PER 1 /* Page erase */
#define STM32L4_FLASH_CR_MER1 2 /* Bank 1 erase */
#define STM32L4_FLASH_CR_MER2 15 /* Bank 2 erase */
#define STM32L4_FLASH_CR_STRT 16 /* Start command */
#define STM32L4_FLASH_CR_OPTSTRT 17 /* Start writing option bytes */
#define STM32L4_FLASH_CR_BKER 11 /* Bank select for page erase */
#define STM32L4_FLASH_CR_PNB 3 /* Page number (8 bits) */
#define STM32L4_FLASH_CR_OBL_LAUNCH 27 /* Option bytes reload */
// Bits requesting flash operations (useful when we want to clear them)
#define STM32L4_FLASH_CR_OPBITS \
(uint32_t)((1lu<<STM32L4_FLASH_CR_PG) | (1lu<<STM32L4_FLASH_CR_PER) \
| (1lu<<STM32L4_FLASH_CR_MER1) | (1lu<<STM32L4_FLASH_CR_MER1))
#define STM32L4_FLASH_CR_OPBITS \
(uint32_t)((1lu<<STM32L4_FLASH_CR_PG) | \
(1lu<<STM32L4_FLASH_CR_PER) | \
(1lu<<STM32L4_FLASH_CR_MER1) | \
(1lu<<STM32L4_FLASH_CR_MER1))
// Page is fully specified by BKER and PNB
#define STM32L4_FLASH_CR_PAGEMASK (uint32_t)(0x1fflu << STM32L4_FLASH_CR_PNB)

Expand Down Expand Up @@ -583,8 +590,7 @@ static int lock_flash_option(stlink_t *sl) {
return 0;
}

static int unlock_flash_option(stlink_t *sl)
{
static int unlock_flash_option(stlink_t *sl) {
uint32_t optkey_reg;
uint32_t optkey1 = FLASH_OPTKEY1;
uint32_t optkey2 = FLASH_OPTKEY2;
Expand Down Expand Up @@ -648,11 +654,11 @@ static void set_flash_cr_pg(stlink_t *sl) {

if (sl->flash_type == STLINK_FLASH_TYPE_F4) {
cr_reg = FLASH_F4_CR;
x |= 1 << FLASH_CR_PG;
x |= (1 << FLASH_CR_PG);
} else if (sl->flash_type == STLINK_FLASH_TYPE_L4) {
cr_reg = STM32L4_FLASH_CR;
x &= ~STM32L4_FLASH_CR_OPBITS;
x |= 1 << STM32L4_FLASH_CR_PG;
x |= (1 << STM32L4_FLASH_CR_PG);
} else if (sl->flash_type == STLINK_FLASH_TYPE_G0 ||
sl->flash_type == STLINK_FLASH_TYPE_G4) {
cr_reg = STM32Gx_FLASH_CR;
Expand All @@ -662,7 +668,7 @@ static void set_flash_cr_pg(stlink_t *sl) {
x |= (1 << FLASH_CR_PG);
} else {
cr_reg = FLASH_CR;
x = 1 << FLASH_CR_PG;
x = (1 << FLASH_CR_PG);
}

stlink_write_debug32(sl, cr_reg, x);
Expand Down Expand Up @@ -704,7 +710,7 @@ static void set_flash_cr_per(stlink_t *sl) {
}

static void set_flash_cr2_per(stlink_t *sl) {
const uint32_t n = 1 << FLASH_CR_PER;
const uint32_t n = (1 << FLASH_CR_PER);
stlink_write_debug32(sl, FLASH_CR2, n);
}

Expand All @@ -728,12 +734,12 @@ static void set_flash_cr_mer(stlink_t *sl, bool v) {

if (sl->flash_type == STLINK_FLASH_TYPE_F4) {
cr_reg = FLASH_F4_CR;
cr_mer = 1 << FLASH_CR_MER;
cr_pg = 1 << FLASH_CR_PG;
cr_mer = (1 << FLASH_CR_MER);
cr_pg = (1 << FLASH_CR_PG);
} else if (sl->flash_type == STLINK_FLASH_TYPE_L4) {
cr_reg = STM32L4_FLASH_CR;
cr_mer = (1 << STM32L4_FLASH_CR_MER1) | (1 << STM32L4_FLASH_CR_MER2);
cr_pg = 1 << STM32L4_FLASH_CR_PG;
cr_pg = (1 << STM32L4_FLASH_CR_PG);
} else if (sl->flash_type == STLINK_FLASH_TYPE_G0 ||
sl->flash_type == STLINK_FLASH_TYPE_G4) {
cr_reg = STM32Gx_FLASH_CR;
Expand All @@ -748,8 +754,8 @@ static void set_flash_cr_mer(stlink_t *sl, bool v) {
cr_pg = (1 << FLASH_CR_PG);
} else {
cr_reg = FLASH_CR;
cr_mer = 1 << FLASH_CR_MER;
cr_pg = 1 << FLASH_CR_PG;
cr_mer = (1 << FLASH_CR_MER);
cr_pg = (1 << FLASH_CR_PG);
}

stlink_read_debug32(sl, cr_reg, &val);
Expand All @@ -767,8 +773,8 @@ static void set_flash_cr_mer(stlink_t *sl, bool v) {
}

static void set_flash_cr2_mer(stlink_t *sl, bool v) {
const uint32_t cr_pg = 1 << FLASH_CR_PER;
const uint32_t cr_mer = 1 << FLASH_CR_MER;
const uint32_t cr_pg = (1 << FLASH_CR_PER);
const uint32_t cr_mer = (1 << FLASH_CR_MER);
uint32_t val;

stlink_read_debug32(sl, FLASH_CR2, &val);
Expand All @@ -785,13 +791,13 @@ static void __attribute__((unused)) clear_flash_cr_mer(stlink_t *sl) {

if (sl->flash_type == STLINK_FLASH_TYPE_F4) {
cr_reg = FLASH_F4_CR;
cr_mer = 1 << FLASH_CR_MER;
cr_mer = (1 << FLASH_CR_MER);
} else if (sl->flash_type == STLINK_FLASH_TYPE_L4) {
cr_reg = STM32L4_FLASH_CR;
cr_mer = (1 << STM32L4_FLASH_CR_MER1) | (1 << STM32L4_FLASH_CR_MER2);
} else {
cr_reg = FLASH_CR;
cr_mer = 1 << FLASH_CR_MER;
cr_mer = (1 << FLASH_CR_MER);
}

stlink_read_debug32(sl, cr_reg, &val);
Expand All @@ -804,20 +810,20 @@ static void set_flash_cr_strt(stlink_t *sl) {

if (sl->flash_type == STLINK_FLASH_TYPE_F4) {
cr_reg = FLASH_F4_CR;
cr_strt = 1 << FLASH_F4_CR_STRT;
cr_strt = (1 << FLASH_F4_CR_STRT);
} else if (sl->flash_type == STLINK_FLASH_TYPE_L4) {
cr_reg = STM32L4_FLASH_CR;
cr_strt = 1 << STM32L4_FLASH_CR_STRT;
cr_strt = (1 << STM32L4_FLASH_CR_STRT);
} else if (sl->flash_type == STLINK_FLASH_TYPE_G0 ||
sl->flash_type == STLINK_FLASH_TYPE_G4) {
cr_reg = STM32Gx_FLASH_CR;
cr_strt = 1 << STM32Gx_FLASH_CR_STRT;
cr_strt = (1 << STM32Gx_FLASH_CR_STRT);
} else if (sl->flash_type == STLINK_FLASH_TYPE_WB) {
cr_reg = STM32WB_FLASH_CR;
cr_strt = 1 << STM32WB_FLASH_CR_STRT;
cr_strt = (1 << STM32WB_FLASH_CR_STRT);
} else {
cr_reg = FLASH_CR;
cr_strt = 1 << FLASH_CR_STRT;
cr_strt = (1 << FLASH_CR_STRT);
}

stlink_read_debug32(sl, cr_reg, &val);
Expand Down Expand Up @@ -2377,8 +2383,7 @@ int stlink_verify_write_flash(stlink_t *sl, stm32_addr_t address, uint8_t *data,

}

int stm32l1_write_half_pages(stlink_t *sl, stm32_addr_t addr, uint8_t* base, uint32_t len, uint32_t pagesize)
{
int stm32l1_write_half_pages(stlink_t *sl, stm32_addr_t addr, uint8_t* base, uint32_t len, uint32_t pagesize) {
unsigned int count;
unsigned int num_half_pages = len / pagesize;
uint32_t val;
Expand Down Expand Up @@ -2742,7 +2747,8 @@ int stlink_parse_ihex(const char* path, uint8_t erased_pattern, uint8_t * * mem,
uint32_t end = 0;
bool eof_found = false;

for (int scan = 0; (res == 0) && (scan < 2); ++scan) { // parse file two times - first to find memory range, second - to fill it
for (int scan = 0; (res == 0) && (scan < 2); ++scan) {
// parse file two times - first to find memory range, second - to fill it
if (scan == 1) {
if (!eof_found) {
ELOG("No EoF recond\n");
Expand Down Expand Up @@ -3015,8 +3021,7 @@ static int stlink_write_option_bytes_gx(stlink_t *sl, uint8_t* base, stm32_addr_
* @param base option bytes to write
* @return 0 on success, -ve on failure.
*/
static int stlink_write_option_bytes_l0(stlink_t *sl, uint8_t* base, stm32_addr_t addr, uint32_t len)
{
static int stlink_write_option_bytes_l0(stlink_t *sl, uint8_t* base, stm32_addr_t addr, uint32_t len) {
uint32_t flash_base = get_stm32l0_flash_base(sl);
uint32_t val;
uint32_t data;
Expand Down Expand Up @@ -3162,8 +3167,7 @@ int stlink_read_option_bytes_generic(stlink_t *sl, uint32_t* option_byte) {
* @param option_byte option value
* @return 0 on success, -ve on failure.
*/
int stlink_read_option_bytes32(stlink_t *sl, uint32_t* option_byte)
{
int stlink_read_option_bytes32(stlink_t *sl, uint32_t* option_byte) {
if (sl->option_base == 0) {
ELOG("Option bytes read is currently not supported for connected chip\n");
return -1;
Expand All @@ -3190,8 +3194,7 @@ int stlink_read_option_bytes32(stlink_t *sl, uint32_t* option_byte)
* @param option_byte value to write
* @return 0 on success, -ve on failure.
*/
int stlink_write_option_bytes32(stlink_t *sl, uint32_t option_byte)
{
int stlink_write_option_bytes32(stlink_t *sl, uint32_t option_byte) {
WLOG("About to write option byte %#10x to target.\n", option_byte);
return stlink_write_option_bytes(sl, sl->option_base, (uint8_t *) &option_byte, 4);
}
Expand All @@ -3203,8 +3206,7 @@ int stlink_write_option_bytes32(stlink_t *sl, uint32_t option_byte)
* @param base option bytes to write
* @return 0 on success, -ve on failure.
*/
int stlink_write_option_bytes(stlink_t *sl, stm32_addr_t addr, uint8_t* base, uint32_t len)
{
int stlink_write_option_bytes(stlink_t *sl, stm32_addr_t addr, uint8_t* base, uint32_t len) {
int ret = -1;

if (sl->option_base == 0) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit e74c6ec

Please sign in to comment.