Skip to content

Commit

Permalink
feature: updated stlink library to develop
Browse files Browse the repository at this point in the history
  • Loading branch information
klonyyy committed Jul 16, 2023
1 parent 1ea3e3e commit 01e1692
Show file tree
Hide file tree
Showing 29 changed files with 1,069 additions and 1,048 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ add_custom_command(TARGET ${EXECUTABLE} POST_BUILD

install(TARGETS ${EXECUTABLE} RUNTIME DESTINATION ${INSTALL_PATH} COMPONENT applications)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/imgui.ini DESTINATION ${INSTALL_PATH})
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/third_party/stlink/chips DESTINATION ${INSTALL_PATH})

if(WIN32)
if(PRODUCTION)
Expand All @@ -158,7 +159,6 @@ if(WIN32)
endif()

if(UNIX)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/third_party/stlink/chips DESTINATION ${INSTALL_PATH})
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/launch/icon.png DESTINATION ${INSTALL_PATH})
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/launch/STMViewer.desktop
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
Expand Down
6 changes: 4 additions & 2 deletions src/TargetMemoryHandler/StlinkHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

#include <cstring>

#include "logging.h"

StlinkHandler::StlinkHandler()
{
#if defined(unix) || defined(__unix__) || defined(__unix)
// #if defined(unix) || defined(__unix__) || defined(__unix)
init_chipids("./chips");
#endif
// #endif
}

bool StlinkHandler::startAcqusition()
Expand Down
64 changes: 33 additions & 31 deletions third_party/stlink/inc/backend.h
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
#ifndef STLINK_BACKEND_H_
#define STLINK_BACKEND_H_
#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, uint32_t size);
} stlink_backend_t;

#endif // STLINK_BACKEND_H_
#endif // BACKEND_H
15 changes: 15 additions & 0 deletions third_party/stlink/inc/calculate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* File: calculate.h
*
* Calculation of sector numbers and pages
*/

#ifndef CALCULATE_H
#define CALCULATE_H

uint32_t calculate_F4_sectornum(uint32_t);
uint32_t calculate_F7_sectornum(uint32_t);
uint32_t calculate_H7_sectornum(stlink_t *, uint32_t, uint32_t);
uint32_t calculate_L4_page(stlink_t *, uint32_t);

#endif // CALCULATE_H
20 changes: 13 additions & 7 deletions third_party/stlink/inc/chipid.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#ifndef STLINK_CHIPID_H_
#define STLINK_CHIPID_H_
/*
* File: chipid.h
*
* Chip-ID parametres
*/

#include <stm32.h>
#include <stlink.h>
#ifndef CHIPID_H
#define CHIPID_H

/* Chipid parametres */
struct stlink_chipid_params {
Expand All @@ -18,10 +21,13 @@ 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);
void init_chipids(char *dir_to_scan);

#endif // STLINK_CHIPID_H_
void dump_a_chip(struct stlink_chipid_params *dev);
void process_chipfile(char *fname);
void init_chipids(char *dir_to_scan);

#endif // CHIPID_H
12 changes: 9 additions & 3 deletions third_party/stlink/inc/commands.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#ifndef STLINK_COMMANDS_H_
#define STLINK_COMMANDS_H_
/*
* File: commands.h
*
* stlink commands
*/

#ifndef COMMANDS_H
#define COMMANDS_H

