본문 바로가기
임베디드/KERNEL

test_app

by sj0020 2020. 12. 17.

1. test_app.c 라즈베리파이에서 작업

test_app.c  : 디바이스 드라이버를 사용하는 애플리케이션

# app Makefile
CC = gcc
OBJECT = test_app.o
TARGET = test_app
CFLAGS = -g -Wall

$(TARGET) : $(OBJECT)
	$(CC) -o $(TARGET) $(OBJECT)

clean:
	rm -f $(OBJECT) $(TARGET)

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>

int main(int argc,char *argv[])
{
	int fd;
	char val;
	if(argc < 2 ){
		printf("usage: %s [no] \n",*argv);
		return 1;
	}
	fd = open("/dev/testdev",O_RDWR); //장치를 RW 권한으로 오픈. R:4 W:2 X:1 / test_dev에 구조체  file_operations test_fops= 의  open 과 연결되어있뜸
	if(fd < 0){
		perror("open failed...");
		return 1;
	}
    //시스템콜
	val = atoi(argv[1]);
	write(fd, &val, sizeof(val));
	read(fd, &val, sizeof(val));
	printf("App val: %d\n",val);
	close(fd);
	return 0;
}

2. test_dev.c 우분투에서 작업

#include <linux/module.h>
#include <linux/init.h>
#include <linux/major.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#define MAJOR_NUM 256
static char data;
int test_open(struct inode *inode, struct file *filp)
{
	printk("Dev test_open() call\n");
	return 0;
}
int test_release(struct inode *inode, struct file *filp)
{
	printk("Dev test_close() call\n");
	return 0;
}
int test_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos)
{
	printk("Dev test_write() call\n");
	data = *buf; //data 라는 변수에 저장됨
	return count;
}
int test_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
	printk("Dev test_read() call\n");
	*buf = data +10;
	return count;
}
struct file_operations test_fops=
{
	owner: THIS_MODULE,
	open :test_open,
	release :test_release,
	write :test_write,
	read :test_read
};
static int __init test_init(void)
{
	int result;
	printk("Dev test_init() call\n");
	result=register_chrdev(MAJOR_NUM, "testdev",&test_fops);
	return 0;
}
static void __exit test_exit(void)
{
	printk("Dev test_exit() call\n");
	unregister_chrdev(MAJOR_NUM, "testdev");
}
module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");

 

 

 

#linux kernel source
KDIR = /home/sss/linux
obj-m := test_dev.o

default:
	$(MAKE) ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -C $(KDIR) M=$$PWD modules
	
clean:
	$(MAKE) -C $(KDIR) M=$$PWD clean

 

 

 

pi@raspberrypi:~/work/test_dev $ ls -l /dev/testdev
crw-r--r-- 1 root root 256, 0 Dec 18 09:58 /dev/testdev
pi@raspberrypi:~/work/test_dev $ cat /proc/devices
Character devices:
  1 mem
  4 /dev/vc/0
  4 tty
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  5 ttyprintk
  7 vcs
 10 misc
 13 input
 14 sound
 29 fb
 81 video4linux
 89 i2c
116 alsa
128 ptm
136 pts
153 spi
162 raw
180 usb
189 usb_device
204 ttyAMA
216 rfcomm
226 drm
235 cec
236 media
237 rpivid-vp9mem
238 rpivid-h264mem
239 rpivid-intcmem
240 rpivid-hevcmem
241 uio
242 vcsm
243 vchiq
244 hidraw
245 rpmb
246 bcm2835-gpiomem
247 vcio
248 vc-mem
249 bsg
250 watchdog
251 BaseRemoteCtl
252 rtc
253 dma_heap
254 gpiochip

Block devices:
  1 ramdisk
  7 loop
  8 sd
 65 sd
 66 sd
 67 sd
 68 sd
 69 sd
 70 sd
 71 sd
