How to print messages to Virtual COM port by using printf() function in an MCU application?
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.
File -> New -> Project...
Create an empty MCU project.
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":
Files in the project.
Note: The header files are added to the Include path by default, therefore no need to add them manually.
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.
Virtual COM Port Interface.
To realize this setting, add the below lines prior to call the RETARGET functions.
Additional headers added:
#include "em_cmu.h" #include "em_gpio.h" #include "retargetserial.h" #include "stdio.h"
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");
Used serial port can be determined by Device Manager:
Terminal on Host PC.
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:
__root uint32_t const DLW @ (LOCKBITS_BASE + (127*4)) = 0xFFFFFFF0;
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