Skip to content

Commit

Permalink
Replace data types with fixed width typedefs (C99)
Browse files Browse the repository at this point in the history
- Unified variable types (Closes #909)
short --> int16_t
unsigned short --> uint16_t
int --> int32_t
unsigned int --> uint32_t
long --> int32_t
unsigned long --> uint32_t
long long --> int64_t
unsigned long long --> uint64_t

- Added missing header includes
  • Loading branch information
Nightwalker-87 committed May 8, 2023
1 parent a9c2139 commit 5e85fd0
Show file tree
Hide file tree
Showing 52 changed files with 908 additions and 836 deletions.
58 changes: 30 additions & 28 deletions inc/backend.h
@@ -1,37 +1,39 @@
#ifndef BACKEND_H
#define BACKEND_H

#include <stdint.h>

typedef struct _stlink_backend {
void (*close) (stlink_t * sl);
int (*exit_debug_mode) (stlink_t * sl);
int (*enter_swd_mode) (stlink_t * sl);
int (*enter_jtag_mode) (stlink_t * stl);
int (*exit_dfu_mode) (stlink_t * stl);
int (*core_id) (stlink_t * stl);
int (*reset) (stlink_t * stl);
int (*jtag_reset) (stlink_t * stl, int value);
int (*run) (stlink_t * stl, enum run_type type);
int (*status) (stlink_t * stl);
int (*version) (stlink_t *sl);
int (*read_debug32) (stlink_t *sl, uint32_t addr, uint32_t *data);
int (*read_mem32) (stlink_t *sl, uint32_t addr, uint16_t len);
int (*write_debug32) (stlink_t *sl, uint32_t addr, uint32_t data);
int (*write_mem32) (stlink_t *sl, uint32_t addr, uint16_t len);
int (*write_mem8) (stlink_t *sl, uint32_t addr, uint16_t len);
int (*read_all_regs) (stlink_t *sl, struct stlink_reg * regp);
int (*read_reg) (stlink_t *sl, int r_idx, struct stlink_reg * regp);
int (*read_all_unsupported_regs) (stlink_t *sl, struct stlink_reg *regp);
int (*read_unsupported_reg) (stlink_t *sl, int r_idx, struct stlink_reg *regp);
int (*write_unsupported_reg) (stlink_t *sl, uint32_t value, int idx, struct stlink_reg *regp);
int (*write_reg) (stlink_t *sl, uint32_t reg, int idx);
int (*step) (stlink_t * stl);
int (*current_mode) (stlink_t * stl);
int (*force_debug) (stlink_t *sl);
int32_t (*exit_debug_mode) (stlink_t * sl);
int32_t (*enter_swd_mode) (stlink_t * sl);
int32_t (*enter_jtag_mode) (stlink_t * stl);
int32_t (*exit_dfu_mode) (stlink_t * stl);
int32_t (*core_id) (stlink_t * stl);
int32_t (*reset) (stlink_t * stl);
int32_t (*jtag_reset) (stlink_t * stl, int32_t value);
int32_t (*run) (stlink_t * stl, enum run_type type);
int32_t (*status) (stlink_t * stl);
int32_t (*version) (stlink_t *sl);
int32_t (*read_debug32) (stlink_t *sl, uint32_t addr, uint32_t *data);
int32_t (*read_mem32) (stlink_t *sl, uint32_t addr, uint16_t len);
int32_t (*write_debug32) (stlink_t *sl, uint32_t addr, uint32_t data);
int32_t (*write_mem32) (stlink_t *sl, uint32_t addr, uint16_t len);
int32_t (*write_mem8) (stlink_t *sl, uint32_t addr, uint16_t len);
int32_t (*read_all_regs) (stlink_t *sl, struct stlink_reg * regp);
int32_t (*read_reg) (stlink_t *sl, int32_t r_idx, struct stlink_reg * regp);
int32_t (*read_all_unsupported_regs) (stlink_t *sl, struct stlink_reg *regp);
int32_t (*read_unsupported_reg) (stlink_t *sl, int32_t r_idx, struct stlink_reg *regp);
int32_t (*write_unsupported_reg) (stlink_t *sl, uint32_t value, int32_t idx, struct stlink_reg *regp);
int32_t (*write_reg) (stlink_t *sl, uint32_t reg, int32_t idx);
int32_t (*step) (stlink_t * stl);
int32_t (*current_mode) (stlink_t * stl);
int32_t (*force_debug) (stlink_t *sl);
int32_t (*target_voltage) (stlink_t *sl);
int (*set_swdclk) (stlink_t * stl, int freq_khz);
int (*trace_enable) (stlink_t * sl, uint32_t frequency);
int (*trace_disable) (stlink_t * sl);
int (*trace_read) (stlink_t * sl, uint8_t* buf, size_t size);
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);
} stlink_backend_t;

