{"id":74,"date":"2025-03-26T08:44:45","date_gmt":"2025-03-26T08:44:45","guid":{"rendered":"https:\/\/tech-kid.de\/wp\/?page_id=74"},"modified":"2025-03-26T18:36:36","modified_gmt":"2025-03-26T18:36:36","slug":"stm8-how-i-do-it","status":"publish","type":"page","link":"https:\/\/tech-kid.de\/wp\/?page_id=74","title":{"rendered":"STM8 &#8211; How I do it"},"content":{"rendered":"\n<p>Since I&#8217;m coming a little more from the hardware-side of those projects, I prefer data sheets and low-level documentation. When tinkering with microcontrollers, I often had the feeling, that I spent too much time &#8220;reverse-engineering&#8221; overpowered abstraction layers, bringing together the code and what was really going on on the machine. I then decided to code the stuff as they call it &#8220;bare-metal&#8221;. The disadvantage is clear: the code may not be as re-usable as it could be. On the other side, I like to use the low-level user manuals directly for programming, because this kind of documentation is very reliable and comprehensive. Additionally, if coding some &#8220;non-standard&#8221; usage of peripherals, one would fall back to &#8220;register level&#8221; easily; so why not only code like this when tinkering near to the hardware?  <\/p>\n\n\n\n<p>On the coding level, when I want to use some peripheral, I define a pointer variable that points to a stuct. The struct represents the registers which are described in the  Reference Manual of the \u00b5C. The addresses of the peripherals are defines in the header file. This way, I can just copy the information from the Reference Manual and the documentation of the manufacturer can directly be found in my code.<\/p>\n\n\n\n<p>The following is an example of &#8220;bare-metal&#8221; definitions for the UART-Peripheral of my STM8L in a header file (<strong>stm8l15x_usart.h<\/strong>):<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:12px\"><code>#ifndef __STM8L15x_USART_H\n#define __STM8L15x_USART_H\n\n#include \"STM8L15x.h\"\n\n#define USART1_OFFS (USART_t*)0x005230\n#define USART2_OFFS (USART_t*)0x0053E0\n#define USART3_OFFS (USART_t*)0x0053F0\n\n#define QQ_BAUD_16M_115k2 \t(uint16_t)0x008B\n#define QQ_BAUD_16M_9k6\t\t(uint16_t)0x0683\n\nstruct USART_s\n{\n    volatile uint8_t SR;\n    volatile uint8_t DR;\n    volatile uint8_t BRR1;\n    volatile uint8_t BRR2;\n    volatile uint8_t CR1;\n    volatile uint8_t CR2;\n    volatile uint8_t CR3;\n    volatile uint8_t CR4;\n    volatile uint8_t CR5;\n    volatile uint8_t GTR;\n    volatile uint8_t PSCR;\n};\ntypedef volatile struct USART_s USART_t;\n\nvoid usart_init(uint32_t baudrate);\n\n#endif\/\/__STM8L15x_USART_<\/code><\/pre>\n\n\n\n<p>The corresponding register set is defined in the STM8L Reference Manual:<\/p>\n\n\n\n<div style=\"background-color: #EAEAEA;\" class=\"wp-block-image\"><br><br><figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"785\" height=\"621\" style=\"border-style:solid;border-width:1px;\" src=\"https:\/\/tech-kid.de\/wp\/wp-content\/uploads\/2025\/03\/grafik.png\" alt=\"\" class=\"wp-image-89\" srcset=\"https:\/\/tech-kid.de\/wp\/wp-content\/uploads\/2025\/03\/grafik.png 785w, https:\/\/tech-kid.de\/wp\/wp-content\/uploads\/2025\/03\/grafik-300x237.png 300w, https:\/\/tech-kid.de\/wp\/wp-content\/uploads\/2025\/03\/grafik-768x608.png 768w\" sizes=\"(max-width: 785px) 100vw, 785px\" \/><figcaption>USART Peripheral Register Set of STM8L MCUs (Excerpt from <a href=\"https:\/\/www.st.com\/resource\/en\/reference_manual\/rm0031-stm8l050j3-stm8l051f3-stm8l052c6-stm8l052r8-mcus-and-stm8l151l152-stm8l162-stm8al31-stm8al3l-lines-stmicroelectronics.pdf\">Reference Manual<\/a>)<\/figcaption><\/figure><br><br><\/div>\n\n\n\n<p>Depending on the need of initialization functions for the peripheral, I create a c-file (e.g. <strong>stm8l15x_usart.c<\/strong>) and link the resulting object file into the project, or not.<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:12px\"><code>#include \"STM8L15x_USART.h\"\n\nextern USART_t *USART;\nextern GPIO_t *PORTG;\nextern CLK_t *CLK;\n\n\nvoid usart_init(uint32_t baudrate) \n{\n    uint16_t baud_div;\n\t\n    CLK-&gt;PCKENR3 |= (1&lt;&lt;4\/*USART3 Enable*\/);\n\n    if(baudrate == 9600)\n    {\n    \tbaud_div = QQ_BAUD_16M_9k6;\n    }\n    else\n    {\n    \tbaud_div = QQ_BAUD_16M_115k2;\n    }\n \n    \/\/ Set Baud Rate\n    USART-&gt;BRR2 =  (baud_div &amp; 0x0F) | ((baud_div &gt;&gt; 8) &amp; 0xF0); \n    \/\/ LSB\n    USART-&gt;BRR1 =  (baud_div &gt;&gt; 4) &amp; 0xFF; \/\/ MSB\n   \n\n    \/\/ Init Control Regs\n    USART-&gt;CR1 = 0x00; \/\/ 8N1\n    USART-&gt;CR2 = 0x0C; \/\/ RX ON, TX ON \n    \n    \n    PORTG-&gt;DDR |= 2;\n    PORTG-&gt;CR1 |= 2;\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since I&#8217;m coming a little more from the hardware-side of those projects, I prefer data sheets and low-level documentation. When tinkering with microcontrollers, I often had the feeling, that I spent too much time &#8220;reverse-engineering&#8221; overpowered abstraction layers, bringing together the code and what was really going on on the machine. I then decided to &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/tech-kid.de\/wp\/?page_id=74\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;STM8 &#8211; How I do it&#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\/74"}],"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=74"}],"version-history":[{"count":12,"href":"https:\/\/tech-kid.de\/wp\/index.php?rest_route=\/wp\/v2\/pages\/74\/revisions"}],"predecessor-version":[{"id":97,"href":"https:\/\/tech-kid.de\/wp\/index.php?rest_route=\/wp\/v2\/pages\/74\/revisions\/97"}],"wp:attachment":[{"href":"https:\/\/tech-kid.de\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}