Skip to content

Commit

Permalink
[refactoring] Clean-up for stlink-flash & -info
Browse files Browse the repository at this point in the history
- Ensure proper function declaration
- Checked & revised header includes
  • Loading branch information
Nightwalker-87 committed Jun 11, 2023
1 parent 5d3f3ec commit 2c33761
Show file tree
Hide file tree
Showing 16 changed files with 155 additions and 75 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ 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
Expand Down
9 changes: 2 additions & 7 deletions inc/stlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -284,14 +286,7 @@ 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 <sg.h>
#include <usb.h>
#include <register.h>
#include <commands.h>
#include <chipid.h>
#include <version.h>
#include <flash_loader.h>
#include <logging.h>

#ifdef __cplusplus
}
Expand Down
20 changes: 15 additions & 5 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
36 changes: 9 additions & 27 deletions src/st-flash/flash.h
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
/*
* File: flash.h
*
* Tool st-flash
*/

#ifndef FLASH_H
#define FLASH_H

#include <stdint.h>

#include <stlink.h>

#define DEBUG_LOG_LEVEL 100
#define STND_LOG_LEVEL 50
#define ENABLE_OPT 1

enum flash_cmd {FLASH_CMD_NONE = 0, FLASH_CMD_WRITE = 1, FLASH_CMD_READ = 2, FLASH_CMD_ERASE = 3, CMD_RESET = 4};
enum flash_format {FLASH_FORMAT_BINARY = 0, FLASH_FORMAT_IHEX = 1};
enum flash_area {FLASH_MAIN_MEMORY = 0, FLASH_SYSTEM_MEMORY = 1, FLASH_OTP = 2, FLASH_OPTION_BYTES = 3, FLASH_OPTION_BYTES_BOOT_ADD = 4, FLASH_OPTCR = 5, FLASH_OPTCR1 = 6};
struct flash_opts {
enum flash_cmd cmd;
uint8_t serial[STLINK_SERIAL_BUFFER_SIZE];
const char* filename;
stm32_addr_t addr;
uint32_t size;
int32_t reset;
int32_t log_level;
enum flash_format format;
enum flash_area area;
uint32_t val;
uint32_t flash_size; // --flash=n[k, M]
int32_t opt; // enable empty tail data drop optimization
int32_t freq; // --freq=n[k, M] frequency of JTAG/SWD
enum connect_type connect;
};

#define FLASH_OPTS_INITIALIZER {0, { 0 }, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

int32_t flash_get_opts(struct flash_opts* o, int32_t ac, char** av);
// static stlink_t *connected_stlink = NULL;
// static void cleanup(int32_t signum);
// static void usage(void);

#endif // FLASH_H
15 changes: 12 additions & 3 deletions src/st-flash/flash_opts.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
/*
* File: flash_opts.c
*
* Flash Options
*/

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

#include <helper.h>

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

#include <helper.h>

static bool starts_with(const char * str, const char * prefix) {
uint32_t n = strlen(prefix);

Expand Down
40 changes: 40 additions & 0 deletions src/st-flash/flash_opts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* File: flash_opts.h
*
* Flash Options
*/

#ifndef FLASH_OPTS_H
#define FLASH_OPTS_H

#define FLASH_OPTS_INITIALIZER {0, { 0 }, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

enum flash_cmd {FLASH_CMD_NONE = 0, FLASH_CMD_WRITE = 1, FLASH_CMD_READ = 2, FLASH_CMD_ERASE = 3, CMD_RESET = 4};
enum flash_format {FLASH_FORMAT_BINARY = 0, FLASH_FORMAT_IHEX = 1};
enum flash_area {FLASH_MAIN_MEMORY = 0, FLASH_SYSTEM_MEMORY = 1, FLASH_OTP = 2, FLASH_OPTION_BYTES = 3, FLASH_OPTION_BYTES_BOOT_ADD = 4, FLASH_OPTCR = 5, FLASH_OPTCR1 = 6};

struct flash_opts {
enum flash_cmd cmd;
uint8_t serial[STLINK_SERIAL_BUFFER_SIZE];
const char* filename;
stm32_addr_t addr;
uint32_t size;
int32_t reset;
int32_t log_level;
enum flash_format format;
enum flash_area area;
uint32_t val;
uint32_t flash_size; // --flash=n[k, M]
int32_t opt; // enable empty tail data drop optimization
int32_t freq; // --freq=n[k, M] frequency of JTAG/SWD
enum connect_type connect;
};

// static bool starts_with(const char * str, const char * prefix);
// static int32_t get_long_integer_from_char_array (const char *const str, uint64_t *read_value);
// static int32_t get_integer_from_char_array (const char *const str, uint32_t *read_value);
// static int32_t invalid_args(const char *expected);
// static int32_t bad_arg(const char *arg);
int32_t flash_get_opts(struct flash_opts* o, int32_t ac, char** av);

#endif // FLASH_OPTS_H
14 changes: 11 additions & 3 deletions src/st-info/info.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
#include <stdio.h>
/*
* File: stinfo.c
*
* Tool st-info
*/

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>

#include <stlink.h>
#include "info.h"

#include <chipid.h>
#include <helper.h>
#include <usb.h>

static void usage(void) {
puts("st-info --version");
Expand Down
18 changes: 18 additions & 0 deletions src/st-info/info.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* File: info.h
*
* Tool st-info
*/

#ifndef INFO_H
#define INFO_H

// static void usage(void);
// static void stlink_print_version(stlink_t *sl);
// static void stlink_print_info(stlink_t *sl);

// static void stlink_probe(enum connect_type connect, int32_t freq) { };
static int32_t print_data(int32_t ac, char **av);
int32_t main(int32_t ac, char** av);

#endif // INFO_H
5 changes: 4 additions & 1 deletion src/st-trace/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
#include <time.h>
#include <unistd.h>

#include <stlink.h>

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

#define DEFAULT_LOGGING_LEVEL 50
#define DEBUG_LOGGING_LEVEL 100
Expand Down
14 changes: 9 additions & 5 deletions src/st-util/gdb-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@
#endif

#include <stlink.h>
#include <helper.h>
#include <logging.h>
#include <flash_loader.h>
#include <common_flash.h>
#include "gdb-remote.h"
#include "gdb-server.h"
#include "gdb-remote.h"
#include "semihosting.h"

#include <chipid.h>
#include <common_flash.h>
#include <flash_loader.h>
#include <helper.h>
#include <logging.h>
#include <register.h>
#include <usb.h>

#define FLASH_BASE 0x08000000

// Semihosting doesn't have a short option, we define a value to identify it
Expand Down
5 changes: 4 additions & 1 deletion src/stlink-gui/gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
#include <gtk/gtk.h>

#include <stlink.h>
#include <common_flash.h>
#include "gui.h"

#include <chipid.h>
#include <common_flash.h>
#include <usb.h>

#define MEM_READ_SIZE 1024

#ifndef G_VALUE_INIT
Expand Down
2 changes: 1 addition & 1 deletion src/stlink-lib/chipid.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct stlink_chipid_params {
uint32_t option_base;
uint32_t option_size;
uint32_t flags;
struct stlink_chipid_params * next;
struct stlink_chipid_params *next;
};

struct stlink_chipid_params *stlink_chipid_get_params(uint32_t chipid);
Expand Down
Loading

0 comments on commit 2c33761

Please sign in to comment.