How to Build Linux Kernel Drivers on the Raspberry Pi | Raspberry Pi GPIO Driver Development in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys what is up welcome back in today's video i'm going to be showing you guys how to write a driver for your raspberry pi we'll do a simple driver today and in the end of this video series we'll have a fully complete gpio driver as you can see here on the screen i've got my raspberry pi hooked up running raspbian os it's the 32-bit version running on the raspberry pi 3b um we're going to write a driver for this raspberry pi all it's going to do is say hello world in kernel mode but what we're going to do is create a series where at the end of the video series we have a fully functioning gpio driver to do this we first need to understand why do we use drivers why are drivers even a thing that we have to worry about when we're doing an embedded development right so if you remember in our previous videos all we're going to try to do is access a gpio pin and set the output type to that pin and the output value to that pin and when we did this in what's called kernel mode or service mode on the arm processor we can just directly go ahead and access the address ox3f2000 which is the address that the gpio interface lives on the raspberry pi 3b which is based off of the broadcom bcm 2837 processor right really straightforward but when you're in an operating system like raspbian os when you write code and you run it you're actually in user mode which is the unprivileged the non-service version of the processor so if you write code that goes to access this address ox3f2000 you're going to get a serious set of errors that will crash your program and not do the functionality that you want what's actually happening here is if you look from left to right we have three things we have on the far left you have the hardware the hardware goes up through a hardware abstraction layer into the kernel in the kernel space we have privileged memory that operates in service mode on the arm processor and then on the right you have user space which is more code when you write code in the user space what's actually happening if you try to access this is you're trying to do two things that are illegal when you're talking about embedded development the first is you're trying to cross over a memory boundary in user space you are accessing memory that is being virtually translated from a virtual address to a physical address either in ram or a physical interface on the processor when you access this without asking the kernel for permission essentially or mapping it into your processor that creates your first error your second error is when you try to directly access the gpio bus without talking to the hardware abstraction layer your code may work for your processor but the minute you try to translate that to a new kernel or to a new version of the os or a new chip it's not going to work either it's the job of these things called drivers to create these they look like tunnels in terms of a diagram but they're really an interface to legally cross this memory boundary and to legally cross this driver interface to the hardware abstraction layer right so what we're going to do is i'm going to show you guys how on the raspberry pi i set up my build environment and write a little bit of code that gets ran on the raspberry pi in kernel mode what we can do is from there with this now kernel mode code we can add features to it that allow us to control the gpio interface on the raspberry pi so we're going to jump back over to our development environment here i'm going to turn off the raspberry pi it's still on in the background i just have the the cam link capturing it turned off so what we actually have right now is i am ssh'd into the raspberry pi with x forwarding turned on um with x forwarding turned on i can do lx terminal and hit enter and that will actually spawn me a terminal on the raspberry pi it takes a few seconds it's all happening over ssh but now you see i have this terminal over here that is a forwarded window through the raspberry pi if you have problems getting this set up or doing x forwarding drop a comment and let me know also do me a favor drop a comment about your latest raspberry pi project i'm really interested to see what you guys are working on we're going to move that over to the first window here and i'm going to increase the size so you guys can actually see it i've already written the nope not that i've already written the basic framework for a uh raspberry pi driver i'm going to walk you through all the different components we need to be aware of when we're writing drivers and make it the code available to you guys on github so that you can test it out yourself on your raspberry pi right the first thing you need to do is sudo apt install raspberry pi kernel headers i'm going to increase the size a little bit here uh raspberry pi kernel header so when you build code for the kernel you need to have the headers of the kernel that you're working on having the headers for the kernel that you're working on will provide you the proper interface into the kernel that will work on your version of the os if you don't use these headers if you use headers for let's say linux 236 when you're on you know linux five four the code will will try to access the kernel in an improper way and the kernel will know how to run your code and it'll just be a huge mess also when you install these kernel headers it actually gives you the build system for that kernels all you have to do is type make essentially and the kernel build system that comes with these headers will build the code for you so you run this i already have this installed so i'm going to cancel out of this this does take a little bit of time this is a fairly large package but once you get this installed let that run and then you should have the kernel headers for your system right and the way you can check that is if you type you name tac r that is the version of linux that you're running right it's the numeric version and you can check if you have the build system properly installed by on your raspberry pi doing ls slash lib flash modules slash shell escape you name attack r and then slash build if that folder exists which it does for my computer because i installed these headers then you have the build system installed now what you need to do is create a make file that describes how do you create your kernel driver right here is the make file to build our example piece of code right and i'm gonna walk through every step of what this is actually doing i don't want to type it in front of you because i think that's honestly kind of boring so this is what's happening in this make file here this line here is saying that i want to produce with the kernel build system a file called low level learning gpio driver.o it just adds it to the list of targets that the the linux kernel driver build system is going to create then i specify this kdir equals this is the you know just a variable that means the kernel directory is equal to what i showed you guys before it's the build system for the linux kernel that we're currently running on um and then i say that there's a target all where you know we're currently in a make file we invoke this by typing make now there is a sub make system that gets ran and we run it inside of the kernel build system right which came from here and we say that the list of files that we want to create the module folder so m is our current working directory so you know pwd print working directory and we are going to build a set of modules so we tell the new build system from the kernel build system run the build system in this folder and the target is to create this file and then if we need to like delete our stuff or like start over we have another target called clean and the clean target does the exact same thing only it cleans instead of builds right so pretty straightforward there um now that we have this low level learning gpio driver dot oh we need to get out of here and actually write low level learning driver.c and i've already done that and i'm gonna walk through each line of the code and tell you what it does this is a very basic linux kernel driver that will run on your raspberry pi and perform very basic functionality so let me walk through each line of code right here we're just including the linux kernel headers for your version of linux that you've downloaded from app remember the raspberry pi linux kernel headers that will install that will include these files in your c file so that the rest of the stuff actually works now we define two functions so they're static ins and we use a special keyword here in it and a special keyword here exit to create a function gpio driver init and it takes no arguments so we put a void there this function gets ran when the driver gets installed and this function gets ran when the driver gets uninstalled then we set that by saying module init the name of the function and then module exit the name of the function what that will do is when i say ins mod so install the module on this driver these this piece of code will run and all the code is going to do is going to run and say welcome to my driver in the linux kernel buffer and then when it exits when i uninstall it with rm mod it's going to print and say that we are leaving my driver very straightforward note here that we're doing print k instead of printf printf is the libc user mode print that prints to standard i o this print k actually prints to the linux kernel system buffer that you can access via d message and that'll show up kind of like a a system alert almost and i'll show you guys how to access that and then here we have some metadata that kind of just describes what license we developed the module under the author of the module what description like what functionality does this module perform and then what version are we um so if we write quit out of here we type make so again i type make and all it does is first thing go into the you know the build system that came from apt it tries to cc our driver and it outputs this low level learning gpio driver.ko ko stands for kernel object and the way we install this and let me make sure that i actually don't have it running ready to check to check the current running drivers on your system you do ls mod this will show you all the drivers that are currently installed on your system so i actually already installed it before i started this video so i'm going to uninstall that real quick just to get you guys a clean system uh gpio driver oh we gotta be sudo here gotta be root okay so to install this we do insmod sudo ins mod low level learning gpiodriver.kl and if we do ls mod rep for low level learning we now see that my driver is installed and running and we can check that it ran by typing d message which will access the kernel system buffer you know the the output messages that we did with print k and we see a bunch of welcome to my driver leaving my driver here and then if we want to uninstall it we do sudo rm mod all over the gpio driver with the message again and said that it's leaving my driver so i hope you guys enjoyed that we got some very basic functionality out of our raspberry pi kernel driver in my next video i'll show you guys how to set up an interface to the user via either the proc fs file system or a character driver and we'll use those to read input and output from the user and pump that into the gpio driver like i showed you in that diagram anyway guys i hope you liked this video if you did do me a favor hit like hit subscribe and i will see you guys in the next video where we add features to our driver make it a little more useful thanks guys take care bye [Music] you
Info
Channel: Low Level Learning
Views: 5,847
Rating: undefined out of 5
Keywords: raspberry pi, pico, rpi, microcontroller, arduino, maker, craft, hobby, electronics, wires, temperature, safety, project, board, electric, leds, led, thonny, python, micropython, os, ide, onewire, ds18b20, circuitpython, review, launch, measure, probe, rp2040, specs, specifications, how to, guide, programming, Pico emulation, retro games raspberry pi pico, etaprime, eta prime, raspberry pi pico, arm cortex m0+, low cost
Id: lWzFFusYg6g
Channel Id: undefined
Length: 11min 1sec (661 seconds)
Published: Fri Sep 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.