EPC-6708 linux 字符设备驱动 读取小灯

1. 查看arch/arm/plat-mxc/include/Mach/Iomax-mx6dl.h     //imx6dl引脚配置

2. 查看arch/arm/Mach-mx6/Board-m6708dl.h                    //epc-6708使用的imx6dl中引脚配置

3. 查看arch/arm/Mach-mx6/Board-m6708.c                       //m6708_board_init 移除关于led的配置



#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/gpio.h>

#define DEVNAME "hello"
static dev_t dev;
static struct class *hello_class;
static struct cdev *hello_cdev;
#define IOCTL_LED_ON        1                 //LED亮状态
#define IOCTL_LED_OFF        0                //LED灭状态


static int hello_open(struct inode *inode,struct file *flp)
{
printk("hello: open111 ");
gpio_direction_output(95,1);
gpio_set_value(95,0);
printk("hello: end ");
 return 0;
}

static int hello_close(struct inode *inode,struct file *flp)
{
gpio_direction_output(95,~(IOCTL_LED_OFF));
gpio_direction_output(95,~(IOCTL_LED_ON));
  return 0;
}
static struct file_operations hello_fops={
  .owner=THIS_MODULE,
  .open =hello_open,
  .release=hello_close,
 };
 
static int __init hello_init(void)
{
    int error;

    error = alloc_chrdev_region(&dev, 0, 2, "hello");
    if (error)
    {
        printk("hello: alloc_chardev_region failed! ");
        goto out;
    }
    hello_cdev = cdev_alloc();
    if (hello_cdev == NULL)
    {
        printk("hello: alloc cdev failed! ");
        error = -ENOMEM;
        goto out_chrdev;
    }
    hello_cdev->ops = &hello_fops;
    hello_cdev->owner = THIS_MODULE;
    error = cdev_add(hello_cdev, dev, 1);
    if (error)
    {
        printk("hello: cdev_add failed! ");
        goto out_cdev;
    }
    hello_class = class_create(THIS_MODULE, DEVNAME);
    if (IS_ERR(hello_class))
    {
        error = PTR_ERR(hello_class);
        goto out_chrdev;
    }
    device_create(hello_class, NULL, dev, NULL, DEVNAME);
    //memset (hello_buf, 0, sizeof(hello_buf));
    //memcpy(hello_buf, DEFAULT_MSG, sizeof(DEFAULT_MSG));
    printk("hello: Hello World! ");
    return 0;

out_cdev:
    cdev_del(hello_cdev);
out_chrdev:
    unregister_chrdev_region(hello_cdev->dev, 2);
out:
    return error;
}
static void __exit hello_exit(void)
{
    device_destroy(hello_class, dev);
    class_destroy(hello_class);
    unregister_chrdev_region(hello_cdev->dev, 2);
    cdev_del(hello_cdev);
    printk("hello: Goodbye World ");
}


module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("YWW");
MODULE_DESCRIPTION("HELLO_CLASS_AUTO_DEV");

 

 

#include <stdio.h>
#define IOCTL_LED_ON        1                 //LED亮状态
#define IOCTL_LED_OFF        0                //LED灭状态
int main()
{
int arg = 0;
    FILE *fp0 = NULL;
int cmd;
   
    /*打开设备文件*/
    fp0 = fopen("/dev/hello","r+");
    if (fp0 == NULL)
    {
        printf("Open file Error!\n");
        return -1;
    }

close(fp0);
    return 0;   

}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值