 Hello! And welcome to our second video of how to create a super simple bootloader. Just to recap, so far we've done the getting started and the first part of the linker file hands-on session. So I'll continue to deploy the linker file hands-on while we do a new exploration. And this time we're gonna place a function in a very specific memory region. I'll start by adding it on the flash and then I'll create a second one in-run. And this is basically where we're gonna add it to allow the section for the flash to be created. That's gonna be called myMemoryRegion and I'll go into more details in the code. So right now I'm opening the linker script and I'm creating the myMemoryRegion. I'm adding it on the last32kb flash. So as we can see I've reduced the main flash to add myMemoryRegion. And then if I go down a little bit into these sections itself I'm gonna create the myMemoryRegion there. So I'm just adding the code in order to create it. I'm also gonna add now a check to see if myMemoryRegion has overflowed. This is a very neat feature I recommend that you have whenever you're creating your own regions. So basically what's gonna do is check the length of my memory and compare between the end and the start of the code that is there. And if it's bigger than of course it's gonna show as a memory overflow message for us. Now let's resume back to our code and I've viewed while I was switching and now I have the myMemoryRegion shown on the ELF file. So we could sit there below and it's still with nothing. So that's why I'm gonna create a small function to reside there. And since I'm gonna reuse the same attribute I'm gonna actually create a define with the attribute to place everything with this locate underscore phone into the section that we just created. The mySection. Right now that I have the define done I can go on and create my own function using that as a somewhat of shortcut key to place the function in the location that I want. So I'm gonna use that and create a very simple function called blink. And that's actually gonna position the blink function inside the myMemoryRegion. So the function itself is very basic. It's just a reuse of the HAL GPI autogoping. And I'm gonna use the defines for the LED green that we already created on the previous video. So and of course I'll add a delay that we received delay ticks as the input parameter. Now all that's left to do is pretty much use the function. So I can scroll down into the main loop and add the blink. And now I can give my code and check upon the ELF. So now I can see that myMemoryRegion has 36 amazing bytes, which is basically the function that we just created residing there. So now one way to check it is of course by debugging. So switch to the debug interface just to prove that this is not gonna cause any hard fall or any pointer going haywire. So this is just a very simple demonstration though. So I'm going to execute the code and just place a breakpoint in the main loop and we can see it reached. There's nothing going wrong. I can assure you guys that the code is working properly as we could see on the debug demonstration. Alright now let's assume that we wanted to use a run function this time instead of the flash. So I'm gonna create a function that will be placed on run using the same command with the attribute. But this time I'm gonna use an already created section of the linker script. So of course I don't expect anyone to remember the names that are available there. That's why I'm gonna also switch to the linker script region and demonstrate where to find it. So by opening the linker script I can scroll down a little bit and check that there are several regions that are related to the run memory. We can see the ones that we created on the past video, the memory that we just assigned. But what we are actually looking for is one that will reside on flash and then will be automatically uploaded to the run. And that's the one. So we can see here the function will be located at run but it's also saved on flash. And that's exactly what we want. So this is the section that is already created for that end. So the run function and I just copy paste here and now I can create a function which in our case is just gonna turn on the LED, the LED. So I'll just create that and pass an argument for if it should be on or off. I know the name is not the best one but bear with me. So the function I'll just copy and paste for now to use it on the main. So let me switch back to the main function. Scroll a little bit, replace the previous one and just add the one that we have. Now after I'm building the application we can actually go to the memory details again and see that we have on the run underscore dot data. We have our turn on LED function located at run in the size of it. So we have the address that it's loaded on flash and then we have the address that runs from run during the application. And again in order to demonstrate that I'm just gonna enter in debug mode and before even hitting play we can go to the function and go into a closer view and see that it actually is located in run. We can see the address and whenever I'm doing the code execution we can again see that the code will probably load and the function will behave normally without any hard fall creation there. All right. So this basically demonstrated how we could create one function in flash and the other one in run. And right now I'm just going to use different concept of function pointers. So instead of calling the function by its name I'm gonna create a pointer of functions. Right now we just have one function that I'm interested in adding which is the blink because it's located on my memory region. And as you guys probably already understood I'm eventually gonna share this memory region between the bootloader and the application. So I'm just adding another building block of the step needed towards our end goal. So of course I need to create a type depth for the pointer as well which I'm doing right now. And now that I have both the function pointer plus the pointer structure itself called functions all I have to do now is go to the main, switch back and replace the blink with the functions. Of course it's a pointer so a few changes need to be done on the code as well to reflect the pointer structure. That's the only position. And then the argument which I'll be keeping the end run 100 milliseconds. And just to prove again I'll enter in debug mode and you guys are gonna see that there's no hard fault in the application that's run as expected. Alright, this was basically what I wanted to show for this video. I hope you guys enjoyed and thank you for watching. Let's see each other on the next video where I'm gonna create the bootloader. Don't forget to like and subscribe if you enjoyed the video. And as always, have a wonderful day.