Introduction to Embedded Linux Part 1 - Buildroot | Digi-Key Electronics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of the things that makes linux such a popular operating system is its ability to be customized if we strip away some of the fancy features like say a graphical interface we can run it on low power devices and that includes things like single board computers and systems on a module in 2018 the eclipse foundation ran a survey asking people what operating system they use for developing iot devices linux outranked everything else by a long shot i don't know what this chart would look like today but you get the idea linux is a very popular operating system for embedded devices over the next few episodes i'm going to talk about why you might want to use linux in an embedded device and show you how to build your own custom linux distribution i recommend having some experience with linux before tackling these projects as we'll be using the command line quite a bit throughout the series let's get started [Music] the first question you might have is why use embedded linux at all let's review some options for embedded development first is bare metal programming this is something you might do on 8 16 and some 32-bit microcontrollers and many of them can be very inexpensive because you can tailor your code to a microcontroller it often means you can write very efficient code with little or no overhead you also get direct control of connected hardware direct control of hardware often means flipping bits in registers so your code will not be very portable as it will only run on that particular microcontroller family without a lot of overhead you have the option of using low power microcontrollers to get the job done so this approach is best for single purpose or simple applications and for times when you need strict timing such as controlling motors or leds next up is real-time operating systems these include a scheduler that's almost always running in the background so there's some overhead in your program because of that overhead it usually means a more powerful microcontroller is required some rtos vendors provide support packages for popular microcontrollers which can include things like libraries and drivers that can make your code more portable among the supported microcontrollers you still have the option of high level control of the hardware as you can still directly flip bits in registers but it might make for less portable code having a scheduler opens up the possibility of multi-threading that allows you to run several tasks concurrently which is useful for things like networking and user interfaces additionally you can still meet strict timing deadlines with a hard real-time operating system or through hardware timers finally we get to embedded general purpose operating systems like embedded linux note that running such an operating system requires essentially a full tiny computer as there is a huge amount of overhead such as a scheduler memory management and a variety of background tasks that need to run you'll almost always need to move to a more powerful microprocessor and have access to external ram and non-volatile memory like an sd card the good news is that you have access to a very fast cpu in the hundreds of megahertz or gigahertz and lots of memory sometimes gigabytes some applications like video audio games and some ai require this much power so you sometimes don't have a choice these operating systems often abstract away much of the low-level hardware control so you're working with files drivers and libraries instead of with registers however because of the popularity of operating systems like linux you can often find vendor or community supported libraries to help you out this means you can write very portable code that will work on different embedded linux boards and it might also save you development time if you don't have to learn the details of a particular hardware architecture it also gives you access to high-level languages like python and java that you might not have with the other two options such operating systems allow you to concurrently run many different threads and processes and they often give you access to advanced features like networking file systems and more complex user interfaces however because of all this abstraction you lose out on direct control of hardware and it becomes difficult if not impossible to meet strict timing deadlines you have the option of writing driver code yourself to have better control of connected peripherals but one of the big reasons to use an embedded os is so that you can leverage code written by others note that real-time operating systems do exist for microprocessor boards like this to give you better control over the timing of tasks but you still don't have the same level of precise hardware control a microcontroller would give you as a result many chip manufacturers include a microcontroller in the same die as their embedded microprocessor that gives you the best of both worlds you can run advanced features like networking and complex algorithms on the microprocessor and low level hardware control and timing on the microcontroller you're probably familiar with the raspberry pi which is one of if not the most popular embedded linux boards in the world however it's actually a fairly poor example of how you might use embedded linux in a professional or industrial setting it was developed as an educational tool and cheap desktop replacement and it's fantastic at doing those things in fact due to it having such a high level of support and just working out of the box i might recommend starting with one to develop prototypes or simply tinker with linux however use cases for embedded linux go way beyond just being a replacement for a personal computer it can be found running things like networking gear such as routers and lightweight servers you'll also find it in some robotic applications that need to do complex tasks some rockets and spacecraft like the spacex falcon 9 rely on linux to run you'll also find it in some consumer electronics and running the interface in various kiosks things like networking databases complex algorithms for something like computer vision multiple processes and so on dictate the need to use a full operating system like linux over something like an rtos or bare metal programming in many of these applications you'll often want a custom operating system in order to save on power and production costs that means picking the hardware that best suits your needs and running a customized version of linux that doesn't have all the overhead found in something like a common raspberry pi distribution note that creating your own custom single board computer is extremely difficult the pcb might be complex but it also means finding or creating drivers for every piece of hardware found on the board like ram usb and wi-fi because of that some vendors offer reference boards that you can copy or even systems on a module that you can design a custom carrier board around what i'm going to show you in this series should work for many popular single board computers and such systems on a module however i'm going to focus on the stm32mp157d dk1 discovery kit as an example if you've worked with a single board computer you're probably familiar with the simplest method of getting embedded linux to run which is where you download a pre-made image file from a website flash it to your sd card and then plug the sd card into your board this method is extremely fast and has the best chance of working out of the box however it means you have to settle for whatever drivers packages and libraries come with that image it could mean a lot of bloated software you don't need and that means longer boot times more flash space used and more power required to run you could perform customization after flashing but it would be time intensive if you have to do so for every board that you produce assuming you're making more than one the hardest possible option is to manually find or write the drivers needed to support your board i would only recommend this method if you were a glutton for punishment this might mean months of work putting together source code for dozens or hundreds of drivers and packages and then fighting with compilers this is probably the biggest reason it's so difficult to design your own single board computer the third option is to use a tool that helps automate that process the three most popular tools are probably build root open wrt and the yakto project note that even if you want to use one of these tool sets to create an image for your custom single board computer you'll still have to create drivers or tell the tools where to find them that's why i recommend starting with a known good single board computer to learn this stuff rather than making your own build root is extremely easy to use but that ease of use often means you have limited configuration settings when it comes to creating a custom linux image additionally each time you make changes you need to run the build process again to rebuild the full image open wrt focuses on mostly networking gear such as routers as a result you might not find many tools and support for creating generic linux images additionally many of the images it creates are based around the concept of users updating the device themselves through a package manager this can make it a little more difficult if you're trying to create a whole fleet of embedded linux devices and want to push updates to them finally we have the octo project this is a large tool set with a very active developer community it offers a lot of customization options when it comes to building linux images however it can be quite difficult to learn and it requires a ton of processing and disk space on your host machine with that i'm going to show you how to make a very quick linux image using build root in later episodes i'll introduce the yakdo project and demonstrate how you can begin developing applications on a custom linux build we'll skip open wrt for now as i'd like to focus on more generic embedded linux but it can be helpful to know that it's there as an option almost all of the tools i listed are going to require you to do things in well linux they're built on top of linux you can use something like ubuntu which is usually the normally recommended distribution for a lot of this i'm using linux mint because well i like to be different but it's also based on debian so a lot of the things that work in ubuntu should also work in linux mint i'm doing everything on a separate computer and i've vnc'd in so that i can record my screen but you're welcome to dual boot your computer you can set up a virtual machine there's windows subsystem for linux that may or may not work i have not tested it or you can often find a docker image to help you out once again i haven't tested it but i know that there are people out there who have created docker images for things like build root and yakdo project to begin open up a console we're going to do almost everything in a console and i'm going to make this a little bit bigger so you can see what i'm doing if you're watching this say on a smartphone for my purposes i like to do everything in a projects directory so i will make a project directory and go in there if you head to the build root user manual you can find a section on the mandatory packages many linux distributions come with these packages but we'll have to install a few extras especially things like build essential and n curses in order to give us a menu config interface which we'll use to configure our build i always recommend doing an apt update and probably an apt upgrade in order to make sure that you're working with the latest packages after doing the update we'll do the upgrade once that's done we'll need to clone the build root project from there go into the build root folder build root has a number of directories feel free to look through them the one that we care about is configs so if we look in there we can see a number of predefined configuration files for a variety of chips and boards in order to build a linux image if you don't see your board in here you can probably look around on the internet to find a build root configuration file for your board i'm using the stm32 mp157d dk1 however what i found is that this a configuration works well enough at least to get something started you may want to see if you can find a d configuration instead of an a but let's try this a config to see what happens to do that we just call make and then the name of that config file def config is default configuration so we run this make will read it and construct a dot config file in the build root project from here we can call make menu config which will read in that configuration file and then bring up a sort of graphical interface in order to adjust the options for our build feel free to look through these this is how you would make changes to things like your kernel or adding packages prior to building the linux image in fact i can go to kernel and you can see the various kernel options that you have available here we won't be exploring these right now so use your arrow keys left and right to highlight exit press enter and then select exit again and hit enter to exit the whole menu config the default configuration should be good enough from here all you need to do is type make and you let this process run for a while this could take one or more hours in order to build the linux image so i recommend going to make a sandwich or grab a cup of coffee or go do something else while this builds but you want to keep an eye on it to look out for any errors that might appear once that process is done you can look for the output of build root in output images here you should see a number of potential image files or maybe even a collection of files that you would copy onto your boot and root file systems in this instance we have an sdcard.img file that we can just copy directly to an sd card bit for bit using a tool like dd to create that linux image for us i'm going to plug an sd card into an sd card reader on my computer and you can see here that it auto enumerates using some other image that i had there previously we need to unmount these partitions so feel free to use the u-mount tool on the command line or just use a graphical interface like i do here from there type lsblk for list block and that's going to show our various storage devices i just plugged in an 8 gigabyte sd card so it looks like it's on mmc block 2. the sd card image file contains all of the partitions needed for our linux image so we just need to do a bit for bit transfer directly to the raw device so we're not going to worry about the partitions here in fact we're about to delete them in this process we'll use the dd tool to do this bit for bit copying i need root privileges to make it happen so i'll call it with the sudo command and i f is the input file that we're going to be copying of is where we want to copy it to so i'm going to point that at mmc block 2. we can also set the block size and something like 1 megabyte is probably fine it just speeds up the copying process i'll put my password in and let it copy by default dd does not output any sort of status so you just kind of have to take a leap of faith that copying is happening and in this case there are a few hundred megabytes that need to be copied so this might take a few minutes when it's done dd should tell you that everything was copied and that there weren't any errors now all we have to do is remove the card from our computer and plug it into our board to see if it works plug the sd card into the board for this particular board you'll want to flip the boot 0 and boot 2 switches to on in order to have it boot from the sd card almost all embedded linux boards will have a serial port dedicated to providing a console on this particular board uart4 is tied to the onboard st-link debugger which also does usb to serial translation by default the configuration file has uart4 being used as the console port so we can just plug a usb micro cable into the st-link port and connect the other end to our computer if your board does not have an on-board usb to serial translator you'll likely have to figure out which uart port the console is on and then connect a separate translator like this ftdi board then connect power to your board which for me is a usbc cable that's plugged into a 5 volt 3 amp supply to get the serial console from our board you'll need to install some sort of serial terminal program on linux this is probably something like screen or minicom i'm a big fan of picocom because it's really simple if you don't have it feel free to install it with whatever package manager you're using it's already installed for me the one thing that i noticed about picocom is that you need to add your current user to the dial out group in order to get it to work otherwise you might get a permission denied even using it with sudo so to correct that we can just call user mod and then add our current user to the dial out group and we can check to make sure that dialout appears as a group for our user by using the command groups from here you just call picocom give it the baud rate with dash b and then tell it what device is connected to your single board computer for my case it's tty acm 0 but that might be something different for you depending on your particular version of linux i'm going to connect it and you might have something or nothing here depending after all of this information is printed about the connection what i like to do is hit the reset button on my board or cycle power which will cause all of the boot messages to appear and you can watch what's going on with the various boot processes there are several boot loaders that need to happen and we'll talk about those in a later episode but for now what we care about is that we have a login prompt that means everything worked during the build root compilation process i looked up the difference between a 157d and a 157a my board is the stm32mp157d that supposedly runs at 800 megahertz whereas the 157a runs at 650 megahertz i'm kind of surprised this works but it happens to work anyway the build root configuration likely tells the clocks to start up at 650 megahertz which means that i'm probably just under clocking my board a little bit which is fine for this experimentation but if you are going to use build root for your processes i highly recommend finding a configuration file that works for your particular board and your particular chip to make sure that you're not missing out on things like clock cycles now that i have everything here i get my login prompt and for a very basic build root image the login is root and there is no password so we are in linux right now running on the single board computer if i take a look at any files nothing here i can look at my current directory that's because i'm in root and i can see what is in the root directory and you can see it's pretty normal linux stuff but there's just not much here there's no user there's really no packages this is as basic as it gets you'll probably find that there's no package manager it's really hard to install stuff unless you bring in cross-compiled binaries so if you wanted gcc you'd have to cross-compile it bring it in and then build your own programs on this board but a lot of times what you'll find is you don't want to do that you want to develop applications on your host computer test them and then bring in the binaries to your deployed image file just because development is faster and it allows you to make an image that can be copied to production copied to any number of devices that you want we'll explore development later but for now let's do a very quick script to see that this is indeed running most of these images come with vi is a very basic editor and i'm going to create a simple script here in vi i'm going to push i to insert and i'm going to echo something really basic to the console press escape to get out of editing mode and then type colon wq for write and quit then we can test this script by running test dot sh and it should print something to the console when you are done with picocom you can press ctrl a and then control x to give it the command that you'd like to exit out of picocom i hope this gives you a good demonstration for how to create your own linux image and get it running on some board oftentimes you'll find that linux devices are really meant to be single tasked not in the sense that you don't have multiple tasks running on the processor but you have a particular application in mind for that particular board and that particular deployment so you would want to create an image that meets those needs and then deploy it to many devices which is where these tools like build root and yakdo come into play they help us create these custom images that can be copied and deployed remember that this is probably the easiest way to create a custom linux image for a board however each time you make changes in build root you have to completely rebuild the entire image which could take hours next time we're going to explore the yakdo project which gives you a lot more control over the configuration and build process see you then [Music] you
Info
Channel: Digi-Key
Views: 91,256
Rating: undefined out of 5
Keywords: embedded linux, linux, embedded system, single board computer, buildroot, stm32mp1
Id: 9vsu67uMcko
Channel Id: undefined
Length: 25min 18sec (1518 seconds)
Published: Mon Sep 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.