我的第一个驱动程序

本文详细介绍了如何使用C语言和Linux内核开发一个LED驱动程序,并提供了完整的代码实现及测试程序,包括初始化、注册字符设备、GPIO配置、驱动操作等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值