Raspberry Pi Kernel Development | Writing a Raspberry Pi ARM GPIO Driver in C | Embedded Concepts

In this video, we talk about the purpose of drivers and why they are necessary when working on embedded systems. Later, we go over how to install the Raspberry Pi kernel headers and use them to write a kernel driver for the device. Finally, we install the kernel driver and prove that it is running on our device. This is part one in a video series for creating a GPIO driver for the Raspberry Pi!
Drop a like and subscribe to be alerted when new videos go out.
🏫 COURSES 🏫
www.lowlevel.academy/courses/
LINKS:
LLL Merch: linktr.ee/lowlevellearning
Code: github.com/lowlevellearning/l...
Follow me on Twitter: / lowlevellearni1
Follow me on Twitch: / lowlevellearning
Join me on Discord!: / discord

Пікірлер: 126

  • @vicktorioalhakim3666
    @vicktorioalhakim3666 Жыл бұрын

    I'm so happy to have discovered this channel... Back in the days finding information about such topic was a nightmare (still kinda is). Thanks a bunch! P.S. would be great if you can make some videos for driver dev on the Rockchip processors, and in particular for the PinePhone.

  • @dmoneyballa
    @dmoneyballa2 жыл бұрын

    Thank you so much. Got assigned to write a driver in work and was looking over other code in the repo, this helped me a ton. Thanks, liked and subbed.

  • @koman35000
    @koman350002 жыл бұрын

    dude you'are aramzing i've been looking for linux embedded driver dev tuto for loooong time you really deserve a support for this high quality content

  • @appusajeev
    @appusajeev2 жыл бұрын

    Exactly what I was looking for.... Waiting for part 2 where you add functionality.

  • @LowLevelLearning

    @LowLevelLearning

    2 жыл бұрын

    Thanks for watching!

  • @Beeblzaphod
    @Beeblzaphod2 жыл бұрын

    So much information in 11 minutes, amazing. Thanks a 1,000,000 for the video.

  • @LowLevelLearning

    @LowLevelLearning

    2 жыл бұрын

    Thanks a million for watching!

  • @toss1123
    @toss11232 жыл бұрын

    amzing stuff, wonder how you learn all these knowledge with details like this, awesome work! many thanks!

  • @phinner4270
    @phinner42702 жыл бұрын

    I never knew writing a basic driver was that simple, thanks mate...

  • @Nunya58294

    @Nunya58294

    Жыл бұрын

    Oh yes my friend! Wish you good luck with your future projects

  • 2 жыл бұрын

    I think this is the best channel on youtube. Thank you for these videos. I can't wait for new videos to be added to the series.

  • @LowLevelLearning

    @LowLevelLearning

    2 жыл бұрын

    More to come!

  • @spaghettimitsoe1705
    @spaghettimitsoe17052 жыл бұрын

    Wow that was really helpful. Was looking for an explanation for a while already

  • @bharadwajn.k9185
    @bharadwajn.k91852 жыл бұрын

    Sir this is one of the best channel which covers all the low level stuff. Thanks a lot. Keep going

  • @LowLevelLearning

    @LowLevelLearning

    2 жыл бұрын

    I’m trying man. Thanks for watching!

  • @zekochyoudesuyo
    @zekochyoudesuyo2 жыл бұрын

    Thanks L^3!!! I like your method of building a solution using a series of short videos. Keep the great work coming.

  • @LowLevelLearning

    @LowLevelLearning

    2 жыл бұрын

    Glad you like them!

  • @arduinoguru7233
    @arduinoguru72332 жыл бұрын

    Perfect man, you are the only person explained to me how making driver for Linux kernel, I read couple PDF explaining this concept, and they were nightmare, please do video about making USB ( _if not possible even simple COM driver_ ) for x86 Linux kerenel

  • @rfctdg9988

    @rfctdg9988

    5 ай бұрын

    Run: stdhw_r -salav 90 -fv -ar_tsui

  • @arduinoguru7233

    @arduinoguru7233

    5 ай бұрын

    @@rfctdg9988 WTF is that?

  • @FloppyDucks
    @FloppyDucks23 күн бұрын

    Love ur stuff low level. Ur da goat

  • @EE_GEEK_369
    @EE_GEEK_3692 жыл бұрын

    You deserve a ton of likes. This has answered many questions I had on adding kernel drivers.

  • @freshnuts864
    @freshnuts8642 жыл бұрын

    Simple, fun, and clear. Thanks for the video

  • @LowLevelLearning

    @LowLevelLearning

    2 жыл бұрын

    You bet!

  • @ThingEngineer
    @ThingEngineer2 жыл бұрын

    Short video, straight to the point, example, perfect.

  • @alexandrohdez3982
    @alexandrohdez3982 Жыл бұрын

    Great video 👏👏👏, I remember coding a easy linux driver late 90s 😂. Thank you

  • @girlswithgames
    @girlswithgames2 жыл бұрын

    i remember messing with this years ago. Video is motivating to dive back in

  • @yashwanthkumar5332
    @yashwanthkumar53322 жыл бұрын

    Great video. Would love to watch more videos from you. Please make more kernel programming videos from beginning to advanced and share materials. Will be waiting for your next video on Kernel programming😅

  • @LowLevelLearning

    @LowLevelLearning

    2 жыл бұрын

    More to come!

  • @johanneswestman935
    @johanneswestman9352 жыл бұрын

    So good. Thanks a bunch!

  • @ScottPlude
    @ScottPlude2 жыл бұрын

    fantastic video. Thanks!

  • @CyReVolt
    @CyReVolt2 жыл бұрын

    How do you feel about trying out 9front on the RPi? Then you need no X11 forwarding, you can just drawterm into it. And you have a much simpler, smaller OS to look at. Would be an interesting next video. :-)

  • @neelabhpaul3024
    @neelabhpaul30242 жыл бұрын

    thanks, really needed some guidance with this

  • @LowLevelLearning

    @LowLevelLearning

    2 жыл бұрын

    Glad it helped!

  • @isaacharris5209
    @isaacharris5209 Жыл бұрын

    Fantastic video

  • @JonathanSteadman2003
    @JonathanSteadman20032 жыл бұрын

    That is awesome.

  • @srivathsa3387
    @srivathsa33872 жыл бұрын

    This driver is exactly same as hello world kernel module, btw good work

  • @CyReVolt
    @CyReVolt2 жыл бұрын

    We just did the same, on a Lichee RV (D1 RISC-V Board), on bare metal, in Rust. It's a lot of fun. :-) Next up is DRAM init. 🥳

  • @johnjoyce
    @johnjoyce2 жыл бұрын

    Much of that explanation of the makefile would make excellent comments in the makefile

  • @ccflan
    @ccflan2 жыл бұрын

    Finally some deep shit on youtube, subscribed

  • @jamesrivettcarnac
    @jamesrivettcarnac2 жыл бұрын

    Perfect timing

  • @comicsansgreenkirby
    @comicsansgreenkirby2 жыл бұрын

    that LV warning is driving the back of my head crazy. >.

  • @piggoy
    @piggoy2 ай бұрын

    Really informative video, love the channel! I'm confused by the structure of the makefile, I would expect there to be a command that invokes a compiler (such as gcc) in the list of instructions. What compiler is actually being used and how is it invoked?

  • @SirDzair
    @SirDzair2 жыл бұрын

    man where was your channel missing all that time !

  • @BenjaminWheeler0510
    @BenjaminWheeler05102 жыл бұрын

    Nice, but I’m not sure if “driver” is the right word for the “tunnel” between user space and kernel space. That’s more of a systemcall than a driver

  • @abhishankpaul

    @abhishankpaul

    17 күн бұрын

    Tis more of a cashier at bank to me

  • @mohammadv8560
    @mohammadv85602 жыл бұрын

    Danke schön 😭

  • @paukpauk4813
    @paukpauk48132 жыл бұрын

    You did something funny and useful. Also, why should those "dispatch" functions be static?

  • @CallousCoder
    @CallousCoder2 жыл бұрын

    So soon we get to see kmalloc and kfree:) Do be a darling, and check the pointer after a kmalloc unlike some developers.

  • @LowLevelLearning

    @LowLevelLearning

    2 жыл бұрын

    Well get there ;D

  • @ethanbradley4282
    @ethanbradley42822 жыл бұрын

    This is the dopest shit ive ever seen

  • @gordon2299
    @gordon22992 жыл бұрын

    I'm really not to comment usually. but, This video is what I really looking for.

  • @vamsikrishna9825
    @vamsikrishna98252 жыл бұрын

    Hello, If RPi has Debian Linux. We can use some inbuilt gpio drivers like (import RPi.GPIO as GPIO) to access GPIO. How is this video different from that? Are both methods works the same or not? Could you please explain me more. I am a beginner for this linux kernal and driver development on RPI. Thanks .

  • @LowLevelLearning
    @LowLevelLearning2 жыл бұрын

    To X-forward a terminal over ssh: do `ssh pi@ -Y`, then on the Raspberry Pi type `lxterminal`. You also don't need to do this if you're okay with coding over the SSH PTY. What's your latest Raspberry Pi project???

  • @CallousCoder

    @CallousCoder

    2 жыл бұрын

    32bit GPIO over I2C

  • @spencersharkey

    @spencersharkey

    2 жыл бұрын

    why do you prefer the xforwarded terminal over just ssh’ing in

  • @HitAndMissLab
    @HitAndMissLab Жыл бұрын

    Do you have a video that explains how to SSH into Raspberry PI and Raspberry Pico and how to setup cables etc. I like your videos but I don't understand where you write code, on your PC or on the Raspberry?

  • @gflorin7761
    @gflorin77613 ай бұрын

    The algorithm finally put me into the right bubble 😎

  • @Jojo-nf1yk
    @Jojo-nf1yk2 жыл бұрын

    Hi, this video seems to be very interesting. To reproduce your example, could I use 32 or 64-bit Raspi-OS ? or probably both ? Thanks for your sharing.

  • @thebeacons4704

    @thebeacons4704

    2 жыл бұрын

    I got this to work on a rpi4 running 64bit raspi-os

  • @scriptles
    @scriptles Жыл бұрын

    Is there a video for how to interact with the driver yet?

  • @Dygear
    @Dygear2 жыл бұрын

    Thanks!

  • @Dygear

    @Dygear

    2 жыл бұрын

    These are gold, thank you so much for making these types of videos!

  • @LowLevelLearning

    @LowLevelLearning

    2 жыл бұрын

    Mark, my man. Thank you so much for this generous gift! I seriously appreciate your support.

  • @Dygear

    @Dygear

    2 жыл бұрын

    @@LowLevelLearning you deserve much more. These videos have been phenomenal. Your videos for assembly on the pico, and the full fat Pi are awesome. It give people like me a blue print for learning that are much easier to follow than reading the manual where I would be entirely lost. Thank you for all of these videos!

  • @vanshajdhar9223
    @vanshajdhar92232 ай бұрын

    This video is 2 years old and I am watching it now 😢

  • @56phil020244
    @56phil0202442 жыл бұрын

    Unrelated question: How does one get a chunk of memory, like alloc, from the heap in ARM64 assembler? I suppose a supervisor call is used, but I can't find anything on the subject. BTW, I'm working on a M1 Mac, so I need the technique for MacOS. I'll be getting a Raspberry PI, soon, so I'd be interested in a linux solution as well. Thanks!

  • @user-ly8ul1tw3q
    @user-ly8ul1tw3q9 ай бұрын

    Hey, I did these steps until 5:25 where I got error that build file does not exist. What should I do if terminal displays that I have the newest version of kernel headers? Would appreciate any feedback!

  • @paulushdk
    @paulushdk2 жыл бұрын

    That's such a good video tho

  • @eshwaranjayaraman1764
    @eshwaranjayaraman17642 жыл бұрын

    I am using pi-zero 2 and I am geting segmentation fault when I unload - sudo rrmmod lll_gpio_driver.

  • @primeix
    @primeix Жыл бұрын

    The piece and part I never understand is how did you know to download and install the kernel headers?

  • @mstrmm35
    @mstrmm352 жыл бұрын

    Your profile background is the same google image I used to create my background

  • @TKR911
    @TKR9112 жыл бұрын

    would you please include a link to configuring Xforwarding and lxterminal ?Tks.

  • @LowLevelLearning

    @LowLevelLearning

    2 жыл бұрын

    I’ll add it in the pinned comment give me a few minutes.

  • @annguyenhoangphu451
    @annguyenhoangphu4512 жыл бұрын

    Hi, thank you for your video. I have follow your instruction but after step "sudo apt install raspberrypi-kernel-headers", it was download but there are no build folder in side the /lib/modules//

  • @vlya1533

    @vlya1533

    2 жыл бұрын

    Probably the installed package doesn't match the loaded kernel, that version must be identical $ uname -r 5.10.60-v7+ $ dpkg -L raspberrypi-kernel-headers | grep '/lib/.*/build' /lib/modules/5.10.60+/build /lib/modules/5.10.60-v7+/build /lib/modules/5.10.60-v7l+/build $ dpkg -S /lib/modules/`uname -r`/build raspberrypi-kernel-headers: /lib/modules/5.10.60-v7+/build

  • @0nlif3nce
    @0nlif3nce Жыл бұрын

    I'm currently working on building complete 64 bit OS for rpi 4 from scratch, i've barely installed debian 10 server with custom bootloader and kernel (rpi-5.19.y). Its part of my RA-ship project and i need to dig deep in device drivers and control over those GPIOs through C and QT... i'm finding hard time for every single step i do bcuz my custom built image lacks a lot of useful plugins (like i cant even do raspi-config since it says it doesnt exist and unable to install that either) that would be wonderful if you guide me a lil bit....

  • @simonedallai48
    @simonedallai482 жыл бұрын

    Good,please make another tutorial! Thanks

  • @LowLevelLearning

    @LowLevelLearning

    2 жыл бұрын

    Sure thing!

  • @eemtech1606
    @eemtech16065 ай бұрын

    If we are writing external device driver, where to put the driver code? Should I just add it to the __init function and call it thorough module_init()? Can you make video explaining how to handle a real external device driver, say, like a mouse driver or a soundcard driver, how the driver handle them and how they interact with other application so we can use them?

  • @tuskiomisham
    @tuskiomisham2 жыл бұрын

    Is direct memory access possible as root?

  • @nelsonamador5412
    @nelsonamador54125 ай бұрын

    I got to build the kernel to have build, sorry for the question, I'm very noob on linux programming

  • @Victoria-fs9jo
    @Victoria-fs9jo2 жыл бұрын

    Can this be done in the Pico?

  • @dougaltolan3017
    @dougaltolan30172 жыл бұрын

    3:14 why use xforwarding and lxterminal instead of the ssh shell?

  • @JosueRodriguez08

    @JosueRodriguez08

    Жыл бұрын

    Why might never know

  • @IgnoreSolutions
    @IgnoreSolutions2 жыл бұрын

    How early can a kernel module run? Ex: I want to show a message on an e-paper display right as Linux is starting.

  • @LowLevelLearning

    @LowLevelLearning

    2 жыл бұрын

    You can provide a list of modules to be loaded on boot, /etc/modules, /etc/modprobe.d, etc. They get loaded from /lib/kernel/

  • @ardawanx
    @ardawanx Жыл бұрын

    Is it possible to build a driver using RustLang, too?

  • @cutiepie0608
    @cutiepie06082 жыл бұрын

    make[1]: *** /lib/modules/5.10.17-v7l+/build: No such file or directory. Stop. i am getting this error, i have installed kernel headers.

  • @hillelbarkan
    @hillelbarkan2 жыл бұрын

    i can not find the next video in your channel

  • @pranathireddy1793
    @pranathireddy17933 ай бұрын

    How to setup the environment

  • @TobiasSample
    @TobiasSample8 ай бұрын

    I thought that you could access the GPIO when using a RPi GPIO library in your chosen language? Or is that library the driver?

  • @warthog618
    @warthog6182 жыл бұрын

    Nice intro into how to compile and load Linux drivers, but I'm curious as to why you would rather write to hardware in a driver than use the Linux GPIO uAPI from userspace, say using libgpiod. Also interested to see how you prevent conflict between your driver and the existing gpiolib/pinctrl drivers present in the Raspberry Pi kernel.

  • @LowLevelLearning

    @LowLevelLearning

    2 жыл бұрын

    There are 100% easier ways to do GPIO on the Raspberry Pi, this is just a good teaching opportunity for Linux Drivers :)

  • @warthog618

    @warthog618

    2 жыл бұрын

    @@LowLevelLearning For sure, but if you are writing Linux drivers then you need to be aware of the the environment you are playing in. You may be sharing the hardware with other parts of the kernel, and the kernel may well provide better ways to do things than accessing the hardware directly.

  • @livesimplyandhumbly
    @livesimplyandhumbly2 жыл бұрын

    My God! I can write a driver. My next project? Write new drivers to replace the buggy nVidia drivers.

  • @StevenCookFX
    @StevenCookFX Жыл бұрын

    For some reason when I type "ls /lib/mod" I get no such directory. You assume that the person on the other end has everything working as you do and I am unable to continue with your tutorial and follow along. :(

  • @kekons23
    @kekons23 Жыл бұрын

    is a driver equivalent to an API?

  • @adarshp6550
    @adarshp65502 жыл бұрын

    Wt if I wanted to get data ( rise and low timing ) from 2 lines (TX detect and rx detect) from my custom lidar but facing issue to get gpio to read both of them... I mean by - Once a sense pulse is given lidar scan 1000 points(ie 1000 of both TX detect and Rx detect signals) TX detect goes high once the laser shoots every single pulse Rx detect goes high on reflection detection

  • @epolpier

    @epolpier

    2 жыл бұрын

    Wouldn't it be wiser to use some dedicated input capture capable timer ?

  • @adarshp6550

    @adarshp6550

    2 жыл бұрын

    @@epolpierwith rpi integration?

  • @epolpier

    @epolpier

    2 жыл бұрын

    @@adarshp6550 I just checked that indeed there is no support for input capture in rpi so you should use a different platform for the use case you would like to implement.

  • @adarshp6550

    @adarshp6550

    2 жыл бұрын

    @@epolpier any suggestions?

  • @epolpier

    @epolpier

    2 жыл бұрын

    @@adarshp6550 stm32mp1 ?

  • @gregoryfenn1462
    @gregoryfenn14622 жыл бұрын

    Why are both “__init func” and “module_init(func)” needed surely one is enough to declare that func is to be run when the driver is installed? (Likewise for __exit and module_exit). It feels like a lot of logical redundancy and boilerplate code.

  • @LowLevelLearning

    @LowLevelLearning

    2 жыл бұрын

    __init tells the linker in what section of the ELF to put that code, where as module_init tells the kernel module loader what function to run first.

  • @gregoryfenn1462

    @gregoryfenn1462

    2 жыл бұрын

    @@LowLevelLearning ah ok cool! I don’t understand linkers very well :(

  • @vrumivruni
    @vrumivruni2 жыл бұрын

    Just curious why we use this over wiringpi c++ raspberry

  • @LowLevelLearning

    @LowLevelLearning

    2 жыл бұрын

    Good question! I’m just using this as an example to talk about embedded driver development.

  • @vrumivruni

    @vrumivruni

    2 жыл бұрын

    @@LowLevelLearning yea. I find it really interesting and cool! Im actually in a computer based systems class, but we arent this deep in it yet. But I am learning a lot from you! Thanks for you content. Been seeing your rust videos too! Here at Oklahoma State University we are trying to make a security key using rust. Something like solokeys

  • @AxelWerner
    @AxelWerner2 жыл бұрын

    Uhm. Help! I don't understand that function definition. For all C I know, a function is defined like this: return_type function_name( parameter list ); now how come there is some __init keyword between return type and function name? What's that exactly? Thanks 🙏

  • @gabiold

    @gabiold

    2 жыл бұрын

    In general, most compilers support a sort of attributes to tell how to compile that function, like where to place it in the binary (can be an absolute location or name of memory segment), which calling convention to use that determines the stack frame handling of the function (this code is in addition to what you wrote inside the function body, like pushing used resisters on stack before executing your code, then restoring it afterwards). For example, (in gcc) there is also an attribute inline keyword, which tells the compiler to actually copy the function code everywhere it is called from. So it won't use call and return, possibly optimize the hell out of the stack frame. Useful for one-line functions which you want as performant as possible. Examine the disassembly of your C code, you can learn a lot from it! To answer the specific __init and __exit keywords, here is a link to a book: www.oreilly.com/library/view/linux-device-drivers/9781785280009/e636c201-5e6f-4ddb-a4b3-9bd72f71b9b0.xhtml

  • @AxelWerner

    @AxelWerner

    2 жыл бұрын

    @@gabiold thanks a lot for this excellent 👌 explanation

  • @fair6685
    @fair66852 жыл бұрын

    nice, now also my black user can access legally to gpi

  • @gcarrillodev
    @gcarrillodev5 ай бұрын

    Note: Capitalize your makefile. I usually just do "makefile" as a name, but apparently the kernel build system is more fussy and requires a "Makefile". Might vary from setup to setup(?)

  • @namename8986
    @namename8986 Жыл бұрын

    shoutout to Johannes 4 gnu/linux ?

  • @n0kodoko143
    @n0kodoko1432 жыл бұрын

    1/3 👍

  • @warplanner8852
    @warplanner8852 Жыл бұрын

    ..good stuff and well taught. But a minuscule grammatical nit pick: it's "..that gets *RUN* in.." not "..that gets *RAN* in..". _Thanks muchly_ for your videos!

  • @FloppyDucks
    @FloppyDucks23 күн бұрын

    I'm a shit programmer and hardley understand anything you talk about...but I try. Why is it a normal program is only has permissions to be executed in user space, but the kernel/driver program you wrote now has privileges to run in kernel space? Is it because the kernel header library you installed? Does that give permissions? Essentially what makes this driver program special to be executed in kernel space? What did I miss?

  • @stephenjohnstone9940
    @stephenjohnstone99402 жыл бұрын

    true, but sure u respect @jeffgerling proving that recompiling kernel, doent have to be durty! ish

  • @galihtanu
    @galihtanu2 жыл бұрын

    Sir my i ask question, i use Ubuntu and get stuck on how to create and compile c code on raspbery Pi Pico? ITS should use rasp Pi OS as environtment? Or can use x86 laptop to create / cross compile it?

  • @TensorWave
    @TensorWave Жыл бұрын

    So all you did was wrote a "hello world" LKM that has nothing to do with Raspberry Pi or GPIO. Title of your video is misleading and clickbait.

  • @lxw5231
    @lxw52314 ай бұрын

    Great tutorial. thank you. after I installed kernel-headers there is no $(uname -r)\build folder created. but I saw $(uname -r)\kernel folder. then I change Makefile. and make, it says pi@raspberrypi:~/lll-gpio-driver $ make make -C /lib/modules/4.19.118-v7l+/kernel M=/home/pi/lll-gpio-driver modules make[1]: Entering directory '/lib/modules/4.19.118-v7l+/kernel' make[1]: *** No rule to make target 'modules'. Stop. make[1]: Leaving directory '/lib/modules/4.19.118-v7l+/kernel' make: *** [Makefile:6: all] Error 2 please let me know what I did wrong. thanks

  • @zrodger2296
    @zrodger22962 жыл бұрын

    Really looking forward to this series. Now I just have to get a Raspberry Pi!

  • @LowLevelLearning

    @LowLevelLearning

    2 жыл бұрын

    Ha! Yes that would be useful. In theory this should work on any embedded platform. Things will obviously change as we add features and get more specific. Thanks for watching!