In this hands-on tutorial, learn how to write and read data using page-based flash programming on STM32 microcontrollers in the Cortex-M0/M3 series. You'll see how to erase pages, write 32-bit data, and read it back—complete with debugging using CubeIDE’s live expressions.
🔗 Download the project code here: [https://controllerstech.com/flash-programming-in-stm32/]
This video only covers microcontrollers, which have memory distributed according to Pages like Cortex M3 and M0 series.
Related videos: • SECTOR Type Flash Memory : [https://youtu.be/dqfgxS3D1k0] • STM32 Tutorials Playlist : [https://www.youtube.com/playlist?list=PLfIJKC1ud8gga7xeUUJ-bRUbeChfTOOBd]
00:00Hello everyone. Welcome to Controllers Tech. Many of you guys requested this, so today we will see
00:16how to program the flash memory in SDM32. This video only cover those microcontrollers,
00:24whose memory is organized in pages, for example, Cortex M3 series, or M0 series. I will make another video about the controllers, whose memory is distributed in sectors, like in Cortex M4, or M7 devices.
00:41I am using SDM32F103, and it's a medium density controller, as mentioned here.
00:54There is not much to do in the setup. I am enabling the external crystal, and setting the clock to run at max 72 MHz.
01:13Save this to generate the code.
01:20Here is our main file. Let's include the library first. Put the C file in source directory, and header file in the include.
01:36Include the FlashPage.h, in the main.
01:53Let's take a look at the reference manual. F103 is a medium density device.
02:15As you can see, the main memory is divided into 128 pages. Each page is 1 kilobyte, and have a range of addresses associated with it.
02:31Now, we are going to program this page. You should always start as lower in the memory as possible.
02:39I will tell you the reason for choosing this later in the code itself.
02:53Let's open the C file, that we included. FlashWriteData will write the data to the memory.
03:00It takes the start page address as the parameter, and also a pointer to 32 bit variable, that you want to store.
03:15First of all, it will calculate, how many 32 bit variables you want to write.
03:21Then, it will calculate the number of pages, needed to store that data.
03:31Flash page size varies according to the controller.
03:35In my case, it is 1024 bytes, like it said in the datasheet.
03:41After calculating the number of pages, it will erase that many pages, starting from the start page, entered in the parameter.
03:50Now, it will start writing data.
03:53Each word takes 4 bytes, so we have to increment the address by 4.
03:59You can also write in half word, or double word, but then, the increment will also change.
04:06I have commented it out here.
04:09And finally the flash will be locked again.
04:13To read the data, we just need to access the memory.
04:21The data will be saved in this variable.
04:24Let's start now.
04:29I will write the data first.
04:32The memory location, that I copied from the datasheet.
04:37I need to define this variable first.
04:41Here, I am defining an array, which can hold two 32 bit variables.
04:47And we will pass the address of this array to the flash right.
04:52Let's build this.
05:01There is some warning, it's fine, let's go ahead and debug the code.
05:16Here is the memory tab, let's enter the memory address, that we want to observe.
05:28You can see it's all clean here.
05:37I also want to show you another memory address.
05:41This is the start of flash memory, for F103 controller.
05:46And you can see, it's not empty.
05:49That's why I did not programmed the start of flash memory.
05:54It is already programmed to run this code, that we are using right now.
05:59I will explain this in a while, that what happens, if we try to overwrite this part of the flash.
06:05You can see, it's occupied up to this address and therefore, we should only program after this part.
06:24That's why I chose the lowest part of the flash memory.
06:30Let's run this now.
06:33We hit the break point.
06:35Note that the memory location is empty right now.
06:39Now let's, step over this.
06:43And you can see the data being stored in the flash memory.
06:50Other than writing the 32 bit numbers, we can also write some string to this memory.
06:59We will write hello world in the flash memory.
07:03Let's build and debug this.
07:17You can see, right now the previous data is stored in the memory.
07:26And as we run this function, the data corresponding to hello world, is stored in the flash memory.
07:44I am creating another variable to store the data, that is read from the memory.
07:49This variable can hold 4 words.
07:57We will read from the same memory address obviously.
08:17I am putting rx data in the live expression.
08:31We can see the data here.
08:33Let's see if it is the same data.
08:36rx data is in the integer format, so we need to select the same here.
08:42And yes, we have the same data present in the memory, and in rx data.
08:48Now I will show you, what happens when we try to write from the beginning in the flash memory.
09:02I will try to write the same data, that is hello world.
09:18Let's open the disassembly tab here.
09:20You can see the instructions are being stored in the same address, that we want to program with hello world.
09:28Let's enter inside this function.
09:30I will put another break point at the erase function.
09:40I will put another break point at the erase function.
09:44I am stepping over now, just to show you guys, where the problem occurs.
10:04Now once we call this function, the entire page will be erased, and all the instructions will be erased along with it.
10:12And that's where the error occurs.
10:14As the instruction, that was supposed to execute, is not present in the memory.
10:28You can see the memory details here.
10:31This entire page is erased.
10:34The next instruction, that was supposed to execute, was present in that page, and is not there anymore.
10:41That's why I advised in the beginning, that we shouldn't write first few pages.
10:47Next, I will show you how to use stlink utility to see the memory details.
10:53It's at the bottom of the output of the book.
10:59Let me give you a simpleusal Albertang.
11:01This is when you press the document.
11:05You will know, making time.
11:06Any note of this, I will show you how to use a piano...
11:10It is very physical, right?
11:11I'll show you how to use a piano, as it is fine.
11:15Once you use children, you are interested in engaging your life.
11:22See this, the beginning of the flash memory is again occupied by the instructions.
11:29We will go to the address, that we have programmed.
11:37You can see the hello world written to the location.
11:43This is it guys.
11:49In the final code, I will include a function to convert the received data, into the string format.
11:56You can download the code from the link in the description.
Be the first to comment