{"id":1961,"date":"2023-09-06T06:14:33","date_gmt":"2023-09-06T06:14:33","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1961"},"modified":"2023-09-06T06:14:35","modified_gmt":"2023-09-06T06:14:35","slug":"building-board-support-package-bsp-for-stm32f411-nucleo64-part15-1-timers-in-basic-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1961","title":{"rendered":"Building Board Support Package (BSP) for STM32F411-Nucleo64 Part15.1: Timers in basic mode"},"content":{"rendered":"\n<p><\/p>\n\n\n\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\/09\/AdobeStock_128586024-1-1024x683.jpeg\" alt=\"\" class=\"wp-image-1962\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/AdobeStock_128586024-1-1024x683.jpeg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/AdobeStock_128586024-1-300x200.jpeg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/AdobeStock_128586024-1-768x512.jpeg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/AdobeStock_128586024-1-1536x1024.jpeg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/AdobeStock_128586024-1-2048x1365.jpeg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/AdobeStock_128586024-1-1150x767.jpeg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/AdobeStock_128586024-1-750x500.jpeg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/AdobeStock_128586024-1-400x267.jpeg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/AdobeStock_128586024-1-250x167.jpeg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>In this 15th part of board support package, we shall develop timer driver that will overflow each 1 second and toggle LED accordingly.<\/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>Developing the header file.<\/li><li>Developing the source file.<\/li><li>Main code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Developing the header file:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>We start off by creating new header file with name of tim_bsp.h.<\/p>\n\n\n\n<p>Within the header file:<\/p>\n\n\n\n<p>Include the header guard 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 TIM_BSP_H_\n#define TIM_BSP_H_\n\n\n\n#endif \/* TIM_BSP_H_ *\/<\/pre><\/div>\n\n\n\n<p>Within the header guard, include the following header files:<\/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#include &quot;stdint.h&quot;<\/pre><\/div>\n\n\n\n<p>We need the main stm32f4xx header file and stdint library to handle data types of uint32_t.<\/p>\n\n\n\n<p>Since STM32F411 has multiple timers as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"338\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-06-at-8.50.18-AM-1024x338.png\" alt=\"\" class=\"wp-image-1963\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-06-at-8.50.18-AM-1024x338.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-06-at-8.50.18-AM-300x99.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-06-at-8.50.18-AM-768x254.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-06-at-8.50.18-AM-1536x508.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-06-at-8.50.18-AM-2048x677.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-06-at-8.50.18-AM-1150x380.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-06-at-8.50.18-AM-750x248.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-06-at-8.50.18-AM-400x132.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/09\/Screenshot-2023-09-06-at-8.50.18-AM-250x83.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We shall create macros for enabling clock for each timer 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;}\">\/*APB2 Timers*\/\n#define __BSP_TIM1_CLK_ENABLE()\t\t\tRCC-&gt;APB2ENR|=RCC_APB2ENR_TIM1EN\n#define __BSP_TIM9_CLK_ENABLE()\t\t\tRCC-&gt;APB2ENR|=RCC_APB2ENR_TIM9EN\n#define __BSP_TIM10_CLK_ENABLE()\t\tRCC-&gt;APB2ENR|=RCC_APB2ENR_TIM10EN\n#define __BSP_TIM11_CLK_ENABLE()\t\tRCC-&gt;APB2ENR|=RCC_APB2ENR_TIM11EN\n\n\/*APB1 Timers*\/\n#define __BSP_TIM2_CLK_ENABLE()\t\t\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN\n#define __BSP_TIM3_CLK_ENABLE()\t\t\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM3EN\n#define __BSP_TIM4_CLK_ENABLE()\t\t\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM4EN\n#define __BSP_TIM5_CLK_ENABLE()\t\t\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM5EN\n<\/pre><\/div>\n\n\n\n<p>Since we are interested in the checking if the timer has elapsed or not, we shall declare an enum to handle this 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;}\">typedef enum\n{\n\tCounting=0,\n\tElapsed=1\n\n}Timer_Update_StatusTypedef;<\/pre><\/div>\n\n\n\n<p>You have two states:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Counting which means the timer has not yet elapsed.<\/li><li>Elapsed which means the timer has overflow and started over again.<\/li><\/ul>\n\n\n\n<p>Declare the following 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_TIM_Start_Basic(TIM_TypeDef *tim, uint16_t prescaler, uint32_t max_value);<\/pre><\/div>\n\n\n\n<p>The function shall start the timer in basic mode as counter. The functions takes the three argument as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>TIM_Typedef to indicate which timer to be used.<\/li><li>Prescaler to set the prescaler value.<\/li><li>max_value to set the maximum value of the timer by setting the ARR value.<\/li><\/ul>\n\n\n\n<p>And the function shall return nothing.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>The second 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;}\">Timer_Update_StatusTypedef BSP_TIM_Elapsed(TIM_TypeDef *tim);<\/pre><\/div>\n\n\n\n<p>This function will return the status of either counting or elapsed and take TIM_Typedef as argument.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Third 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;}\">void BSP_TIM_Stop(TIM_TypeDef *tim);<\/pre><\/div>\n\n\n\n<p>This function will stop the timer and it takes TIM_Typedef as argument and returns nothing.<\/p>\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 TIM_BSP_H_\n#define TIM_BSP_H_\n\n#include &quot;stm32f4xx.h&quot;\n#include &quot;stdint.h&quot;\n\n\/*APB2 Timers*\/\n#define __BSP_TIM1_CLK_ENABLE()\t\t\tRCC-&gt;APB2ENR|=RCC_APB2ENR_TIM1EN\n#define __BSP_TIM9_CLK_ENABLE()\t\t\tRCC-&gt;APB2ENR|=RCC_APB2ENR_TIM9EN\n#define __BSP_TIM10_CLK_ENABLE()\t\tRCC-&gt;APB2ENR|=RCC_APB2ENR_TIM10EN\n#define __BSP_TIM11_CLK_ENABLE()\t\tRCC-&gt;APB2ENR|=RCC_APB2ENR_TIM11EN\n\n\/*APB1 Timers*\/\n#define __BSP_TIM2_CLK_ENABLE()\t\t\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN\n#define __BSP_TIM3_CLK_ENABLE()\t\t\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM3EN\n#define __BSP_TIM4_CLK_ENABLE()\t\t\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM4EN\n#define __BSP_TIM5_CLK_ENABLE()\t\t\tRCC-&gt;APB1ENR|=RCC_APB1ENR_TIM5EN\n\n\ntypedef enum\n{\n\tCounting=0,\n\tElapsed=1\n\n}Timer_Update_StatusTypedef;\n\n\nvoid BSP_TIM_Start_Basic(TIM_TypeDef *tim, uint16_t prescaler, uint32_t max_value);\n\nTimer_Update_StatusTypedef BSP_TIM_Elapsed(TIM_TypeDef *tim);\n\nvoid BSP_TIM_Stop(TIM_TypeDef *tim);\n\n#endif \/* TIM_BSP_H_ *\/<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Developing the source file:<\/h2>\n\n\n\n<p>We start off by creating new source file with name of tim_bsp.c file.<\/p>\n\n\n\n<p>Within the source file, include the following header 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;tim_bsp.h&quot;<\/pre><\/div>\n\n\n\n<p>Now, we shall populate the functions:<\/p>\n\n\n\n<p>For the timer start in basic mode:<\/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_TIM_Start_Basic(TIM_TypeDef *tim, uint16_t prescaler, uint32_t max_value)\n{\n\ttim-&gt;PSC=prescaler;\n\ttim-&gt;ARR=max_value;\n\ttim-&gt;CNT=0;\n\ttim-&gt;CR1|=TIM_CR1_CEN;\n}<\/pre><\/div>\n\n\n\n<p>For timer if elapsed or not:<\/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;}\">Timer_Update_StatusTypedef BSP_TIM_Elapsed(TIM_TypeDef *tim)\n{\n\tif ((tim-&gt;SR &amp; TIM_SR_UIF)==TIM_SR_UIF)\n\t{\n\t\ttim-&gt;SR &amp;=~(1&lt;&lt;TIM_SR_UIF_Pos);\n\t\treturn Elapsed;\n\t}\n\n\telse\n\t{\n\t\treturn Counting;\n\t}\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>For timer stop:<\/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_TIM_Stop(TIM_TypeDef *tim)\n{\n\ttim-&gt;CR1&amp;=~TIM_CR1_CEN;\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, the entire 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;tim_bsp.h&quot;\n\nvoid BSP_TIM_Start_Basic(TIM_TypeDef *tim, uint16_t prescaler, uint32_t max_value)\n{\n\ttim-&gt;PSC=prescaler;\n\ttim-&gt;ARR=max_value;\n\ttim-&gt;CNT=0;\n\ttim-&gt;CR1|=TIM_CR1_CEN;\n}\n\nTimer_Update_StatusTypedef BSP_TIM_Elapsed(TIM_TypeDef *tim)\n{\n\tif ((tim-&gt;SR &amp; TIM_SR_UIF)==TIM_SR_UIF)\n\t{\n\t\ttim-&gt;SR &amp;=~(1&lt;&lt;TIM_SR_UIF_Pos);\n\t\treturn Elapsed;\n\t}\n\n\telse\n\t{\n\t\treturn Counting;\n\t}\n}\n\nvoid BSP_TIM_Stop(TIM_TypeDef *tim)\n{\n\ttim-&gt;CR1&amp;=~TIM_CR1_CEN;\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. Main code:<\/h2>\n\n\n\n<p>Within 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#include &quot;tim_bsp.h&quot;\n\nvoid clock_config(void);\n\nGPIO_Output_Typedef PA5_LED;\n\nint main()\n{\n\n\n\t#if FPU_EN\n\t\tSCB-&gt;CPACR |= ((3UL &lt;&lt; 10*2)|(3UL &lt;&lt; 11*2));\n\t#endif\n\n\tclock_config();\n\n\tBSP_Ticks_Init(100000000);\n\n\tGPIO_Configure_Typedef PA5_LED_Config;\n\n\tGPIOA_CLOCK_ENABLE();\n\n\tPA5_LED_Config.PinNumber=pin5;\n\n\tPA5_LED_Config.Mode=OUTPUT;\n\n\tPA5_LED.pinNumber=pin5;\n\n\tGPIO_Initialization(GPIOA,&amp;PA5_LED_Config);\n\n\t__BSP_TIM2_CLK_ENABLE();\n\n\tBSP_TIM_Start_Basic(TIM2,10000,10000);\n\n\n\n\n\twhile(1)\n\t{\n\t\tif(BSP_TIM_Elapsed(TIM2)==Elapsed)\n\t\t{\n\t\t\tGPIO_TogglePin(GPIOA,&amp;PA5_LED);\n\t\t}\n\n\t}\n}\n\nvoid clock_config(void)\n{\n\tClock_Config_Typedef clockConfig;\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_Configuration(&amp;clockConfig);\n}\n<\/pre><\/div>\n\n\n\n<p>Since the SMT32F411 is set to run at 100MHz and the APB2 is running at 100MHz, we used prescaller of 10000 and max value of 10000 which will give us 1 second toggle rate.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Results:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>After uploading the code, you should get the following results:<\/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 this 15th part of board support package, we shall develop timer driver that will overflow each 1 second and toggle LED accordingly. In this guide, we shall cover the following: Developing the header file. Developing the source file. Main code. Results. 1. Developing the header file: We start off by creating new header file [&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,9,12],"tags":[],"class_list":["post-1961","post","type-post","status-publish","format-standard","hentry","category-data-structures","category-embedded-systems","category-peripheral-drivers","category-state-machines","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1961"}],"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=1961"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1961\/revisions"}],"predecessor-version":[{"id":1964,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1961\/revisions\/1964"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}