Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] ITM functionality for ST-Link V2 and STM32Fxx chipsets #1072

Merged
merged 46 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
27d3144
Adding framework for st-trace.
simplerobot Nov 25, 2020
0f411da
Getting to build on raspberry pi.
Nov 25, 2020
5ad3bb9
Separating out method to check if stlink is usable.
simplerobot Nov 26, 2020
4aed320
Fixed that serial numbers are stored as binary values.
simplerobot Nov 26, 2020
f5cc12c
Cleaning up logging a bit.
simplerobot Nov 26, 2020
be8d86f
Fixing to use built in logging.
simplerobot Nov 26, 2020
2fd0ab8
Library already supported specifying which stlink to use.
simplerobot Nov 26, 2020
380444a
In progress. Added defines for trace debug commands. Added initial …
simplerobot Nov 27, 2020
568b0d1
Adding backend code to start/stop tracing and to read the incoming data.
simplerobot Nov 27, 2020
2900571
In progress.
simplerobot Nov 28, 2020
737894c
Got basic trace data transfer working. Still needs to decode the dat…
simplerobot Nov 29, 2020
9c86d53
Added a state machine to parse trace output.
simplerobot Nov 29, 2020
8793f3c
Tabs to spaces
simplerobot Nov 29, 2020
3531e34
Code cleanup.
simplerobot Nov 30, 2020
0b3f39f
Checked that the trace port is configured. Added log messages to sha…
simplerobot Nov 30, 2020
acbb78d
Checking that stlink supports tracing.
simplerobot Nov 30, 2020
722224e
Adding a force flag to ignore errors.
simplerobot Nov 30, 2020
3188fb4
Updating to use register names to make code more readable.
simplerobot Dec 1, 2020
074f061
Verified sleep timer is about right.
simplerobot Dec 1, 2020
f51f065
Added a check that looks at connection results after 10 seconds and d…
simplerobot Dec 1, 2020
b8c459f
Avoiding duplicating logs for unknown sources.
simplerobot Dec 1, 2020
7ceb09d
Avoiding reading register from check method as this seems to halt the…
simplerobot Dec 1, 2020
2e4c81e
Was using continouous triggering wrong.
simplerobot Dec 1, 2020
b88b192
Flushing stdout after every newline. This is needed for things to ru…
simplerobot Dec 1, 2020
c2356fa
Cleaning up code before code review.
simplerobot Dec 1, 2020
793088f
Updating to use chip flags instead of multiple boolean values.
simplerobot Dec 1, 2020
660dee3
Moved chip trace support into chipid where it belongs.
simplerobot Dec 1, 2020
ce7f0a1
One last cleanup before I feel this code is ready to submit for CR.
simplerobot Dec 1, 2020
4f76250
Using debug instead of write.
simplerobot Dec 1, 2020
aaaae8a
Minor change didn't compile.
simplerobot Dec 1, 2020
ac104eb
Allowed configuring trace frequency.
simplerobot Dec 1, 2020
a331da4
Improving diagnostics, especially for sw buffer overruns.
simplerobot Dec 1, 2020
57f1bc7
Making sure TPI async prescalar is within the available range.
simplerobot Dec 2, 2020
305b7a4
Inproving huristic to handle cases where there is lots of valid data …
simplerobot Dec 2, 2020
fdeb172
When a USB error occurs, chip-id does not appear to be initialized.
simplerobot Dec 2, 2020
2b54a11
Merge branch 'develop' into st-trace
Nightwalker-87 Dec 5, 2020
373eee2
Adding back has_dual_bank.
simplerobot Dec 5, 2020
0ab25c5
Merge https://github.com/stlink-org/stlink into st-trace
simplerobot Dec 6, 2020
3e9e2bb
Adding back flag.
simplerobot Dec 6, 2020
67bf390
Fixing funcation naming convention.
simplerobot Dec 6, 2020
7db0477
Adding windows signal handler type behavior.
simplerobot Dec 7, 2020
dc9726c
Set abort routine wrong in windows. Found in code review.
simplerobot Dec 7, 2020
d1a9fd8
Moved register definitions to common reg.h file.
simplerobot Dec 7, 2020
08a8474
Removing unused variable warning.
simplerobot Dec 7, 2020
d5c63c3
Moving has_dual_bank into the flags field.
simplerobot Dec 7, 2020
cbd2633
Fixing binary compatibility with the stlink structure.
simplerobot Dec 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ build-mingw
obj-*
*.user*
.cmake/
*.swp
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ install(TARGETS ${STLINK_LIB_STATIC} ARCHIVE DESTINATION ${STLINK_LIBRARY_PATH})
set(ST-FLASH_SOURCES src/st-flash/flash.c src/st-flash/flash_opts.c)
set(ST-INFO_SOURCES src/st-info/info.c)
set(ST-UTIL_SOURCES src/st-util/gdb-remote.c src/st-util/gdb-server.c src/st-util/semihosting.c)
set(ST-TRACE_SOURCES src/st-trace/trace.c)

