Skip to content

Commit

Permalink
[refactoring] Clean-up for stlink-lib
Browse files Browse the repository at this point in the history
- Ensure proper function declaration
- Moved some functions to related modules
- Checked & revised header includes
- Renamed "md5" to "lib_md5"
- New source file "md5"
  • Loading branch information
Nightwalker-87 committed Jun 8, 2023
1 parent 67ae7a1 commit be2e7e3
Show file tree
Hide file tree
Showing 30 changed files with 554 additions and 525 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ set(STLINK_HEADERS
src/stlink-lib/chipid.h
src/stlink-lib/commands.h
src/stlink-lib/common_flash.h
src/stlink-lib/common.h
src/stlink-lib/flash_loader.h
src/stlink-lib/helper.h
src/stlink-lib/libusb_settings.h
src/stlink-lib/lib_md5.h
src/stlink-lib/logging.h
src/stlink-lib/map_file.h
src/stlink-lib/md5.h
Expand All @@ -194,6 +194,7 @@ set(STLINK_SOURCE
src/stlink-lib/helper.c
src/stlink-lib/logging.c
src/stlink-lib/map_file.c
src/stlink-lib/lib_md5.c
src/stlink-lib/md5.c
src/stlink-lib/option_bytes.c
src/stlink-lib/read_write.c
Expand Down
18 changes: 3 additions & 15 deletions inc/stlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include <stddef.h>
#include <stdbool.h>

#include "stm32.h"
#include "stm32flash.h"
#include <stm32.h>
#include <stm32flash.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -263,24 +263,13 @@ 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);
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);

//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);

int32_t stlink_erase_flash_page(stlink_t* sl, stm32_addr_t flashaddr);
uint32_t stlink_calculate_pagesize(stlink_t *sl, uint32_t flashaddr);
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);
Expand All @@ -293,16 +282,15 @@ int32_t write_buffer_to_sram(stlink_t *sl, flash_loader_t* fl, const uint8_t* bu
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);

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 <flash_loader.h>
#include <version.h>
#include <flash_loader.h>
#include <logging.h>

#ifdef __cplusplus
Expand Down
3 changes: 2 additions & 1 deletion src/st-util/gdb-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
#include <stlink.h>
#include <helper.h>
#include <logging.h>
#include "flash_loader.h"
#include <flash_loader.h>
#include <common_flash.h>
#include "gdb-remote.h"
#include "gdb-server.h"
#include "semihosting.h"
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 @@ -5,6 +5,7 @@
#include <gtk/gtk.h>

#include <stlink.h>
#include <common_flash.h>
#include "gui.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 @@ -8,6 +8,7 @@

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

#include "common_flash.h"

