{"id":800,"date":"2022-03-04T04:36:26","date_gmt":"2022-03-04T04:36:26","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=800"},"modified":"2022-03-22T04:10:37","modified_gmt":"2022-03-22T04:10:37","slug":"retargeting-printf-use-serial-wire-output-swo-with-printf","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=800","title":{"rendered":"Retargeting printf: use Serial Wire Output (SWO) with printf"},"content":{"rendered":"\n<p>In this guide, we shall retargeting printf which part of stdio library to debug you code. For example, interrupt handler, variable to be displayed etc.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Serial Wire Output.<\/li><li>Implementing header file.<\/li><li>Implementing source code for keil uVision.<\/li><li>Implementing source code of STM32CubeIDE.<\/li><li>Setup Serial Wire Viewer in Keil uVison.<\/li><li>Setup Serial Wire Viewer in STM32CubeIDE.<\/li><li>Demo.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Serial Wire Output: <\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Serial Wire Output (SWO) alongside Serial Wire Debug (SWD) allows for the CPU to emit real-time trace data. In particular, when used with an Instrumentation Trace Macrocell (ITM), it can be used to form a Serial Wire Viewer (SWV). The ITM ports are provided by the ARM controller. The SWV typically implements a form of&nbsp;<code>printf<\/code>&nbsp;style debugging for embedded systems.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Implementing header file:<\/h2>\n\n\n\n<p>After we created a new header file with name of swo.h, we can implementing the functions such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>log_error<\/li><li>log_info<\/li><li>log_debug<\/li><li>log_debug_array<\/li><\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#ifndef __SWO__h\n#define __SWO__h\n#include &lt;stdio.h&gt;\n#include &quot;stm32f4xx.h&quot;                  \/\/ Device header\n\nvoid log_error(char *p);\nvoid log_info(char *p);\nvoid log_debug(char *p);\nvoid log_debug_array(char const * const label, void const *array, uint16_t const len);\n#endif<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Implementing source code for keil uVision:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>In Keil uVision, the printf function can be retargeted as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">struct __FILE {int handle;\/* Add whatever you need here *\/};\n\nFILE __stdout;\nFILE __stdin;\n\nint fputc(int ch, FILE *f)\n{\n  ITM_SendChar(ch);\n  return(ch);\n}<\/pre><\/div>\n\n\n\n<p>For the other functions:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void log_error(char *p)\n{\n\n\tprintf(&quot;log Error: &quot;);\n\n\t\tprintf((char*)p);\nprintf(&quot;\\r\\n&quot;);\n\n\n}\n\nvoid log_info(char *p)\n{\n\n\tprintf(&quot;log info: &quot;);\n\n\t\tprintf((char*)p);\nprintf(&quot;\\r\\n&quot;);\n\n\n}\n\nvoid log_debug(char *p)\n{\n\n\tprintf(&quot;log debug: &quot;);\n\n\t\tprintf((char*)p);\nprintf(&quot;\\r\\n&quot;);\n\n\n}\n\nvoid log_debug_array(char const * const label, void const *array, uint16_t const len)\n{\n  \tprintf(&quot;log debug array: &quot;);\n       \n    for (uint16_t i = 0; i &lt; len; i++)\n    {\n    \tuint8_t val = *((uint8_t *)(array + i));\n    \tprintf(&quot;0x%02X&quot;, val);\n    \t\n    \t\/\/ Add &quot;, &quot; after all elements except the last one.\n    \tif (i &lt; len - 1)\n    \t{\n    \t    printf(&quot;, &quot;);\n    \t}\n    }\n\tprintf(&quot;}\\n&quot;);\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Implementing source code for STM32CubeIDE:<\/h2>\n\n\n\n<p>In STM32CubeIDE, the printf functionality can be retargeted as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">int __io_putchar(int ch)\n\t{\n\t ITM_SendChar(ch);\n\treturn ch;\n\t}\n\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Setup Serial Wire Viewer in Keil uVison:<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"585\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.18.43-AM-1024x585.png\" alt=\"\" class=\"wp-image-801\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.18.43-AM-1024x585.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.18.43-AM-300x172.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.18.43-AM-768x439.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.18.43-AM-1536x878.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.18.43-AM-2048x1171.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.18.43-AM-1150x658.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.18.43-AM-750x429.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.18.43-AM-400x229.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.18.43-AM-250x143.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>After you compile the code and enter a debug session:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"585\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.26.49-AM-1024x585.png\" alt=\"\" class=\"wp-image-802\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.26.49-AM-1024x585.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.26.49-AM-300x172.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.26.49-AM-768x439.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.26.49-AM-1536x878.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.26.49-AM-2048x1171.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.26.49-AM-1150x658.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.26.49-AM-750x429.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.26.49-AM-400x229.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-04-at-7.26.49-AM-250x143.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Setup for STM32CubeIDE:<\/h2>\n\n\n\n<p>Please check the video from here:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Re-targeting printf: use Serial Wire Output (SWO) with printf Setup for STM32CubeIDE\" width=\"1170\" height=\"878\" src=\"https:\/\/www.youtube.com\/embed\/NCuaM0ziSiA?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. Results:<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/camo.githubusercontent.com\/a80837ae3c21eccc039c539bbe7d9846fc1da3f0ca067434d66850aacc15dc70\/68747470733a2f2f692e696d6775722e636f6d2f4d4643427230312e706e67\" alt=\"\" \/><\/figure>\n\n\n\n<p>Happy coding \ud83d\ude42 <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall retargeting printf which part of stdio library to debug you code. For example, interrupt handler, variable to be displayed etc. In this guide, we shall cover the following: Serial Wire Output. Implementing header file. Implementing source code for keil uVision. Implementing source code of STM32CubeIDE. Setup Serial Wire Viewer in [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-800","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/800"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=800"}],"version-history":[{"count":5,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/800\/revisions"}],"predecessor-version":[{"id":843,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/800\/revisions\/843"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}