if (MSVC)
# Add getopt to sources
Expand All @@ -253,20 +254,24 @@ endif ()
add_executable(st-flash ${ST-FLASH_SOURCES})
add_executable(st-info ${ST-INFO_SOURCES})
add_executable(st-util ${ST-UTIL_SOURCES})
add_executable(st-trace ${ST-TRACE_SOURCES})

if (WIN32 OR APPLE)
target_link_libraries(st-flash ${STLINK_LIB_STATIC} ${SSP_LIB})
target_link_libraries(st-info ${STLINK_LIB_STATIC} ${SSP_LIB})
target_link_libraries(st-util ${STLINK_LIB_STATIC} ${SSP_LIB})
target_link_libraries(st-trace ${STLINK_LIB_STATIC} ${SSP_LIB})
else ()
target_link_libraries(st-flash ${STLINK_LIB_SHARED} ${SSP_LIB})
target_link_libraries(st-info ${STLINK_LIB_SHARED} ${SSP_LIB})
target_link_libraries(st-util ${STLINK_LIB_SHARED} ${SSP_LIB})
target_link_libraries(st-trace ${STLINK_LIB_SHARED} ${SSP_LIB})
endif ()

install(TARGETS st-flash DESTINATION ${CMAKE_INSTALL_BINDIR})
install(TARGETS st-info DESTINATION ${CMAKE_INSTALL_BINDIR})
install(TARGETS st-util DESTINATION ${CMAKE_INSTALL_BINDIR})
install(TARGETS st-trace DESTINATION ${CMAKE_INSTALL_BINDIR})


###
Expand Down
3 changes: 3 additions & 0 deletions inc/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
int (*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);
} stlink_backend_t;