enum stlink_commands {
STLINK_GET_VERSION = 0xF1,
Expand Down Expand Up @@ -54,4 +60,4 @@ enum stlink_dfu_commands {
STLINK_DFU_EXIT = 0x07
};

#endif // STLINK_COMMANDS_H_
#endif // COMMANDS_H
51 changes: 51 additions & 0 deletions third_party/stlink/inc/common_flash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* File: common_flash.h
*
* Flash operations
*/

#ifndef COMMON_FLASH_H
#define COMMON_FLASH_H

#define BANK_1 0
#define BANK_2 1

uint32_t get_stm32l0_flash_base(stlink_t *);
uint32_t read_flash_cr(stlink_t *, uint32_t);
void lock_flash(stlink_t *);
// static inline int32_t write_flash_sr(stlink_t *sl, uint32_t bank, uint32_t val);
void clear_flash_error(stlink_t *);
uint32_t read_flash_sr(stlink_t *sl, uint32_t bank);
uint32_t is_flash_busy(stlink_t *sl);
void wait_flash_busy(stlink_t *);
int32_t check_flash_error(stlink_t *);
// static inline uint32_t is_flash_locked(stlink_t *sl);
// static void unlock_flash(stlink_t *sl);
int32_t unlock_flash_if(stlink_t *);
int32_t lock_flash_option(stlink_t *);
// static bool is_flash_option_locked(stlink_t *sl);
// static int32_t unlock_flash_option(stlink_t *sl);
int32_t unlock_flash_option_if(stlink_t *);
void write_flash_cr_psiz(stlink_t *, uint32_t, uint32_t);
void clear_flash_cr_pg(stlink_t *, uint32_t);
// static void wait_flash_busy_progress(stlink_t *sl);
// static inline void write_flash_ar(stlink_t *sl, uint32_t n, uint32_t bank);
// static inline void write_flash_cr_snb(stlink_t *sl, uint32_t n, uint32_t bank);
// static void set_flash_cr_per(stlink_t *sl, uint32_t bank);
// static void clear_flash_cr_per(stlink_t *sl, uint32_t bank);
// static inline void write_flash_cr_bker_pnb(stlink_t *sl, uint32_t n);
// static void set_flash_cr_strt(stlink_t *sl, uint32_t bank);
// static void set_flash_cr_mer(stlink_t *sl, bool v, uint32_t bank);
int32_t stlink_erase_flash_page(stlink_t *sl, stm32_addr_t flashaddr);
int32_t stlink_erase_flash_section(stlink_t *sl, stm32_addr_t base_addr, uint32_t size, bool align_size);
int32_t stlink_erase_flash_mass(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_fcheck_flash(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_check_address_range_validity(stlink_t *sl, stm32_addr_t addr, uint32_t size);
int32_t stlink_check_address_alignment(stlink_t *sl, stm32_addr_t addr);
int32_t stlink_write_flash(stlink_t *sl, stm32_addr_t addr, uint8_t *base, uint32_t len, uint8_t eraseonly);
void stlink_fwrite_finalize(stlink_t *, stm32_addr_t);

#endif // COMMON_FLASH_H
32 changes: 20 additions & 12 deletions third_party/stlink/inc/flash_loader.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
/*
* File: stlink.h
* File: flash_loader.h
*
* This should contain all the common top level stlink interfaces,
* regardless of how the backend does the work....
* Flash loaders
*/

#ifndef STLINK_FLASH_LOADER_H_
#define STLINK_FLASH_LOADER_H_
#ifndef FLASH_LOADER_H
#define FLASH_LOADER_H

#include <stdint.h>
#include <stddef.h>
int32_t stlink_flash_loader_init(stlink_t *sl, flash_loader_t* fl);
// static int32_t loader_v_dependent_assignment(stlink_t *sl,
// const uint8_t **loader_code, uint32_t *loader_size,
// const uint8_t *high_v_loader, uint32_t high_v_loader_size,
// const uint8_t *low_v_loader, uint32_t low_v_loader_size);
int32_t stlink_flash_loader_write_to_sram(stlink_t *sl, stm32_addr_t* addr, uint32_t* size);
int32_t stlink_flash_loader_run(stlink_t *sl, flash_loader_t* fl, stm32_addr_t target, const uint8_t* buf, uint32_t size);

#include <stlink.h>

int stlink_flash_loader_init(stlink_t *sl, flash_loader_t* fl);
int stlink_flash_loader_write_to_sram(stlink_t *sl, stm32_addr_t* addr, size_t* size);
int stlink_flash_loader_run(stlink_t *sl, flash_loader_t* fl, stm32_addr_t target, const uint8_t* buf, size_t size);
/* === Functions from old header file flashloader.h === */

#endif // STLINK_FLASH_LOADER_H_
int32_t stm32l1_write_half_pages(stlink_t *sl, stm32_addr_t addr, uint8_t *base, uint32_t len, uint32_t pagesize);
// static void set_flash_cr_pg(stlink_t *sl, uint32_t bank);
// static void set_dma_state(stlink_t *sl, flash_loader_t *fl, int32_t bckpRstr);
int32_t stlink_flashloader_start(stlink_t *sl, flash_loader_t *fl);
int32_t stlink_flashloader_write(stlink_t *sl, flash_loader_t *fl, stm32_addr_t addr, uint8_t *base, uint32_t len);
int32_t stlink_flashloader_stop(stlink_t *sl, flash_loader_t *fl);

#endif // FLASH_LOADER_H
15 changes: 10 additions & 5 deletions third_party/stlink/inc/helper.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#ifndef SYS_HELPER_H
#define SYS_HELPER_H
/*
* File: helper.h
*
* General helper functions
*/

unsigned time_ms();
#ifndef HELPER_H
#define HELPER_H

int arg_parse_freq(const char *str);
uint32_t time_ms();
int32_t arg_parse_freq(const char *str);

#endif /* SYS_HELPER_H */
#endif // HELPER_H
65 changes: 65 additions & 0 deletions third_party/stlink/inc/lib_md5.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* WjCryptLib_Md5 (https://github.com/WaterJuice/WjCryptLib)
* Implementation of MD5 hash function. Originally written by Alexander Peslyak.
* Modified by WaterJuice retaining Public Domain license.
* This is free and unencumbered software released into the public domain - June 2013 - waterjuice.org
*/

#ifndef LIB_MD5_H
#define LIB_MD5_H

#pragma once

/* TYPES */

/* Md5Context
* This must be initialised using Md5Initialised.
* Do not modify the contents of this structure directly.
*/
typedef struct {
uint32_t lo;
uint32_t hi;
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
uint8_t buffer[64];
uint32_t block[16];
} Md5Context;

#define MD5_HASH_SIZE (128 / 8)

typedef struct {
uint8_t bytes [MD5_HASH_SIZE];
} MD5_HASH;

/* PUBLIC FUNCTIONS */

/* Md5Initialise
* Initialises an MD5 Context.
* Use this to initialise/reset a context.
*/
void Md5Initialise(Md5Context* Context /* [out] */);

/* Md5Update
* Adds data to the MD5 context.
* This will process the data and update the internal state of the context.
* Keep on calling this function until all the data has been added.
* Then call Md5Finalise to calculate the hash.
*/
void Md5Update(Md5Context* Context /* [in out] */, void const* Buffer /* [in] */, uint32_t BufferSize /* [in] */);

/* Md5Finalise
* Performs the final calculation of the hash and returns the digest
* (16 byte buffer containing 128bit hash).
* After calling this, Md5Initialised must be used to reuse the context.
*/
void Md5Finalise(Md5Context* Context /* [in out] */, MD5_HASH* Digest /* [in] */);

/* Md5Calculate
* Combines Md5Initialise, Md5Update, and Md5Finalise into one function.
* Calculates the MD5 hash of the buffer.
*/
void Md5Calculate(void const* Buffer /* [in] */, uint32_t BufferSize /* [in] */, MD5_HASH* Digest /* [in] */);

#endif // LIB_MD5_H
13 changes: 9 additions & 4 deletions third_party/stlink/inc/libusb_settings.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* File: libusb_settings.h
*
* Settings for libusb library
*/

#ifndef LIBUSB_SETTINGS_H
#define LIBUSB_SETTINGS_H

Expand All @@ -19,6 +25,7 @@
* v1.0.23 | 0x01000107
* v1.0.24 | 0x01000108
* v1.0.25 | 0x01000109
* v1.0.26 | 0x01000110
*/

#if defined (__FreeBSD__)
Expand All @@ -32,11 +39,9 @@
#if defined (__FreeBSD__)
#define MINIMAL_API_VERSION 0x01000102 // v1.0.16
#elif defined (__OpenBSD__)
#define MINIMAL_API_VERSION 0x01000105 // v1.0.21
#define MINIMAL_API_VERSION 0x01000106 // v1.0.22
#elif defined (__linux__)
#define MINIMAL_API_VERSION 0x01000105 // v1.0.21
#elif defined (__APPLE__)
#define MINIMAL_API_VERSION 0x01000109 // v1.0.25
#define MINIMAL_API_VERSION 0x01000106 // v1.0.22
#elif defined (_WIN32)
#define MINIMAL_API_VERSION 0x01000109 // v1.0.25
#endif
Expand Down
Loading

0 comments on commit 01e1692

Please sign in to comment.