QSPI in STM32 || Write and Read || N25Q

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone welcome to controllers tech today in the video we will see how to use quad spi in stm32 quan spi is mainly used to interface the external flash memory and it's very fast compared to the regular spi chips for this reason the quad spi flash memories can also be used as the internal flash memory of the microcontroller our focus will be on this part how to use the flash memory as the internal flash in this video i will focus on the setup process and how to read and write data to the external flash the next video will cover the mapping of the external flash as the internal memory and also how to load the application from it stm32 microcontrollers mainly have three different types of flash memories n25q mt-25 and mx-25 out of these three memories i have access to n25q and mt25 so i have written the codes for these two memories but i will show you how to make it work for the mx25 also as i don't have the access to mx25 series i can't say if it will work for it so let's start the video now sd microelectronics already have a github tutorial on these memories i will leave the link to this in the description here you can see there are three qspi drivers i have also uploaded the drivers for two of them on my github page these files are slightly different but i have kept the formatting same as that of the sds drivers let's see how to make these work create a new project in cube id i am using f750a discovery board for this tutorial give some name to this project and click finish first thing we are going to do is set up the clock select the external high speed crystal for the clock if you can figure anything wrong here the project won't work so make sure you configure the right crystal frequency as per your board as you can see the n 25 q can reach up to 108 megahertz we will take advantage of it i am running the system at 216 megahertz even if you run at a bit lower frequency it's all right since 108 is the maximum it can also perform well at slightly lower frequencies so the clock has been configured to run at 216 megahertz as you can see here in the data sheet the quad spi is connected to the ahb1 which runs at 216 megahertz also so right now the qual spi clock is also 216 megahertz which we will reduce later by using the prescaler all right the clock is configured now if you are using cortex m7 enable the cache now we will go to the quad spi and enable the quad spi lines for communication this here is the schematics for the f75z board and here you can see it only have one qspi memory chip this is why i can only use bank 1 while enabling the quad spi next we need to make sure that the pin connection is correct as per the schematics i have said it a lot of times during the ethernet videos and some people still configured the wrong pins cube mx sometimes configures the wrong pins for these modules so you need to cross check the pins with the schematics then we need to make sure that the pins are configured to run at highest possible frequency press ctrl a to select all the pins and go below and change the output speed to very high now let's configure the parameters the qspi clock is 216 megahertz right now and this n25q can run at 108 megahertz max so we need to use the prescaler of 2 to bring the q-s-p-i clock to 108 megahertz here we will enter 2-1 that is 1. this configuration we are doing is for the single transfer rate mode just set the fifo threshold to 4. next is the sample shifting it also depends on the mode we are using for the dual transfer rate there will be no shifting and for single transfer rate there will be shifting by half cycle flash size depends on the memory size for example i have the 128 megabits which is 16 megabytes of flash storage 2 to the power 24 gives us the 16 megabytes so the flash size will be 24 minus 1 that is 23. chip select high time let's keep it to 6 cycles clock mode should be low flash id is 1 since there is only one flash memory dual flash is disabled as there is no second flash available that's it for the configuration one last thing we need to do is create separate files for the peripherals like i mentioned in the beginning i am trying to keep it as per the sd's qspi drivers and that's how they have put it enable it here now click save to generate the project if you check the q s p i drivers by the s t they have separate files for q s p i dot c and dot h so if you want to use these drivers you need to put this code in the user code zero part and the rest of them in the user code one part and the same goes for the header file also you need to put these functions wherever they are defined in these files of course you need to change this memory size now if you take a look at my github the functions are almost the same the only change i have made is i have added the read function so that we can read the data from a particular address in the memory now to make it work you need to download these files first i have them here now we will copy the n25q header file in the project folder the rest we will modify let's start with the q s p i dot c file all right first we will put these functions in the user code zero and then we will copy all these in the user code one now let's see the header file the write function seems to be defined twice let me delete one and i will update the github for this we need to copy these private defines only so that's it i forgot to include the n 25 q header file here in the n25q header file you can see the memory size is defined here you can change it as per the memory size for your qspi device let's build it once to check for any errors all right we are good to go we will initialize the qspi first then erase the entire chip then we will write data to the memory we need to define the data first let's create another variable to store the data read from the memory so we will write this data into the q s p i at the address zero then we will read the data from the same address the write and read address is zero which means that it will be the start of the qspi address which in most cases is 90 million hexa i am saying most cases because there are very few microcontrollers where the qspi address starts at a 0 million hexa few warnings but that's all right let's debug it now i am putting a break point at while and one in the error handler just in case if any of the functions fail it will hit the error handler let's run it now we hit the while loop which means the functions were executed or right read buff do contain the data that we stored in the memory we can also check the data in the cube programmer just connect the controller now click the external loader search for your controller here you can see that the qspi address starts at 90 million hexa i didn't uncheck the others let me do that quickly all right now it's fine now we will enter the qspi address here and hit enter and here you can see the data that we stored in the flash memory some of you guys always have problems while storing the numerical values i will show you the simplest approach to it let's say the number is 1 2 3 4. create one buffer also now we will use the s print f to convert the number to the string and we will store this string into the flash memory include the stdio for the s print f just ignore these warnings and let's debug it here we hit the break point and you can see the string of the number that we stored you can later convert it back to the integer format i know this is not the effective way of storing the numbers but the purpose of these qspi tutorials is not to store the data into the flash anyway we will use this flash memory as the internal flash memory so as to compensate for the low flash in some microcontrollers for storing data into the external flash i will make another video using the wideband ics as they are widely available and we can simply use spi for that purpose still if you want to store the numbers into the flash you need to use the union method you can check out the eep rom code on my github and see how i have used union to store the number and floats now similar to this n25q there is the code for mt25 you need to follow the same steps here too if you check the initialization function i have kept everything similar to what i did in the n25 queue and in the header file 2 the similar function are being used i have h745 discovery board which have the two flash memories each 512 megabits in size so i have modified this particular code to work with dual flash also the steps are similar to what you saw in this video just in case if you want to see the working for mt 25 also let me know in the comment section i will make another video for it or else in the next video we will see the memory mapped mode where these external flash memories will be treated as the internal flash we will also see how to load the application from these external flash memories that's it for today the code is on the github so get it from there leave comments in case of any doubt keep watching and have a nice day ahead [Music]
Info
Channel: Controllers Tech
Views: 2,802
Rating: undefined out of 5
Keywords: stm32, stm32f4, f103, discovery, nucleo, STM32F7, STM32H7, QSPI, quad, SPI, tutorial, N25Q, MT24, MX25, memory, flash, read, write, mapped, mode, XIP, external, loader, application
Id: xIfh_uYy-OU
Channel Id: undefined
Length: 16min 51sec (1011 seconds)
Published: Sat Sep 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.