Raspberry Pi 2에서 character device driver 예제 테스트Raspberry Pi/Raspberry Pi 활용2015. 10. 17. 22:51
Table of Contents
반응형
simple_dev.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/cdev.h>
- #include <linux/device.h>
- #include <linux/fs.h>
- #include <linux/slab.h>
- #include <asm/uaccess.h>
- dev_t id;
- struct cdev cdev;
- struct class *class;
- struct device *dev;
- char temp[100];
- #define DEVICE_NAME "simple"
- int simple_open (struct inode *inode, struct file *filp)
- {
- printk( "open\n" );
- memset( temp, 0, 0 );
- return 0;
- }
- int simple_close (struct inode *inode, struct file *filp)
- {
- printk( "close\n" );
- return 0;
- }
- ssize_t simple_read(struct file *filp, char *buf, size_t size, loff_t *offset)
- {
- printk( "simple_read\n" );
- printk( "DEV : write something\n" );
- printk( "%s %dbytes\n", temp, strlen(temp) );
- int ret = copy_to_user( buf, temp, strlen(temp)+1 );
- return strlen(temp);
- }
- ssize_t simple_write (struct file *filp, const char *buf, size_t size, loff_t *offset)
- {
- printk( "simple_write\n" );
- printk( "DEV : read something\n");
- int ret = copy_from_user( temp, buf, size );
- printk( "%s %dbytes\n", temp, size );
- return size;
- }
- long simple_ioctl ( struct file *filp, unsigned int cmd, unsigned long arg)
- {
- printk( "ioctl\n" );
- return 0;
- }
- struct file_operations simple_fops =
- {
- .owner = THIS_MODULE,
- .read = simple_read,
- .write = simple_write,
- .unlocked_ioctl = simple_ioctl,
- .open = simple_open,
- .release = simple_close,
- };
- int simple_init(void)
- {
- int ret;
- ret = alloc_chrdev_region( &id, 0, 1, DEVICE_NAME );
- if ( ret ){
- printk( "alloc_chrdev_region error %d\n", ret );
- return ret;
- }
- cdev_init( &cdev, &simple_fops );
- cdev.owner = THIS_MODULE;
- ret = cdev_add( &cdev, id, 1 );
- if (ret){
- printk( "cdev_add error %d\n", ret );
- unregister_chrdev_region( id, 1 );
- return ret;
- }
- class = class_create( THIS_MODULE, DEVICE_NAME );
- if ( IS_ERR(class)){
- ret = PTR_ERR( class );
- printk( "class_create error %d\n", ret );
- cdev_del( &cdev );
- unregister_chrdev_region( id, 1 );
- return ret;
- }
- dev = device_create( class, NULL, id, NULL, DEVICE_NAME );
- if ( IS_ERR(dev) ){
- ret = PTR_ERR(dev);
- printk( "device_create error %d\n", ret );
- class_destroy(class);
- cdev_del( &cdev );
- unregister_chrdev_region( id, 1 );
- return ret;
- }
- return 0;
- }
- void simple_exit(void)
- {
- device_destroy(class, id );
- class_destroy(class);
- cdev_del( &cdev );
- unregister_chrdev_region( id, 1 );
- }
- module_init(simple_init);
- module_exit(simple_exit);
- MODULE_LICENSE("Dual BSD/GPL");
Makefile
- obj-m+=simple_dev.o
- ARCH :=arm
- PWD := $(shell pwd)
- all:
- make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) ARCH=$(ARCH) modules
- clean:
- make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) ARCH=$(ARCH) clean
simpleApp.c
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/ioctl.h>
- #include <fcntl.h>
- #include <unistd.h>
- #define DEVICE_FILENAME "/dev/simple"
- int main()
- {
- int dev, ret;
- char buf[100], buf2[100];
- memset( buf2, 0, 0 );
- strcpy(buf, "life is good" );
- int len = strlen( buf );
- printf( "device file open\n");
- dev = open( DEVICE_FILENAME, O_RDWR|O_NDELAY );
- if( dev >= 0 )
- {
- printf( "App : write something\n");
- ret = write(dev, buf, len );
- printf( "%s %dbytes\n", buf, ret );
- printf( "App : read something\n");
- ret = read(dev, buf2, 100 );
- printf( "%s %dbytes\n", buf2, ret );
- printf( "ioctl function call\n");
- ret = ioctl(dev, 0x100, 30 );
- printf( "ret = %d\n", ret );
- printf( "device file close\n");
- ret = close(dev);
- printf( "ret = %d\n", ret );
- }
- return 0;
- }
반응형
'Raspberry Pi > Raspberry Pi 활용' 카테고리의 다른 글
python으로 raspberry pi 2 제어 - 2. 버튼 (0) | 2015.11.11 |
---|---|
python으로 raspberry pi 2 제어 - 1. LED (0) | 2015.11.10 |
Raspberry Pi 2에서 Linux Kernel Module 작성하기 (0) | 2015.10.17 |
Raspberry Pi 2에서 NFS 사용하여 리눅스 컴퓨터의 디렉토리 공유하기 (0) | 2015.09.28 |
raspberry pi 2에서 bcm2835 데이터 시트를 사용할 수 있다는데... (0) | 2015.09.15 |