0% found this document useful (0 votes)
109 views31 pages

Linux Driver

This document discusses device drivers in Linux embedded systems. It begins with an introduction to device drivers and their functions. It then covers the structure of a driver in Linux, including how to make, install, use, and remove a device driver. Examples are provided on controlling LEDs and buttons to illustrate interacting with hardware through the driver. The key functions of a Linux device driver like open(), close(), read(), write(), and ioctl() are also outlined.

Uploaded by

Trạng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views31 pages

Linux Driver

This document discusses device drivers in Linux embedded systems. It begins with an introduction to device drivers and their functions. It then covers the structure of a driver in Linux, including how to make, install, use, and remove a device driver. Examples are provided on controlling LEDs and buttons to illustrate interacting with hardware through the driver. The key functions of a Linux device driver like open(), close(), read(), write(), and ioctl() are also outlined.

Uploaded by

Trạng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

EMBEDDED SYSTEM

DRIVERS IN LINUX
HCMUTE – Faculty of Mechanical Engineering
Lecturer: PhD. Bui Ha Duc
Outline
• Introduction to Device driver

• Functions of a device driver

• Structure of a Diver in Linux

• How to make, install, use and remove


device driver in Linux?
Device Driver
Embedded system

Driver

HARDWARE
Device Driver
• Device driver is a computer program
that operates or controls a
particular type of device that is
attached to a computer
• Driver provides a software interface
to hardware devices, enabling
operating systems and other
computer programs to access
hardware functions.
Functions of Driver
• Isolate the user program from the complexity of
the hardware device
• E.g. open, copy a file in hard disk

• Provides a consistent user interface to a large


variety of hardware device
Example: LED control
• Raspberry pin 11

GPIO : General Purpose IO. GPIO


can be configured in different ways
and perform different functions :
input, output, analog

GPIO 17

LED on when GPB5 = ?

How to control the LEDs using microcontroller?


Example: LED control
Without driver
• Get information about the microprocessor
• Configure GPIO pins as digital
• Configure GPIO pins as output
• Set logic levels of the output
• Send the code to the microprocessor using programmer
Kernel vs User space
Control LEDs in Linux
Application

Device files

Device driver

Hardware

• Device Name:
/dev/gpio “gpio” command
• /dev directory contains the special device files for all the
devices
10

Control GPIO Using sysfs


• sysfs provide a virtual file system for device access at
/sys/class
• Using sysfs, GPIO can be control in Shell language
• Functions:
• Export the particular GPIO pin for user control.
echo 30 > /sys/class/gpio/export
• Change the GPIO pin direction to in/out
echo "out" > /sys/class/gpio/gpio30/direction
echo “in" > /sys/class/gpio/gpio160/direction
• Change the value
echo 1 > /sys/class/gpio/gpio30/value
echo 0 > /sys/class/gpio/gpio30/value
• Unexport the GPIO pin
echo 30 > /sys/class/gpio/unexport
Device Driver Architecture
• Linux lets you add and remove kernel components at
runtime
• Provide flexibility
• Enhance upgrade capability
• Module can be stored on media other than root
• Linux device driver are broadly classified into two basic
categories:
• Character devices can be thought of as serial streams of
sequential data.
e.g. serial ports and keyboards.
• Block devices are characterized by the capability to read and write
blocks of data to and from random locations on an addressable
medium
e.g. hard drives and USB Flash drives.
Driver location
• Generally, a driver is stored in folder
/lib/modules/<kernel_version>/kernel

• Driver is stored as *.ko file


Minimal Device Driver

Notes:
• No main() function
• No built-in C function
• 2 fundamental function
• module_init()
• module_exit()
• printk : print messeage to kernel log files
Compile a Driver
• A device driver must be compiled against the kernel on
which it will execute
• Method 1: Create a Make file and put it in the same folder
with file hello.c
obj-m := hello1.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules

• Run the Make file


• Load the module
Compile a Driver
• Method 2: Add code to the kernel source tree, and do the
appropriate configuration
• Step 1. Edit the configuration file Kconfig (linux-
2.6.32.2/drivers/char/Kconfig)
Compile a Driver
• Step 2. edit the Makefile in the kernel configuration (linux-
2.6.32.2/drivers/char/Makefile)
Compile a Driver
• Step 3. back to linux-2.6.32.2 root directory, run the
makefile
make modules

Then copy the .ko file to /lib/modules/2.6.32.4-FriendlyARM


folder on the FriendlyARM board
Loading a driver
• modprobe : a utility used to insert a driver into a running
kernel.
modprobe mini2440_hello_module
• Similar to insmod

• modprobe –r : remove a driver


• Similar to rmmod
rmmod mini2440_hello_module

• lsmod : list of driver which is inserted in to the kernel


Practical Device Driver
• Beside module_init and module_exit, we need other
functions to interface the device with the program
• open() : prepare the driver for subsequent operations
after the device driver is loaded into a live kernel

• release() : provided to clean up after operations are


complete

• ioctl() : a special system call for nonstandard


communication with the driver
Device Nodes and mknod
• A device node is a special file type in Linux that
represents a device
• Linux keep device nodes in a directory called /dev.
• A dedicated utility is used to create a device node on a file
• System is called mknod.
mknod /dev/hello1 c 234 0
c means that a char device is to be created
234 major number registered with the kernel
0 minor number, not registered with the kernel
How to use device driver
1. Call open() function

2. Call desired command


Summary
• The minimum infrastructure to load a device driver is only
a few lines of code
• Device drivers configured as loadable modules can be
inserted into and removed from a running kernel after
kernel boot
• Device nodes on your file system provide the glue
between your user space application and the device
driver.
27

Giao tiếp với Devices


• Device files: ls –l /dev

• Kiểm tra danh sách các thiết bị


• Gõ lệnh ls –al /dev
28

Cơ chế lập trình


Sử dụng các hàm vào ra file
• Open
• Close
• Read
• write
Sử dụng hàm điều khiển vào ra: ioctl
29

Giao tiếp nút nhấn


30

Giao tiếp nút nhấn

Gọi driver
buttons_fd = open(“/dev/buttons”,0)
Đọc trạng thái nút nhấn
read(buttons_fd,current_buttons,sizeof(current_buttons)
Đóng driver
close(buttons_fd)
Đóng file driver
linux-2.6.32.2/drivers/char/mini2440_buttons.c
31

Giao tiếp nút nhấn


32

Giao tiếp chân IO


33

Giao tiếp chân IO


• Trước khi sử dụng 1 chân gpio, cần export ra không gian
người dùng. Để export 1 chân gpio, ghi số hiệu (ID
number) của nó vào file /sys/class/gpio/export
echo 160 > /sys/class/gpio/export
• Thiết lập gpio160 là chân output hoặc là chân input
$ echo "out" > /sys/class/gpio/gpio160/direction
$ echo "in" > /sys/class/gpio/gpio160/direction
• Thiết lập giá trị xuất là giá trị logic “0” hay “1”
$ echo 0 > /sys/class/gpio/gpio160/value
$ echo 1 > /sys/class/gpio/gpio160/value
Make your program run at startup
• On Linux startup, it will first run the init scripts in
/etc/init.d/ directory.
add scripts to make it run your program
• Step 1: Write a simple shell script (e.g. appscript.sh)
#!/bin/sh
cd /path_to_program
./program_name
• Step 2: move this file to /etc/init.d/ folder, give
permission with chmod a+x command
• Step 3: Open /etc/init.d/rcS, add this line
/ect/init.d/appscript

You might also like