20200409-02 platform基于设备树配套 led 例程

设备树

/{
	gpioled {
	        compatible = "atkalpha-gpioled";
	        #address-cells = <1>;
	        #size-cells = <1>;
	        pinctrl-names = "default";
	        pinctrl-0 = <&pinctrl_led>;
	        led-gpio = <&gpio4 14 GPIO_ACTIVE_LOW>;
	        status = "okay";
	};

};
&iomuxc {
	pinctrl_led: ledgrp {
	        fsl,pins = <
	                MX6UL_PAD_NAND_CE1_B__GPIO4_IO14        0x40017059
	        >;
	};
};

驱动

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/platform_device.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>


#define LEDDEV_CNT 1 //个数
#define LEDDEV_NAME "dstplatled"
#define LEDOFF 0
#define LEDON  1

struct leddev_dev {
    dev_t devid;    //设备号
    struct cdev cdev;       //cdev
    struct class *class;    //类
    struct device *device;  //设备
    int major;              //主设备号
    struct device_node *nd; //设备节点
    int led0;           //led gpio 编号
};

struct leddev_dev leddev;

void led0_switch(u8 sta)
{
    if (LEDON == sta) {
        gpio_set_value(leddev.led0, 1);
    } else {
        gpio_set_value(leddev.led0, 0);
    }
}

/*!
 * \brief led_open: 打开设备
 * \param inode: 传递给驱动的 inode
 * \param filp: 设备文件,file 结构体中的 private_data 一般需要在 open
 *  的时候将其指向设备结构体
 * \return 0:success 其他: fault
 */
static int led_open(struct inode* inode, struct file* filp)
{
    filp->private_data = &leddev;
    return 0;
}

static ssize_t led_write(struct file* filp, const char __user* buf,
                         size_t cnt, loff_t* offt)
{
    int retvalue = 0;
    unsigned char databuf[2];
    unsigned char ledstat = 0;

    retvalue = copy_from_user(databuf, buf, cnt);
    if (0 > retvalue) {
        printk("kernel write failed ! \n");
        return -EFAULT;
    }

    ledstat = databuf[0];
    if (LEDON == ledstat) {
        led0_switch(LEDON);
    } else {
        led0_switch(LEDOFF);
    }
    return 0;
}

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

static int led_probe(struct platform_device* dev)
{
    printk("led driver and device was match! \n");

    //设置进程号
    if (leddev.major) {
        leddev.devid = MKDEV(leddev.major, 0);
        register_chrdev_region(leddev.devid, LEDDEV_CNT, LEDDEV_NAME);
    } else {
        alloc_chrdev_region(&leddev.devid, 0, LEDDEV_CNT, LEDDEV_NAME);
        leddev.major = MAJOR(leddev.devid);
    }

    //注册设备
    cdev_init(&leddev.cdev, &led_fops);
    cdev_add(&leddev.cdev, leddev.devid, LEDDEV_CNT);

    //创建类
    leddev.class = class_create(THIS_MODULE, LEDDEV_NAME);
    if (IS_ERR(leddev.class)) {
        return PTR_ERR(leddev.class);
    }
    //创建设备
    leddev.device = device_create(leddev.class, NULL, leddev.devid, NULL, LEDDEV_NAME);
    if (IS_ERR(leddev.device)) {
        return PTR_ERR(leddev.device);
    }
    //初始化IO
    leddev.nd = of_find_node_by_path("/gpioled");
    if (NULL == leddev.nd) {
        printk("gpioled node nost find! \n");
        return -EINVAL;
    }
    leddev.led0 = of_get_named_gpio(leddev.nd, "led-gpio", 0);
    if (leddev.led0 < 0) {
        printk("can't get led-gpio \n");
        return -EINVAL;
    }

    gpio_request(leddev.led0, "led0");
    gpio_direction_output(leddev.led0, 1); //默认拉高
    return 0;
}