#endif // STLINK_BACKEND_H_
36 changes: 34 additions & 2 deletions inc/stlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern "C" {

/* Max data transfer size */
// 6kB = max mem32_read block, 8kB sram
// #define Q_BUF_LEN 96
// #define Q_BUF_LEN 96
#define Q_BUF_LEN (1024 * 100)

// STLINK_DEBUG_RESETSYS, etc:
Expand Down Expand Up @@ -83,6 +83,33 @@ enum target_state {

#define STLINK_V3_MAX_FREQ_NB 10

#define STLINK_TRACE_BUF_LEN 2048
#define STLINK_V2_MAX_TRACE_FREQUENCY 2000000
#define STLINK_V3_MAX_TRACE_FREQUENCY 24000000
#define STLINK_DEFAULT_TRACE_FREQUENCY 2000000

/* Cortex Debug Control Block */
#define DCB_DHCSR 0xE000EDF0
simplerobot marked this conversation as resolved.
Show resolved Hide resolved
#define DCB_DCRSR 0xE000EDF4
#define DCB_DCRDR 0xE000EDF8
#define DCB_DEMCR 0xE000EDFC

/* DCB_DHCSR bit and field definitions */
#define DBGKEY (0xA05F << 16)
#define C_DEBUGEN (1 << 0)
#define C_HALT (1 << 1)
#define C_STEP (1 << 2)
#define C_MASKINTS (1 << 3)
#define S_REGRDY (1 << 16)
#define S_HALT (1 << 17)
#define S_SLEEP (1 << 18)
#define S_LOCKUP (1 << 19)
#define S_RETIRE_ST (1 << 24)
#define S_RESET_ST (1 << 25)

/* DCB_DEMCR field definitions */
#define DEMCR_TRCENA (1 << 24)

/* Map the relevant features, quirks and workaround for specific firmware version of stlink */
#define STLINK_F_HAS_TRACE (1 << 0)
#define STLINK_F_HAS_SWD_SET_FREQ (1 << 1)
Expand Down Expand Up @@ -189,6 +216,7 @@ struct _stlink {
char serial[STLINK_SERIAL_MAX_SIZE];
int serial_size;
int freq; // set by stlink_open_usb(), values: STLINK_SWDCLK_xxx_DIVISOR
uint32_t max_trace_freq; // set by stlink_open_usb()

enum stlink_flash_type flash_type;
// stlink_chipid_params.flash_type, set by stlink_load_device_params(), values: STLINK_FLASH_TYPE_xxx
Expand All @@ -213,6 +241,8 @@ struct _stlink {
size_t sys_size; // stlink_chipid_params.bootrom_size, set by stlink_load_device_params()

struct stlink_version_ version;

uint32_t chip_flags; // stlink_chipid_params.flags, set by stlink_load_device_params(), values: CHIP_F_xxx
};

int stlink_enter_swd_mode(stlink_t *sl);
Expand Down Expand Up @@ -243,7 +273,9 @@ 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_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);
Expand Down
25 changes: 24 additions & 1 deletion src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,7 @@ int stlink_load_device_params(stlink_t *sl) {
DLOG("Loading device parameters....\n");
const struct stlink_chipid_params *params = NULL;
stlink_core_id(sl);
uint32_t chip_id;
uint32_t chip_id = 0;
uint32_t flash_size;

stlink_chip_id(sl, &chip_id);
Expand Down Expand Up @@ -1362,6 +1362,7 @@ int stlink_load_device_params(stlink_t *sl) {
sl->sys_size = params->bootrom_size;
sl->option_base = params->option_base;
sl->option_size = params->option_size;
sl->chip_flags = params->flags;

// medium and low devices have the same chipid. ram size depends on flash size.
// STM32F100xx datasheet Doc ID 16455 Table 2
Expand Down Expand Up @@ -1542,6 +1543,11 @@ void _parse_version(stlink_t *sl, stlink_version_t *slv) {
if (sl->version.jtag_v >= 15) {
sl->version.flags |= STLINK_F_HAS_GETLASTRWSTATUS2;
}

if (sl->version.jtag_v >= 13) {
sl->version.flags |= STLINK_F_HAS_TRACE;
sl->max_trace_freq = STLINK_V2_MAX_TRACE_FREQUENCY;
}
}
} else {
// V3 uses different version format, for reference see OpenOCD source
Expand All @@ -1555,6 +1561,8 @@ void _parse_version(stlink_t *sl, stlink_version_t *slv) {
slv->jtag_api = STLINK_JTAG_API_V3;
/* preferred API to get last R/W status */
sl->version.flags |= STLINK_F_HAS_GETLASTRWSTATUS2;
sl->version.flags |= STLINK_F_HAS_TRACE;
sl->max_trace_freq = STLINK_V3_MAX_TRACE_FREQUENCY;
}

return;
Expand Down Expand Up @@ -1754,6 +1762,20 @@ int stlink_current_mode(stlink_t *sl) {
return(STLINK_DEV_UNKNOWN_MODE);
}

int stlink_trace_enable(stlink_t* sl, uint32_t frequency) {
DLOG("*** stlink_trace_enable ***\n");
return(sl->backend->trace_enable(sl, frequency));
}

int stlink_trace_disable(stlink_t* sl) {
DLOG("*** stlink_trace_disable ***\n");
return(sl->backend->trace_disable(sl));
}

int stlink_trace_read(stlink_t* sl, uint8_t* buf, size_t size) {
return(sl->backend->trace_read(sl, buf, size));
}


// End of delegates.... Common code below here...

Expand Down Expand Up @@ -4199,3 +4221,4 @@ int stlink_fwrite_option_bytes(stlink_t *sl, const char* path, stm32_addr_t addr

return(err);
}

Loading