i.MX8 添加一个新的字符设备模块步骤

① 完成驱动程序代码
myled.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

static struct class *myled_class;
static struct class_device	*myled_class_devs;

static int myled_drv_open(struct inode *inode, struct file *file)
{
	printk("[pual] Enter %s - %d -- \n",__func__,__LINE__);
	return 0;
}

static ssize_t myled_drv_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
	printk("[pual] Enter %s - %d -- \n",__func__,__LINE__);
	return 0;
}

static ssize_t myled_drv_write(struct file *file,const char __user *buf,size_t count,loff_t *ppos)
{
	int val;
	printk("[pual] Enter %s - %d -- \n",__func__,__LINE__);
	 //把用户空间的值 copy 给内核空间
       copy_from_user(&val, buf, count); //    copy_to_user();

	if (val == 1)
       {
       	 // 点灯
       	 printk("val is %d -- \n",val);
       }
       else
       {
     	  	 // 灭灯
     	   	 printk("val is %d -- \n",val);
       }

	return 0;
}

static struct file_operations myled_drv_fops = {
		.owner = THIS_MODULE,
		.open = myled_drv_open,
		.read = myled_drv_read,
		.write = myled_drv_write,

};

int major;
static int __init myled_init(void)
{
	major = register_chrdev(0,"myled",&myled_drv_fops);
	
      //创建设备信息,执行后会出现 /sys/class/myled
      myled_class = class_create(THIS_MODULE, "myled");

	//创建设备节点,就是根据上面的设备信息来的
      myled_class_devs = device_create(myled_class,
       NULL, MKDEV(major, 0), NULL, "myled"); /* /dev/myled */
	
	return 0;
}

static void __exit myled_exit(void)
{
	unregister_chrdev(major,"myled");
	
	//删除节点及信息
      device_unregister(myled_class_devs);
      class_destroy(myled_class);
}

module_init(myled_init);
module_exit(myled_exit);

/* 描述驱动程序的一些信息,不是必须的 */
MODULE_AUTHOR("WPI Pual Lin");
MODULE_VERSION("1.0");
MODULE_DESCRIPTION("i.MX8 LED Driver");
MODULE_LICENSE("GPL");

② 将驱动程序添加到 i.MX8 开发板驱动代码目录下:kernel_imx/drivers/leds
③ 在驱动代码目录下,找到 MakefileKconfig 文件,添加代码如下:
Makefile

obj-$(CONFIG_MYLED)                     += myled.o

Kconfig

config MYLED
         tristate "MYLED support"
         depends on LEDS_CLASS
         help
           This option enables support for userspace LEDs. Say 'y' to enable this
           support in kernel. To compile this driver as a module, choose 'm' here:
           the module will be called uleds.

④ 在默认配置文件 arch/arm64/configs/android_defconfig 下,添加代码:

CONFIG_MYLED=m

⑤ 编译 kernel 代码:

cd ~/imx/imx8qmmek/android/imx-p9.0.0_2.1.0-auto-ga/android_build
source build/envsetup.sh
lunch mek_8q-eng
make bootimage -j16

编译完成后会在 android_build/out/target/product/mek_8q/obj/KERNEL_OBJ/drivers/leds 目录下生成 myled.ko 文件

⑥ 完成应用程序代码
myled_test.c

#include   
#include   
#include   
#include   

/* myled_test on 
 * myled_test off 
 */
 
int main(int argc, char **argv)
{
    	int fd;
    	int val = 1;
		
    	fd = open("/dev/myled",O_RDWR);
    	if(fd < 0)
    	{
       	printf("can't open!\n");
   	}

    	if(argc != 2)
    	{
    		printf("enter error \n");
    		return 0;
    	}

    	 if (!strcmp("on", argv[1]))
    	 {
        	// 亮灯
        	val = 1;
        	write(fd, &val, 1);
    	 }
    	 else if (!strcmp("off", argv[1]))
    	 {
        	// 灭灯
        	val = 0;
        	write(fd, &val, 1);
    	 }
    	 else
    	 {
        	printf("enter error on/off \n");
        	return 0;
    	  }

    	//write(fd,&val,4);
    	return 0;
}

⑦ 编译应用程序,执行命令:

arm-linux-gcc -o myled_test myled_test.c -static

⑧ 通过 ADB 命令将 myled.komyled_test 发送到开发板系统中

adb push myled.ko /data/pual/led_test
adb push myled_test /data/pual/led_test

⑨ 开发板系统内修改文件 myled_test 的权限

chmod 777 myled_test 

⑩ 挂载 ko 文件后执行测试程序

insmod myled.ko
./myled_test on

你可能感兴趣的:(imx8,led,设备模块驱动)