嵌入式linux简单程序.docx
《嵌入式linux简单程序.docx》由会员分享,可在线阅读,更多相关《嵌入式linux简单程序.docx(10页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、1、demodemo.c#ifndef _KERNEL_#define _KERNEL_#endif#ifndef MODULE#define MODULE#endif#include #include /模块相关#include /内核相关#include /file_operations#include /ssize_t定义文件#include /_init和_exit相关#include #include #include /copy_to_user()和copy_from_user()在此定义#include /*相关宏定义*/#define DEVICE_NAMEdemo/设备名称#
2、define demo_MAJOR 88/主设备号#define demo_MINOR 0/次设备号#define ERROR -1static int MAX_BUF_LEN=1024;/数值的最大值static int WRI_LENGTH=0;/*结构体的定义*/static int demo_major=demo_MAJOR;struct demo_devstruct cdev cdev;char drv_buf1024;struct demo_dev * demo_devp;/*/*demo设备文件打开*/int demo_open(struct inode * inode,stru
3、ct file *filp)filp-private_data=demo_devp;sprintf(demo_devp-drv_buf,device open sucess!n);printk(device open sucess!n);return 0;/*/*demo设备文件关闭*/int demo_release(struct inode * inode,struct file *filp)return 0;/*/*逆序排列缓冲区数据*/static void do_write(char * drv_buf)int i;int len = WRI_LENGTH;char tmp;for(
4、i = 0; i private_data;if(count MAX_BUF_LEN)count = MAX_BUF_LEN;copy_from_user(dev-drv_buf, buffer, count);WRI_LENGTH = count;printk(user write data to drivern);do_write(dev-drv_buf);return count;/*/static ssize_t demo_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)struct demo_dev*
5、dev=filp-private_data;if(count MAX_BUF_LEN)count=MAX_BUF_LEN;copy_to_user(buffer, dev-drv_buf,count);printk(user read data from drivern);return count;/*/static int demo_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)printk(ioctl runingn);switch(cmd)case 1:printk(ru
6、ning command 1 n);break;case 2:printk(runing command 2 n);break;default:printk(error cmd numbern);break;return 0;/*/*demo的模块加载函数*/static const struct file_operations demo_fops=.owner=THIS_MODULE,.read=demo_read,.write=demo_write,.ioctl=demo_ioctl,.open=demo_open,.release=demo_release,;/*/*demo的模块加载函
7、数*/int _init demo_init(void)int result;int err;dev_t devno=MKDEV(demo_MAJOR,demo_MINOR); if(demo_MAJOR) result=register_chrdev_region(devno,1,demo); else result=alloc_chrdev_region(&devno,0,1,demo); demo_major=MAJOR(devno); if(resultcdev,&demo_fops);demo_devp-cdev.owner=THIS_MODULE;err=cdev_add(&dem
8、o_devp-cdev,devno,1);if(err)printk(注册设备失败);unregister_chrdev_region(MKDEV(demo_MAJOR,demo_MINOR),1);return err;printk(demo init seccess !n);return 0;/*/*demo的模块卸载函数*/void _exit demo_exit(void)cdev_del(&demo_devp-cdev);kfree(demo_devp);unregister_chrdev_region(MKDEV(demo_MAJOR,demo_MINOR),1);MODULE_A
9、UTHOR(Liang Baoqiang);MODULE_LICENSE(Dual BSD/GPL);module_init(demo_init);module_exit(demo_exit);Makefileobj-m += demo.o#mod1-y := mod_a.oKVERSION = $(shell uname -r)all: make -C /lib/modules/$(KVERSION)/build M=$(PWD) modulesclean:make -C /lib/modules/$(KVERSION)/build M=$(PWD) cleanrm -f *.o *.ko
10、*.cmdtest_demo.c#include #include #include #include #include void showbuf(char *buf);int MAX_LEN=32;int main()int fd;int i;char buf255;for(i=0; iMAX_LEN; i+)bufi=i;fd=open(/dev/demo,O_RDWR);if(fd 0)printf(#DEMO device open fail#n);return (-1);printf(write %d bytes data to /dev/demo n,MAX_LEN);showbu
11、f(buf);write(fd,buf,MAX_LEN);printf(Read %d bytes data from /dev/demo n,MAX_LEN);read(fd,buf,MAX_LEN);showbuf(buf);ioctl(fd,1,NULL);ioctl(fd,4,NULL);close(fd);return 0;void showbuf(char *buf)int i,j=0;for(i=0;iMAX_LEN;i+)if(i%4 =0)printf(n%4d: ,j+);printf(%4d ,bufi);printf(n*n);2、hello.c #include#in
12、cludeMODULE_LICENSE(Dual BSD/GPL);static int hello_init(void) printk(KERN_ALERT Hello,init the module!); return 0;static void hello_exit(void) printk(KERN_ALERT Goodbye,exit the module!); module_init(hello_init);module_exit(hello_exit);LINUX驱动版本的hello world 终于下决心好好学习LINUX内核和驱动开发了,不怕千万人耻笑,勇敢将自己的学习过程写
13、出来:1、关于目录 /lib/modules/2.6.9-42.ELsmp/build/ 这个是内核源码所在的目录 一般使用这样的命令进入这个目录:cd /lib/modules/$(uname -r)/build/ 这个目录实际上指向了:/usr/src/kernels/2.6.9-42.EL-smp-i6862、编译驱动所使用的makefile 实际上编译驱动的时候是使用预先提供的一个makefile的,位置在:/lib/modules/$(uname -r)/build/Makefile 注意:M是大写的3、网上抄录的Linux驱动Hello world的源码:/ hello.c#inc
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 嵌入式 linux 简单 程序
限制150内