mirror of
				https://e.coding.net/weidongshan/01_all_series_quickstart.git
				synced 2025-11-04 13:05:59 +08:00 
			
		
		
		
	添加APP入门实验班的LED驱动源码
This commit is contained in:
		
										
											Binary file not shown.
										
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
 | 
			
		||||
# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
 | 
			
		||||
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
 | 
			
		||||
# 2.1 ARCH,          比如: export ARCH=arm64
 | 
			
		||||
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
 | 
			
		||||
# 2.3 PATH,          比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin 
 | 
			
		||||
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
 | 
			
		||||
#       请参考各开发板的高级用户使用手册
 | 
			
		||||
 | 
			
		||||
#KERN_DIR =  /home/book/100ask_imx6ull-sdk/Linux-4.9.88 # 板子所用内核源码的目录
 | 
			
		||||
#KERN_DIR =  /home/book/100ask_imx6ull_mini-sdk/Buildroot_2020.02.x/output/build/linux-origin_master
 | 
			
		||||
KERN_DIR =  /home/book/100ask_imx6ull-sdk/Buildroot_2020.02.x/output/build/linux-origin_master
 | 
			
		||||
 | 
			
		||||
all:
 | 
			
		||||
	make -C $(KERN_DIR) M=`pwd` modules 
 | 
			
		||||
	$(CROSS_COMPILE)gcc -o led_test led_test.c
 | 
			
		||||
clean:
 | 
			
		||||
	make -C $(KERN_DIR) M=`pwd` modules clean
 | 
			
		||||
	rm -rf modules.order  led_test
 | 
			
		||||
 | 
			
		||||
# 参考内核源码drivers/char/ipmi/Makefile
 | 
			
		||||
# 要想把a.c, b.c编译成ab.ko, 可以这样指定:
 | 
			
		||||
# ab-y := a.o b.o
 | 
			
		||||
# obj-m += ab.o
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
obj-m += led_drv.o
 | 
			
		||||
 | 
			
		||||
@@ -0,0 +1,161 @@
 | 
			
		||||
#include "asm-generic/errno-base.h"
 | 
			
		||||
#include "asm-generic/gpio.h"
 | 
			
		||||
#include "asm/uaccess.h"
 | 
			
		||||
#include <linux/module.h>
 | 
			
		||||
#include <linux/poll.h>
 | 
			
		||||
 | 
			
		||||
#include <linux/fs.h>
 | 
			
		||||
#include <linux/errno.h>
 | 
			
		||||
#include <linux/miscdevice.h>
 | 
			
		||||
#include <linux/kernel.h>
 | 
			
		||||
#include <linux/major.h>
 | 
			
		||||
#include <linux/mutex.h>
 | 
			
		||||
#include <linux/proc_fs.h>
 | 
			
		||||
#include <linux/seq_file.h>
 | 
			
		||||
#include <linux/stat.h>
 | 
			
		||||
#include <linux/init.h>
 | 
			
		||||
#include <linux/device.h>
 | 
			
		||||
#include <linux/tty.h>
 | 
			
		||||
#include <linux/kmod.h>
 | 
			
		||||
#include <linux/gfp.h>
 | 
			
		||||
#include <linux/gpio/consumer.h>
 | 
			
		||||
#include <linux/platform_device.h>
 | 
			
		||||
#include <linux/of_gpio.h>
 | 
			
		||||
#include <linux/of_irq.h>
 | 
			
		||||
#include <linux/interrupt.h>
 | 
			
		||||
#include <linux/irq.h>
 | 
			
		||||
#include <linux/slab.h>
 | 
			
		||||
#include <linux/fcntl.h>
 | 
			
		||||
#include <linux/timer.h>
 | 
			
		||||
 | 
			
		||||
struct gpio_desc{
 | 
			
		||||
	int gpio;
 | 
			
		||||
	int irq;
 | 
			
		||||
    char *name;
 | 
			
		||||
    int key;
 | 
			
		||||
	struct timer_list key_timer;
 | 
			
		||||
} ;
 | 
			
		||||
 | 
			
		||||
static struct gpio_desc gpios[2] = {
 | 
			
		||||
    {131, 0, "led0", },
 | 
			
		||||
    //{132, 0, "led1", },
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* 主设备号                                                                 */
 | 
			
		||||
static int major = 0;
 | 
			
		||||
static struct class *gpio_class;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* 实现对应的open/read/write等函数,填入file_operations结构体                   */
 | 
			
		||||
static ssize_t gpio_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
 | 
			
