{"id":844,"date":"2022-03-22T05:29:26","date_gmt":"2022-03-22T05:29:26","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=844"},"modified":"2022-03-22T05:29:55","modified_gmt":"2022-03-22T05:29:55","slug":"investigating-bsrr-register-bit-set-reset","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=844","title":{"rendered":"Investigating BSRR register (Bit Set Reset)"},"content":{"rendered":"\n<p>In this guide, we shall investigate why there is BSRR (Bit Set Reset Register) in STM32 to set\/reset a specific GPIO pin.<\/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>Toggle pin with ODR and BSRR<\/li><li>Reason behind BSRR<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Toggle pin with ODR and BSRR:<\/h2>\n\n\n\n<p>In out most guides, we are using ODR (Output Data Register) to toggle the LED. Take for example getting started with STM32L0 (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=837\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=837\" target=\"_blank\">Here<\/a>) and with STM32F7 (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=514\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=514\" target=\"_blank\">here<\/a>) to toggle the LEDs.<\/p>\n\n\n\n<p>Mostly, we are using ODR to toggle the pin as following: (from STM32L0)<\/p>\n\n\n\n<p>Set pin to high:<\/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;}\">GPIOA-&gt;ODR |= GPIO_ODR_OD5;<\/pre><\/div>\n\n\n\n<p>Set pin to low:<\/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;}\">GPIOA-&gt;ODR &amp;=~ GPIO_ODR_OD5;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>On another handle, to set pin high with BSRR:<\/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;}\">GPIOA-&gt;BSRR= GPIO_BSRR_BS_5;<\/pre><\/div>\n\n\n\n<p>To set the pin low:<\/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;}\">GPIOA-&gt;BSRR= GPIO_BSRR_BR_0;<\/pre><\/div>\n\n\n\n<p>You might ask, why both?<\/p>\n\n\n\n<p>It comes in performance and what called race condition, take the following to screenshot for toggling the pin as fast as possible using both method.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"480\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint7.png\" alt=\"\" class=\"wp-image-845\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint7.png 800w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint7-300x180.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint7-768x461.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint7-750x450.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint7-400x240.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint7-250x150.png 250w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><figcaption>toggle using ODR<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"480\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint1.png\" alt=\"\" class=\"wp-image-846\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint1.png 800w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint1-300x180.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint1-768x461.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint1-750x450.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint1-400x240.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/DS1Z_QuickPrint1-250x150.png 250w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><figcaption>toggle using BSRR<\/figcaption><\/figure>\n\n\n\n<p>Take a look at the frequency, toggling the pin using BSRR is faster and almost has 50% duty cycle compared to ODR where you get low state longer than the high state.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Reason behind BSRR:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>In order to toggle a pin using ODR, the MCU has perform the following three steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Read the register.<\/li><li>Modify the register.<\/li><li>Write back to the register.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"344\" height=\"608\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-22-at-8.06.05-AM.png\" alt=\"\" class=\"wp-image-847\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-22-at-8.06.05-AM.png 344w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-22-at-8.06.05-AM-170x300.png 170w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-22-at-8.06.05-AM-250x442.png 250w\" sizes=\"(max-width: 344px) 100vw, 344px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Using BSRR, the MCU has to perform one step only which is:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Write the new value to the register.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>This method allows the user to set the pin atomically. <\/p>\n\n\n\n<p>&nbsp;you simply perform a single 32-bit write to the BSRR to set or only the relevant bits.<\/p>\n\n\n\n<p>This often means you don&#8217;t have to disable interrupts or use other concurrency protections when using the BSRR, and results in smaller and faster code for bit twiddling operations.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Digging deeper:<\/h2>\n\n\n\n<p>Check this video where the process of writing each pin using ODR individually and the process is being interrupt each 1 millisecond:<\/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=\"IMG 8058\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/KGG2yh0s4Gs?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Note how the blinking interval is being inconsistent. This condition called race condition.<\/p>\n\n\n\n<p>This happen when the toggle operation is getting interrupted.<\/p>\n\n\n\n<p>This is fine for toggling LEDs since it won&#8217;t have catastrophic effect. In real time embedded systems, this might lead  catastrophic  problems. For example, a nuclear plant needs to be cooled down and the process get interrupted and lead to turn off the cooling system. This will have catastrophic consequences.<\/p>\n\n\n\n<p>For that alone, chip manufacturers came up with BSRR (STM32) to avoid race condition.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall investigate why there is BSRR (Bit Set Reset Register) in STM32 to set\/reset a specific GPIO pin. In this guide, we shall cover the following: Toggle pin with ODR and BSRR Reason behind BSRR 1. Toggle pin with ODR and BSRR: In out most guides, we are using ODR (Output [&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],"tags":[],"class_list":["post-844","post","type-post","status-publish","format-standard","hentry","category-embedded-systems"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/844"}],"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=844"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/844\/revisions"}],"predecessor-version":[{"id":849,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/844\/revisions\/849"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}