当前位置:文档之家› 嵌入式linux简单程序

嵌入式linux简单程序

MODULE_LICENSE("Dual BSD/GPL");
module_init(demo_init);
module_exit(demo_exit);
Makefile
obj-m += demo.o
#mod1-y := mod_a.o
KVERSION = $(shell uname -r)
all:
printk("注册设备失败");
unregister_chrdev_region(MKDEV(demo_MAJOR,demo_MINOR),1);
return err;
}
printk("demo init seccess !\n");
return 0;
}
/*************************************************************************************/
demo_major=MAJOR(devno);
}
if(result<0)
{
return result;
}
demo_devp=kmalloc(sizeof(struct demo_dev),GFP_KERNEL);
if(!demo_devp)
{
printk("空间申请失败\n");
return ERROR;
#include <linux/fs.h>//file_operations
#include <linux/types.h>//ssize_t定义文件
#include <linux/init.h>//__init和__exit相关
#include <linux/errno.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>//copy_to_user()和copy_from_user()在此定义
#include <asm/system.h>
/*相关宏定义*/
#define DEVICE_NAME"demo"//设备名称
#define demo_MAJOR 88//主设备号
static int demo_ioctl(struct inode *inode, struct file *file,
unsigned int cmd, unsigned long arg)
{
printk("ioctl runing\n");
switch(cmd){
case 1:printk("runing command 1 \n");break;
}
printk("start demo init!\n");
cdev_init(&demo_devp->cdev,&demo_fops);
demo_devp->cdev.owner=THIS_MODULE;
err=cdev_add(&demo_devp->cdev,devno,1);
if(err)
{
#define demo_MINOR 0//次设备号
#define ERROR -1
static int MAX_BUF_LEN=1024;//数值的最大值
static int WRI_LENGTH=0;
/*结构体的定义*/
static int demo_major=demo_MAJOR;
struct demo_dev
copy_to_user(buffer, dev->drv_buf,count);
printk("user read data from driver\n");
return count;
}
/*************************************************************************************/
return 0;
}
/*************************************************************************************/
/*demo设备文件关闭*/
int demo_release(struct inode * inode,struct file *filp)
static ssize_t demo_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)
{
struct demo_dev* dev=filp->private_data;
if(count > MAX_BUF_LEN)
count=MAX_BUF_LEN;
case 2:printk("runing command 2 \n");break;
default:
printk("error cmd number\n");break;
}
return 0;
}
/*************************************************************************************/
copy_from_user(dev->drv_buf, buffer, count);
WRI_LENGTH = count;
printk("user write data to driver\n");
do_write(dev->drv_buf);
return count;
}
/*************************************************************************************/
/*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,
{
return 0;
}
/*************************************************************************************/
/*逆序排列缓冲区数据*/
static void do_write(char * drv_buf)
{
int i;
}
2、hello.c
#include<linux/init.h>
#include<linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello,init the module!");
/*demo的模块卸载函数*/
void __exit demo_exit(void)
{
cdev_del(&demo_devp->cdev);
kfree(demo_devp);
unregister_chrdev_region(MKDEV(demo_MAJOR,demo_MINOR),1);
}
MODULE_AUTHOR("Liang Baoqiang");
close(fd);
return 0;
}
void showbuf(char *buf)
{
int i,j=0;
for(i=0;i<MAX_LEN;i++)
{
if(i%4 ==0)
printf("\n%4d: ",j++);
printf("%4d ",buf[i]);
}
printf("\n*****************************************************\n");
1、demo
demo.c
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#include <linux/config.h>
#include <linux/module.h>//模块相关
#include <linux/kernel.h>//内核相关
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
void showbuf(char *buf);
int MAX_LEN=32;
int main()
ar buf[255];
};
/*************************************************************************************/
相关主题