{"id":14,"date":"2025-03-24T07:04:20","date_gmt":"2025-03-24T07:04:20","guid":{"rendered":"https:\/\/tech-kid.de\/wp\/?page_id=14"},"modified":"2025-05-22T04:14:08","modified_gmt":"2025-05-22T04:14:08","slug":"stm8-tinkering-with","status":"publish","type":"page","link":"https:\/\/tech-kid.de\/wp\/?page_id=14","title":{"rendered":"STM8 &#8211; Programming and debugging with low cost Linux tools"},"content":{"rendered":"\n<p>One thing that made me curious about the 8 bit processors of the STM8 architecture was the little current consumption the devices have, when entering some low power modes. I hoped that getting started with 8-bit processors would be easy and the learning curve would be steep. An advantage of the STM8 Toolchain is the possibility to use the Gnu Debugger (GDB) to debug the code generated by the &#8220;Small Device C Compiler&#8221; (SDCC).<\/p>\n\n\n\n<p>As I&#8217;m a friend of small and robust tools like text mode editors and bash scripts, I find the text user interface (TUI) of GDB a very useful tool. With this UI, it is very easy to debug c code even via Shell connections.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"773\" src=\"https:\/\/tech-kid.de\/wp\/wp-content\/uploads\/2025\/03\/1000015252-1024x773.png\" alt=\"\" class=\"wp-image-66\" srcset=\"https:\/\/tech-kid.de\/wp\/wp-content\/uploads\/2025\/03\/1000015252-1024x773.png 1024w, https:\/\/tech-kid.de\/wp\/wp-content\/uploads\/2025\/03\/1000015252-300x227.png 300w, https:\/\/tech-kid.de\/wp\/wp-content\/uploads\/2025\/03\/1000015252-768x580.png 768w, https:\/\/tech-kid.de\/wp\/wp-content\/uploads\/2025\/03\/1000015252-1536x1160.png 1536w, https:\/\/tech-kid.de\/wp\/wp-content\/uploads\/2025\/03\/1000015252-1568x1184.png 1568w, https:\/\/tech-kid.de\/wp\/wp-content\/uploads\/2025\/03\/1000015252.png 1768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>the Nucleo Board used in this article<\/figcaption><\/figure>\n\n\n\n<h2>The Hardware: NUCLEO-8L152R8<\/h2>\n\n\n\n<p>For my research, I used the &#8220;NUCLEO-8L152R8&#8221; Board, which I got for less than 15\u20ac. The \u00b5C assembled, belongs to the &#8220;<strong>High-density and medium+ density STM8L15xx6\/8<\/strong>&#8221; group, which is important to know, because there are differences between the &#8220;device-density&#8221;-groups concerning the implementation of some peripherals and the memory organisation.<\/p>\n\n\n\n<h2>STM8 : C-Compiler \/ Debugger \/ Flasher &#8211; the Tools to install (3 steps)!<\/h2>\n\n\n\n<p>When looking at my apt, I found that the compiler I need was already available through the packet magager:<\/p>\n\n\n\n<p><strong>1.) Installation with apt: <\/strong><\/p>\n\n\n\n<ul><li><strong>SDCC (the Small Device C Compiler)<\/strong><\/li><li><strong>OPENOCD debugging tool<\/strong> (with openocd, I use the config file &#8220;<a href=\"https:\/\/github.com\/openocd-org\/openocd\/blob\/master\/tcl\/interface\/stlink.cfg\" data-type=\"URL\" data-id=\"https:\/\/github.com\/openocd-org\/openocd\/blob\/master\/tcl\/interface\/stlink.cfg\">stlink.cfg<\/a>&#8221; to detect my STM8 Nucleo Board)<\/li><\/ul>\n\n\n\n<p>There is a patch for the binutils software suite, with which the necessary utilities like the assembler or the object manipulation tools like objcopy coming along with an STM8-version of the Gnu Debugger (stm8-gdb) can be compiled for STM8 and usage with GDB with just a few commands.<\/p>\n\n\n\n<p><strong>2.) Download: <a href=\"https:\/\/sourceforge.net\/projects\/stm8-binutils-gdb\/files\/ \">https:\/\/sourceforge.net\/projects\/stm8-binutils-gdb\/files\/ <\/a><\/strong><\/p>\n\n\n\n<p>I could easily compile and install this piece of software.  After cd&#8217;ing into the un-zipped folder, I found the scripts to run:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:12px\"><code>.\/patch_binutils\n.\/configure_binutils\ncd binutils-2.30\nmake clean\nmake\nsudo make install<\/code><\/pre>\n\n\n\n<p>After installation, we can &#8220;stm8- tab tab&#8221; and get:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:12px\"><code>stm8-addr2line  stm8-as         stm8-elfedit    stm8-gprof      stm8-ld.bfd     stm8-objcopy    stm8-ranlib     stm8-size       stm8-strip      \nstm8-ar         stm8-c++filt    stm8-gdb        stm8-ld         stm8-nm         stm8-objdump    stm8-readelf    stm8-strings    \n<\/code><\/pre>\n\n\n\n<p><strong>3.) Download: <a href=\"https:\/\/github.com\/vdudouyt\/stm8flash\">https:\/\/github.com\/vdudouyt\/stm8flash<\/a><\/strong><\/p>\n\n\n\n<p>Installation was as easy as:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:12px\"><code>make\nmake install<\/code><\/pre>\n\n\n\n<p>with the resulting stm8flash program:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:12px\"><code>stm8-addr2line  stm8-as         stm8-elfedit    stm8-gdb        stm8-ld         stm8-nm         stm8-objdump    stm8-readelf    stm8-strings    \nstm8-ar         stm8-c++filt    <strong><span style=\"color:#f43003\" class=\"has-inline-color\">stm8flash<\/span><\/strong>       stm8-gprof      stm8-ld.bfd     stm8-objcopy    stm8-ranlib     stm8-size       stm8-strip      \n<\/code><\/pre>\n\n\n\n<h2>Hello World<\/h2>\n\n\n\n<p>As a debuggable minimal program, I used a main.c file with an endless loop inside the main function. SDCC needs main to return an int.<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:12px\"><code>int main(void)\n{\n  volatile char c=0;\n  while(1)\n  {\n    c++;\n    if(c&gt;10)\n    {\n      c=1;\n    }\n  }  \n}<\/code><\/pre>\n\n\n\n<p>I used the files in this <a href=\"\/Downloads\/?file=STM8L152_Minimal.tgz\"><strong>Minimal Example .tgz-File<\/strong><\/a>:<\/p>\n\n\n\n<ul><li>main.c (the Source Code I want to debug (above))<\/li><li><strong>domake.sh<\/strong> (my compile script)<\/li><li><strong>flash.sh<\/strong> (my flasher script)<\/li><li><strong>debug.sh<\/strong> (my debug script)<\/li><li>gdb.commandscript (contains some instructions for GDB to start the target)<\/li><\/ul>\n\n\n\n<p>I used the three scritps &#8220;<strong>domake.sh<\/strong>&#8220;, &#8220;<strong>flash.sh<\/strong>&#8221; and &#8220;<strong>debug.sh<\/strong>&#8221; to control the code and the target:<\/p>\n\n\n\n<p>&#8220;<strong>domake.sh<\/strong>&#8221; generates the binaries from the code twice: once with output-type &#8220;ELF&#8221; and once with output type &#8220;IHX&#8221;. The &#8220;ELF&#8221; File is used for debugging, while the IHX File is used for flashing. Before flashing, the &#8220;IHX&#8221; is converted to a &#8220;HEX&#8221; File with the packihx command.<\/p>\n\n\n\n<p>&#8220;<strong>flash.sh<\/strong>&#8221; flashes the main.hex File into the microcontroller.<\/p>\n\n\n\n<h2>Running all that stuff<\/h2>\n\n\n\n<p>After compiling and flashing, I found that GDB&#8217;s TUI is running very good with the STM8 Setup. I can report that it took me some minutes to get the debugger run correctly. My solution is based on two independent builds of which one results in the ELF file and the other one generates the HEX file. With this setup, I can debug most parts of the microcontroller. Without explicit naming of the calling convention, I ran into trouble when calling functions in the debugger. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"866\" height=\"629\" src=\"https:\/\/tech-kid.de\/wp\/wp-content\/uploads\/2025\/03\/DebuggerHelloWorld.png\" alt=\"\" class=\"wp-image-48\" srcset=\"https:\/\/tech-kid.de\/wp\/wp-content\/uploads\/2025\/03\/DebuggerHelloWorld.png 866w, https:\/\/tech-kid.de\/wp\/wp-content\/uploads\/2025\/03\/DebuggerHelloWorld-300x218.png 300w, https:\/\/tech-kid.de\/wp\/wp-content\/uploads\/2025\/03\/DebuggerHelloWorld-768x558.png 768w\" sizes=\"(max-width: 866px) 100vw, 866px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One thing that made me curious about the 8 bit processors of the STM8 architecture was the little current consumption the devices have, when entering some low power modes. I hoped that getting started with 8-bit processors would be easy and the learning curve would be steep. An advantage of the STM8 Toolchain is the &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/tech-kid.de\/wp\/?page_id=14\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;STM8 &#8211; Programming and debugging with low cost Linux tools&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/tech-kid.de\/wp\/index.php?rest_route=\/wp\/v2\/pages\/14"}],"collection":[{"href":"https:\/\/tech-kid.de\/wp\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/tech-kid.de\/wp\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/tech-kid.de\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tech-kid.de\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=14"}],"version-history":[{"count":33,"href":"https:\/\/tech-kid.de\/wp\/index.php?rest_route=\/wp\/v2\/pages\/14\/revisions"}],"predecessor-version":[{"id":129,"href":"https:\/\/tech-kid.de\/wp\/index.php?rest_route=\/wp\/v2\/pages\/14\/revisions\/129"}],"wp:attachment":[{"href":"https:\/\/tech-kid.de\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}