{"id":1716,"date":"2023-04-26T05:48:17","date_gmt":"2023-04-26T05:48:17","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1716"},"modified":"2023-04-26T05:48:20","modified_gmt":"2023-04-26T05:48:20","slug":"writing-to-internal-flash-of-stm32fxxx","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1716","title":{"rendered":"Writing to Internal Flash of STM32Fxxx"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/AdobeStock_64788699-1-1024x768.jpeg\" alt=\"\" class=\"wp-image-1717\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/AdobeStock_64788699-1-1024x768.jpeg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/AdobeStock_64788699-1-300x225.jpeg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/AdobeStock_64788699-1-768x576.jpeg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/AdobeStock_64788699-1-1536x1152.jpeg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/AdobeStock_64788699-1-2048x1536.jpeg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/AdobeStock_64788699-1-1150x863.jpeg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/AdobeStock_64788699-1-750x563.jpeg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/AdobeStock_64788699-1-400x300.jpeg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/AdobeStock_64788699-1-250x188.jpeg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=1708\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=1708\" target=\"_blank\">here<\/a>), we took a look how to read from the internal flash of STM32F411. In this guide, we shall see how to write to the internal flash storage of STM32F411.<\/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>Steps required to write to internal flash.<\/li><li>Developing the driver.<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. Steps required to write to internal flash:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>The steps as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Unlock the flash.<\/li><li>Set the parallelism for the write operation.<\/li><li>Set the PG flag in FLASH-&gt;CR register.<\/li><li>Wait for busy flag to be idle.<\/li><li>Disable the PG bit in FLASH-&gt;CR.<\/li><li>Lock the flash.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Developing the driver:<\/h2>\n\n\n\n<p>Before writing to the internal flash. First, it needs to be unlocked by following these two steps:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"230\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.17.44-AM-1024x230.png\" alt=\"\" class=\"wp-image-1718\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.17.44-AM-1024x230.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.17.44-AM-300x68.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.17.44-AM-768x173.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.17.44-AM-1536x346.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.17.44-AM-2048x461.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.17.44-AM-1150x259.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.17.44-AM-750x169.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.17.44-AM-400x90.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.17.44-AM-250x56.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Hence the  to unlock the flash 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 flash_unlock(void)\n{\n\t\n\t\/*Check if the Lock is active*\/\n\tif(FLASH-&gt;CR &amp; FLASH_CR_LOCK)\n\t{\n\t\tFLASH-&gt;KEYR = 0x45670123U;\n\t\tFLASH-&gt;KEYR = 0xCDEF89ABU;\n  }\n}<\/pre><\/div>\n\n\n\n<p>It is recommended to check the lock status before unlocking the flash.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>To lock the flash, set lock bit FLASH-&gt;CR register to 1 as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"452\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.21.40-AM-1024x452.png\" alt=\"\" class=\"wp-image-1719\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.21.40-AM-1024x452.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.21.40-AM-300x132.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.21.40-AM-768x339.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.21.40-AM-1536x677.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.21.40-AM-2048x903.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.21.40-AM-1150x507.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.21.40-AM-750x331.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.21.40-AM-400x176.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.21.40-AM-250x110.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;}\">void flash_lock(void)\n{\n\tFLASH-&gt;CR |= FLASH_CR_LOCK;\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>To write the to flash the 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 flash_write32(uint32_t addrs, uint32_t *wr32, uint32_t len)\n{\n\t\/\/Write data in 16-bit at a time\n\tuint32_t tempAddrs = addrs;\n\t\n\t\/*Set the parallelism to 32-bit (x32)*\/\n\tFLASH-&gt;CR |=FLASH_CR_PSIZE_1;\n\t\n\tfor(uint32_t i=0; i&lt;len; i++)\n\t{\n\t\t\/\/Set PG bit\n\t\tFLASH-&gt;CR |= FLASH_CR_PG;\n\t\t*(__IO uint32_t*) tempAddrs = wr32[i];\n\t\ttempAddrs+=4;\n\t\n\t\t\/\/Wait for BSY flag to be idle\n\t\twhile(((FLASH-&gt;SR)&amp;(FLASH_SR_BSY)));\n\t\n\t}\n\t\t\/\/Wait for BSY flag to be idle\n\t\twhile(((FLASH-&gt;SR)&amp;(FLASH_SR_BSY)));\n\t\t\/\/Disable PG bit\n\t\tFLASH-&gt;CR &amp;=~ FLASH_CR_PG;\n}\n<\/pre><\/div>\n\n\n\n<p>Steps:<\/p>\n\n\n\n<p>Store the passed address to temporary local variable (uint32_t tempAddrs = addrs).<\/p>\n\n\n\n<p>Set the parallelism to 32-bit:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"367\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.38.11-AM-1024x367.png\" alt=\"\" class=\"wp-image-1720\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.38.11-AM-1024x367.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.38.11-AM-300x108.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.38.11-AM-768x275.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.38.11-AM-1536x551.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.38.11-AM-2048x735.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.38.11-AM-1150x412.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.38.11-AM-750x269.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.38.11-AM-400x143.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.38.11-AM-250x90.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-full\"><img loading=\"lazy\" decoding=\"async\" width=\"928\" height=\"342\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.38.47-AM.png\" alt=\"\" class=\"wp-image-1721\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.38.47-AM.png 928w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.38.47-AM-300x111.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.38.47-AM-768x283.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.38.47-AM-750x276.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.38.47-AM-400x147.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.38.47-AM-250x92.png 250w\" sizes=\"(max-width: 928px) 100vw, 928px\" \/><\/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\/*Set the parallelism to 32-bit (x32)*\/\n\tFLASH-&gt;CR |=FLASH_CR_PSIZE_1;<\/pre><\/div>\n\n\n\n<p>Within for loop:<\/p>\n\n\n\n<p>Set the PG to 1:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"438\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.40.19-AM-1024x438.png\" alt=\"\" class=\"wp-image-1722\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.40.19-AM-1024x438.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.40.19-AM-300x128.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.40.19-AM-768x328.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.40.19-AM-1536x657.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.40.19-AM-1150x492.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.40.19-AM-750x321.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.40.19-AM-400x171.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.40.19-AM-250x107.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.40.19-AM.png 2044w\" 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\t\/\/Set PG bit\n\t\tFLASH-&gt;CR |= FLASH_CR_PG;<\/pre><\/div>\n\n\n\n<p>Write to the address and increment the address by 4:<\/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\t*(__IO uint32_t*) tempAddrs = wr32[i];\n\t\ttempAddrs+=4;<\/pre><\/div>\n\n\n\n<p>wait for flash to be idle by checking busy flag in FLASH-&gt;SR register:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"390\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.42.23-AM-1024x390.png\" alt=\"\" class=\"wp-image-1723\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.42.23-AM-1024x390.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.42.23-AM-300x114.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.42.23-AM-768x293.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.42.23-AM-1536x585.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.42.23-AM-2048x780.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.42.23-AM-1150x438.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.42.23-AM-750x286.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.42.23-AM-400x152.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-8.42.23-AM-250x95.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\t\/\/Wait for BSY flag to be idle\n\t\twhile(((FLASH-&gt;SR)&amp;(FLASH_SR_BSY)));<\/pre><\/div>\n\n\n\n<p>Outside the for loop:<\/p>\n\n\n\n<p>wait again for flash to be free:<\/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\/\/Wait for BSY flag to be idle\n\twhile(((FLASH-&gt;SR)&amp;(FLASH_SR_BSY)));<\/pre><\/div>\n\n\n\n<p>disable PG bit:<\/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\/\/Disable PG bit\n\tFLASH-&gt;CR &amp;=~ FLASH_CR_PG;<\/pre><\/div>\n\n\n\n<p>Thats all for the driver:<\/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;stdio.h&quot;\n#include &quot;delay.h&quot;\n#include &quot;flash.h&quot;\n#include &quot;uart.h&quot;\n\nuint32_t flash_data[4];\nuint32_t data_write_flash[4]={0xdeadbeef, 0xbabababa,0xdadadada,0x12345678};\n\nuint32_t address=0x08008000;\n\nint main(void)\n{\n\tuart2_tx_init(115200,16000000);\n\n\tsystick_init_ms(16000000);\n\n\tflash_unlock();\n\n\tflash_write32(address, data_write_flash, 4);\n\n\tflash_lock();\n\n\tflash_read32(address,flash_data,4);\n\n\tprintf(&quot;Written data at address 0x%x:\\r\\n&quot;,address);\n\n\tfor (int i=0;i&lt;4;i++)\n\t{\n\t\tprintf(&quot;%d -&gt; 0x%x\\r\\n&quot;,i,flash_data[i]);\n\t}\n\n\twhile(1)\n\t{\n\n\n\n\t}\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code:<\/h2>\n\n\n\n<p>You may download the source code from here:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/FLASH_Write.zip\">FLASH_Write<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/FLASH_Write.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Results:<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"952\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-7.53.36-AM-1024x952.png\" alt=\"\" class=\"wp-image-1724\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-7.53.36-AM-1024x952.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-7.53.36-AM-300x279.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-7.53.36-AM-768x714.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-7.53.36-AM-1150x1069.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-7.53.36-AM-750x697.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-7.53.36-AM-400x372.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-7.53.36-AM-250x232.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/04\/Screenshot-2023-04-26-at-7.53.36-AM.png 1340w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We have successfully wrote 4 words to the flash and read them back.<\/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 read from the internal flash of STM32F411. In this guide, we shall see how to write to the internal flash storage of STM32F411. In this guide, we shall cover the following: Steps required to write to internal flash. Developing the driver. Code. Results. 1. [&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,12],"tags":[],"class_list":["post-1716","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1716"}],"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=1716"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1716\/revisions"}],"predecessor-version":[{"id":1726,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1716\/revisions\/1726"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}