How to print messages to Virtual COM port by using printf() function in an MCU application?
Answer
We provide so called RETARGET functions with the aim of avoiding the needs of the low level UART peripheral configuration. The following step-by-step guide shows how to add all the necessary files and functions to a project from the scratch.
1. Create an empty MCU project
File -> New -> Project...
Figure 1.
Create an empty MCU project.
2. Copy (or link) the following emlib and RETARGET files
emlib from "<StudioPath>\v4\developer\sdks\gecko_sdk_suite\v2.7\platform\emlib\src":
em_cmu.c
em_core.c
em_gpio.c
em_usart.c
RETARGET from "<StudioPath>\v4\developer\sdks\gecko_sdk_suite\v2.7\hardware\kit\common\drivers":
retargetio.c
retargetserial.c
Figure 2.
Files in the project.
Note: The header files are added to the Include path by default, therefore no need to add them manually.
3. Place the RETARGET functions
The Starter Kits have a bidirectional analog switch between the MCU and the Board Controller.
The VCOM_ENABLE pin should be in high logical state for allowing the data flow through to the board controller.
Find the given pin in the User's Guide of the kit.
Figure 3.
Virtual COM Port Interface.
To realize this setting, add the below lines prior to call the RETARGET functions.
Additional headers added:
and place the following code after the CHIP_Init()
//Enable GPIO clk prior to write its registers
CMU_ClockEnable(cmuClock_GPIO,true);
//Enable the switch with VCOM_ENABLE pin, see User's Guide of the board
GPIO_PinModeSet(gpioPortA,5,gpioModePushPull,1);
RETARGET_SerialInit();
RETARGET_SerialCrLf(1);
printf("VCOM is working!\n");
4. Start a terminal emulator
Used serial port can be determined by Device Manager:
IAR makes it relatively easy to locate an initialized variable in flash. One use-case of this might be to set the Debug Lock Word (DLW) in the lock bits page to lock debug access to the device.
To locate a variable in IAR, you can use the @ sign.
uint32_t myVar @ 0x2000000;
Only 'const' variables can be located and initialized however:
uint32_t const myVar @ 0x00001000 = 0x0000AABB;
Finally, in order to keep variables that will otherwise be unused, they must be declared as '__root'. So, to clear the debug lock word purely with a global variable statement:
32-bit Knowledge Base
Using printf() in MCU application
Question
How to print messages to Virtual COM port by using printf() function in an MCU application?
Answer
We provide so called RETARGET functions with the aim of avoiding the needs of the low level UART peripheral configuration. The following step-by-step guide shows how to add all the necessary files and functions to a project from the scratch.
1. Create an empty MCU project
File -> New -> Project...
Figure 1.
Create an empty MCU project.
2. Copy (or link) the following emlib and RETARGET files
emlib from "<StudioPath>\v4\developer\sdks\gecko_sdk_suite\v2.7\platform\emlib\src":
RETARGET from "<StudioPath>\v4\developer\sdks\gecko_sdk_suite\v2.7\hardware\kit\common\drivers":
Figure 2.
Files in the project.
Note: The header files are added to the Include path by default, therefore no need to add them manually.
3. Place the RETARGET functions
The Starter Kits have a bidirectional analog switch between the MCU and the Board Controller.
The VCOM_ENABLE pin should be in high logical state for allowing the data flow through to the board controller.
Find the given pin in the User's Guide of the kit.
Figure 3.
Virtual COM Port Interface.
To realize this setting, add the below lines prior to call the RETARGET functions.
Additional headers added:
and place the following code after the CHIP_Init()
4. Start a terminal emulator
Used serial port can be determined by Device Manager:
Figure 4.
Device Manager.
5. Build and download the image
Figure 5.
Terminal on Host PC.
IAR - clearing debug lock word from code - variable declaration
IAR makes it relatively easy to locate an initialized variable in flash. One use-case of this might be to set the Debug Lock Word (DLW) in the lock bits page to lock debug access to the device.
To locate a variable in IAR, you can use the @ sign.
Only 'const' variables can be located and initialized however:
Finally, in order to keep variables that will otherwise be unused, they must be declared as '__root'. So, to clear the debug lock word purely with a global variable statement:
Make sure that, after flashing the device, the device is reset appropriately to engage the debug lock.
To clear the debug lock word from executing code, see the following example: https://github.com/SiliconLabs/peripheral_examples/tree/master/series1/msc/debug_lock