#endif // BACKEND_H
108 changes: 54 additions & 54 deletions inc/stlink.h
Expand Up @@ -196,17 +196,17 @@ struct _stlink {
unsigned char c_buf[C_BUF_LEN];
// data transferred from or to device
unsigned char q_buf[Q_BUF_LEN];
int q_len;
int32_t q_len;

// transport layer verboseness: 0 for no debug info, 10 for lots
int verbose;
int opt;
int32_t verbose;
int32_t opt;
uint32_t core_id; // set by stlink_core_id(), result from STLINK_DEBUGREADCOREID
uint32_t chip_id; // set by stlink_load_device_params(), used to identify flash and sram
enum target_state core_stat; // set by stlink_status()

char serial[STLINK_SERIAL_BUFFER_SIZE];
int freq; // set by stlink_open_usb(), values: STLINK_SWDCLK_xxx_DIVISOR
int32_t freq; // set by stlink_open_usb(), values: STLINK_SWDCLK_xxx_DIVISOR

enum stm32_flash_type flash_type;
// stlink_chipid_params.flash_type, set by stlink_load_device_params(), values: STM32_FLASH_TYPE_xx
Expand Down Expand Up @@ -236,67 +236,67 @@ struct _stlink {
uint32_t max_trace_freq; // set by stlink_open_usb()
};

int stlink_enter_swd_mode(stlink_t *sl);
int stlink_enter_jtag_mode(stlink_t *sl);
int stlink_exit_debug_mode(stlink_t *sl);
int stlink_exit_dfu_mode(stlink_t *sl);
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);
int32_t stlink_exit_dfu_mode(stlink_t *sl);
void stlink_close(stlink_t *sl);
int stlink_core_id(stlink_t *sl);
int stlink_reset(stlink_t *sl, enum reset_type type);
int stlink_run(stlink_t *sl, enum run_type type);
int stlink_status(stlink_t *sl);
int stlink_version(stlink_t *sl);
int stlink_read_debug32(stlink_t *sl, uint32_t addr, uint32_t *data);
int stlink_read_mem32(stlink_t *sl, uint32_t addr, uint16_t len);
int stlink_write_debug32(stlink_t *sl, uint32_t addr, uint32_t data);
int stlink_write_mem32(stlink_t *sl, uint32_t addr, uint16_t len);
int stlink_write_mem8(stlink_t *sl, uint32_t addr, uint16_t len);
int stlink_read_all_regs(stlink_t *sl, struct stlink_reg *regp);
int stlink_read_all_unsupported_regs(stlink_t *sl, struct stlink_reg *regp);
int stlink_read_reg(stlink_t *sl, int r_idx, struct stlink_reg *regp);
int stlink_read_unsupported_reg(stlink_t *sl, int r_idx, struct stlink_reg *regp);
int stlink_write_unsupported_reg(stlink_t *sl, uint32_t value, int r_idx, struct stlink_reg *regp);
int stlink_write_reg(stlink_t *sl, uint32_t reg, int idx);
int stlink_step(stlink_t *sl);
int stlink_current_mode(stlink_t *sl);
int stlink_force_debug(stlink_t *sl);
int stlink_target_voltage(stlink_t *sl);
int stlink_set_swdclk(stlink_t *sl, int freq_khz);
int stlink_trace_enable(stlink_t* sl, uint32_t frequency);
int stlink_trace_disable(stlink_t* sl);
int stlink_trace_read(stlink_t* sl, uint8_t* buf, size_t size);
int stlink_erase_flash_mass(stlink_t* sl);
int stlink_erase_flash_section(stlink_t *sl, stm32_addr_t base_addr, size_t size, bool align_size);
int stlink_write_flash(stlink_t* sl, stm32_addr_t address, uint8_t* data, uint32_t length, uint8_t eraseonly);
int stlink_parse_ihex(const char* path, uint8_t erased_pattern, uint8_t * * mem, size_t * size, uint32_t * begin);
int32_t stlink_core_id(stlink_t *sl);
int32_t stlink_reset(stlink_t *sl, enum reset_type type);
int32_t stlink_run(stlink_t *sl, enum run_type type);
int32_t stlink_status(stlink_t *sl);
int32_t stlink_version(stlink_t *sl);
int32_t stlink_read_debug32(stlink_t *sl, uint32_t addr, uint32_t *data);
int32_t stlink_read_mem32(stlink_t *sl, uint32_t addr, uint16_t len);
int32_t stlink_write_debug32(stlink_t *sl, uint32_t addr, uint32_t data);
int32_t stlink_write_mem32(stlink_t *sl, uint32_t addr, uint16_t len);
int32_t stlink_write_mem8(stlink_t *sl, uint32_t addr, uint16_t len);
int32_t stlink_read_all_regs(stlink_t *sl, struct stlink_reg *regp);
int32_t stlink_read_all_unsupported_regs(stlink_t *sl, struct stlink_reg *regp);
int32_t stlink_read_reg(stlink_t *sl, int32_t r_idx, struct stlink_reg *regp);
int32_t stlink_read_unsupported_reg(stlink_t *sl, int32_t r_idx, struct stlink_reg *regp);
int32_t stlink_write_unsupported_reg(stlink_t *sl, uint32_t value, int32_t r_idx, struct stlink_reg *regp);
int32_t stlink_write_reg(stlink_t *sl, uint32_t reg, int32_t idx);
int32_t stlink_step(stlink_t *sl);
int32_t stlink_current_mode(stlink_t *sl);
int32_t stlink_force_debug(stlink_t *sl);
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);
uint8_t stlink_get_erased_pattern(stlink_t *sl);
int stlink_mwrite_flash(stlink_t *sl, uint8_t* data, uint32_t length, stm32_addr_t addr);
int stlink_fwrite_flash(stlink_t *sl, const char* path, stm32_addr_t addr);
int stlink_mwrite_sram(stlink_t *sl, uint8_t* data, uint32_t length, stm32_addr_t addr);
int stlink_fwrite_sram(stlink_t *sl, const char* path, stm32_addr_t addr);
int stlink_verify_write_flash(stlink_t *sl, stm32_addr_t address, uint8_t *data, uint32_t length);
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);

