You are on page 1of 23

Chng 2

Lp trnh vo ra c bn

Lp trnh nhng ARM-Linux

57

Mc tiu chng 2
Sau khi kt thc chng n{y, sinh vin c th
Nm c nguyn tc lp trnh giao tip v{o ra c
bn trn h iu h{nh Linux nhng
Lp trnh giao tip thit b (ghp ni GPIO) vi
driver ~ c (led, button)
Bit c|ch lp trnh giao tip GPIO m rng da trn
giao din sysfs (gpiolib)

Lp trnh nhng ARM-Linux

58

Ni dung bi hc
2.1. C ch lp trnh giao tip thit b
2.2. Lp trnh iu khin led n
2.3. Lp trnh giao tip nt bm
2.4. Lp trnh giao tip GPIO m rng

Lp trnh nhng ARM-Linux

59

2.1. C ch lp trnh giao tip thit b


Device files, Device number
Kim tra danh s|ch device driver, thit b
C ch giao tip

Lp trnh nhng ARM-Linux

60

M hnh giao tip ng dng thit b


Phn mm ng dng

User Space
Cc hm giao tip chun:

Device files

Device Drivers

open
close
read
write
ioctl

Kernel Space
Phn cng

(Ton quyn truy xut trc tip ti


nguyn phn cng ca h thng)

Lp trnh nhng ARM-Linux

61

Device files, Device number


Device files: ls l /dev
Device file khng phi l{ file thng thng, khng
phi l{ mt vng d liu trn h thng file
Qu| trnh c ghi device file
Giao tip vi device driver
c, ghi phn cng ca thit b

Ph}n loi device files


Character device: thit b phn cng c, ghi mt
chui c|c byte d liu
Block device: thit b phn cng c, ghi mt khi
d liu
Lp trnh nhng ARM-Linux

62

Device files, Device number


Device number: mi thit b c x|c nh bi hai
gi| tr
Major device number: x|c nh thit b n{y s dng
drvier no
Minor device number: ph}n bit gia c|c thit b
kh|c nhau cng s dng chung mt device driver

Lp trnh nhng ARM-Linux

63

Kim tra danh sch thit b


Kim tra danh s|ch c|c thit b
G lnh ls al /dev

Gii thch thng tin


Loi thit b: char device hay block device
Ti khon ngi dng
Tn thit b
Major v minor number
Mount ponint
Lp trnh nhng ARM-Linux

64

Kim tra danh sch thit b


Kim tra danh s|ch c|c nhm thit b
G lnh cat /proc/devices

Lp trnh nhng ARM-Linux

65

C ch lp trnh giao tip thit b


C ch lp trnh
S dng c|c h{m v{o ra file
open
close
read
write

S dng h{m iu khin v{o ra: ioctl

Lp trnh nhng ARM-Linux

66

2.2. Lp trnh iu khin led n


S dng led driver ~ c
4 led n, ghp ni qua GPB5,6,7,8
iu khin led on/off, to hiu ng: nhp nh|y,
chy ui,
Cn s dng h{m tr (delay): sleep, usleep (th
vin sys/time.h)

Lp trnh nhng ARM-Linux

67

M hnh giao tip iu khin led


Phn mm ng dng

Device files

Device Drivers

Phn cng

leds.c

/dev/leds

Hm giao tip:
open
close
ioctl

Mini2440_leds.c

GPIO Port

Lp trnh nhng ARM-Linux

68

Lp trnh iu khin led n


fd=open(/dev/leds,0)
fd: file id
/dev/leds: device file
0: WRITE_ONLY

ioctl(fd, on, led_no)


Ioctl: IO control
iu khin bt/tt led n c s hiu led_no

Driver cho led n:


linux-2.6.32.2/drivers/char/mini2440_leds.c
Lp trnh nhng ARM-Linux

69

M ngun minh ha iu khin led n

Lp trnh nhng ARM-Linux

70

2.3. Lp trnh giao tip nt bm


Giao tip qua driver ~ c

Lp trnh nhng ARM-Linux

71

M hnh giao tip iu khin nt bm


Phn mm ng dng

Device files

Device Drivers

Phn cng

Buttons.c

Hm giao tip:
open
close
read

/dev/buttons

Mini2440_buttons.c

GPIO Port

Lp trnh nhng ARM-Linux

72

Lp trnh ghp ni nt bm
buttons_fd=open(/dev/buttons,0)
buttons_fd: file id
/dev/buttons: device file

read(buttons_fd,current_buttons,sizeof(curren
t_buttons)
c trng th|i c|c nt bm

close(buttons_fd): ng file
M~ ngun driver cho nt bm
linux2.6.32.2/drivers/char/mini2440_buttons.c
Lp trnh nhng ARM-Linux

73

M ngun chng trnh c nt bm

Lp trnh nhng ARM-Linux

74

2.4. Lp trnh giao tip GPIO m rng


2 c|ch s dng giao tip gpio (t Linux user space)
Cch 1: Vit gpio driver (trn khng gian nh}n h
iu h{nh, kernel space), giao tip qua driver n{y.
(V d vi led, button ~ l{m)
Cch 2: giao tip c|c ch}n gpio trc tip t khng
gian ngi dng (user space) da trn API th vin
gpiolib cung cp. Linux cung cp giao din GPIO
sysfs cho php thao t|c vi bt k ch}n GPIO t
userspace.

Lp trnh nhng ARM-Linux

75

Lp trnh giao tip GPIO m rng


Tt c c|c giao din iu khin GPIO thng qua sysfs
nm trong th mc /sys/class/gpio
Kim tra bng lnh: ls /sys/class/gpio

Lp trnh nhng ARM-Linux

76

Lp trnh giao tip GPIO m rng


Giao din n{y cung cp c|c files iu khin sau }y:

Lp trnh nhng ARM-Linux

77

Lp trnh giao tip GPIO m rng


V d minh ha
Cu hnh chn GPF5 (micro2440) output, v xut gi tr 0
ra chn ny
echo 165 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio165/direction
echo 0 > /sys/class/gpio/gpio165/value

Chi tit xem b{i vit:


https://sites.google.com/site/embedded247/ddcour
se/giao-tiep-gpio-tu-userspace-1
Lp trnh nhng ARM-Linux

78

Tho lun

Lp trnh nhng ARM-Linux

79

You might also like