		||||
{
 | 
			
		||||
	char tmp_buf[2];
 | 
			
		||||
	int err;
 | 
			
		||||
    int count = sizeof(gpios)/sizeof(gpios[0]);
 | 
			
		||||
 | 
			
		||||
	if (size != 2)
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
 | 
			
		||||
	err = copy_from_user(tmp_buf, buf, 1);
 | 
			
		||||
 | 
			
		||||
	if (tmp_buf[0] >= count)
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
 | 
			
		||||
	tmp_buf[1] = gpio_get_value(gpios[tmp_buf[0]].gpio);
 | 
			
		||||
 | 
			
		||||
	err = copy_to_user(buf, tmp_buf, 2);
 | 
			
		||||
	
 | 
			
		||||
	return 2;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static ssize_t gpio_drv_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
 | 
			
		||||
{
 | 
			
		||||
    unsigned char ker_buf[2];
 | 
			
		||||
    int err;
 | 
			
		||||
 | 
			
		||||
    if (size != 2)
 | 
			
		||||
        return -EINVAL;
 | 
			
		||||
 | 
			
		||||
    err = copy_from_user(ker_buf, buf, size);
 | 
			
		||||
    
 | 
			
		||||
    if (ker_buf[0] >= sizeof(gpios)/sizeof(gpios[0]))
 | 
			
		||||
        return -EINVAL;
 | 
			
		||||
 | 
			
		||||
    gpio_set_value(gpios[ker_buf[0]].gpio, ker_buf[1]);
 | 
			
		||||
    return 2;    
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* 定义自己的file_operations结构体                                              */
 | 
			
		||||
static struct file_operations gpio_key_drv = {
 | 
			
		||||
	.owner	 = THIS_MODULE,
 | 
			
		||||
	.read    = gpio_drv_read,
 | 
			
		||||
	.write   = gpio_drv_write,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* 在入口函数 */
 | 
			
		||||
static int __init gpio_drv_init(void)
 | 
			
		||||
{
 | 
			
		||||
    int err;
 | 
			
		||||
    int i;
 | 
			
		||||
    int count = sizeof(gpios)/sizeof(gpios[0]);
 | 
			
		||||
    
 | 
			
		||||
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
 | 
			
		||||
	
 | 
			
		||||
	for (i = 0; i < count; i++)
 | 
			
		||||
	{		
 | 
			
		||||
		/* set pin as output */
 | 
			
		||||
		err = gpio_request(gpios[i].gpio, gpios[i].name);
 | 
			
		||||
		if (err < 0) {
 | 
			
		||||
			printk("can not request gpio %s %d\n", gpios[i].name, gpios[i].gpio);
 | 
			
		||||
			return -ENODEV;
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		gpio_direction_output(gpios[i].gpio, 1);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* 注册file_operations 	*/
 | 
			
		||||
	major = register_chrdev(0, "100ask_led", &gpio_key_drv);  /* /dev/gpio_desc */
 | 
			
		||||
 | 
			
		||||
	gpio_class = class_create(THIS_MODULE, "100ask_led_class");
 | 
			
		||||
	if (IS_ERR(gpio_class)) {
 | 
			
		||||
		printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
 | 
			
		||||
		unregister_chrdev(major, "100ask_led_class");
 | 
			
		||||
		return PTR_ERR(gpio_class);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	device_create(gpio_class, NULL, MKDEV(major, 0), NULL, "100ask_led"); /* /dev/100ask_gpio */
 | 
			
		||||
	
 | 
			
		||||
	return err;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
 | 
			
		||||
 */
 | 
			
		||||
static void __exit gpio_drv_exit(void)
 | 
			
		||||
{
 | 
			
		||||
    int i;
 | 
			
		||||
    int count = sizeof(gpios)/sizeof(gpios[0]);
 | 
			
		||||
    
 | 
			
		||||
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
 | 
			
		||||
 | 
			
		||||
	device_destroy(gpio_class, MKDEV(major, 0));
 | 
			
		||||
	class_destroy(gpio_class);
 | 
			
		||||
	unregister_chrdev(major, "100ask_led");
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < count; i++)
 | 
			
		||||
	{
 | 
			
		||||
		gpio_free(gpios[i].gpio);		
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* 7. 其他完善:提供设备信息,自动创建设备节点                                     */
 | 
			
		||||
 | 
			
		||||
module_init(gpio_drv_init);
 | 
			
		||||
module_exit(gpio_drv_exit);
 | 
			
		||||
 | 
			
		||||
MODULE_LICENSE("GPL");
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -0,0 +1,74 @@
 | 
			
		||||
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/stat.h>
 | 
			
		||||
#include <fcntl.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <poll.h>
 | 
			
		||||
#include <signal.h>
 | 
			
		||||
 | 
			
		||||
static int fd;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//int led_on(int which);
 | 
			
		||||
//int led_off(int which);
 | 
			
		||||
//int led_status(int which);
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * ./led_test <0|1|2|..>  on 
 | 
			
		||||
 * ./led_test <0|1|2|..>  off
 | 
			
		||||
 * ./led_test <0|1|2|..>
 | 
			
		||||
 */
 | 
			
		||||
int main(int argc, char **argv)
 | 
			
		||||
{
 | 
			
		||||
	int ret;
 | 
			
		||||
	char buf[2];
 | 
			
		||||
 | 
			
		||||
	int i;
 | 
			
		||||
	
 | 
			
		||||
	/* 1. 判断参数 */
 | 
			
		||||
	if (argc < 2) 
 | 
			
		||||
	{
 | 
			
		||||
		printf("Usage: %s <0|1|2|...> [on | off]\n", argv[0]);
 | 
			
		||||
		return -1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	/* 2. 打开文件 */
 | 
			
		||||
	fd = open("/dev/100ask_led", O_RDWR);
 | 
			
		||||
	if (fd == -1)
 | 
			
		||||
	{
 | 
			
		||||
		printf("can not open file /dev/100ask_led\n");
 | 
			
		||||
		return -1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (argc == 3)
 | 
			
		||||
	{
 | 
			
		||||
		/* write */
 | 
			
		||||
		buf[0] = strtol(argv[1], NULL, 0);
 | 
			
		||||
 | 
			
		||||
		if (strcmp(argv[2], "on") == 0)
 | 
			
		||||
			buf[1] = 0;
 | 
			
		||||
		else
 | 
			
		||||
			buf[1] = 1;
 | 
			
		||||
		
 | 
			
		||||
		ret = write(fd, buf, 2);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		buf[0] = strtol(argv[1], NULL, 0);
 | 
			
		||||
		ret = read(fd, buf, 2);
 | 
			
		||||
		if (ret == 2)
 | 
			
		||||
		{
 | 
			
		||||
			printf("led %d status is %s\n", buf[0], buf[1] == 0 ? "on" : "off");
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	close(fd);
 | 
			
		||||
	
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user