s3c2410下 platform 总线设备和驱动 led_drv.c led_dev.c 和test

/led_drv.c


#include <linux/fs.h>

#include <linux/module.h>

#include <linux/errno.h>

#include <linux/kernel.h>

#include <linux/init.h>

#include <linux/cdev.h>

#include <linux/ioport.h>

#include <linux/pci.h>

#include <asm/uaccess.h>

#include <asm/io.h>

#include <asm/irq.h>

#include <linux/interrupt.h>

#include <asm/mach/irq.h>

#include <asm/arch/regs-gpio.h>

#include <linux/poll.h>

#include <linux/platform_device.h>


static int major = 0;

static volatile unsigned long *gpio_con;
static volatile unsigned long *gpio_dat;
static int pin1;
static int pin2;

static struct class *cls;


int led_open(struct inode *inode, struct file *file)

{
    /* 设置对应的引脚为输出引脚 */

    *gpio_con &= ~(0x3 << (pin1 * 2));
    *gpio_con |= (0x1 << (pin1 * 2));

    *gpio_con &= ~(0x3 << (pin2 * 2));
    *gpio_con |= (0x1 << (pin2 * 2));
    return 0;
}


ssize_t led_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)

{
    char ker_buf[1];
    
    /* 根据buf传入的值点/灭灯 */
    /* buf[0] - 亮/灭 , 0 - 亮, 1 - 灭 */

    
    if (size != 1)
    
    {
        
        return -EINVAL;
    
    }

    
    copy_from_user(ker_buf, buf, 1);
    
    
    if ((ker_buf[0] != 0) && (ker_buf[0] != 1))
        
        return -EINVAL;

    
    if (ker_buf[0])
    {
        // 某个LED
        *gpio_dat |= (0x1<< pin1);
        *gpio_dat |= (0x1<< pin2);
    }
    else
    {
        // 某个LED
        *gpio_dat &= ~(0x1<<pin1);
        *gpio_dat &= ~(0x1<<pin2);
    }
    
    return 1;

}


static struct file_operations led_fops = {
    .owner  = THIS_MODULE,
    .open   = led_open,
    .write  = led_write,
};

static int led_probe(struct platform_device *dev)
{
    struct resource *r;
    
    /* 根据平台设备确定点哪一个LED */
    r = platform_get_resource(dev, IORESOURCE_MEM, 0);
    
    gpio_con = ioremap(r->start, r->end - r->start + 1);
    gpio_dat = gpio_con + 1;

    /*第三个参数表示的是哪类资源的哪一个*/
    r = platform_get_resource(dev, IORESOURCE_IRQ, 0);
    pin1 = r->start;

    r = platform_get_resource(dev, IORESOURCE_IRQ, 1);
    pin2 = r->start;
    
    /* 注册驱动 */
    major = register_chrdev(0, "led", &led_fops);

    /* sysfs  ==> 挂接到/sys */
    cls = class_create(THIS_MODULE, "led_class");
    class_device_create(cls, NULL, MKDEV(major, 0), NULL, "led");

    // mdev会根据/sys下的这些内容创建/dev/led

    return 0;
}

int led_remove(struct platform_device *dev)
{
    unregister_chrdev(major, "led");
    class_device_destroy(cls, MKDEV(major, 0));
    class_destroy(cls);
    
    iounmap(gpio_con);
        
    return 0;
}

static struct platform_driver led_driver = {
    .probe        = led_probe,  /* 平台总线下增加一个平台设备时,调用枚举函数 */
    .remove        = led_remove, /* 平台总线下去掉一个平台设备时,调用remove函数 */
    .driver        = {
        .name    = "myled",    /* 能支持名为"myled"的平台设备 */
        .owner    = THIS_MODULE,
    },

};
        

static int led_drv_init(void)
{
    platform_driver_register(&led_driver);
    return 0;
}

static int led_drv_exit(void)
{
    platform_driver_unregister(&led_driver);
    return 0;
}

module_init(led_drv_init);
module_exit(led_drv_exit);

MODULE_LICENSE("GPL");

//

//led_dev.c


#include <linux/fs.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/ioport.h>
#include <linux/pci.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <linux/interrupt.h>
#include <asm/mach/irq.h>
#include <asm/arch/regs-gpio.h>
#include <linux/poll.h>
#include <linux/platform_device.h>


static struct resource led_resource[] = {
    [0] = {
        .start = 0x56000050,
        .end   = 0x56000057,
        .flags = IORESOURCE_MEM,
    },
    [1] = {
        .start = 4,
        .end   = 5,
        .flags = IORESOURCE_IRQ,
    },
    [2] = {
        .start = 5,
        .end   = 5,
        .flags = IORESOURCE_IRQ,
    },
    
};

void led_release(struct device *dev)
{    

}

struct platform_device led_device = {
    .name          = "myled", /* 使用名为"myled"的平台驱动 */
    .id          = -1,
    .dev = {
        .release = led_release,
    },
    .num_resources      = ARRAY_SIZE(led_resource),
    .resource      = led_resource,
};

int led_dev_init(void)
{
    platform_device_register(&led_device);
    return 0;
}

void led_dev_exit(void)
{
    platform_device_unregister(&led_device);
}

module_init(led_dev_init);
module_exit(led_dev_exit);

MODULE_LICENSE("GPL");

///

//   test.c  


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>


/* led_test <dev> <on|off>
 * led_test /dev/led on
 */

void print_usage(char *str)
{
    printf("Usage:\n");
    printf("%s <dev> <on|off>\n", str);
}
 
int main(int argc, char **argv)
{
    int fd;
    char buf[4];
    int ret;
    int i;

    if (argc != 3 && argc != 2)
    {
        print_usage(argv[0]);
        return -1;
    }

    fd = open(argv[1], O_RDWR);
    if (fd < 0)
    {
        printf("can't open %s\n", argv[1]);
        return -1;
    }

    if (strcmp(argv[2], "on") == 0)
    {
        buf[0] = 0;
    }
    else
    {
        buf[0] = 1;
    }

    ret = write(fd, buf, 1);
    if (ret != 1)
    {
        printf("error!\n");
        return -1;
    }
    
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值