Semihosting stdin spammed with internal strings - scanf() not usable
#4 621 ouverte le 11 mai 2023
Métriques du dépôt
- Stars
- (7 329 stars)
- Métriques de merge PR
- (Merge moyen 109j 16h) (5 PRs mergées en 30 j)
Description
What kind of issue is this?
Configuration
Operating system: Win11
PlatformIO Version (platformio --version):
6.1.7
Description of the problem
I didn't have much luck making scanf() usable.
It is producing a lot of extra characters when using Platformio semihosting.
I have a suspicion it has something to do with monitor arm semihosting_fileio enable, as without and custom external openOCD terminal, the behavior is fine.
Steps to Reproduce
- Clone https://github.com/dzid26/STM32_BluePill_Semihosting.git
- Debug F5,
- Continue F5
- Type something in Debug Console
Actual Results
In debug console, I typed 6 and Enter and got this.
Processing bluepill_f103c8 (platform: ststm32; board: bluepill_f103c8; framework: cmsis)
--------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/bluepill_f103c8.html
PLATFORM: ST STM32 (15.4.1) > BluePill F103C8
HARDWARE: STM32F103C8T6 72MHz, 20KB RAM, 64KB Flash
DEBUG: Current (stlink) External (blackmagic, cmsis-dap, jlink, stlink)
PACKAGES:
- framework-cmsis @ 2.50501.200527 (5.5.1)
- framework-cmsis-stm32f1 @ 4.3.1
- tool-ldscripts-ststm32 @ 0.2.0
- toolchain-gccarmnoneeabi @ 1.70201.0 (7.2.1)
Hello, world
6
24-interpreter-exec console "6"
��r�
��r�04F17�06� c�� o�F��IFPF����010��00�02�21#03` F26������0000 -��CaF�F)H00!25F����04F*F00!8F����c3425� F��34�&�F0FIF��06F0F��-�03(02�038�B� F����O��4 F03�����04" F
�0201��G�03(�ݝ�040S+�ѝ�050H+�ѝ�060F+�ѝ�a0B+��01"AF F��J�00(��*F9F F��)�05F F����(F����04F���31007�00!01" M L01�)`"`����00(aݝ�04003�010203�0203*`#`03�0�03m00;m00_m000000000000000000000000000000000000000000000000000000000000000000�0300000000000000000000040000pio_reset_run_target
-gdb-exit
Sometimes it is less crazy and it would be just interpreter-exec console "6"
If I use c = scanf("%c", &c); instead of c = getchar();, there is bunch of 0101:
01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101
Expected Results
This is what I get if use:
debug_extra_cmds =
monitor arm semihosting enable
debug_tool = custom
debug_port = :3333
OpenOCD terminal:
Executing task: cd ~/.platformio/packages/tool-openocd && bin/openocd -f scripts/interface/stlink.cfg -c 'set CPUTAPID 0' -f target/stm32f1x.cfg
xPack OpenOCD x86_64 Open On-Chip Debugger 0.11.0+dev (2021-10-16-21:19)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
0
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : DEPRECATED target event trace-config; use TPIU events {pre,post}-{enable,disable}
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : clock speed 1000 kHz
Info : STLINK V2J34M25 (API v2) VID:PID 0483:374B
Info : Target voltage: 3.238046
Info : stm32f1x.cpu: Cortex-M3 r1p1 processor detected
Info : stm32f1x.cpu: target has 6 breakpoints, 4 watchpoints
Info : Listening on port 3333 for gdb connections
Info : accepting 'gdb' connection on tcp/3333
Info : device id = 0x20036410
Info : flash size = 64kbytes
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x0800015c msp: 0x20005000
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x0800015c msp: 0x20005000
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x0800015c msp: 0x20005000
semihosting is enabled
Hello, world
6
Warn : keep_alive() was not invoked in the 1000 ms timelimit. GDB alive packet not sent! (12230 ms). Workaround: increase "set remotetimeout" in GDB
6
6
Warn : keep_alive() was not invoked in the 1000 ms timelimit. GDB alive packet not sent! (5189 ms). Workaround: increase "set remotetimeout" in GDB
6
5
8
u
w
q
Warn : keep_alive() was not invoked in the 1000 ms timelimit. GDB alive packet not sent! (1310 ms). Workaround: increase "set remotetimeout" in GDB
q
q
Warn : keep_alive() was not invoked in the 1000 ms timelimit. GDB alive packet not sent! (1074 ms). Workaround: increase "set remotetimeout" in GDB
q
q
q
If problems with PlatformIO Build System:
The content of platformio.ini:
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:bluepill_f103c8]
platform = ststm32
board = bluepill_f103c8
framework = cmsis
; Add The following lines of code to your project's platformio.ini file.
upload_protocol = stlink
upload_flags = -c set CPUTAPID 0 ; Allow Original and Clone STM32 to be detected by OpenOCD debugger.
debug_build_flags = -O0 -ggdb3 ; Apply no optimization to the code written.
;debug_init_break = tbreak Reset_Handler ; Uncomment to activate: Adds a break point in the reset handler.
; ============ Enable Arm Semihosting in debugger to show logs =============
; Add The following code lines to enable semihosting feature.
; To disable semihosting please comment all of the following code lines by adding semicolon ";" before each one:
extra_scripts = extra_script.py ; The "extra_script.py" file must be located in the main project folder
build_unflags =
--specs=nosys.specs
-lnosys
debug_extra_cmds =
monitor arm semihosting enable
monitor arm semihosting_fileio enable
debug_tool = stlink
; ===========================================================================
Source file to reproduce issue:
initialise_monitor_handles(); /* This Function MUST come before the first printf() */
printf("Hello, world\n"); /* IMPORTANT: Always use the "\n" newline character at the end of each string to be printed using printf() function when using semihosting */
int c;
do{ //wait for the user
c = getchar();
(void) printf("%c", c);
if(c == '@')
break;
}while(1);