How to enable printf and Storyboard debug trace output via UART on an MCU target runtime

The Storyboard trace output will by default go to your toolchain and platform stdout stream, often where printf() output ends up on your system though this is normally determined by your IAR or GCC system library configuration.  

If target your platform has a filesystem implementation such as FatFS or FileX that has been bound to the greal posix FileIO abstraction layer for Storyboard in your platform code then you are potentially able to redirect the trace output to a file using plugin options.

Most embedded MCU targets however don't have this requirement for a writeable filesystem and the system complexity that comes with it. Additionally many embedded MCU platforms may have more limited connectivity options for piping the trace output.

The logger, trace and metrics plugins all use this stdout routing unless you redirect this stream to another endpoint.
 
We have two API calls that enable you to control the trace output redirection and verbosity to gain a great deal of insight that can be useful in initial platform bring-up and testing :

  • gr_application_register_log_cb

  • gr_application_debug

Below is a code snippet example where trace output from the Storyboard engine is redirected at start-up to a custom PRINTF() implementation of your crafting and verbosity set to GR_LOG_INFO which is the equivalent to level 3 (-vvv in the simulator).

This code is typical of what you will use in the sample file sbengine_task.c where the Storyboard task is starting the application model and runtime engine is initialised:

#define USE_CUSTOM_STORYBOARD_TRACE_HANDLER	1	// Enable trace redirection to custom handler function
#if defined(USE_CUSTOM_STORYBOARD_TRACE_HANDLER) && USE_CUSTOM_STORYBOARD_TRACE_HANDLER == 1
extern int 	g_debug_log_level;
extern void (*g_debug_log_writer)(gr_application_t *app, int level, const char *logmsg, void *udata);
extern void *g_debug_log_writer_data;

/* Optionally add a callback handler to redirect default Storyboard trace output (stdio)
* to e.g: DebugConsole_printf() which will be debug UART over USB CMSIS DAP or JTAG back channel on EVK * - Use Putty or similar: COMx @ 115200,N,8,1 * - Trace output uses CR LF line termination */ static void logger_cb(gr_application_t *app, int level, const char *logmsg, void *udata){ PRINTF("%s\n",logmsg); } #endif // USE_CUSTOM_STORYBOARD_TRACE_HANDLER
/**
* This function uses the Storyboard API functions to initialise, configure and start the Storyboard * Runtime Engine task. * * Note: This function normally does not return unless the application model exits */ gr_application_t *app = NULL; static void run_storyboard_app(const char *bundle, int flags, char * const *options, int option_count)
{
#if defined(USE_CUSTOM_STORYBOARD_TRACE_HANDLER) && USE_CUSTOM_STORYBOARD_TRACE_HANDLER == 1 // Turn on detailed logging callback before startup to capture engine initialisation steps // NOTE: Feature available only on runtime release 8.2 or later g_debug_log_writer = logger_cb; g_debug_log_level = GR_LOG_DIAG1; #endif // USE_CUSTOM_STORYBOARD_TRACE_HANDLER
// Initialise all Storyboard engine libraries and plugins then load the model
app = gr_application_create_args(bundle, flags, options, option_count);
if(!app) { return; } #if defined(USE_CUSTOM_STORYBOARD_TRACE_HANDLER) && USE_CUSTOM_STORYBOARD_TRACE_HANDLER == 1 // Register our custom debug console logger callback gr_application_register_log_cb(app, &logger_cb, NULL, NULL, NULL); #endif // USE_CUSTOM_STORYBOARD_TRACE_HANDLER
// Sets the application logging verbosity. For more logging verbosities, refer to gre.h. gr_application_debug(app, GR_DEBUG_CMD_VERBOSITY, GR_LOG_INFO ); // Start the application gr_application_run(app); // Free the application instance and close any resources. gr_application_free(app); }

As the trace output is serialized through a relatively slow output stream such as a UART, the recommendation is to reduce any performance impact by setting the verbosity for production to GR_LOG_WARNING. 

0

Comments

0 comments

Please sign in to leave a comment.

Didn't find what you were looking for?

New post