我的第一个驱动程序
来源:互联网 发布:浙大pat有用吗 知乎 编辑:程序博客网 时间:2024/05/05 11:39
arm编译器版本:3.32
内核版本:2.6.28.7
arm号:s3c2440
驱动源码:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/types.h>
#include <linux/device.h>
#include <asm/hardware.h>
#include <asm/regs-gpio.h>
#define DEVICE_NAME "leds"
#define LED_MAJOR 237
dev_t devnm=MKDEV(LED_MAJOR,0);
static struct cdev *leds_cdev;
static struct class *my_class;
MODULE_LICENSE("Dual BSD/GPL");
static int leds_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)
{
switch(cmd)
{
case 0: s3c2410_gpio_setpin(S3C2410_GPF5,cmd);return 0;break;
case 1: s3c2410_gpio_setpin(S3C2410_GPF5,cmd);return 0;break;
default: return -EINVAL;
}
}
static struct file_operations leds_fops={
.owner=THIS_MODULE,
.ioctl=leds_ioctl,
};
static int leds_cdev_init(struct cdev *dev,int index)
{
int err;
dev->owner=THIS_MODULE;
dev->ops=&leds_fops;
cdev_init(dev,&leds_fops);
err=cdev_add(dev,devnm,1);
if(err)
{
printk("cdev err!\n");
return err;
}
my_class=class_create(THIS_MODULE,DEVICE_NAME);
if(IS_ERR(my_class))
{
printk("err class!\n");
return -1;
}
device_create(my_class,NULL,devnm,NULL,"leds");
return 0;
}
static int leds_init(void)
{
int ret=0;
leds_cdev=cdev_alloc();
if(leds_cdev==NULL)
{
printk(" cdev_alloc err!\n");
return -1;
}
ret=register_chrdev(LED_MAJOR,DEVICE_NAME,&leds_fops);
if(ret<0)
{
printk("DEVICE_NAME cat't register number!\n");
return ret;
}
ret=leds_cdev_init(leds_cdev,0);
s3c2410_gpio_cfgpin(S3C2410_GPF5,S3C2410_GPF5_OUTP);
s3c2410_gpio_setpin(S3C2410_GPF5,1);
printk(KERN_ALERT "Hello ,world\n");
return 0;
}
static void leds_exit(void)
{
cdev_del(leds_cdev);
unregister_chrdev(LED_MAJOR,DEVICE_NAME);
device_destroy(my_class,devnm);
class_destroy(my_class);
printk(KERN_ALERT "Goodbye ,cruel world\n");
}
module_init(leds_init);
module_exit(leds_exit);
编译Makefile :
obj-m:=leds.o
KDIR:=/home/share/linux
PWD:=$(shell pwd)
all:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
@rm -rf *.mod.*
@rm -rf .*.cmd
@rm -rf *.o
@rm -rf Module.*
clean:
@rm -rf *.ko
测试程序:
#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
int main(void)
{
int on=0;
int fp;
fp=open("/dev/leds",0);
if(fp<0)
{
printf("open led err!\n");
return -1;
}
printf("start...................\n");
while(1)
{
printf("helo %d\n",on);
ioctl(fp,on,0);
on=!on;
usleep(400000);
}
close(fp);
return 0;
}
- 我的第一个驱动程序
- 我的第一个驱动程序
- 我的第一个驱动程序
- 我的第一个uClinux驱动程序
- 我的第一个Winxp sp2驱动程序(hello world)
- Tiny6410 我的第一个嵌入式linux驱动程序模块
- Tiny6410 我的第一个嵌入式Linux驱动程序模块
- 我的第一个字符设备驱动程序-First_drv
- 我的第一个Linux开发板字符驱动程序
- 驱动程序开发学习之旅(1):我的第一个驱动程序
- 我的第一个
- 我的第一个
- 第二章 第二节 Linux设备驱动程序之--我的第一个程序 Hello World
- <ARM嵌入式开发学习>我的第一个驱动程序-hello world
- 【Linux】我的第一个驱动程序——scull字符设备驱动
- 写第一个驱动程序的总结
- 第一个LINUX下的驱动程序
- 第一个嵌入式Linux的驱动程序
- Eclipse 插件
- linux下pdf与txt互相转换方法
- unresolved external symbol __endthreadex错误解决
- C#中怎么计算两时间相差多少
- Mountain Huangshan Scenic Spot1
- 我的第一个驱动程序
- 如何取得Android手机的当前语言设置
- cglib源码学习交流
- Java实现【快速离散余弦变换FDCT】参考:《图像数字水印的JAVA实现 刘剑鸣 著》
- 复习简单的mysql语句
- 最好的礼物
- datatable使用groupby进行分组统计
- Oracle 11g 启动服务批处理
- 1112