Microcontroller Firmware from Scratch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everybody I'm here to talk about how to make film therefore microcontroller from scratch it's not to say that you should do that normally but rather explain an Avenue from through which you can learn more about microcontrollers quickly I'm a software engineer at Red Hat and I work on the kernel continuous integration project and I also maintain the diamond project which makes the drivers for graphics tablets and in my free time I do and better than the electronics our subject will be this little development kit which is called loopy but not officially just by people it's very cheap and all you need is this detail serial cable to program it by the way I'm going very fast because we don't have much time and links are in the yellow so if you open the slides you can find them on the internet you can get there get all the source code and everything so what I managed to do with this is a for example this little car made out of Lego and parts from printers and scanners I tried to make it with Arduino but I burned it out quickly and and it was expensive so I bought this one and it served me very well much cheaper and I had burned one yet another one is extracting EEPROM from a from a printer just just to not there you know an old printer I extracted see from another one was a present for my wife Christmas I built the hardware quite quickly but it took me until next Christmas to make the sauce right so it was a little late and the current project is I'm making a interface for a thermal printer to connect it to an old ZX Spectrum using the ZX printer interface it's a very educating and interesting what I'm trying to tell you here is to how to program it from scratch meaning that you don't have to use any libraries or any vendor tools just open-source software but you will still need one library of course the documentation and we can organize the documentation in a stack here going from top to bottom starting this board documentation from the manufacturer and elsewhere the my particular microcontrollers datasheet the reference manual for the microcontroller family the programming manual with with some programming tricks and there and the routines then the particular course reference manual and finally the architecture reference manual for arms v7 and the architecture behind those microcontrollers and this is about 2500 pages but they're all very well organized and fun to read actually so the blue peel is this has the USB socket the boot configuration jumpers reset button the MCU itself the system clock crystal the real-time clock crystal the power LED user LED and the programming programming header which we won't be using because this t SW the interface the microcontroller itself can run on 72 minute up to 72 megahertz maybe you can overclock it a day and I haven't tried it it has single cycle multiplication Hardware division which is awesome it has 128 kilobytes of flash memory 20 kilo bytes of SRAM and that's that's plenty for a microcontroller it also has lots of general-purpose pins even this little package which has like I don't know 26 pins was capable of communicating to a parallel interface EEPROM which needs a lot of pins and finally it has lots of other communication interfaces including as you've seen USB so first thing you want to know is how to get your program on there and if you look at the data sheet for them for the particular microcontroller you can see that it has a bootloader which is capable of programming a flash memory and it's doing that by you sort and that's why we did the serial port that's why we're using the TTL cable and this is the particular configuration of good pins to get it to boot from that system memory where it is located if you look at the board documentation we can find particular combination there as well it's called ISP this time and this is the actual configuration you can deduce that from the documentation that's how you need to put the jumpers there finally we need to find out where to be which exact things we need to connect our Cyril Cyril cable to and you can take a look at there another document which is bootloader documentation it says there that he used speech and for our X and P a nine foot 2 X so now we're ready to connect the serial cable we don't forget to cross our tracks under X and we can power it from the power of the board from the serial cable as well but sometimes it doesn't work so good so you can power it from exam for example from there my Chris B cable this is how it will look connected and powered on now we need to find the tool which would talk to that bootloader and you can readily find it in your repository stay awesome stm32 flash which has tons of features and very nice to use even though you don't have a program you can already run this tool to just check your connection and see some basic report parameters so that people don't say it often enough and if you look further in the slides it might look a little scary but actually and better it is easy and fun unless you you're paid to do it of course so nature of the start is blinking an LED we'll take a look at the schematics to take a look how they how did it actually connect it and we find what what is the led for us and it says that d2 is our user LED and here it is connected to PC 13 but in a kind of a quirky configuration because it's connected to power one end and the other end to the pin and that's a little unusual we might say but if you look at the documentation to the pins there is a little node down there which is saying that PC 13 our pin she could not be driven faster than 2 megahertz and also must not be used as a current source for example to drive an LED but it doesn't say that it cannot be used to the current sink and that's why the board designer used it this way it basically used one of the most useless pins there on the microcontroller to drive the use and you know hard-hard the soldiers de team so that was very economical so we can start sketching out our program we need to start on reset configure our pin fault for open drea drain output and two megahertz max speed as required we will go in an endless loop do a little delay and then Tokelau pain that's all we need to do there's of course a little bit more setup involved so the first thing is we need to find out how to how to start our routine on reset so we take a look at the architecture reference manual and there's towards the end a little part that says that we take address of the vector table from veto register we take the first 4 bytes to initialize this stack pointer and if you take a look further and for example in the programmers manual but it's mentioned in several places says that we have food the second stack that means that stack pointer for an empty stack should point right above the stack further on the next row by the entry we load that and we jump to that so basically our vector table is first the stack pointer then the reset address and now we can start making our a vector table first we locate the stack then we put the address right above it as the first entry and then the address of the research routine as the second entry now we still don't know where our what's our deeds or register containing that at reset where it's pointing where is the vector table supposed to be so we take a look there at the architecture France manual it says well as implementation defined so we take a look at the implementation of this area it's fixed at address zero which is fine great so we take a look at the memory map there we see the address 0 and it says there is a block of memory that is a list to flash or system memory depends on boot pins and that's how we get to boot from the system memory by the way there that's that's where our bootloader lives but if you change the boot pins the flash memory appears there instead so the flash memory is also and naturally at 0 8 all zeros and if you take a look further we can see where our SRAM easting our memory is to all zeros now we can start writing the link shrug the link her where to put all the parts of our program we take the first define the memory regions we say that flesh is at zero eight of zeros and it's ritalin and executable and then we say there is RAM add to all zeros and it's read write and not executable and then we start putting in the sections first we put the vectors in flash so they are first there are zero then we put the our code there and then we put the read-only data and finally put our readwrite memory into the SRAM and finally again we instruct GCC to put our vectors table into the vector instruction so it ends at the start of the flash next we need to talk to actual three throws and to blink the LED to control that pin and on arm the peripherals are controlled using memory mapped i/o BER basically you talk to the peripherals by read and write and certain locations and if you look at the memory map there are peripherals here all of them and the reflows usually are in certain memory areas as pointed there in the table so further on it doesn't say that explicitly in the documentation is like you know widespread knowledge but you're supposed to normally enable your peripherals somehow not always like Atmel microcontrollers some of them at least they don't require any configuration the peripherals can work right from the start but sometimes you need to save power and in this in case of this microcontroller you need to enable the clock but for example if you take big ole bone the SOC there it requires enable and the clock and power and it is more complicated but in our case just the clock so you look at the clock tree it might look a little complicated but it's not that difficult to figure out and it says there further on and another part that it's by default when you turn give the power the system is running from high-speed internal oscillator which is basically an RC oscillator inside there and it's running at about 8 megahertz and one thing it doesn't say though where's our to pay or C port connected so we don't know how we are supposed to how do we are supposed to enable it but there on the system architecture diagram we can actually find it there it is it is connected via APB to buzz through HP system bus so now we can find a tv2 bus here and it's connected like this through APB to prescaler HP rescuer and clock switch that's how the clock has been fed to our GPIO C port now we didn't actually need to go look at the clock 3 but it's instructive we could have just you know looked for the port C somewhere in the document maybe and we can find immediately that the RCC peripheral has the APB to peripheral clock enable registered there at address 18 hex and bit four controls the clock to i/o port C so now we need to find out where our RCC peripheral is there it is and these cans can enable the co-opted port so we define the absolute address to register and raise the bit for now our clock for the port season Abel next we need to configure the GPIO for the open train and the maximum speed so normally I would go through the chapter the whole chapter and read about the period flow to learn about it more and know how to you know what it can do what it cannot do in this case I'll just run through the registers this is quite actually simple there are two registers for general configuration low register and high register because there are 16 pins for bit per each pin then the input data register will read the pin pin stayed from and the out the data register where you write in state 2 and there are two registers which control like when she can control the ins differently by writing once in two different parts and finally the configuration lock register which could be used to prevent damage to external hardware like Siddhant obese configuration finally there is team at the table summarizing all the possible configurations of the GPIO port and it's tells us how to configure the general purpose i/o port up and drain with these particular bits and a particular speed until we can control it with the audio audio register output data register so we will be writing to output data register and reading from it because we had talked minute and that would control the N Mo's transistor connecting the pin to ground and not connecting it so 0 will become activate to ground and one will not be connecting it to anything I just opposite the usual push-pull configuration they are alternatively connected to granter to power so we take the configuration register high because it's been 13 it spilled over its addresses for these are all bits we take the out the output mode max speed to megahertz as as instructed take the open drain output take a look at word our ports is located in memory and we are ready to configure it there's our register address and there is like too much math there but it wasn't so necessary try to make it clear what's going on but I failed then finally we need to toggle the pin actually turn it on and off there is our outfit data register and at address 0 C and the pink bits 13 controls PC 13 port C pin 13 such as bit clean enough and there's our register and we are talking our bit already all we need to add is a little weight there we go that's crude but effective so now we need to build it the tool chain is generally available and distros this is just as your unknown ABI means that there is no host operating system that's what known stands for non stands for and is enough to install then we saw the compiler to compile for cortex m3 which is our SOC in thumb mode this is rather redundant because it's the only mode it supports but all the GCC versions require that we just compile it the object file then tell the linker to use the linker script to place the sections from the object file into right places and then we can that this produces the elf binary done we can inspect our elf binary and see that everything went where it should and we see that vectors went precisely to the show to the flash address 0 8 all zeros and there is just 8 bytes to enters by 4 bytes it followed by this followed by the code and finally our stack was placed in SRAM next we extract the the flash part from the from the elf binary and write it into raw binary and beer we can actually take a look again how it turned out so there is our stack address 400 hex bytes into the into the into the SRAM followed by the address of the reset routine with bit 0 set because it indicates some mode and it starts right after our stack there it is our code now we're ready to flash this is enough without so give the file and sell it to verify after writing and it goes quicker and we all ready to run but first we need to switch the our jumper back from the bootloader to boot from the flash so that's Alice to the address 0 we you can take a look at the board documentation again set our jumper and off we go works so as you could have seen this program was very short that's thanks to hardware designers tweaking tweaking it ticking the default in a way that is that is well suitable for most basic use cases and not so basic ones and that's that's actually what actually makes it much easier to figure all this out like you read the documentation but you can trust that what basically this case you're trying to do there are probably the defaults will be very good and you can check those in the register description so this gets boring right and all those register values and addresses by hand so naturally you would want to make a library and there is many ways to do it well that's how I did it you basically define the layout of registers for a peripheral defined three four addresses and then bit meanings and that will take quite quite a bit quite quite a long way towards making them functional so the bits right that's right now the microcontroller runs at eight megahertz and it's capable of running on 72 so how you can do that you can take a look at the clock tree and figure out how you can connect the external oscillator to the system clock and that's going through this part configuring the payload to multiply it by 9 then originate to stabilize and then you switch it over the system clock from the internal oscillator to the external Slater to the PLL that's being fed external oscillator but there of course some gorgeous like the flash cannot keep up with the CPU or this high speed so you have to tell it to slow down the CPU above certain system clock speeds so that so that the CPU can actually read the instructions that supposed to execute and another one is that it will be one a bus that has some some peripherals is cannot work faster than thirty six megahertz you're supposed to solve that down to so here it is that's the all the configuration for the speed and if you want more peripherals it's also very well explained there it's actually more than you need explained here and that's that's all you need to talk to to send answer to transmit and receive one byte throw it you sort here you can see how to configure the PWM mode and that's how you blink an LED without CPU that's how you can read the bio from IDC this is also simple enough and there is a little bit more involved in the description and that's how you can send byte and receive a bytes or SPI and finally don't be afraid buy some rewards and experiment thank you so if you have any questions I have a present for the best question one of those development kids go ahead so I really know very little about this so what would happen if you run the same program but without the code that configures that timer and the GPIO you can actually run this program that said this is a working program you don't need to do anything else and there is a if you click on the link on the slide you get that program and if you execute the comment comments that are in these slides you will get a working program exactly you don't need to configure anything these are our extras that's how where you can go later that's the whole program that you need to run that program and the linker script and that's all was that your question to run the program without which part without the setting the clock and GPIO big bloke and GPIO what kind of program would that be trying to write that the SATA state would be doing something I don't remember what's happening I suppose nothing would happen well you won't see anything most likely there will be no activity heartful probably if if the bus tells the processor that it's that something is wrong there will be a heart fault probably an exception and it will try to jump to address it on the vector table we should their own and it will get go haywire so I have a question a little bit from the practical side so we've seen a lot of documentation being someone who hasn't touched it before how would you go about finding out what I don't know because right now I'm in a situation where I don't know what I don't know so I don't know which documentation to look at right well you can start by asking questions and searching through that documentation first question is how do you program this thing so if you try to looking like program flash in the documentation you can stumble upon the mention of the bootloader and how you can program you can take it from there then you dig further like how the bootloader is booting and what controls it that's that's how I was finding it and also the if once you buy the board there is the board documentation it says ISP haha and certain system programmer that's I can give you a clue anymore yes so if you run to maybe like constrain it's a reason for this I would think Goods a fun thing you want to constrain in size or you want to make sure that all the code that's in there is a code that you've audited somehow so what about taking this approach and taking one of those like Lib open cm or cm sis and understanding it and then removing as much as you can would that be another approach of understanding out they as that microcontroller works it would take that that's a good question so I would say without understanding how the microcontroller works you don't know which parts of the library you're supposed to remove that's the problem and that's why that's why I actually went with this first before trying lip open cm3 because they're providing know how it works I okay I can go around it but it's difficult to understand if you don't know how they work so when you were going through the slides I've seen a mention of a JTAG ons stm32 which bears a question what sort of debugging tool do you have well usually I erase a pin or blink an LED that's good enough for me but of course you can use an SVD this WD interface there is plenty of cheap cheap tools available like to you to debug it through that interface did you try them yeah I have only Mac's use BOC th which supports it but that's actually expensive but only way you can find like these tools which connect there and are able to debug this like stop the execution step through instructions and all stuff you want I just didn't find it that were worth the trouble more questions Oh oops yeah have you ever run into a case where the ROM bootloader has information or defaults or magic numbers that you need to look up and then use in your program if you were to then jump to flash and run as or something that kind of like on UNIX or on a desktop computer with like UEFI is that does that occur ever with the microcontrollers no truly because you only use the bootloader to flash the board once you switch the boot pin to below the result of the path it's it's not getting executed it's jump straight into your corner and full control okay go in another there's another one there we have time yeah I haven't tried it I'd like to I think that the bootstrapping would probably be harder than C because thrust is a little more involved on that part and I've seen now there's a link there discussing how to do like to put strapping and make it work there but I assume that well it could be more fun I expected that was see what C gives you like real hard feel to it alright so thank you thank you [Applause]
Info
Channel: All Systems Go!
Views: 10,028
Rating: undefined out of 5
Keywords: 2019 -, Day 1, Cage, 2019, asg2019 ov, asg2019 eng, Nikolai Kondrashov, asg2019
Id: vznYVh5ugnk
Channel Id: undefined
Length: 25min 52sec (1552 seconds)
Published: Sat Sep 21 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.