static int led_remove(struct platform_device *dev)
{
    gpio_set_value(leddev.led0, 0); //拉低

    cdev_del(&leddev.cdev);
    unregister_chrdev_region(leddev.devid, LEDDEV_CNT);
    device_destroy(leddev.class, leddev.devid);
    class_destroy(leddev.class);
    return 0;
}

//匹配表
static const struct of_device_id led_of_match[] = {
    { .compatible = "atkalpha-gpioled" },
    {   }
};
//platform 驱动结构体
static struct platform_driver led_driver = {
    .driver = {
        .name = "imx6ul-led",           //驱动名称,用于设备匹配
        .of_match_table = led_of_match, //设备树匹配表
    },
    .probe = led_probe,
    .remove = led_remove,
};

static int __init led_init(void)
{
    return platform_driver_register(&led_driver);
}

static void __exit led_exit(void)
{
    platform_driver_unregister(&led_driver);
}




module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zzz");

测试程序

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

#define LEDOFF 0
#define LEDON  1

int main(int argc, char* argv[])
{
    int fd, retvalue;
    char* filename;
    unsigned char databuf[1];

    if (argc != 3) {
        printf("Error Usage! \n");
        return -1;
    }

    filename = argv[1];

    fd = open(filename, O_RDWR);
    if (fd < 0) {
        printf("file %s open failed ! \n", argv[1]);
        return -1;
    }

    databuf[0] = atoi(argv[2]);

    retvalue = write(fd, databuf, sizeof(databuf));
    if (retvalue < 0) {
        printf("LED Control Failed ! \n");
        return -1;
    }

    retvalue = close(fd);
    if (retvalue < 0) {
        printf("file %s close failed !\n", argv[1]);
        return -1;
    }
    return 0;
}

Makefile

KERNELDIR := /home/x/workspace/01_linux_kernel/test/imx_4.1.15_2.0.0_ga_rc3/
CURRENT_PATH := $(shell pwd)
obj-m := devicetree_driver.o

build: kernel_modules

kernel_modules:
        $(MAKE) ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- EXTRA_CFLAGS=-fno-pic -C $(KERNELDIR) M=`pwd` modules 

clean:
        $(MAKE) ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -C $(KERNELDIR) M=`pwd` clean


编译

测试App // arm-linux-gnueabihf-gcc driver_test.c -o driver_test
驱动    // make -j32

执行

insmod devicetree_driver.ko
./driver_test /dev/dtsplatled 1
./driver_test /dev/dtsplatled 0

《基于EtherCAT实现控制LED开发例程使用手册》 这份使用手册将详细介绍如何使用基于IGH EtherCAT技术来进行LED控制开发的例程。EtherCAT是一种高性能实时通信协议,经常被用于工业自动化领域。IGH EtherCAT是一个开源的EtherCAT主站实现,提供了丰富的功能和易用性。 本手册首先会介绍LED控制开发的基本原理和流程。然后,将引导用户安装和配置所需的软件和硬件环境,包括使用的开发板和组件。 接下来,手册将详细说明如何使用IGH EtherCAT来设置和配置EtherCAT主站,并将其连接到所需的从站设备。这将包括设置EtherCAT网络和拓扑结构,以及配置从站的参数和功能。 然后,手册将介绍如何编写控制LED的开发例程。用户将学习如何使用IGH EtherCAT提供的API来实现基本的LED控制功能,如开关和亮度调节。这将涉及到编写控制逻辑和使用适当的函数和指令。 接下来,手册将提供示例代码和代码说明,以便用户更好地理解和运用开发例程。用户可以根据自己的需求进行修改和扩展,以实现更复杂的LED控制功能。 最后,手册将包含其他有关LED控制开发的相关资源和参考资料,以帮助用户进一步深入学习和探索。 本手册将以简明易懂的语言和图示进行说明,并尽量避免使用过多的技术术语,以便初学者也能轻松上手。希望这份手册能够帮助用户顺利进行基于IGH EtherCAT的LED控制开发,并取得满意的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值