Help Raspberry Pi driver development

Thread Starter

King2

Joined Jul 17, 2022
155
Hello,

I'm eager to dive into the world of driver development and want to create a device driver like an LED driver for my Raspberry Pi 3, primarily for learning purposes. I've already downloaded the kernel source code. I understand that basic LEDs typically don't require drivers, but I believe it would be a valuable learning exercise to start with.

I've been searching through various websites and even the Raspberry Pi official forum, but I'm struggling to find comprehensive resources to guide me through this process. It seems that developing drivers for the Raspberry Pi can be quite challenging due to the limited available learning materials.

I'm reaching out to you @nsaspook because I came across one of your posts where you mentioned that you have experience in developing drivers for the Raspberry Pi. I believe you might be the right person to gain knowledge in this area. If you have any useful information, sample examples, or advice to offer, it would be a great help for me.

Thank you.
 

nsaspook

Joined Aug 27, 2009
12,329
Hello,

I'm eager to dive into the world of driver development and want to create a device driver like an LED driver for my Raspberry Pi 3, primarily for learning purposes. I've already downloaded the kernel source code. I understand that basic LEDs typically don't require drivers, but I believe it would be a valuable learning exercise to start with.

I've been searching through various websites and even the Raspberry Pi official forum, but I'm struggling to find comprehensive resources to guide me through this process. It seems that developing drivers for the Raspberry Pi can be quite challenging due to the limited available learning materials.

I'm reaching out to you @nsaspook because I came across one of your posts where you mentioned that you have experience in developing drivers for the Raspberry Pi. I believe you might be the right person to gain knowledge in this area. If you have any useful information, sample examples, or advice to offer, it would be a great help for me.

Thank you.
First you need to study and understand the Linux kernel device driver model. There are device hardware drivers and protocol/platform drivers that interface with each other to make a usable system.
https://docs.kernel.org/driver-api/driver-model/overview.html

Then you need specific information on Raspberry Pi ARM Drivers.

It's a lot of fun (if you like walking on a tightrope while juggling hand-grenades) once you get the hang of it but to really understand what's happening you need a good background in computer OS concepts in general to see 'why' things are done the they are instead of just following a cookie-cutter method of making one.
https://www.geeksforgeeks.org/introduction-of-operating-system-set-1/#
 
Last edited:

Thread Starter

King2

Joined Jul 17, 2022
155
First you need to study and understand the Linux kernel device driver model. There are device hardware drivers and protocol/platform drivers that interface with each other to make a usable system.
Thank you for your response, and I appreciate your help into understanding the Linux kernel device driver model. I agree that it can be quite a complex but rewarding area to delve into.

I've been diligently studying relevant materials and books to build a solid foundation. However, I'm encountering a specific challenge that I hoped you might be able to shed some light on. My main struggle is understanding the process of adding our custom driver to the Pi OS.

For instance, I'm interested in experimenting with driver replacement. Suppose I have a driver's source code, and I'd like to replace the existing driver and load mine. This is where I'm encountering difficulties. Despite my extensive searches, I haven't been able to find clear documentation or resources that walk through this process.

Given your experience, I thought you might have insights into this technique or the general process of adding custom drivers to Pi OS. Any guidance or pointers you could provide would be immensely valuable in helping me grasp this aspect.

Thank you once again for your willingness to help and share your knowledge.
 

nsaspook

Joined Aug 27, 2009
12,329
Could you please share details about the driver you've developed for the Raspberry Pi ? It would be easier to understand the process.

I've visited so many links that confused me too. Additionally, could you explain the role of the device tree (device model) in your project?
I could but I don't think it will help you at this stage. Have you completed a 'Hello World from the Kernel' example?
https://tldp.org/LDP/lkmpg/2.6/html/x121.html
 

Thread Starter

King2

Joined Jul 17, 2022
155
I could but I don't think it will help you at this stage. Have you completed a 'Hello World from the Kernel' example?
https://tldp.org/LDP/lkmpg/2.6/html/x121.html
I've been doing extensive searches, but I haven't come across a any article that explains clearly the process of creating an LED driver for Raspberry Pi 3.

I've managed to create a character driver for Ubuntu successfully. However, I'm currently facing difficulties in creating a simple 'Hello World' or LED driver for the Raspberry Pi due to the lack of available information.
 

nsaspook

Joined Aug 27, 2009
12,329
I've been doing extensive searches, but I haven't come across a any article that explains clearly the process of creating an LED driver for Raspberry Pi 3.

I've managed to create a character driver for Ubuntu successfully. However, I'm currently facing difficulties in creating a simple 'Hello World' or LED driver for the Raspberry Pi due to the lack of available information.
You need to use device-tree to drive a LED on the RPi.
https://raw.githubusercontent.com/raspberrypi/firmware/master/boot/overlays/README

There really is no difference in building a simple 'Hello World' kprint driver for both.

Show your and failures here. I'll see if I can boot up a RPi to follow along.

A RPi driver from a few years ago that's still working on this old machine I just dusted off.
1695406188352.png
1695406216921.png
1695406260659.png
https://github.com/nsaspook/daq_gert

Driver C code: https://raw.githubusercontent.com/nsaspook/daq_gert/master/daq_gert.c

The driver is for this board.
https://www.sparkfun.com/products/retired/11773
 
Last edited:

Thread Starter

