{"id":1879,"date":"2023-07-05T06:06:19","date_gmt":"2023-07-05T06:06:19","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1879"},"modified":"2023-07-05T06:06:21","modified_gmt":"2023-07-05T06:06:21","slug":"building-board-support-package-bsp-for-stm32f411-nucleo64-part3-system-tick-implementation","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1879","title":{"rendered":"Building Board Support Package (BSP) for STM32F411-Nucleo64 Part3:  System Tick implementation"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-1-1024x683.jpeg\" alt=\"\" class=\"wp-image-1880\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-1-1024x683.jpeg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-1-300x200.jpeg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-1-768x512.jpeg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-1-1536x1024.jpeg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-1-2048x1365.jpeg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-1-1150x767.jpeg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-1-750x500.jpeg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-1-400x267.jpeg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/AdobeStock_128586024-1-250x167.jpeg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=1871\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1871\" target=\"_blank\">here<\/a>), we took a look how to build core configuration for STM32F411. In this guide, we shall see how to use System Time (SysTick) to implement system tick in your board support package.<\/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>What is system tick in embedded systems.<\/li><li>System Tick implementation.<\/li><li>Code.<\/li><li>Result.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is system tick in embedded system:<\/h2>\n\n\n\n<p>The system tick is\u00a0<strong>the time unit that OS timers and delays are based on<\/strong>. The system tick is a scheduling event &#8211; i.e. it causes the scheduler to run and may cause a context switch &#8211; for example if a timer has expired or a task delay completed.<\/p>\n\n\n\n<p>In shoot, it is the heart of the embedded system.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.  System Tick implementation:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Within the bsp.h header file, declare the following three 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 BSP_Ticks_Init(uint32_t freq);<\/pre><\/div>\n\n\n\n<p>The function take frequency (freq) as an argument and return nothing (void).<\/p>\n\n\n\n<p><\/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;}\">uint32_t BSP_Get_Ticks(void);<\/pre><\/div>\n\n\n\n<p>The function takes no argument and return the current time in milliseconds.<\/p>\n\n\n\n<p><\/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 BSP_Delay(uint32_t delay_ms);<\/pre><\/div>\n\n\n\n<p>This function will force delay the CPU and it takes the delay amount in milliseconds as argument and returns nothing.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Thats all for the header file.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>For more details how to configure the systick, please refer to this guide <a href=\"https:\/\/blog.embeddedexpert.io\/?p=1644\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1644\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p>In bsp.c source file, declare the following volatile:<\/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;}\">volatile uint32_t ticks;<\/pre><\/div>\n\n\n\n<p>This will hold the ticks of the system and it will be incremented in the interrupt handler. Hence, the volatile keyword.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>For tick initialising:<\/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 BSP_Ticks_Init(uint32_t freq)\n{\n\t\/*Load the SysTick value to be the core frequency over 1000\n\t *\n\t * Since the core frequency is in MHz, dividing it by 1000 will get 1ms period\n\t * *\/\n\tSysTick-&gt;LOAD=(freq\/1000)-1;\n\n\t\/*Set the source to be internal core clock*\/\n\tSysTick-&gt;CTRL=(1&lt;&lt;SysTick_CTRL_CLKSOURCE_Pos);\n\n\t\/*Enable The interrupt *\/\n\n\tSysTick-&gt;CTRL|=(1&lt;&lt;SysTick_CTRL_TICKINT_Pos);\n\n\t\/*Enable Systick Interrupt in NIVC*\/\n\n\tNVIC_EnableIRQ(SysTick_IRQn);\n\n\t\/*Enable Systick*\/\n\tSysTick-&gt;CTRL|=(1&lt;&lt;SysTick_CTRL_ENABLE_Pos);\n\n}\n<\/pre><\/div>\n\n\n\n<p>SysTick interrupt handler:<\/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 SysTick_Handler(void)\n{\n\tticks++;\n}\n<\/pre><\/div>\n\n\n\n<p>Get tick function:<\/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;}\">uint32_t BSP_Get_Ticks(void)\n{\n\tuint32_t current_ticks;\n\t__disable_irq();\n\tcurrent_ticks=ticks;\n\t__enable_irq();\n\t\/*Return the counter value*\/\n\treturn current_ticks;\n}<\/pre><\/div>\n\n\n\n<p>Since we need to return a volatile variable, it is recommend to do the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Disable global variable.<\/li><li>Store the volatile variable in local variable.<\/li><li>Enable the global variable.<\/li><li>Return the local variable.<\/li><\/ul>\n\n\n\n<p>This way, you will eliminate whats called race condition.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>For delay function:<\/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 BSP_Delay(uint32_t delay_ms)\n{\n\n\tuint32_t ticks_start=BSP_Get_Ticks();\n\n\twhile(BSP_Get_Ticks()-ticks_start&lt;delay_ms);\n}\n<\/pre><\/div>\n\n\n\n<p>This delay will spin lock the CPU for doing nothing to achieve the delay.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Thats all for the source file.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In main.c file:<\/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;}\">#include &quot;bsp.h&quot;\n\nGPIO_Output_Typedef LED;\n\n\nClock_Config_Typedef clockConfig;\n\n\n\nint main()\n{\n\n\n\n\tGPIOA_CLOCK_ENABLE();\n\n\tGPIO_Configure_Typedef LED_Config;\n\n\tLED_Config.PinNumber=pin5;\n\tLED_Config.Mode=OUTPUT;\n\n\tLED.pinNumber=pin5;\n\n\tGPIO_Initialization(GPIOA,&amp;LED_Config);\n\n\tclockConfig.PLL_M= 4;\n\tclockConfig.PLL_N= 200;\n\tclockConfig.PLL_P= 4;\n\n\tclockConfig.AHB1Prescaler=AHB1_Prescaler1;\n\tclockConfig.APB1Prescaler=APB1_Prescaler2;\n\tclockConfig.APB2Prescaler=APB2_Prescaler1;\n\n\tclockConfig.clockSourc=External_Oscillator;\n\tclockConfig.flash_latency= Three_wait_state;\n\n\tClock_Confgiuration(&amp;clockConfig);\n\n\tBSP_Ticks_Init(100000000);\n\n\n\twhile(1)\n\t{\n\n\t\tGPIO_TogglePin(GPIOA, &amp;LED);\n\t\tBSP_Delay(1000);\n\t}\n\n}\n\n\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>You may download the source code from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/BSP-STM32F411_Nucleo64-1.zip\">BSP-STM32F411_Nucleo64-1<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/07\/BSP-STM32F411_Nucleo64-1.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Results:<\/h2>\n\n\n\n<p>By testing the delay function, we can make sure that everything related to systick is working:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>By using 1000ms delay, we shall get toggle rate of 1Hz.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Board Support Pack of STM32F411: Core Configuration\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/WWzam1Le-wc?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we took a look how to build core configuration for STM32F411. In this guide, we shall see how to use System Time (SysTick) to implement system tick in your board support package. In this guide, we shall cover the following: What is system tick in embedded systems. System Tick implementation. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2,11,12],"tags":[],"class_list":["post-1879","post","type-post","status-publish","format-standard","hentry","category-data-structures","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1879"}],"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=1879"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1879\/revisions"}],"predecessor-version":[{"id":1882,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1879\/revisions\/1882"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}