{"id":1644,"date":"2023-03-19T05:39:06","date_gmt":"2023-03-19T05:39:06","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1644"},"modified":"2023-06-07T04:38:48","modified_gmt":"2023-06-07T04:38:48","slug":"implementing-timeout-in-your-firmware","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1644","title":{"rendered":"Implementing Timeout in Your Firmware"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_577764529-1024x1024.jpeg\" alt=\"\" class=\"wp-image-1645\" width=\"512\" height=\"512\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_577764529-1024x1024.jpeg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_577764529-300x300.jpeg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_577764529-150x150.jpeg 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_577764529-768x768.jpeg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_577764529-1536x1536.jpeg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_577764529-2048x2048.jpeg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_577764529-1150x1150.jpeg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_577764529-750x750.jpeg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_577764529-400x400.jpeg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/AdobeStock_577764529-250x250.jpeg 250w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall discuss how implement timeout for peripheral driver in polling mode.<\/p>\n\n\n\n<p><\/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>Implementing time out.<\/li><li>Example on I2C.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Implementing Time Out:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>In order to implement timeout in your firmware, you need timer. Since STM32 is based on ARM, SysTick will be used to handle time out and delay function which available across all ARM Cortex M devices.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Open your project and create new source and header file with name of Time_Out.c and Time_Out.h respectively.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Within the header file, include stdint 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;}\">#include &quot;stdint.h&quot;<\/pre><\/div>\n\n\n\n<p>Declare the following enum for OK and ERROR and call it TypedefStatus:<\/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;}\">typedef enum\n{\n\n\tOK=0,\n\tError=1\n\n}TypdedefStatus;<\/pre><\/div>\n\n\n\n<p>Add the following 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 Ticks_Init(uint32_t freq);\n\nuint32_t get_Ticks();\n\nvoid delay(uint32_t delay_ms);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, the entire header file 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;}\">#ifndef TIMEOUT_H_\n#define TIMEOUT_H_\n\n#include &quot;stdint.h&quot;\n\ntypedef enum\n{\n\n\tOK=0,\n\tError=1\n\n}TypdedefStatus;\n\nvoid Ticks_Init(uint32_t freq);\n\nuint32_t get_Ticks();\n\nvoid delay(uint32_t delay_ms);\n\n\n#endif \/* TIMEOUT_H_ *\/<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Now, head for the source file and include the Time_Out.h header 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;}\">#include &quot;TimeOut.h&quot;<\/pre><\/div>\n\n\n\n<p>Include the header file for STM32Fxxx (F4 in this example):<\/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;stm32f4xx.h&quot;<\/pre><\/div>\n\n\n\n<p>Declare the following variable to handle timing:<\/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 current_ticks;<\/pre><\/div>\n\n\n\n<p>We start off by Ticks_init:<\/p>\n\n\n\n<p>The init function takes one argument which is the core frequency:<\/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 Ticks_Init(uint32_t freq)<\/pre><\/div>\n\n\n\n<p>Within the function, we shall load the value of the freq\/1000 -1 to generate 1ms period as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"645\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.12.11-AM-1024x645.png\" alt=\"\" class=\"wp-image-1646\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.12.11-AM-1024x645.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.12.11-AM-300x189.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.12.11-AM-768x483.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.12.11-AM-1536x967.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.12.11-AM-2048x1289.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.12.11-AM-1150x724.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.12.11-AM-750x472.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.12.11-AM-400x252.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.12.11-AM-250x157.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">\t\/*Load the SysTick value to be the core frequency over 1000\n\t *\n\t * Since th core frequency is in MHz, dividing it by 1000 will get 1ms period\n\t * *\/\n\tSysTick-&gt;LOAD=(freq\/1000)-1;<\/pre><\/div>\n\n\n\n<p>We shall configure the Systick with the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Clock source to be processor clock.<\/li><li>Enable Systick interrupt.<\/li><li>Enable the counter.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"325\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.15.02-AM-1024x325.png\" alt=\"\" class=\"wp-image-1647\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.15.02-AM-1024x325.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.15.02-AM-300x95.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.15.02-AM-768x244.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.15.02-AM-1536x488.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.15.02-AM-2048x651.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.15.02-AM-1150x365.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.15.02-AM-750x238.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.15.02-AM-400x127.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.15.02-AM-250x79.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"346\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.18.41-AM-1024x346.png\" alt=\"\" class=\"wp-image-1648\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.18.41-AM-1024x346.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.18.41-AM-300x101.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.18.41-AM-768x259.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.18.41-AM-1536x518.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.18.41-AM-2048x691.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.18.41-AM-1150x388.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.18.41-AM-750x253.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.18.41-AM-400x135.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/03\/Screenshot-2023-03-19-at-8.18.41-AM-250x84.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">\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);<\/pre><\/div>\n\n\n\n<p>Thats all for the initializing function.<\/p>\n\n\n\n<p>For the 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()\n{\n\t\/*Increment the counter*\/\n\tcurrent_ticks++;\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Also, we need to get the current ticks 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;}\">uint32_t get_Ticks()\n{\n\t\/*Return the counter value*\/\n\treturn current_ticks;\n}<\/pre><\/div>\n\n\n\n<p>Spin lock the CPU for delay:<\/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;}\">\/*Spin lock the CPU to force delay*\/\nvoid delay(uint32_t delay_ms)\n{\n\n\tuint32_t ticks_start=get_Ticks();\n\n\twhile(get_Ticks()-ticks_start&lt;delay_ms);\n}\n<\/pre><\/div>\n\n\n\n<p>Hence, the source file 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;}\">#include &quot;stm32f4xx.h&quot;\n\n#include &quot;TimeOut.h&quot;\n\n\nvolatile uint32_t current_ticks;\n\n\n\nvoid Ticks_Init(uint32_t freq)\n{\n\t\/*Load the SysTick value to be the core frequency over 1000\n\t *\n\t * Since th 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_Msk);\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}\n\nvoid SysTick_Handler()\n{\n\t\/*Increment the counter*\/\n\tcurrent_ticks++;\n}\n\nuint32_t get_Ticks()\n{\n\t\/*Return the counter value*\/\n\treturn current_ticks;\n}\n\n\/*Spin lock the CPU to force delay*\/\nvoid delay(uint32_t delay_ms)\n{\n\n\tuint32_t ticks_start=get_Ticks();\n\n\twhile(get_Ticks()-ticks_start&lt;delay_ms);\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Example on I2C Driver:<\/h2>\n\n\n\n<p>The idea to use I2C is the driver requires ton of while loop as seen in this guide from <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=503\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=503\" target=\"_blank\">here<\/a>.<\/p>\n\n\n\n<p>The new declaration for the function 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;}\">TypdedefStatus i2c_readByte(char saddr,char maddr, char *data, uint32_t timeout)<\/pre><\/div>\n\n\n\n<p>The function returns TypedefStatus and takes 4 arguments as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Slave address.<\/li><li>Memory address.<\/li><li>Pointer to the data to read.<\/li><li>timeout for timeout handling.<\/li><\/ul>\n\n\n\n<p>Within the function, we start off by declaring Tick_start with initial value of current ticks:<\/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 Tick_start=get_Ticks();<\/pre><\/div>\n\n\n\n<p>Now, within each while loop, check if timeout occurs 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;}\">\twhile(I2C1-&gt;SR2&amp;I2C_SR2_BUSY)\n\t{\n\t\tif(get_Ticks() - Tick_start &gt; timeout) {return ERROR;}\n\t}\n\n\tI2C1-&gt;CR1|=I2C_CR1_START;\n\n\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_SB))\n\t{\n\t\tif(get_Ticks() - Tick_start &gt; timeout) {return ERROR;}\n\t}\n\n\tI2C1-&gt;DR=saddr&lt;&lt;1;\n\n\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_ADDR))\n\t{\n\t\tif(get_Ticks() - Tick_start &gt; timeout) {return ERROR;}\n\t}\n\n\t(void)I2C1-&gt;SR2;\n\n\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_TXE))\n\t{\n\t\tif(get_Ticks() - Tick_start &gt; timeout) {return ERROR;}\n\t}\n\n\tI2C1-&gt;DR=maddr;\n\n\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_TXE))\n\t{\n\t\tif(get_Ticks() - Tick_start &gt; timeout) {return ERROR;}\n\t}\n\n\tI2C1-&gt;CR1|=I2C_CR1_START;\n\n\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_SB))\n\t{\n\t\tif(get_Ticks() - Tick_start &gt; timeout) {return ERROR;}\n\t}\n\n\tI2C1-&gt;DR=saddr&lt;&lt;1|1;\n\n\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_ADDR))\n\t{\n\t\tif(get_Ticks() - Tick_start &gt; timeout) {return ERROR;}\n\t}\n\n\tI2C1-&gt;CR1&amp;=~I2C_CR1_ACK;\n\n\t(void)I2C1-&gt;SR2;\n\n\tI2C1-&gt;CR1|=I2C_CR1_STOP;\n\n\twhile(!(I2C1-&gt;SR1&amp;I2C_SR1_RXNE))\n\t{\n\t\tif(get_Ticks() - Tick_start &gt; timeout) {return ERROR;}\n\t}\n\n\t*data++=I2C1-&gt;DR;\n\n\treturn OK;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Now, the I2C read byte function won&#8217;t spin lock the cpu for ever in case something went wrong and will return error which can be handled within your application.<\/p>\n\n\n\n<p><\/p>\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 discuss how implement timeout for peripheral driver in polling mode. In this guide, we shall cover the following: Implementing time out. Example on I2C. 1. Implementing Time Out: In order to implement timeout in your firmware, you need timer. Since STM32 is based on ARM, SysTick will be used to [&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,7,12],"tags":[],"class_list":["post-1644","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-state-machine","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1644"}],"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=1644"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1644\/revisions"}],"predecessor-version":[{"id":1813,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1644\/revisions\/1813"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}