128 sd
129 sd
130 sd
131 sd
132 sd
133 sd
134 sd
135 sd
179 mmc
259 blkext
pi@raspberrypi:~/work/test_dev $ sudo insmod test_dev.ko
pi@raspberrypi:~/work/test_dev $ lsmod
Module                  Size  Used by
test_dev               16384  0
cmac                   16384  1
rfcomm                 49152  4
bnep                   20480  2
hci_uart               40960  1
btbcm                  16384  1 hci_uart
bluetooth             372736  29 hci_uart,bnep,btbcm,rfcomm
ecdh_generic           16384  2 bluetooth
ecc                    40960  1 ecdh_generic
8021q                  32768  0
garp                   16384  1 8021q
stp                    16384  1 garp
llc                    16384  2 garp,stp
spidev                 20480  0
brcmfmac              319488  0
brcmutil               20480  1 brcmfmac
sha256_generic         16384  0
libsha256              20480  1 sha256_generic
vc4                   241664  0
cec                    49152  1 vc4
cfg80211              692224  1 brcmfmac
v3d                    69632  0
drm_kms_helper        184320  2 vc4
gpu_sched              36864  1 v3d
rfkill                 28672  6 bluetooth,cfg80211
bcm2835_codec          36864  0
drm                   466944  5 v3d,vc4,gpu_sched,drm_kms_helper
v4l2_mem2mem           32768  1 bcm2835_codec
raspberrypi_hwmon      16384  0
bcm2835_v4l2           49152  0
bcm2835_isp            32768  0
bcm2835_mmal_vchiq     28672  3 bcm2835_isp,bcm2835_codec,bcm2835_v4l2
videobuf2_dma_contig    20480  2 bcm2835_isp,bcm2835_codec
videobuf2_vmalloc      16384  1 bcm2835_v4l2
videobuf2_memops       16384  2 videobuf2_dma_contig,videobuf2_vmalloc
drm_panel_orientation_quirks    16384  1 drm
videobuf2_v4l2         28672  4 bcm2835_isp,bcm2835_codec,bcm2835_v4l2,v4l2_mem2mem
videobuf2_common       57344  5 bcm2835_isp,bcm2835_codec,bcm2835_v4l2,v4l2_mem2mem,videobuf2_v4l2
spi_bcm2835            24576  0
videodev              237568  6 bcm2835_isp,bcm2835_codec,videobuf2_common,bcm2835_v4l2,v4l2_mem2mem,videobuf2_v4l2
snd_soc_core          200704  1 vc4
mc                     40960  6 bcm2835_isp,bcm2835_codec,videobuf2_common,videodev,v4l2_mem2mem,videobuf2_v4l2
snd_bcm2835            28672  1
snd_compress           20480  1 snd_soc_core
snd_pcm_dmaengine      16384  1 snd_soc_core
vc_sm_cma              32768  2 bcm2835_isp,bcm2835_mmal_vchiq
snd_pcm                98304  4 vc4,snd_pcm_dmaengine,snd_bcm2835,snd_soc_core
snd_timer              32768  1 snd_pcm
rpivid_mem             16384  0
snd                    73728  7 snd_compress,snd_timer,snd_bcm2835,snd_soc_core,snd_pcm
syscopyarea            16384  1 drm_kms_helper
sysfillrect            16384  1 drm_kms_helper
sysimgblt              16384  1 drm_kms_helper
fb_sys_fops            16384  1 drm_kms_helper
uio_pdrv_genirq        16384  0
uio                    20480  1 uio_pdrv_genirq
i2c_dev                20480  0
ip_tables              28672  0
x_tables               32768  1 ip_tables
ipv6                  466944  32

pi@raspberrypi:~/work/test_dev $ cat /proc/devices
Character devices:
  1 mem
  4 /dev/vc/0
  4 tty
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  5 ttyprintk
  7 vcs
 10 misc
 13 input
 14 sound
 29 fb
 81 video4linux
 89 i2c
116 alsa
128 ptm
136 pts
153 spi
162 raw
180 usb
189 usb_device
204 ttyAMA
216 rfcomm
226 drm
235 cec
236 media
237 rpivid-vp9mem
238 rpivid-h264mem
239 rpivid-intcmem
240 rpivid-hevcmem
241 uio
242 vcsm
243 vchiq
244 hidraw
245 rpmb
246 bcm2835-gpiomem
247 vcio
248 vc-mem
249 bsg
250 watchdog
251 BaseRemoteCtl
252 rtc
253 dma_heap
254 gpiochip
256 testdev

Block devices:
  1 ramdisk
  7 loop
  8 sd
 65 sd
 66 sd
 67 sd
 68 sd
 69 sd
 70 sd
 71 sd
128 sd
129 sd
130 sd
131 sd
132 sd
133 sd
134 sd
135 sd
179 mmc
259 blkext
pi@raspberrypi:~/work/test_dev $