How to Dual-Boot STM32 micro-controller | BootLoader Design | CUBEMX+TrueSTUDIO

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys in this video i am going to show you how i dual booted my stm32 microcontroller here i have two firmwares that does simple task of blinking leds and throw some data on the uart i have built these firmwares on atollic crew studio i am going to place both these firmwares in the flash memory of stm32 and then run any of these as per as my name before we go ahead and see how it is done let me tell you how it works all the user program of the microcontroller is located in the flash memory and this particular microcontroller that i am using that is stm32f411 has 5-12 kbs of flash divided unequally into 8 sectors i am going to place my firmware 1 starting at sector 3 and firmware 2 starting at sector 6. but by default when the microcontroller is configured to boot from flash memory the controller boots from the address 0x08000 which falls in sector 0. so here at sector 0 i need to write a code that will ask me which firmware i want to be running it that particular instance and the microcontroller should boot from that particular sector such piece of code is called as bootloader so dual booting a microcontroller is all about making a bootloader before i show you how to make a bootloader for stm32 let's see what these firmwares 1 and 2 does so here you see firmware one so on flashing this on to the controller you can see something printing on the uart so it says welcome to application one and then it keeps blinking red and orange leds and then it keeps printing during application one so now let me flash the firmware to here it's similar to the firmware one but it on the start it says welcome to application 2 and then it keeps printing your an application too and then it also blinks blue and green leds yeah so here you can see it blinks blue and green leds so now let me go and start building boot loader let me show you how i am doing so here i am using htm 32 cube mx software to just to generate the initialization code i think this is very handy for every project so searching my discovery board stm32f411 looks messy clearing all of pinots so i'll be using four leds here that that are on board that is red orange green and blue so i'll also be requiring one uart here so that from so that i can take input from the user using ur2 and then enabling interrupt here so actually there is some problem with this particular board here a lot of noise on the uart lines so i i will have to pull up the uart lines here so i'm using internal pull up so enabling internal pull up here on the software so and the major hardware change i've made here is in this particular bootloader firmware i'm actually using external crystal that is high speed external crystal eight megahertz crystal that is on the board but for both firmware one and firmware two i showed earlier i haven't used any external crystal i was using internal high speed internal oscillator itself so the code is generated and now let me enable the interrupts here with this particular function so i'm using receive interrupt so this is the receive interrupt enable 8-bit data array just to receive the data into that array so this is the function i would be requiring to call to enable the interrupt and receive data as well but this also requires and call back function okay receive complete callback function that yeah this is the receive complete callback function so i'll be calling this in my main function i'll be declaring it in my main file so here it goes so first i'll be checking whether the interrupt is received from ur 2 or not ok so if this particular interrupt is from ur2 then i'll be checking what the data is so here i'm only going to check whether the data is either one or two so let's keep it simple for now so if the receive first byte of the received data is one then i'll say jump to firmware one if the first byte of received data is two then let me jump to firmware 2 all right now declaring this m2 variable here okay and initializing it to zero all right so now let me pull for this variable in my while loop before that uh these are some debug scripts that i always use so i'll just copy my debug scripts so that i need not write every time okay yeah so now i'll be polling for the gem 2 variable here in case 1 when jump 2 is 1 i want i'm just printing jumping to let's say application one or firmware one then breaking this thing and then in case 2 where i'll be jump printing jumping to firmware 2. this is not the best debug script here so while in default case i will be toggling the leds here at 1000 millisecond frequency okay in the start let me also say welcome to beyond breadboards bootloader looks great and then ask user which firmware he wants to jump to choose the firmware to jump to one slash two question mark all right looks great okay let's build it uh till here and check whether the interrupt is working fine okay all right now it's time let me uh write this functions to jump to user code so here i'm making new files that is bootloader.h and dot c so in these files i'm going to implement my jump to user application code first jump to firmware one code and then and also declare the macros to your firmware base addresses that is base address of sector 3 and sector 6. so before we continue let me first show you how the data in the stm flash is organized the first word from the base address of the firmware is the address in the ram and on the boot the main stack pointer is set to this address the second word from the base address of the firmware is the address of the reset handler of this particular firmware so now let me assign the reset handler address to a function pointer here okay yeah so the second as i said you as i said to you guys that second word is the address of the reset handler of that particular user application so before we jump to user firmware so it is important for us to actually disable some important system calls so let me do rcc dnit so the rccd unit actually resets the whole system clock and then it runs everything on the internal crystal and then all the initialize actually resets all the peripherals after switching to the internal clock now since we don't want any interrupts from systick as well let's load everything to zero so we don't want any systick time interrupts or any interrupts that will that is going to disturb our code as this is the crucial part since this is a this is the part where we are handling the whole control to different application so here let me get the msp value here so the first word in the started the base address of any particular firmware is going to be taken by the msp oh before that what we actually required to do is there's a vtor register in after [Music] arm cortex m3 family so this is m4 so here also there is video register that should be set to the base address so later the msp should be set and now let's call the app reset handler all right let's copy the same code for gem 2 firmware 2 and let's change all these addresses we also need to actually place our firmware 1 and firmware 2 in their respective positions there so what we'll first do is we'll place this firmware before we run our bootloader flash our bootloader code there so in in that particular firmware you have a linker file and change the origin of that in that linker file to your respective start address or the place where you want to or the location where you want to place it so here my firmware y one lies in sector 3 so i am putting it in changing the origin to 0x 0 8 0 0 c 0 0 0. and another change that we have to do is we have to change the vt or offset even in the application code as well so that you can do it in this system underscore stm32f4xx or file so remember the flash base address on ordering the flash base address the vector table offset you have to get your firmware start address that particular sector start address so my firmware vector table offset would be c000 in my previous score for firmware 1 it would be c000 and for firmware 2 it would be 4 0 0 0 0. so now let me go and place both the firmware and see whether it is placed in the respective location or not so before showing let me erase the complete ship and flash it again with debug itself so you can see the whole uh memory is empty right now there's nothing at either of these addresses nothing here as well okay now let me go and flash firmware one after our settings are done all right and check for the start address so it is 0x08 0 0 c000 yeah this is the firmware one started right oops it seems wrong okay now okay the starter is actually zero x zero eight zero c000 yeah so here you can see there is a code there is a firmware here that that is flashed from this particular address and the first value of first word of the start address is actually the address that stack pointer will take in and the second word is the reset hand this particular word is a reset handler address of the reset handler of this particular firmware so handling to main function lies in the reset handler part so now going to flash firmware to there in that in its respective address that is sector six okay all right so now let me go and add the implemented gem to firmware one and firmware two functions in my main.c there before that i need to include my bootloader dot h here and then call gem to firmware one in case one and jump to firmware 2 in case 2. yeah it's almost done and compiled without errors so i also added a small uart animation kind of thing so that it can become more interesting there yeah it works fine so here when i press reset it says it's asking me which firmware i want to go and when i press one it jumps to application one you can see it says welcome to application one and the red and orange led is blinking and when i press 2 it says welcome to application 2 and you can see blue and green leds blinking there you
Info
Channel: Beyond Breadboard
Views: 2,029
Rating: undefined out of 5
Keywords: stm32, microcontrollers, dualboot, discovery board, truestudio, firmware, embedded architecture, IoT design, bin files, stm32f411re, flash memory
Id: V9AqrHbBcsA
Channel Id: undefined
Length: 25min 10sec (1510 seconds)
Published: Mon Aug 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.