Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

STM32 Ecosystem workshop

T.O.M.A.S Team
Goal of this part 2

Debug console inside System Workbench For STM32 (SW4STM32)

Printf implementation using semihosting mechanism


Another debug tool
Using printf() over semihosting
Using Semihosting
introduction 4

• With Cortex-M based MCUs, the bottom level of the C library contains a special
BKPT instruction. The execution of this is trapped by the debug tools which
determine what operation is being requested - in the case of a printf, for
example, this will effectively be a "write character to stdout". The debug tools
will then read the character from the memory of the target board - and display it
in the console window within the IDE.

• Semihosting also provides support for a number of other I/O operations. For
example it provides the ability for scanf to read its input from the IDE console. It
also allows file operations, such that fopen can open a file on your PC's hard
drive, and fscanf can then be used to read from that file.
Semihosting example
5

• We can use printf() and scanf() as a terminal of our system


• Use L4_Blinky project generated by STM32CubeMX
Semihosting example
6

/* Private variables -------------------*/


char string[30];
In main.c source file: /* USER CODE END PV */

• Add external function prototype


/* Private function prototypes --------*/
extern void initialise_monitor_handles(void);
/* USER CODE END PFP */
• Call function
/* USER CODE BEGIN 1 */
initialise_monitor_handles();
printf(“System initialize. \n”);
• Use semihosted function like /* USER CODE END 1 */
printf, scanf etc..
/* USER CODE BEGIN 3 */
printf("Put some text here, please\n");
scanf("%s", string);
printf("Catched text: %s\n", string);
/* USER CODE END 3 */
Semihosting example
7

In linker settings for the project please add: -specs=rdimon.specs –lc –lrdimon
Semihosting example
8

In debug configuration:
• Add “monitor arm semihosting enable”
• Compile and run the code
• Look on console in SW4STM32
Semihosting – important remark
9

• When you have linked with the semihosting library, your application will no longer work standalone - it
will only work when connected to the debugger.

• Semihosting operations cause the CPU to drop into "debug state", which means that for the duration
of the data transfer between the target and the host PC no code (including interrupts) will get
executed on the target. Thus if you application uses interrupts, then it is normally advisable to avoid
the use of semihosting whilst interrupts are active. If you still need to use printf, then you can retarget
the bottom level of the C library to use an alternative communication channel, such as a UART or
SWO.

• More about semihosting:


http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0058d/CIHBDHFF.html
What we have learnt 10

Debug console inside System Workbench For STM32 (SW4STM32)

Printf implementation using semihosting mechanism


Enjoy!

/STM32 @ST_World st.com/e2e

www.st.com/mcu

You might also like