//int stlink_chip_id(stlink_t *sl, uint32_t *chip_id);
int stlink_cpu_id(stlink_t *sl, cortex_m3_cpuid_t *cpuid);
//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);

int stlink_erase_flash_page(stlink_t* sl, stm32_addr_t flashaddr);
int32_t stlink_erase_flash_page(stlink_t* sl, stm32_addr_t flashaddr);
uint32_t stlink_calculate_pagesize(stlink_t *sl, uint32_t flashaddr);
int stlink_check_address_range_validity(stlink_t *sl, stm32_addr_t addr, size_t size);
int stlink_check_address_alignment(stlink_t *sl, stm32_addr_t addr);
uint16_t read_uint16(const unsigned char *c, const int pt);
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);
unsigned int is_bigendian(void);
uint32_t read_uint32(const unsigned char *c, const int pt);
uint32_t is_bigendian(void);
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);
int write_buffer_to_sram(stlink_t *sl, flash_loader_t* fl, const uint8_t* buf, size_t size);
int write_loader_to_sram(stlink_t *sl, stm32_addr_t* addr, size_t* size);
int stlink_fread(stlink_t* sl, const char* path, bool is_ihex, stm32_addr_t addr, size_t size);
int stlink_load_device_params(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_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_load_device_params(stlink_t *sl);

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

#include <sg.h>
#include <usb.h>
Expand Down
2 changes: 2 additions & 0 deletions inc/stm32.h
Expand Up @@ -7,6 +7,8 @@
#ifndef STM32_H
#define STM32_H

#include <stdint.h>

/* STM32 Cortex-M core ids (CPUTAPID) */
enum stm32_core_id {
STM32_CORE_ID_M0_SWD = 0x0bb11477, // (RM0091 Section 32.5.3) F0 SW-DP
Expand Down
2 changes: 2 additions & 0 deletions inc/stm32flash.h
@@ -1,6 +1,8 @@
#ifndef STM32FLASH_H
#define STM32FLASH_H

#include <stdint.h>

/* STM32Fx FPEC flash controller interface, PM0063 manual */
// STM32F05x is identical, based on RM0091 (DM00031936, Doc ID 018940 Rev.2, Aug 2012)
#define FLASH_REGS_ADDR 0x40022000
Expand Down
15 changes: 8 additions & 7 deletions src/st-flash/flash.c
@@ -1,6 +1,7 @@
/* Simple wrapper around the stlink_flash_write function */

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

Expand All @@ -20,7 +21,7 @@

static stlink_t *connected_stlink = NULL;

static void cleanup(int signum) {
static void cleanup(int32_t signum) {
(void)signum;

if (connected_stlink) { // switch back to mass storage mode before closing
Expand Down Expand Up @@ -52,10 +53,10 @@ static void usage(void) {
puts("example write option control register1 byte: ./st-flash --area=optcr1 write 0xXXXXXXXX");
}

int main(int ac, char** av) {
int32_t main(int32_t ac, char** av) {
stlink_t* sl = NULL;
struct flash_opts o;
int err = -1;
int32_t err = -1;
uint8_t * mem = NULL;

o.size = 0;
Expand All @@ -82,7 +83,7 @@ int main(int 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", (unsigned int)sl->flash_size);
printf("Forcing flash size: --flash=0x%08X\n", (uint32_t)sl->flash_size);
}

sl->verbose = o.log_level;
Expand Down Expand Up @@ -222,8 +223,8 @@ int main(int ac, char** av) {
} else if (o.area == FLASH_OPTION_BYTES) {
size_t remaining_option_length = sl->option_size / 4;
DLOG("@@@@ Read %u (%#x) option bytes from %#10x\n",
(unsigned)remaining_option_length,
(unsigned)remaining_option_length,
(uint32_t)remaining_option_length,
(uint32_t)remaining_option_length,
sl->option_base);

uint32_t option_byte = 0;
Expand All @@ -232,7 +233,7 @@ int main(int ac, char** av) {
printf("could not read option bytes (%d)\n", err);
goto on_error;
} else if (NULL != o.filename) {
int fd = open(o.filename, O_RDWR | O_TRUNC | O_CREAT | O_BINARY, 00700);
int32_t fd = open(o.filename, O_RDWR | O_TRUNC | O_CREAT | O_BINARY, 00700);
if (fd == -1) {
fprintf(stderr, "open(%s) == -1\n", o.filename);
goto on_error;
Expand Down
10 changes: 5 additions & 5 deletions src/st-flash/flash.h
Expand Up @@ -18,19 +18,19 @@ struct flash_opts {
const char* filename;
stm32_addr_t addr;
size_t size;
int reset;
int log_level;
int32_t reset;
int32_t log_level;
enum flash_format format;
enum flash_area area;
uint32_t val;
size_t flash_size; // --flash=n[k, M]
int opt; // enable empty tail data drop optimization
int freq; // --freq=n[k, M] frequency of JTAG/SWD
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}

int flash_get_opts(struct flash_opts* o, int ac, char** av);
int32_t flash_get_opts(struct flash_opts* o, int32_t ac, char** av);

#endif // FLASH_H
15 changes: 8 additions & 7 deletions src/st-flash/flash_opts.c
@@ -1,3 +1,4 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Expand All @@ -18,7 +19,7 @@ static bool starts_with(const char * str, const char * prefix) {
// support decimal, hexadecimal, octal, binary format like 0xff 12 1k 1M, 0b1001
// negative numbers are not supported
// return 0 if success else return -1
static int get_long_integer_from_char_array (const char *const str, uint64_t *read_value) {
static int32_t get_long_integer_from_char_array (const char *const str, uint64_t *read_value) {
uint64_t value;
char *tail;

Expand Down Expand Up @@ -50,9 +51,9 @@ static int get_long_integer_from_char_array (const char *const str, uint64_t *re
// support decimal, hexadecimal, octal, binary format like 0xff 12 1k 1M, 0b1001
// negative numbers are not supported
// return 0 if success else return -1
static int get_integer_from_char_array (const char *const str, uint32_t *read_value) {
static int32_t get_integer_from_char_array (const char *const str, uint32_t *read_value) {
uint64_t value;
int result = get_long_integer_from_char_array (str, &value);
int32_t result = get_long_integer_from_char_array (str, &value);

if (result != 0) {
return(result);
Expand All @@ -65,24 +66,24 @@ static int get_integer_from_char_array (const char *const str, uint32_t *read_va
}
}

static int invalid_args(const char *expected) {
static int32_t invalid_args(const char *expected) {
fprintf(stderr, "*** Error: Expected args for this command: %s\n", expected);
return(-1);
}

static int bad_arg(const char *arg) {
static int32_t bad_arg(const char *arg) {
fprintf(stderr, "*** Error: Invalid value for %s\n", arg);
return(-1);
}

int flash_get_opts(struct flash_opts* o, int ac, char** av) {
int32_t flash_get_opts(struct flash_opts* o, int32_t ac, char** av) {

// defaults
memset(o, 0, sizeof(*o));
o->log_level = STND_LOG_LEVEL;

// options
int result;
int32_t result;

while (ac >= 1) {
if (strcmp(av[0], "--version") == 0) {
Expand Down

2 comments on commit 5e85fd0

@slyshykO
Copy link
Collaborator

@slyshykO slyshykO commented on 5e85fd0 May 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nightwalker-87
it is deeply wrong to change int to int32_t and so on without context.
All system calls like open, recv, send, and others return int. And we should respect it. Ohterway it led us to a problem on 64-bit systems or other settings where int is bigger than int32_t.

So please, revert this commit.

@Nightwalker-87
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The width of int on the most common architectures ILP32LL, LLP64, and LP64 is 32-bit each, so there should technically be no difference here (see also: https://pvs-studio.com/fr/blog/posts/cpp/a0042/).
Please also see: https://barrgroup.com/embedded-systems/how-to/c-fixed-width-integers-c99 as one reference among others.
With a view to further refactoring work and perspectives there are indeed good arguments for this approach.

Please sign in to comment.