Skip to content

Commit

Permalink
[refactoring] Clean-up for stlink-lib
Browse files Browse the repository at this point in the history
- Moved declarations for read/write functions to read_write.h .
- Checked & revised header includes
- Changed some datatypes for write_buffer_to_sram() to avoid explicit
casting.
  • Loading branch information
Nightwalker-87 committed Jun 25, 2023
1 parent 041517b commit 8fad9be
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 57 deletions.
2 changes: 1 addition & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The following versions of the stlink toolset are currently being supported.<br /
| Version | Supported |
| ------- | ------------------ |
| develop | :white_check_mark: |
| 1.7.x | :white_check_mark: |
| 1.7.0 | :white_check_mark: |
| 1.6.x | :x: |
| 1.5.x | :x: |
| 1.4.0 | :x: |
Expand Down
24 changes: 4 additions & 20 deletions inc/stlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ struct _stlink {
/* 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_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);
Expand All @@ -246,17 +246,6 @@ 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);
Expand All @@ -265,23 +254,18 @@ 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, uint32_t size);
int32_t stlink_parse_ihex(const char* path, uint8_t erased_pattern, uint8_t * * mem, uint32_t * size, uint32_t * begin);
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_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_chip_id(stlink_t *sl, uint32_t *chip_id);
int32_t stlink_cpu_id(stlink_t *sl, cortex_m3_cpuid_t *cpuid);
uint32_t stlink_calculate_pagesize(stlink_t *sl, uint32_t flashaddr);
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);
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);
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 write_buffer_to_sram(stlink_t *sl, flash_loader_t* fl, const uint8_t* buf, uint16_t size);
// int32_t write_loader_to_sram(stlink_t *sl, stm32_addr_t* addr, uint16_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);
Expand Down
1 change: 1 addition & 0 deletions src/st-trace/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

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

Expand Down
1 change: 1 addition & 0 deletions src/st-util/gdb-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <flash_loader.h>
#include <helper.h>
#include <logging.h>
#include <read_write.h>
#include <register.h>
#include <usb.h>

Expand Down
3 changes: 2 additions & 1 deletion src/st-util/semihosting.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
#include <unistd.h>

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

#include <logging.h>
#include "semihosting.h"
#include <read_write.h>

static int32_t mem_read_u8(stlink_t *sl, uint32_t addr, uint8_t *data) {
int32_t offset = addr % 4;
Expand Down
1 change: 1 addition & 0 deletions src/stlink-gui/gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

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

#define MEM_READ_SIZE 1024
Expand Down
1 change: 1 addition & 0 deletions src/stlink-lib/calculate.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "calculate.h"

#include "common_flash.h"
#include "read_write.h"

uint32_t calculate_F4_sectornum(uint32_t flashaddr) {
uint32_t offset = 0;
Expand Down
11 changes: 6 additions & 5 deletions src/stlink-lib/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "logging.h"
#include "map_file.h"
#include "md5.h"
#include "read_write.h"
#include "register.h"
#include "usb.h"

Expand Down Expand Up @@ -808,20 +809,20 @@ int32_t stlink_fread(stlink_t *sl, const char *path, bool is_ihex, stm32_addr_t
}

// 300
int32_t write_buffer_to_sram(stlink_t *sl, flash_loader_t *fl, const uint8_t *buf, uint32_t size) {
int32_t write_buffer_to_sram(stlink_t *sl, flash_loader_t *fl, const uint8_t *buf, uint16_t size) {
// write the buffer right after the loader
int32_t ret = 0;
uint32_t chunk = size & ~0x3;
uint32_t rem = size & 0x3;
uint16_t chunk = size & ~0x3;
uint16_t rem = size & 0x3;

if (chunk) {
memcpy(sl->q_buf, buf, chunk);
ret = stlink_write_mem32(sl, fl->buf_addr, (uint16_t)chunk);
ret = stlink_write_mem32(sl, fl->buf_addr, chunk);
}

if (rem && !ret) {
memcpy(sl->q_buf, buf + chunk, rem);
ret = stlink_write_mem8(sl, (fl->buf_addr) + chunk, (uint16_t)rem);
ret = stlink_write_mem8(sl, (fl->buf_addr) + chunk, rem);
}

return (ret);
Expand Down
1 change: 1 addition & 0 deletions src/stlink-lib/common_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "logging.h"
#include "map_file.h"
#include "md5.h"
#include "read_write.h"

#define DEBUG_FLASH 0

Expand Down
1 change: 1 addition & 0 deletions src/stlink-lib/flash_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "common_flash.h"
#include "helper.h"
#include "logging.h"
#include "read_write.h"
#include "register.h"

#define FLASH_REGS_BANK2_OFS 0x40
Expand Down
2 changes: 2 additions & 0 deletions src/stlink-lib/map_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <stlink.h>
#include "map_file.h"

#include "read_write.h"

#ifndef O_BINARY
#define O_BINARY 0
#endif
Expand Down
1 change: 1 addition & 0 deletions src/stlink-lib/option_bytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "logging.h"
#include "map_file.h"
#include "md5.h"
#include "read_write.h"

/**
* Read option control register F0
Expand Down
67 changes: 37 additions & 30 deletions src/stlink-lib/read_write.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
/*
* File: read_write.c
*
* Read and write operations
*/

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

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

#include "logging.h"

// Endianness
// https://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html
// These functions encode and decode little endian uint16 and uint32 values.

void write_uint32(unsigned char *buf, uint32_t ui) {
buf[0] = ui;
buf[1] = ui >> 8;
buf[2] = ui >> 16;
buf[3] = ui >> 24;
uint16_t read_uint16(const unsigned char *c, const int32_t pt) {
return ((uint16_t)c[pt]) | ((uint16_t)c[pt + 1] << 8);
}

void write_uint16(unsigned char *buf, uint16_t ui) {
Expand All @@ -27,8 +31,11 @@ uint32_t read_uint32(const unsigned char *c, const int32_t pt) {
((uint32_t)c[pt + 2] << 16) | ((uint32_t)c[pt + 3] << 24);
}

uint16_t read_uint16(const unsigned char *c, const int32_t pt) {
return ((uint16_t)c[pt]) | ((uint16_t)c[pt + 1] << 8);
void write_uint32(unsigned char *buf, uint32_t ui) {
buf[0] = ui;
buf[1] = ui >> 8;
buf[2] = ui >> 16;
buf[3] = ui >> 24;
}

int32_t stlink_read_debug32(stlink_t *sl, uint32_t addr, uint32_t *data) {
Expand All @@ -46,48 +53,33 @@ int32_t stlink_write_debug32(stlink_t *sl, uint32_t addr, uint32_t data) {
return sl->backend->write_debug32(sl, addr, data);
}

int32_t stlink_write_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
DLOG("*** stlink_write_mem32 %u bytes to %#x\n", len, addr);
int32_t stlink_read_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
DLOG("*** stlink_read_mem32 ***\n");

if (len % 4 != 0) {
if (len % 4 != 0) { // !!! never ever: fw gives just wrong values
ELOG("Data length doesn't have a 32 bit alignment: +%d byte.\n", len % 4);
return (-1);
}

return (sl->backend->write_mem32(sl, addr, len));
return (sl->backend->read_mem32(sl, addr, len));
}

int32_t stlink_read_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
DLOG("*** stlink_read_mem32 ***\n");
int32_t stlink_write_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
DLOG("*** stlink_write_mem32 %u bytes to %#x\n", len, addr);

if (len % 4 != 0) { // !!! never ever: fw gives just wrong values
if (len % 4 != 0) {
ELOG("Data length doesn't have a 32 bit alignment: +%d byte.\n", len % 4);
return (-1);
}

return (sl->backend->read_mem32(sl, addr, len));
return (sl->backend->write_mem32(sl, addr, len));
}

int32_t stlink_write_mem8(stlink_t *sl, uint32_t addr, uint16_t len) {
DLOG("*** stlink_write_mem8 ***\n");
return (sl->backend->write_mem8(sl, addr, len));
}

int32_t stlink_read_all_regs(stlink_t *sl, struct stlink_reg *regp) {
DLOG("*** stlink_read_all_regs ***\n");
return (sl->backend->read_all_regs(sl, regp));
}

int32_t stlink_read_all_unsupported_regs(stlink_t *sl, struct stlink_reg *regp) {
DLOG("*** stlink_read_all_unsupported_regs ***\n");
return (sl->backend->read_all_unsupported_regs(sl, regp));
}

int32_t stlink_write_reg(stlink_t *sl, uint32_t reg, int32_t idx) {
DLOG("*** stlink_write_reg\n");
return (sl->backend->write_reg(sl, reg, idx));
}

int32_t stlink_read_reg(stlink_t *sl, int32_t r_idx, struct stlink_reg *regp) {
DLOG("*** stlink_read_reg\n");
DLOG(" (%d) ***\n", r_idx);
Expand All @@ -100,6 +92,11 @@ int32_t stlink_read_reg(stlink_t *sl, int32_t r_idx, struct stlink_reg *regp) {
return (sl->backend->read_reg(sl, r_idx, regp));
}

int32_t stlink_write_reg(stlink_t *sl, uint32_t reg, int32_t idx) {
DLOG("*** stlink_write_reg\n");
return (sl->backend->write_reg(sl, reg, idx));
}

int32_t stlink_read_unsupported_reg(stlink_t *sl, int32_t r_idx,
struct stlink_reg *regp) {
int32_t r_convert;
Expand Down Expand Up @@ -145,3 +142,13 @@ int32_t stlink_write_unsupported_reg(stlink_t *sl, uint32_t val, int32_t r_idx,

return (sl->backend->write_unsupported_reg(sl, val, r_convert, regp));
}

int32_t stlink_read_all_regs(stlink_t *sl, struct stlink_reg *regp) {
DLOG("*** stlink_read_all_regs ***\n");
return (sl->backend->read_all_regs(sl, regp));
}

int32_t stlink_read_all_unsupported_regs(stlink_t *sl, struct stlink_reg *regp) {
DLOG("*** stlink_read_all_unsupported_regs ***\n");
return (sl->backend->read_all_unsupported_regs(sl, regp));
}
27 changes: 27 additions & 0 deletions src/stlink-lib/read_write.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* File: read_write.h
*
* Read and write operations
*/

#ifndef READ_WRITE_H
#define READ_WRITE_H

uint16_t read_uint16(const unsigned char *c, const int32_t pt);
void write_uint16(unsigned char *buf, uint16_t ui);
uint32_t read_uint32(const unsigned char *c, const int32_t pt);
void write_uint32(unsigned char *buf, uint32_t ui);

int32_t stlink_read_debug32(stlink_t *sl, uint32_t addr, uint32_t *data);
int32_t stlink_write_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_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_reg(stlink_t *sl, 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_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_read_all_regs(stlink_t *sl, struct stlink_reg *regp);
int32_t stlink_read_all_unsupported_regs(stlink_t *sl, struct stlink_reg *regp);

#endif // READ_WRITE_H
1 change: 1 addition & 0 deletions src/stlink-lib/sg.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@

#include "commands.h"
#include "logging.h"
#include "read_write.h"
#include "register.h"
#include "usb.h"
// #include <stlink.h> // TODO: Check use
Expand Down
1 change: 1 addition & 0 deletions src/stlink-lib/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "commands.h"
#include "logging.h"
#include "read_write.h"
#include "register.h"

static inline uint32_t le_to_h_u32(const uint8_t* buf) {
Expand Down
1 change: 1 addition & 0 deletions tests/sg.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stlink.h>

#include <logging.h>
#include <read_write.h>
#include <sg.h>

#if defined(_MSC_VER)
Expand Down
1 change: 1 addition & 0 deletions tests/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <stlink.h>

#include <read_write.h>
#include <register.h>
#include <usb.h>

Expand Down

0 comments on commit 8fad9be

Please sign in to comment.