/rpi-gpio-driver

GPIO Driver Code in C for Raspberry Pi 4 Linux Kernel with Bit Manipulation of Values in Special Registers mapped to GPIO Pins

Primary LanguageCGNU General Public License v2.0GPL-2.0

Raspberry Pi 4 GPIO Access Kernel Driver for Linux

White Minimalist Profile LinkedIn Banner (1)

Introduction

This is a GPIO Driver for Raspberry Pi 4 with Direct Memory Access to GPIO register and turning it ON/OFF. It is a /proc based driver. This is an experimental driver for controlling GPIO pins and is free to modify and distribute under GPL-2 License.

Compiling

Install dependencies before compilation: (following command is for debian, use the respective package manager for your distro)

sudo apt install -y build-essential linux-headers-$(uname -r) kmod

The Makefile provided in the files. Use the command make to build the driver file.

Use make clean for cleaning the project repository with all the files generated with make command.

After compilation, gpio_driver.ko file would be generated, which is the Kernel Object file.

Loading the Driver

To load the driver into the Kernel, use the following command:

sudo insmod gpio_driver.ko

This will load the driver into the Kernel. For ensuring the driver is connected properly, verify it by these two commands:

sudo lsmod 
sudo dmesg

If the driver is loaded, it will be listed by the lsmod command. It is also recommended to check the output of the dmesg command. The dmesg prints the content of the Kernel Buffer which contains logs of messages generated by the drivers loaded into the kernel.

Using the GPIO Driver

This driver is a char category of driver and works with system calls like read() and write(). For out purpose, write() system call is enough to get the command reach to the driver from user space, which can be easily attended by echo command. The source code of echo command the write() system call. It is recommended to use echo with root privilages since your commands would be going into Kernel Space and hence, you require root privilages.

echo "<gpio_pin>,<state>" > /proc/maverick

where gpio_pin is the GPIO number and state is either 1 or 0 (ON and OFF).

Theory: How it's Done

The Raspberry Pi 4 is powered by BCM2711 ARM Processor. The memory address mapped with these range of processor is 0xfe200000. This drivers manipulates the values stored in this memory location and controls the GPIO Pin.

License

This project is licensed under GPL-2.0 License.