Chapter 6. Embedded System Development
Chapter 6. Embedded System Development
Set up
kernel=kernel-myconfig.img
Configuring the Kernel
Device Driver (Kernel Module)
• Building Device driver
• Install Device driver (Install module)
• Building Kernel with device driver
Device Driver (Kernel Module)
• Build and install Kernel Module on Ubuntu
• Build and install Kernel Module to control GPIO on
raspberry Pi 4
• Build and install Device Driver on Ubuntu
• Build and install Device Driver to control GPIO on
raspberry Pi 4
Trình điều khiển thiết bị (Device Driver, Kernel
Module)
• Biên dịch và cài đặt Kernel Module trên Ubuntu
• Biên dịch và cài đặt Kernel Module điều khiển
GPIO trên raspberry Pi 4
• Biên dịch và cài đặt Device Driver trên Ubuntu
• Biên dịch và cài đặt Device Driver điều khiển
GPIO trên raspberry Pi 4
Device Driver
Hardware Kernel
User Space
GPIO Space
Raspberry Pi 4 I/O
Device Driver (Kernel Module)
Install package
Sudo apt install raspberrypi-kernel-headers
Device Driver (Kernel Module)
Kernel module
Device driver
Kernel Module
File operation structure
The file_operations structure is defined in linux/fs.h, and holds
pointers to functions defined by the driver that perform various
operations on the device. Each field of the structure corresponds to
the address of some function defined by the driver to handle a
requested operation.
File operation structure
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char *, size_t, loff_t *);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, struct dentry *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*readv) (struct file *, const struct iovec *, unsigned long,
loff_t *);
ssize_t (*writev) (struct file *, const struct iovec *, unsigned long,
loff_t *);
};
Kernel Module - Write my-driver.c
Lab.01.Module
test module
Echo “hello” > /proc/my-driver
Dmeg
Kernel Module - Write my-driver.c
int alloc_chrdev_region ( dev_t * dev,
unsigned baseminor,
unsigned count,
const char * name);
In the kernel versions before 2.6 these two numbers were 8 bit
numbers thus allowing only 256 drivers with each controlling
maximum of 256 devices.
But this was not enough for the modern systems like the big servers
which have a large number of devices connected to them.
Thus from 2.6 these two numbers are combined and stored in one 32
bit number of the data type dev_t. Out of the 32 bits the MSB 12 bits
represent the major number and the LSB 20 bits represent the minor
number
Kernel Module
Install kernel module
Sudo insmod my-driver.ko
Debug
dmesg
Uninstall kernel module
Sudo rmmod my-driver
dmesg
Device Driver (Kernel Module)
Write Makefile
obj-m += test-driver.o
KDIR = /lib/modules/$(shell uname -r)/build
all:
make -C $(KDIR) M=$(shell pwd) modules
clean:
make -C $(KDIR) M=$(shell pwd) clean
Kernel Module
Device Driver (Kernel Module)
List the Installed modules
Lsmod
Install module
sudo insmod test-driver.ko
Check the installation
Lsmod | grep test
put_user store the value val to user-space address address; Type can be one on 8, 16, 32, 64 bit
(the maximum supported type depends on the hardware platform);
get_user analogue to the previous function, only that val will be set to a value identical to the value
at the user-space address given by address;
copy_to_user copies n bytes from the kernel-space, from the address referenced by from in user-
space to the address referenced by to;
copy_from_user copies n bytes from user-space from the address referenced by from in kernel-space
to the address referenced by to.
Device Driver (Kernel Module)
Set GPIO
Device Driver (Kernel Module)
To Find the GIPO Address: 0xFE200000
cat /proc/iomem
Device Driver (Kernel Module)
Next, install the kernel modules onto the SD card
Device Driver (Kernel Module)
Next, install the kernel modules onto the SD card