uint32_t calculate_F4_sectornum(uint32_t flashaddr) {
Expand Down
13 changes: 8 additions & 5 deletions src/stlink-lib/chipid.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
* Chip-ID parametres
*/

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

#include "chipid.h"
#include <stlink.h>
#include <stm32.h>
#include <stlink.h>
#include "chipid.h"

#include "logging.h"

// #include <ctype.h> // TODO: Check use
// #include <errno.h> // TODO: Check use

static struct stlink_chipid_params *devicelist;

Expand Down
3 changes: 0 additions & 3 deletions src/stlink-lib/chipid.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

#include <stdint.h>

#include <stm32.h>
#include <stlink.h>

/* Chipid parametres */
struct stlink_chipid_params {
char *dev_type;
Expand Down
86 changes: 8 additions & 78 deletions src/stlink-lib/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <helper.h>
#include <sys/stat.h>
#include <sys/types.h>
// #include <sys/stat.h> // TODO: Check use
// #include <sys/types.h> // TODO: Check use

#include <stlink.h>
#include <stm32.h>

#include "calculate.h"
#include "chipid.h"
#include "common_flash.h"
#include "helper.h"
#include "logging.h"
#include "map_file.h"
#include "md5.h"

#include "common.h"
#include "register.h"
#include "usb.h"

#ifndef O_BINARY
#define O_BINARY 0
Expand Down Expand Up @@ -1213,78 +1215,6 @@ void stlink_run_at(stlink_t *sl, stm32_addr_t addr) {
}
}

/* Limit the block size to compare to 0x1800 as anything larger will stall the
* STLINK2 Maybe STLINK V1 needs smaller value!
*/
int32_t check_file(stlink_t *sl, mapped_file_t *mf, stm32_addr_t addr) {
size_t off;
size_t n_cmp = sl->flash_pgsz;

if (n_cmp > 0x1800) {
n_cmp = 0x1800;
}

for (off = 0; off < mf->len; off += n_cmp) {
size_t aligned_size;

size_t cmp_size = n_cmp; // adjust last page size

if ((off + n_cmp) > mf->len) {
cmp_size = mf->len - off;
}

aligned_size = cmp_size;

if (aligned_size & (4 - 1)) {
aligned_size = (cmp_size + 4) & ~(4 - 1);
}

stlink_read_mem32(sl, addr + (uint32_t)off, (uint16_t)aligned_size);

if (memcmp(sl->q_buf, mf->base + off, cmp_size)) {
return (-1);
}
}

return (0);
}

void md5_calculate(mapped_file_t *mf) {
// calculate md5 checksum of given binary file
Md5Context md5Context;
MD5_HASH md5Hash;
Md5Initialise(&md5Context);
Md5Update(&md5Context, mf->base, (uint32_t)mf->len);
Md5Finalise(&md5Context, &md5Hash);
printf("md5 checksum: ");

for (int32_t i = 0; i < (int32_t)sizeof(md5Hash); i++) {
printf("%x", md5Hash.bytes[i]);
}

printf(", ");
}

void stlink_checksum(mapped_file_t *mp) {
/* checksum that backward compatible with official ST tools */
uint32_t sum = 0;
uint8_t *mp_byte = (uint8_t *)mp->base;

for (size_t i = 0; i < mp->len; ++i) {
sum += mp_byte[i];
}

printf("stlink checksum: 0x%08x\n", sum);
}

void stlink_fwrite_finalize(stlink_t *sl, stm32_addr_t addr) {
uint32_t val;
// set PC to the reset routine
stlink_read_debug32(sl, addr + 4, &val);
stlink_write_reg(sl, val, 15);
stlink_run(sl, RUN_NORMAL);
}

static int32_t stlink_read(stlink_t *sl, stm32_addr_t addr, size_t size,
save_block_fn fn, void *fn_arg) {

Expand Down
23 changes: 0 additions & 23 deletions src/stlink-lib/common.h

This file was deleted.

32 changes: 22 additions & 10 deletions src/stlink-lib/common_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
*/

#include <stdint.h>

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

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

#include "calculate.h"
#include "flash_loader.h"
#include "common_flash.h"
#include "logging.h"
#include "map_file.h"
#include "common.h"
#include "md5.h"

#define DEBUG_FLASH 0

Expand Down Expand Up @@ -742,6 +745,7 @@ void clear_flash_cr_pg(stlink_t *sl, uint32_t bank) {
n = read_flash_cr(sl, bank) & ~(1 << bit);
stlink_write_debug32(sl, cr_reg, n);
}

/* ------------------------------------------------------------------------ */

static void wait_flash_busy_progress(stlink_t *sl) {
Expand Down Expand Up @@ -1170,7 +1174,7 @@ int32_t stlink_erase_flash_section(stlink_t *sl, stm32_addr_t base_addr, size_t
return (-1);
}

fprintf(stdout, "-> Flash page at %#x erased (size: %#x)\n", addr, page_size);
fprintf(stdout, "-> Flash page at %#x erased (size: %#x)\r", addr, page_size);
fflush(stdout);

// check the next page is within the range to erase
Expand Down Expand Up @@ -1232,8 +1236,7 @@ int32_t stlink_erase_flash_mass(stlink_t *sl) {
return (err);
}

int32_t stlink_mwrite_flash(stlink_t *sl, uint8_t *data, uint32_t length,
stm32_addr_t addr) {
int32_t stlink_mwrite_flash(stlink_t *sl, uint8_t *data, uint32_t length, stm32_addr_t addr) {
/* Write the block in flash at addr */
int32_t err;
uint32_t num_empty, idx;
Expand Down Expand Up @@ -1416,12 +1419,10 @@ int32_t stlink_check_address_alignment(stlink_t *sl, stm32_addr_t addr) {
return 0;
}

int32_t stlink_write_flash(stlink_t *sl, stm32_addr_t addr, uint8_t *base,
uint32_t len, uint8_t eraseonly) {
int32_t stlink_write_flash(stlink_t *sl, stm32_addr_t addr, uint8_t *base, uint32_t len, uint8_t eraseonly) {
int32_t ret;
flash_loader_t fl;
ILOG("Attempting to write %d (%#x) bytes to stm32 address: %u (%#x)\n", len,
len, addr, addr);
ILOG("Attempting to write %d (%#x) bytes to stm32 address: %u (%#x)\n", len, len, addr, addr);
// check addr range is inside the flash
stlink_calculate_pagesize(sl, addr);

Expand All @@ -1438,6 +1439,9 @@ int32_t stlink_write_flash(stlink_t *sl, stm32_addr_t addr, uint8_t *base,
(uint32_t)(sl->flash_pgsz));
return (-1);
}
if ((len % 16 <= 8) & (sl->flash_type == STM32_FLASH_TYPE_L5_U5)) {
len += 8;
}

// make sure we've loaded the context with the chip details
stlink_core_id(sl);
Expand All @@ -1464,3 +1468,11 @@ int32_t stlink_write_flash(stlink_t *sl, stm32_addr_t addr, uint8_t *base,

return (stlink_verify_write_flash(sl, addr, base, len));
}

void stlink_fwrite_finalize(stlink_t *sl, stm32_addr_t addr) {
uint32_t val;
// set PC to the reset routine
stlink_read_debug32(sl, addr + 4, &val);
stlink_write_reg(sl, val, 15);
stlink_run(sl, RUN_NORMAL);
}
Loading

0 comments on commit be2e7e3

Please sign in to comment.