Bare metal embedded lecture-1: Build process

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in this section we are going to explore building and running bare-metal executables for armed target using new tools so in this section we are going to understand about the two chains who are going to understand compiling a C program for an embedded target without using any IDE and writing microcontroller starter file for stm32f4 microcontroller and you are going to write your own C start of code that is the code which runs before May and we are going to understand different sections of the relocatable object file that is dot o files and in this section we are going to learn writing linker script file from scratch and understanding the various section placements and we're going to explore linking multiple dot o files using linker script and generating application executable such as dot e.l.f bin Intel hex format etc and after that finally we are going to explore loading the final executable on the target using open OCD and gdb client so all these things we are going to do without using any ID so everything we are going to write from scratch we are going to write linker script from scratch the start of file and after that we are going to use command line tools to compile link and generate the binary and after that transfer the binary from host to target machine by again using the command-line tools given by the open OCD and the gdb now to do this exercise you should first have a sample C program so let's do one thing let's take one of the sample programs what we wrote in the previous lectures so I'm going to take some files from the project task killer so this project we actually created in the earlier part of the course didn't it so what you do is you just create one folder in your computer let's say my workspace and inside this copy all these files from this project LD dot c LD dot H main dot C and main dot H so go to the top scheduler project and copy LD dot C main door C and healthy daughter and main dot H so you should copy that into a folder called my workspace so you should do this and after that I will show you how to compile these files using the cross compiler let's move forward so now we have our sample program that is Dorsey files that is actually written using the high-level language that is C so now our goal is to convert the high-level language file into an executable so an executable is nothing but it's a collection of embedded target-specific machine codes for that high level language program what you have written isn't it after that this executable we are going to transfer into our target hold and we are going to run the program for this you have to do something called as cross compilation so what exactly the cross compilation cross compilation is a process in which the cross tool chain runs on the host machine and creates executable that run on different machine so in our case the host machine is our computer isn't it and the target machine is stm32 microcontroller based bowl and the microcontroller that is stm32f4 is based on ARM architecture isn't it so basically we have to create a machine instructions for our market action so now what exactly the cross compilation tool chain so a tool chain or a cross compilation to chain is a collection of binaries which allows you to compile assemble and link your applications so it also contains binaries to debug the application on the target to churn also comes with other binaries which will help you to analyze the executables like you can disassemble it you can dissect different sections of the executable you can extract symbol information size information you can convert the executable to other formats such as binary or Intel hex format and the tool chain also provides you the C standard libraries so which you will be using in your program so now for ARM architecture so we have got two popular tool chains one is provided by the gun ooh that is GCC for arm embedded processors which is completely free and open source and arm also provides its own tool chain and that is called as arm cc so this actually ships with Kyle IDE and the cord restriction version is actually free but if you want to remove that restriction you should purchase the license so that's why we will not be using arm CC in any of our courses so we'll be using the open source one that is the new tools so throughout this course we have been using the gun new compiler collections to check that is the GCC toolchain and we will be using the GCC toolchain for this exercise as well so next how to download the GCC toolchain for arm embedded processes so if you have already installed stm32 cube ide and this tool chain would have already installed as a part of installation so because stm32 cube ide uses GCC to child so that's why it would have already installed in your computer or you can download it from official website so if you don't have this tool chain so let's download the GCC toolchain for arm embedded processors so what you do is just search for GCC forearm and follow this thing developer dot arm calm and here go to this section and click on downloads and here you can get the latest worship so since I'm using Windows machine I would be using this one winter do dot exe if you are using Linux then you can go for this if you are using Mac then you can go for this like we don't know this I have a comeback so I hope you don't loaded this so now let me install let's select English ear and next I agree and it will be installing in C Drive under Program Files x86 that would be fine let's click install so you can see that it is installing various libraries so now it is completed so now very important step you have to check this ad path to environment variable so that is very important so just check that and click on finish so after that it launches the readme file where you can get the information about installing executables on Linux or if you are using Mac OS then you can refer this readme let me close this and you can close this as well then open your command prompt so here are what you should do is just type a RM - none - EAB I - GCC trust enter and this operation should detect the executable that is DCC here you can see that we are indeed able to run this command on Nandi ABI GCC which is nothing but our cross compiler and it says that there are no input files that's ok but you can see that the command prompt detected that command so that is very important let's go to the installation folder of the tooth chain that is in Program Files x86 so here it is GNU tools for our member it and here let's go under bin and here you can't see that all these are binaries which came with this tool chain installation here you can identify your cross compiler so this is a cross compiler GCC right here and this is assembler and this is a linker and these are the files which are used to analyze the e.l.f executable and there are various other binaries which are included with this tool chain installation so this what we call as a tool chain or a cross future so all these executables will run on host machine for example the compiler this is a compiler which runs on host machine but it knows how to create the executables for the ARM architecture so complete up to here and make sure that you are able to execute this command so you can also give - - were shown here to know the version of that tool chain so now we install the cross - Chinese it itself and these are smuggling foreign binaries which you must be aware of how the first one is armed and here BITC see this not only does compilation so this also does link keep assembling so with this one come on you can compile assemble and link your applications and if you want to invoke the linker explicitly then you can use this comma armed and EA bi LT and this is actually an assembler so assembler is nothing but it converts assembly level language file to machine codes so that's what we call as assembler after that you have got the LF file analyzers by object dumb read elf nm etc and also there is something called as format converted like object copy which is used to convert a one executable format to another executable file so which you will see practically as we make a progress in this lecture series so in the next lecture I am going to cover build process and I'll see you in the next lecture let's understand the build process and who can later try this practically let's say you have made dot C when you first invoke the compiler to compile your source file first ages pre-processing stage in this stage all pre-processing directives of the source file will be resolved and the output file main dot I is created for main dot C main dot I is created this is not compilation remember that this is a pre-processing stage of the compilation which is usually done by the compiler itself the pre-processing directive such as all hash includes the C macros or the conditional compilation macros of the source file will be resolved and a pre processed file is created that has the extension of dot I after that what comes is the code generation stage that is translating a source file into assembly language that is if the input is main dot I then output will be main dot s here what happens is higher-level language course statements will be converted into processor architecture level mnemonics higher-level language in this case is C isn't it C statement will be converted into the assembly level language that's what we call as mnemonics after that comes the assembler stage all these things will be done by the compiler itself you need not to invoke any other command everything will be done by the compiler in our case the compiler is armed and EAB IGCC because we are doing the cross compilation in the assembler stage the assembly level mnemonics are converted into op codes of course are nothing but machine codes for various instructions because at the end of the day the target or the processor understands numbers or machine codes that's why the assembler does this job of converting the assembly level demonics into the machine codes and it creates output file called relocatable object file if the input is Maine dot s then output will be Maine dot o that's what we call as relocatable object file I'll explain you what exactly I mean by relocatable so I will explain that later this is a whole process in our case we have got two source files isn't it main dot C and ended up C which will undergo pre-processing and after that parsing where some checks will be carried out further C syntax after that it goes into code generation unit of the compiler the output will be dot s file and in assembler stage assembler converts the dot s file that is assembly level mnemonics into machine code it generates the output file dot o dot all contains processor architectures specific machine codes with no absolute addresses that's the reason why it is called as relocatable the machine codes which are stored in dot o file they don't carry absolute addresses of the program memory they carry an address which can be relocatable we'll explore that later when we do this compilation practically this is a process of converting high-level C code to machine codes remember that so you can't see all these dot I and dot s files unless you instruct the compiler to save those files we can do that but by default compiler doesn't save dot i and dot s files which are created in between the process after that comes the linking stage all the relocatable objects files will be taken up by the linker it will resolve all the symbols and other information and it will merge the different sections of the dot o files to create one executable that is the final executable and the format of that executable will be dot here left dot L F stands for executable and linkable format all relocatable object files are merged together to create one executable after that you can take that executable which is in yellow format to create some other formats such as binary format which has the extension of dot bin or you can convert that DLL file into Intel hex format using the tool object copy this is a complete build process in summary the first comes the pre-processing followed by compilation followed by linking and all these steps will be carried out by using only one binary that is arm none EAB IGCC only one command you have run which does all these tasks you need not to explicitly in walker assembler or linkers like that so you need not do that you can just use one command in the next lecture let run this command to cross compile our dot c files let's execute some commands to compile our Souls files now head over to the folder that is our workspace under which we have kept our source files isn't it to have God LD dot c LD dot H may dot Z and may not H main dot C and LD dot C are our source files first let's compile male dot C for that we have to invoke the cross compiler the cross compiler is armed none EAB i TC c then let's give the input file that is main dot c after that let's mention the output file by using the compiler argument - Oh which instructs the compiler to generate an output file let's give the output file name main dot o this command what it does is it takes the input file may not see and creates the relocatable object file but the default behavior of this command is to compile assemble and link but we don't want to link we just want to compile so we just want to compile main dot C and we want to generate the relocatable optic file if your goal is just to compile then you have to instruct the compiler explicitly that we do by mentioning another compiler argument you can give that compiler argument before you mention the input files I mean it's not monetary but you can do that it's better to give the compiler arguments before mentioning the input source files I would give the compiler argument - see this instruct the compiler to just compile and assemble and not to link all these compiler arguments you can explore by going through the documentation that is using a new compiler collection documentation the link to this online documentation I have given in the resource section you can just explore here just click on options controlling the kind of output if you browse this document you will get - see compiled or assemble the source files but do not link the linking stay simply is not done the ultimate output is in the form of an object file for each source file by default the object file name for a source file is made by replacing the suffix dot C dot I or daughters with dot o let's go back and hit enter so the compilation didn't go well there is a problem with the assembler now let's analyze this error what's the goal of this command the goal of this command is to generate main dot o file that is relocatable object code which is nothing but collection of machine codes the main dot C will go through all these phases when it reached assembler there was a problem the assembler couldn't able to generate a machine level instructions so these codes are there in our source file isn't it in the form of inline assembly code the error says that the compiler couldn't able to understand those inline assembly codes that's because in this command we have not informed the compiler about our processor architecture that is very important because this cross compiler can generate instructions for various processor architectures like cortex m4 codecs m0 arm9 arm7 for various processor architectures you can use this compiler in our case the processor is arm cortex-m for and either you have to tell the processor name or you have to mention the processor architecture that we can do by using some more compiler options or arguments let's go back to our documentation and now let's explore about machine dependent options here you can see that GCC supports all these machine options and we should be looking at arm options here just search for machine architecture it supports various architectures but the default one I'm not sure if you don't supply this argument I don't know what it takes by default that's why it's very important that you know you should initialize this argument to Army seven which is true in our case our arm cortex-m for processor is designed based on arm v7 architecture if you are not sure about the architecture then don't worry go to another option here that is called as MC P you here is MC P this option you can mention the arguments for this option is same as m2 you can pick one option among these various options you can see that it has codecs a76 codecs m0 if you are using cortex-m 0 + then you should be using this cortex m1 m3 m4 let's say what it says this specifies the name of the target ARM processor GCC uses this name to derive the name of the target ARM architecture and the ARM processor type for which to tune for performance let's use this option em CPU let me go back clear this let me mention here - em CPU is equal to cortex - m4 this mentions the target processor after that so one more compiler argument here to use that is am thumb there are two options em come and M are in our case we'll have to use em thumb if you don't use this then M arm will be considered by default what this does this helps the compiler to make a selection between generating code that executes in arm and thumb States remember that the cortex-m processors they support only thumb state that means they execute those instructions which are there in thumb instruction set architecture if you make a thumb processor to execute arm instruction then there will be an exception there are some processors which support both arm and thumb stage you can switch between arm anthem states but that is not possible in ARM Cortex MX processors because these processors support only one state that is thumb instruction state that's the reason you should use this option otherwise GCC will include instructions from arm instruction set architecture which our processor will not be able to execute that's why you can see are the default for most configurations is to generate code that executes in arm state so you have to override this there's a reason we should use em come here let's go back and um hum this much is sufficient for a time being later we can explore some more compare arguments let's hit enter the compilation went well and here you can see that the main dot o is created this is just a compilation there is no linking as I said we have stopped linking by using - see if you just want to create assembly file that means if you want to stop compilation at this stage instead of - see you can use - capitalist let's explore that so here a stop up to the stage of compliation proper do not assemble the output is in the form of an assembler code file for each non assembler input file what I do is you have to give output file name as main dot s and hit enter this command generated assembly file for the dot C file that's it here it is main dot s let's explore this file you can open in a notepad no problem and here you can see that this is a assembly level mnemonics which are generated for our Darci file these are the tongue instructions which are generated by the compiler so you just learned how to invoke the cross compiler with few arguments to create the object file for your dot C file in the next lecture let's see how to write a make file and automate this process because every time you know invoking the compiler and mentioning the arguments mentioning input files output files it would be a tedious process that's why it would be good if we create a make file add automate the build I will teach you that in the next video
Info
Channel: Fastbit Embedded Brain Academy
Views: 39,536
Rating: undefined out of 5
Keywords: arm cortex, cortex m4, arm embedded, bare metal embedded, embedded systems tutorial, stm32 tutorial, arm programming tutorial, keil programming, stm32cube, microcontroller, microcontroller programming, embedded software, stm32 discovery board, cross toolchain, build process, embedded projects, stm32
Id: qWqlkCLmZoE
Channel Id: undefined
Length: 27min 58sec (1678 seconds)
Published: Wed Apr 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.