King2

Joined Jul 17, 2022
155
Show your work instead of just asking gimme questions.
I created a DTS file named 'gpio-led-overlay.dts' by pasting the code from GitHub that you provided in the link. Then, I compiled the file using the command
Code:
dtc -I dts -O dtb -o gpio-led-overlay.dtb gpio-led-overlay.dts
After compiling, I opened the '/boot/config.txt' file using:
Code:
sudo nano /boot/config.txt
I added the following line to load the overlay:
Code:
dtoverlay=gpio-led,gpio=19,label=heart,trigger=heartbeat
However, when I checked the loaded overlays using

Code:
dtoverlay -l | grep gpio-led
I didn't get any results. It seems like the overlay isn't loading as expected. Any suggestions on how to resolve this?
 

nsaspook

Joined Aug 27, 2009
12,329
I created a DTS file named 'gpio-led-overlay.dts' by pasting the code from GitHub that you provided in the link. Then, I compiled the file using the command
Code:
dtc -I dts -O dtb -o gpio-led-overlay.dtb gpio-led-overlay.dts
After compiling, I opened the '/boot/config.txt' file using:
Code:
sudo nano /boot/config.txt
I added the following line to load the overlay:
Code:
dtoverlay=gpio-led,gpio=19,label=heart,trigger=heartbeat
However, when I checked the loaded overlays using

Code:
dtoverlay -l | grep gpio-led
I didn't get any results. It seems like the overlay isn't loading as expected. Any suggestions on how to resolve this?
After doing that you need to reboot.

You need to be the root user for command line for testing
Did you try to manual load the overlay?
dtoverlay gpio-led label=moo gpio=19

This version works on my system.
/opt/vc/bin/dtoverlay gpio-led label=moo gpio=19

root@raspberrypi:/boot/overlays# /opt/vc/bin/dtoverlay -l|grep gpio-led
0: gpio-led label=moo gpio=19
root@raspberrypi:/boot/overlays#

TEST

root@raspberrypi:/boot/overlays# cat /sys/class/leds/moo/brightness
0
root@raspberrypi:/boot/overlays# echo 1 > /sys/class/leds/moo/brightness
root@raspberrypi:/boot/overlays# cat /sys/class/leds/moo/brightness
255
I see the moo led responding to commands here with the overlay loaded.
 

nsaspook

Joined Aug 27, 2009
12,329
Shut the RPi2 down to connect a LED to gpio 19 to be sure it's working.
1696107338897.png
1696107605360.png
gpio 19 jumper connected to D1 (led) on the gertboard.

1696107366962.png
echo 0 > /sys/class/leds/moo/brightness
1696107441100.png
echo 1 > /sys/class/leds/moo/brightness

With:
root@raspberrypi:~# echo cpu > /sys/class/leds/moo/trigger
root@raspberrypi:~# echo heartbeat > /sys/class/leds/moo/trigger

It blinks.
 
Last edited:

Thread Starter

King2

Joined Jul 17, 2022
155
I see the moo led responding to commands here with the overlay loaded.
It's blinking now that I've connected it to the correct GPIO pin for the LED.

However, I'm still wondering where we've developed the driver since our main goal was to create a driver, but we've only developed the device tree source (DTS) so far. I believe a driver is essentially a program written in the C language. Typically, we can have both the driver and a user application.
 

nsaspook

Joined Aug 27, 2009
12,329
It's blinking now that I've connected it to the correct GPIO pin for the LED.

However, I'm still wondering where we've developed the driver since our main goal was to create a driver, but we've only developed the device tree source (DTS) so far. I believe a driver is essentially a program written in the C language. Typically, we can have both the driver and a user application.
Baby steps grasshopper. We are working from the bottom up. It takes the wheels, transmission, engine and a driver. Device tree source are the wheels so at least the driver can push it before working on the engine/transmission.

Let's take a look at the drivers used with that DTS:
dtoverlay=gpio-led,gpio=19,label=heart,trigger=heartbeat
dtoverlay=gpio-led,gpio=26,label=brain,trigger=cpu

https://github.com/torvalds/linux/blob/master/drivers/leds/leds-gpio.c
https://github.com/raspberrypi/linux/blob/rpi-5.4.y/drivers/leds/trigger/ledtrig-heartbeat.c
https://github.com/raspberrypi/linux/blob/rpi-5.4.y/drivers/leds/trigger/ledtrig-cpu.c
 
Last edited:

Thread Starter

King2

Joined Jul 17, 2022
155
Baby steps grasshopper. We are working from the bottom up. It takes the wheels, transmission, engine and a driver. Device tree source are the wheels so at least the driver can push it before working on the engine/transmission.

Let's take a look at the drivers used with that DTS:
dtoverlay=gpio-led,gpio=19,label=heart,trigger=heartbeat
dtoverlay=gpio-led,gpio=26,label=brain,trigger=cpu

https://github.com/torvalds/linux/blob/master/drivers/leds/leds-gpio.c
https://github.com/raspberrypi/linux/blob/rpi-5.4.y/drivers/leds/trigger/ledtrig-heartbeat.c
https://github.com/raspberrypi/linux/blob/rpi-5.4.y/drivers/leds/trigger/ledtrig-cpu.c
I've gone through the provided code, and it appears that these are kernel driver programs. They seem to be responsible for managing LED triggers
 
Top