{"id":2151,"date":"2023-11-30T06:03:41","date_gmt":"2023-11-30T06:03:41","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2151"},"modified":"2023-11-30T06:03:44","modified_gmt":"2023-11-30T06:03:44","slug":"working-with-stm32-and-external-flash-w25qxx-part2-reading-data","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2151","title":{"rendered":"Working with STM32 and External Flash W25QXX Part2: Reading data"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"436\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/145104325.png-1.webp\" alt=\"\" class=\"wp-image-2152\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/145104325.png-1.webp 600w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/145104325.png-1-300x218.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/145104325.png-1-400x291.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/145104325.png-1-250x182.webp 250w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure><\/div>\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=2142\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=2142\" target=\"_blank\">here<\/a>), we took a look at the W25QXX chip and it&#8217;s feature and how to reset the chip and get the full ID.<\/p>\n\n\n\n<p>In this second part, we shall see how the read process is handled and how to read multiple data from the sectors.<\/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>The process of reading.<\/li><li>Getting the correct position.<\/li><li>Developing the driver.<\/li><li>Main code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. The process of reading:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>The process of reading as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Set the CS pin low.<\/li><li>Send the instruction 0x03h.<\/li><li>Then send the 24-bit or 32-bit address (depending on the size).<\/li><li>After the address has been written, the data shall be shifted with MSB first and stream of the data will be available as long as the CLK is available.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"433\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.19.07\u202fAM-1024x433.png\" alt=\"\" class=\"wp-image-2154\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.19.07\u202fAM-1024x433.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.19.07\u202fAM-300x127.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.19.07\u202fAM-768x325.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.19.07\u202fAM-1536x650.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.19.07\u202fAM-1150x487.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.19.07\u202fAM-750x317.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.19.07\u202fAM-400x169.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.19.07\u202fAM-250x106.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.19.07\u202fAM.png 1994w\" 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=\"359\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.24.41\u202fAM-1024x359.png\" alt=\"\" class=\"wp-image-2155\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.24.41\u202fAM-1024x359.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.24.41\u202fAM-300x105.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.24.41\u202fAM-768x269.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.24.41\u202fAM-1536x539.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.24.41\u202fAM-2048x719.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.24.41\u202fAM-1150x403.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.24.41\u202fAM-750x263.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.24.41\u202fAM-400x140.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.24.41\u202fAM-250x88.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Getting the correct position:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>The memory layout of W25QXX is as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"517\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.28.03\u202fAM-517x1024.png\" alt=\"\" class=\"wp-image-2156\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.28.03\u202fAM-517x1024.png 517w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.28.03\u202fAM-151x300.png 151w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.28.03\u202fAM-768x1521.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.28.03\u202fAM-775x1536.png 775w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.28.03\u202fAM-750x1486.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.28.03\u202fAM-400x792.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.28.03\u202fAM-250x495.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.28.03\u202fAM.png 838w\" sizes=\"(max-width: 517px) 100vw, 517px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>W25Q32 is consist of 64 blocks of memory (block 0 to block 63). Each block consists of 256 Byte of data as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"275\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.36.25\u202fAM-1024x275.png\" alt=\"\" class=\"wp-image-2157\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.36.25\u202fAM-1024x275.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.36.25\u202fAM-300x81.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.36.25\u202fAM-768x206.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.36.25\u202fAM-1536x412.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.36.25\u202fAM-2048x550.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.36.25\u202fAM-1150x309.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.36.25\u202fAM-750x201.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.36.25\u202fAM-400x107.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-8.36.25\u202fAM-250x67.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, to get the address of the data to be read, the following formula is utilised:<\/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 memAddr = (sector*256) + offset; \/*Calculate the address *\/<\/pre><\/div>\n\n\n\n<p>the sector is the block number, offset the is data position to be read. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Developing the driver:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>We start off by the header file.<\/p>\n\n\n\n<p>Open the W25QXX.h header file and include the following new 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;}\">\/*\n * @brief This function will set the number of sectors for W25QXX.\n * @param numOfSector is the number of sectors available in W25QXX.\n * @return nothing\n * @see W25QXX datasheet for the number of sectors.\n *\n * *\/\nvoid W25QXX_NumberOfSector(uint16_t numOfSector);<\/pre><\/div>\n\n\n\n<p>This function will set the number of the sector and takes number of sector as argument and returns nothing.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>The second 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;}\">\/*\n * @brief This function will read from W25QXX.\n * @param sector is the sector number of the flash\n * @param offset is the offset of the memory location to be read.\n * @param *rData is a pointer to buffer that will hold the data to be read.\n * @param size if the data to be read.\n * @return nothing\n *\n * *\/\nvoid W25QXX_Read(uint32_t sector, uint8_t offset, uint8_t *rData, uint32_t size);<\/pre><\/div>\n\n\n\n<p>This function will read from the  desired address and it takes the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Sector number which is the sector number.<\/li><li>Offset, offset from the sector to be read.<\/li><li>Pointer to buffer to hold the data to be read.<\/li><li>Size of the data to be read.<\/li><\/ul>\n\n\n\n<p>Thats all for the header file.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Now, open W25QXX.c source file.<\/p>\n\n\n\n<p>Declare new global variable 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;}\">uint16_t NumberOfSector;<\/pre><\/div>\n\n\n\n<p>This will hold the number of sectors within the W25QXX.<\/p>\n\n\n\n<p>Now, for the set number of sector function which is as simple as setting the global variable to same value passed as argument 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 W25QXX_NumberOfSector(uint16_t numOfSector)\n{\n\tNumberOfSector=numOfSector;\n}<\/pre><\/div>\n\n\n\n<p>For the read 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 W25QXX_Read(uint32_t sector, uint8_t offset, uint8_t *rData, uint32_t size)\n{<\/pre><\/div>\n\n\n\n<p>We start with declare an array to hold the command and the address to be send 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;}\">uint8_t txData[6]; \/*This will hold the command and address to be send*\/<\/pre><\/div>\n\n\n\n<p>variable to hold the address:<\/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 memAddr = (sector*256) + offset; \/*Calculate the address *\/<\/pre><\/div>\n\n\n\n<p>Set the CS 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;}\">W25QXX_CS_LOW();<\/pre><\/div>\n\n\n\n<p>Now, if the sector size is less than 512 which is for flash size less than 256Mb (32MB):<\/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;}\">\tif (NumberOfSector&lt;512) \/*less than 256Mb*\/\n\t{\n\t\ttxData[0] = 0x03;  \/\/ enable Read\n\t\ttxData[1] = (memAddr&gt;&gt;16)&amp;0xFF;  \/\/ MSB of the memory Address\n\t\ttxData[2] = (memAddr&gt;&gt;8)&amp;0xFF;\n\t\ttxData[3] = (memAddr)&amp;0xFF; \/\/ LSB of the memory Address\n\n\t\tSPI1_Transmit(txData,4);\n\t}<\/pre><\/div>\n\n\n\n<p>In case bigger than 512 (32MB):<\/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;}\">\telse \/*bigger than 256Mb*\/\n\t{\n\t\ttxData[0] = 0x13;  \/\/ Read Data with 4-Byte Address\n\t\ttxData[1] = (memAddr&gt;&gt;24)&amp;0xFF;  \/\/ MSB of the memory Address\n\t\ttxData[2] = (memAddr&gt;&gt;16)&amp;0xFF;\n\t\ttxData[3] = (memAddr&gt;&gt;8)&amp;0xFF;\n\t\ttxData[4] = (memAddr)&amp;0xFF; \/\/ LSB of the memory Address\n\n\t\tSPI1_Transmit(txData,5);\n\t}<\/pre><\/div>\n\n\n\n<p>After sending the command and address, start receiving the data 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;}\">SPI1_Receive(rData, size);<\/pre><\/div>\n\n\n\n<p>Set CS 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;}\">W25QXX_CS_HIGH();<\/pre><\/div>\n\n\n\n<p>Hence, 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;}\">void W25QXX_Read(uint32_t sector, uint8_t offset, uint8_t *rData, uint32_t size)\n{\n\tuint8_t txData[6]; \/*This will hold the command and address to be send*\/\n\tuint32_t memAddr = (sector*256) + offset; \/*Calculate the address *\/\n\n\tW25QXX_CS_LOW();\n\n\tif (NumberOfSector&lt;512) \/*less than 256Mb*\/\n\t{\n\t\ttxData[0] = 0x03;  \/\/ enable Read\n\t\ttxData[1] = (memAddr&gt;&gt;16)&amp;0xFF;  \/\/ MSB of the memory Address\n\t\ttxData[2] = (memAddr&gt;&gt;8)&amp;0xFF;\n\t\ttxData[3] = (memAddr)&amp;0xFF; \/\/ LSB of the memory Address\n\n\t\tSPI1_Transmit(txData,4);\n\t}\n\n\telse \/*bigger than 256Mb*\/\n\t{\n\t\ttxData[0] = 0x13;  \/\/ Read Data with 4-Byte Address\n\t\ttxData[1] = (memAddr&gt;&gt;24)&amp;0xFF;  \/\/ MSB of the memory Address\n\t\ttxData[2] = (memAddr&gt;&gt;16)&amp;0xFF;\n\t\ttxData[3] = (memAddr&gt;&gt;8)&amp;0xFF;\n\t\ttxData[4] = (memAddr)&amp;0xFF; \/\/ LSB of the memory Address\n\n\t\tSPI1_Transmit(txData,5);\n\t}\n\n\tSPI1_Receive(rData, size);\n\tW25QXX_CS_HIGH();\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Main code:<\/h2>\n\n\n\n<p>In main.c code:<\/p>\n\n\n\n<p>Define the ID of W25Q32  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;}\">#define W25Q32_ID\t(uint32_t)0xEF4016<\/pre><\/div>\n\n\n\n<p>Declare an array to hold the data to be read:<\/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;}\">uint8_t rxData[10];<\/pre><\/div>\n\n\n\n<p>In main function, after the initialization of SPI, reset the chip and getting the ID, we shall check if the ID is matching 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;}\">\t\/*If the ID is matched read the data*\/\n\tif(W25QXX_ID==W25Q32_ID) <\/pre><\/div>\n\n\n\n<p>Read 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;}\">\t\t\/*Do something here*\/\n\t\tW25QXX_Read(0,0,rxData,10);\n\n\t\tW25QXX_Read(10,10,rxData,10);\n\n\t\tW25QXX_Read(20,20,rxData,10);<\/pre><\/div>\n\n\n\n<p>Hence, the main code 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;delay.h&quot;\n#include &quot;SPI1.h&quot;\n#include &quot;W25QXX.h&quot;\n\nuint32_t W25QXX_ID;\n#define W25Q32_ID\t(uint32_t)0xEF4016\n\nuint8_t rxData[10];\n\n\nint main(void)\n{\tdelay_init(16000000);\n\n\tSPI1_Pins_Init();\n\tW25QXX_CS_Pins_Init();\n\tSPI1_Config();\n\n\tW25QXX_Reset();\n\tdelay(10);\n\n\tW25QXX_ID=W25QXX_ReadID();\n\t\n\t\/*If the ID is matched read the data*\/\n\tif(W25QXX_ID==W25Q32_ID) \n\t{\n\t\t\/*Do something here*\/\n\t\tW25QXX_Read(0,0,rxData,10);\n\n\t\tW25QXX_Read(10,10,rxData,10);\n\n\t\tW25QXX_Read(20,20,rxData,10);\n\n\t}\n\n\n\twhile(1)\n\t{\n\n\n\t}\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\">5. Results:<\/h2>\n\n\n\n<p>Start a debugging session and add rxData to the live expression and run the code and you should get the following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"981\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-9.01.25\u202fAM-1024x981.png\" alt=\"\" class=\"wp-image-2158\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-9.01.25\u202fAM-1024x981.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-9.01.25\u202fAM-300x287.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-9.01.25\u202fAM-768x736.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-9.01.25\u202fAM-1150x1101.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-9.01.25\u202fAM-750x718.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-9.01.25\u202fAM-400x383.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-9.01.25\u202fAM-250x239.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/11\/Screenshot-2023-11-30-at-9.01.25\u202fAM.png 1468w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>We have successfully read multiple data in different sectors of the W25QXX. Since the flash is empty, it shows 0xFF.<\/p>\n\n\n\n<p>Next, we shall start writing data to the flash.<\/p>\n\n\n\n<p>Happy coding.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we took a look at the W25QXX chip and it&#8217;s feature and how to reset the chip and get the full ID. In this second part, we shall see how the read process is handled and how to read multiple data from the sectors. In this guide, we shall cover [&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-2151","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\/2151"}],"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=2151"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2151\/revisions"}],"predecessor-version":[{"id":2159,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2151\/revisions\/2159"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}