mirror of
https://e.coding.net/weidongshan/linux/doc_and_source_for_drivers.git
synced 2025-12-03 05:11:14 +08:00
add 07_GPIO/02,03,04
This commit is contained in:
BIN
IMX6ULL/doc_pic/07_GPIO/02_使用GPIO子系统要掌握的重要概念.docx
Normal file
BIN
IMX6ULL/doc_pic/07_GPIO/02_使用GPIO子系统要掌握的重要概念.docx
Normal file
Binary file not shown.
BIN
IMX6ULL/doc_pic/07_GPIO/02_使用GPIO子系统要掌握的重要概念.tif
Normal file
BIN
IMX6ULL/doc_pic/07_GPIO/02_使用GPIO子系统要掌握的重要概念.tif
Normal file
Binary file not shown.
BIN
IMX6ULL/doc_pic/07_GPIO/03_基于GPIO子系统的LED驱动程序.docx
Normal file
BIN
IMX6ULL/doc_pic/07_GPIO/03_基于GPIO子系统的LED驱动程序.docx
Normal file
Binary file not shown.
BIN
IMX6ULL/doc_pic/07_GPIO/03_基于GPIO子系统的LED驱动程序.tif
Normal file
BIN
IMX6ULL/doc_pic/07_GPIO/03_基于GPIO子系统的LED驱动程序.tif
Normal file
Binary file not shown.
BIN
IMX6ULL/doc_pic/07_GPIO/04_在100ASK_IMX6ULL上机实验.docx
Normal file
BIN
IMX6ULL/doc_pic/07_GPIO/04_在100ASK_IMX6ULL上机实验.docx
Normal file
Binary file not shown.
BIN
IMX6ULL/doc_pic/07_GPIO/04_在100ASK_IMX6ULL上机实验.tif
Normal file
BIN
IMX6ULL/doc_pic/07_GPIO/04_在100ASK_IMX6ULL上机实验.tif
Normal file
Binary file not shown.
29
IMX6ULL/source/07_GPIO/01_led/Makefile
Normal file
29
IMX6ULL/source/07_GPIO/01_led/Makefile
Normal file
@@ -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 = # 板子所用内核源码的目录
|
||||||
|
|
||||||
|
all:
|
||||||
|
make -C $(KERN_DIR) M=`pwd` modules
|
||||||
|
$(CROSS_COMPILE)gcc -o ledtest ledtest.c
|
||||||
|
|
||||||
|
clean:
|
||||||
|
make -C $(KERN_DIR) M=`pwd` modules clean
|
||||||
|
rm -rf modules.order
|
||||||
|
rm -f ledtest
|
||||||
|
|
||||||
|
# 参考内核源码drivers/char/ipmi/Makefile
|
||||||
|
# 要想把a.c, b.c编译成ab.ko, 可以这样指定:
|
||||||
|
# ab-y := a.o b.o
|
||||||
|
# obj-m += ab.o
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
obj-m += leddrv.o
|
||||||
|
|
||||||
166
IMX6ULL/source/07_GPIO/01_led/leddrv.c
Normal file
166
IMX6ULL/source/07_GPIO/01_led/leddrv.c
Normal file
@@ -0,0 +1,166 @@
|
|||||||
|
#include <linux/module.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>
|
||||||
|
|
||||||
|
|
||||||
|
/* 1. 确定主设备号 */
|
||||||
|
static int major = 0;
|
||||||
|
static struct class *led_class;
|
||||||
|
static struct gpio_desc *led_gpio;
|
||||||
|
|
||||||
|
|
||||||
|
/* 3. 实现对应的open/read/write等函数,填入file_operations结构体 */
|
||||||
|
static ssize_t led_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* write(fd, &val, 1); */
|
||||||
|
static ssize_t led_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
char status;
|
||||||
|
//struct inode *inode = file_inode(file);
|
||||||
|
//int minor = iminor(inode);
|
||||||
|
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
err = copy_from_user(&status, buf, 1);
|
||||||
|
|
||||||
|
/* 根据次设备号和status控制LED */
|
||||||
|
gpiod_set_value(led_gpio, status);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int led_drv_open (struct inode *node, struct file *file)
|
||||||
|
{
|
||||||
|
//int minor = iminor(node);
|
||||||
|
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
/* 根据次设备号初始化LED */
|
||||||
|
gpiod_direction_output(led_gpio, 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int led_drv_close (struct inode *node, struct file *file)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 定义自己的file_operations结构体 */
|
||||||
|
static struct file_operations led_drv = {
|
||||||
|
.owner = THIS_MODULE,
|
||||||
|
.open = led_drv_open,
|
||||||
|
.read = led_drv_read,
|
||||||
|
.write = led_drv_write,
|
||||||
|
.release = led_drv_close,
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 4. 从platform_device获得GPIO
|
||||||
|
* 把file_operations结构体告诉内核:注册驱动程序
|
||||||
|
*/
|
||||||
|
static int chip_demo_gpio_probe(struct platform_device *pdev)
|
||||||
|
{
|
||||||
|
//int err;
|
||||||
|
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
|
||||||
|
/* 4.1 设备树中定义有: led-gpios=<...>; */
|
||||||
|
led_gpio = gpiod_get(&pdev->dev, "led", 0);
|
||||||
|
if (IS_ERR(led_gpio)) {
|
||||||
|
dev_err(&pdev->dev, "Failed to get GPIO for led\n");
|
||||||
|
return PTR_ERR(led_gpio);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 4.2 注册file_operations */
|
||||||
|
major = register_chrdev(0, "100ask_led", &led_drv); /* /dev/led */
|
||||||
|
|
||||||
|
led_class = class_create(THIS_MODULE, "100ask_led_class");
|
||||||
|
if (IS_ERR(led_class)) {
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
unregister_chrdev(major, "led");
|
||||||
|
gpiod_put(led_gpio);
|
||||||
|
return PTR_ERR(led_class);
|
||||||
|
}
|
||||||
|
|
||||||
|
device_create(led_class, NULL, MKDEV(major, 0), NULL, "100ask_led%d", 0); /* /dev/100ask_led0 */
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static int chip_demo_gpio_remove(struct platform_device *pdev)
|
||||||
|
{
|
||||||
|
device_destroy(led_class, MKDEV(major, 0));
|
||||||
|
class_destroy(led_class);
|
||||||
|
unregister_chrdev(major, "100ask_led");
|
||||||
|
gpiod_put(led_gpio);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const struct of_device_id ask100_leds[] = {
|
||||||
|
{ .compatible = "100ask,leddrv" },
|
||||||
|
{ },
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 1. 定义platform_driver */
|
||||||
|
static struct platform_driver chip_demo_gpio_driver = {
|
||||||
|
.probe = chip_demo_gpio_probe,
|
||||||
|
.remove = chip_demo_gpio_remove,
|
||||||
|
.driver = {
|
||||||
|
.name = "100ask_led",
|
||||||
|
.of_match_table = ask100_leds,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 2. 在入口函数注册platform_driver */
|
||||||
|
static int __init led_init(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
|
||||||
|
err = platform_driver_register(&chip_demo_gpio_driver);
|
||||||
|
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 3. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
|
||||||
|
* 卸载platform_driver
|
||||||
|
*/
|
||||||
|
static void __exit led_exit(void)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
|
||||||
|
platform_driver_unregister(&chip_demo_gpio_driver);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* 7. 其他完善:提供设备信息,自动创建设备节点 */
|
||||||
|
|
||||||
|
module_init(led_init);
|
||||||
|
module_exit(led_exit);
|
||||||
|
|
||||||
|
MODULE_LICENSE("GPL");
|
||||||
|
|
||||||
|
|
||||||
165
IMX6ULL/source/07_GPIO/01_led/leddrv_未测试的原始版本.c
Normal file
165
IMX6ULL/source/07_GPIO/01_led/leddrv_未测试的原始版本.c
Normal file
@@ -0,0 +1,165 @@
|
|||||||
|
#include <linux/module.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>
|
||||||
|
|
||||||
|
|
||||||
|
/* 1. 确定主设备号 */
|
||||||
|
static int major = 0;
|
||||||
|
static struct class *led_class;
|
||||||
|
static struct gpio_desc *led_gpio;
|
||||||
|
|
||||||
|
|
||||||
|
/* 3. 实现对应的open/read/write等函数,填入file_operations结构体 */
|
||||||
|
static ssize_t led_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* write(fd, &val, 1); */
|
||||||
|
static ssize_t led_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
char status;
|
||||||
|
struct inode *inode = file_inode(file);
|
||||||
|
int minor = iminor(inode);
|
||||||
|
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
err = copy_from_user(&status, buf, 1);
|
||||||
|
|
||||||
|
/* 根据次设备号和status控制LED */
|
||||||
|
gpiod_set_value(led_gpio, status);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int led_drv_open (struct inode *node, struct file *file)
|
||||||
|
{
|
||||||
|
int minor = iminor(node);
|
||||||
|
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
/* 根据次设备号初始化LED */
|
||||||
|
gpiod_direction_output(led_gpio, 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int led_drv_close (struct inode *node, struct file *file)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 定义自己的file_operations结构体 */
|
||||||
|
static struct file_operations led_drv = {
|
||||||
|
.owner = THIS_MODULE,
|
||||||
|
.open = led_drv_open,
|
||||||
|
.read = led_drv_read,
|
||||||
|
.write = led_drv_write,
|
||||||
|
.release = led_drv_close,
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 4. 从platform_device获得GPIO
|
||||||
|
* 把file_operations结构体告诉内核:注册驱动程序
|
||||||
|
*/
|
||||||
|
static int chip_demo_gpio_probe(struct platform_device *pdev)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
|
||||||
|
/* 4.1 设备树中定义有: led-gpios=<...>; */
|
||||||
|
led_gpio = gpiod_get(&pdev->dev, "led", 0);
|
||||||
|
if (IS_ERR(led_gpio)) {
|
||||||
|
dev_err(&pdev->dev, "Failed to get GPIO for led\n");
|
||||||
|
return PTR_ERR(led_gpio);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 4.2 注册file_operations */
|
||||||
|
major = register_chrdev(0, "100ask_led", &led_drv); /* /dev/led */
|
||||||
|
|
||||||
|
led_class = class_create(THIS_MODULE, "100ask_led_class");
|
||||||
|
if (IS_ERR(led_class)) {
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
unregister_chrdev(major, "led");
|
||||||
|
gpiod_free(led_gpio);
|
||||||
|
return PTR_ERR(led_class);
|
||||||
|
}
|
||||||
|
|
||||||
|
device_create(led_class, NULL, MKDEV(major, 0), NULL, "100ask_led%d", 0); /* /dev/100ask_led0 */
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static int chip_demo_gpio_remove(struct platform_device *pdev)
|
||||||
|
{
|
||||||
|
device_destroy(led_class, MKDEV(major, 0));
|
||||||
|
class_destroy(led_class);
|
||||||
|
unregister_chrdev(major, "100ask_led");
|
||||||
|
gpiod_free(led_gpio);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const struct of_device_id ask100_leds[] = {
|
||||||
|
{ .compatible = "100ask,leddrv" },
|
||||||
|
{ },
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 1. 定义platform_driver */
|
||||||
|
static struct platform_driver chip_demo_gpio_driver = {
|
||||||
|
.probe = chip_demo_gpio_probe,
|
||||||
|
.remove = chip_demo_gpio_remove,
|
||||||
|
.driver = {
|
||||||
|
.name = "100ask_led",
|
||||||
|
.of_match_table = ask100_leds,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 2. 在入口函数注册platform_driver */
|
||||||
|
static int __init led_init(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
|
||||||
|
err = platform_driver_register(&chip_demo_gpio_driver);
|
||||||
|
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 3. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
|
||||||
|
* 卸载platform_driver
|
||||||
|
*/
|
||||||
|
static void __exit led_exit(void)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
|
||||||
|
platform_driver_unregister(&chip_demo_gpio_driver);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* 7. 其他完善:提供设备信息,自动创建设备节点 */
|
||||||
|
|
||||||
|
module_init(led_init);
|
||||||
|
module_exit(led_exit);
|
||||||
|
|
||||||
|
MODULE_LICENSE("GPL");
|
||||||
|
|
||||||
|
|
||||||
50
IMX6ULL/source/07_GPIO/01_led/ledtest.c
Normal file
50
IMX6ULL/source/07_GPIO/01_led/ledtest.c
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ./ledtest /dev/100ask_led0 on
|
||||||
|
* ./ledtest /dev/100ask_led0 off
|
||||||
|
*/
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
char status;
|
||||||
|
|
||||||
|
/* 1. 判断参数 */
|
||||||
|
if (argc != 3)
|
||||||
|
{
|
||||||
|
printf("Usage: %s <dev> <on | off>\n", argv[0]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 2. 打开文件 */
|
||||||
|
fd = open(argv[1], O_RDWR);
|
||||||
|
if (fd == -1)
|
||||||
|
{
|
||||||
|
printf("can not open file %s\n", argv[1]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 3. 写文件 */
|
||||||
|
if (0 == strcmp(argv[2], "on"))
|
||||||
|
{
|
||||||
|
status = 1;
|
||||||
|
write(fd, &status, 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
status = 0;
|
||||||
|
write(fd, &status, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -223,6 +223,10 @@ git clone https://e.coding.net/weidongshan/linux/doc_and_source_for_drivers.git
|
|||||||
|
|
||||||
```shell
|
```shell
|
||||||
01_GPIO子系统视频介绍
|
01_GPIO子系统视频介绍
|
||||||
|
02_使用GPIO子系统要掌握的重要概念
|
||||||
|
03_基于GPIO子系统的LED驱动程序
|
||||||
|
04_在100ASK_IMX6ULL上机实验
|
||||||
|
04_在100ASK_STM32MP157上机实验
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
BIN
STM32MP157/doc_pic/07_GPIO/02_使用GPIO子系统要掌握的重要概念.docx
Normal file
BIN
STM32MP157/doc_pic/07_GPIO/02_使用GPIO子系统要掌握的重要概念.docx
Normal file
Binary file not shown.
BIN
STM32MP157/doc_pic/07_GPIO/02_使用GPIO子系统要掌握的重要概念.tif
Normal file
BIN
STM32MP157/doc_pic/07_GPIO/02_使用GPIO子系统要掌握的重要概念.tif
Normal file
Binary file not shown.
BIN
STM32MP157/doc_pic/07_GPIO/03_基于GPIO子系统的LED驱动程序.docx
Normal file
BIN
STM32MP157/doc_pic/07_GPIO/03_基于GPIO子系统的LED驱动程序.docx
Normal file
Binary file not shown.
BIN
STM32MP157/doc_pic/07_GPIO/03_基于GPIO子系统的LED驱动程序.tif
Normal file
BIN
STM32MP157/doc_pic/07_GPIO/03_基于GPIO子系统的LED驱动程序.tif
Normal file
Binary file not shown.
BIN
STM32MP157/doc_pic/07_GPIO/04_在100ASK_STM32MMP157上机实验.docx
Normal file
BIN
STM32MP157/doc_pic/07_GPIO/04_在100ASK_STM32MMP157上机实验.docx
Normal file
Binary file not shown.
BIN
STM32MP157/doc_pic/07_GPIO/04_在100ASK_STM32MMP157上机实验.tif
Normal file
BIN
STM32MP157/doc_pic/07_GPIO/04_在100ASK_STM32MMP157上机实验.tif
Normal file
Binary file not shown.
29
STM32MP157/source/A7/07_GPIO/01_led/Makefile
Normal file
29
STM32MP157/source/A7/07_GPIO/01_led/Makefile
Normal file
@@ -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 = # 板子所用内核源码的目录
|
||||||
|
|
||||||
|
all:
|
||||||
|
make -C $(KERN_DIR) M=`pwd` modules
|
||||||
|
$(CROSS_COMPILE)gcc -o ledtest ledtest.c
|
||||||
|
|
||||||
|
clean:
|
||||||
|
make -C $(KERN_DIR) M=`pwd` modules clean
|
||||||
|
rm -rf modules.order
|
||||||
|
rm -f ledtest
|
||||||
|
|
||||||
|
# 参考内核源码drivers/char/ipmi/Makefile
|
||||||
|
# 要想把a.c, b.c编译成ab.ko, 可以这样指定:
|
||||||
|
# ab-y := a.o b.o
|
||||||
|
# obj-m += ab.o
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
obj-m += leddrv.o
|
||||||
|
|
||||||
161
STM32MP157/source/A7/07_GPIO/01_led/leddrv.c
Normal file
161
STM32MP157/source/A7/07_GPIO/01_led/leddrv.c
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
#include <linux/module.h>
|
||||||
|
#include <linux/platform_device.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/of.h>
|
||||||
|
|
||||||
|
|
||||||
|
/* 1. 确定主设备号 */
|
||||||
|
static int major = 0;
|
||||||
|
static struct class *led_class;
|
||||||
|
static struct gpio_desc *led_gpio;
|
||||||
|
|
||||||
|
|
||||||
|
/* 3. 实现对应的open/read/write等函数,填入file_operations结构体 */
|
||||||
|
static ssize_t led_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* write(fd, &val, 1); */
|
||||||
|
static ssize_t led_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
char status;
|
||||||
|
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
err = copy_from_user(&status, buf, 1);
|
||||||
|
|
||||||
|
/* 根据次设备号和status控制LED */
|
||||||
|
gpiod_set_value(led_gpio, status);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int led_drv_open (struct inode *node, struct file *file)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
/* 根据次设备号初始化LED */
|
||||||
|
gpiod_direction_output(led_gpio, 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int led_drv_close (struct inode *node, struct file *file)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 定义自己的file_operations结构体 */
|
||||||
|
static struct file_operations led_drv = {
|
||||||
|
.owner = THIS_MODULE,
|
||||||
|
.open = led_drv_open,
|
||||||
|
.read = led_drv_read,
|
||||||
|
.write = led_drv_write,
|
||||||
|
.release = led_drv_close,
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 4. 从platform_device获得GPIO
|
||||||
|
* 把file_operations结构体告诉内核:注册驱动程序
|
||||||
|
*/
|
||||||
|
static int chip_demo_gpio_probe(struct platform_device *pdev)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
|
||||||
|
/* 4.1 设备树中定义有: led-gpios=<...>; */
|
||||||
|
led_gpio = gpiod_get(&pdev->dev, "led", 0);
|
||||||
|
if (IS_ERR(led_gpio)) {
|
||||||
|
dev_err(&pdev->dev, "Failed to get GPIO for led\n");
|
||||||
|
return PTR_ERR(led_gpio);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 4.2 注册file_operations */
|
||||||
|
major = register_chrdev(0, "100ask_led", &led_drv); /* /dev/led */
|
||||||
|
|
||||||
|
led_class = class_create(THIS_MODULE, "100ask_led_class");
|
||||||
|
if (IS_ERR(led_class)) {
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
unregister_chrdev(major, "led");
|
||||||
|
gpiod_put(led_gpio);
|
||||||
|
return PTR_ERR(led_class);
|
||||||
|
}
|
||||||
|
|
||||||
|
device_create(led_class, NULL, MKDEV(major, 0), NULL, "100ask_led%d", 0); /* /dev/100ask_led0 */
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static int chip_demo_gpio_remove(struct platform_device *pdev)
|
||||||
|
{
|
||||||
|
device_destroy(led_class, MKDEV(major, 0));
|
||||||
|
class_destroy(led_class);
|
||||||
|
unregister_chrdev(major, "100ask_led");
|
||||||
|
gpiod_put(led_gpio);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const struct of_device_id ask100_leds[] = {
|
||||||
|
{ .compatible = "100ask,leddrv" },
|
||||||
|
{ },
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 1. 定义platform_driver */
|
||||||
|
static struct platform_driver chip_demo_gpio_driver = {
|
||||||
|
.probe = chip_demo_gpio_probe,
|
||||||
|
.remove = chip_demo_gpio_remove,
|
||||||
|
.driver = {
|
||||||
|
.name = "100ask_led",
|
||||||
|
.of_match_table = ask100_leds,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 2. 在入口函数注册platform_driver */
|
||||||
|
static int __init led_init(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
|
||||||
|
err = platform_driver_register(&chip_demo_gpio_driver);
|
||||||
|
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 3. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
|
||||||
|
* 卸载platform_driver
|
||||||
|
*/
|
||||||
|
static void __exit led_exit(void)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
|
||||||
|
platform_driver_unregister(&chip_demo_gpio_driver);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* 7. 其他完善:提供设备信息,自动创建设备节点 */
|
||||||
|
|
||||||
|
module_init(led_init);
|
||||||
|
module_exit(led_exit);
|
||||||
|
|
||||||
|
MODULE_LICENSE("GPL");
|
||||||
|
|
||||||
|
|
||||||
165
STM32MP157/source/A7/07_GPIO/01_led/leddrv_未测试的原始版本.c
Normal file
165
STM32MP157/source/A7/07_GPIO/01_led/leddrv_未测试的原始版本.c
Normal file
@@ -0,0 +1,165 @@
|
|||||||
|
#include <linux/module.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>
|
||||||
|
|
||||||
|
|
||||||
|
/* 1. 确定主设备号 */
|
||||||
|
static int major = 0;
|
||||||
|
static struct class *led_class;
|
||||||
|
static struct gpio_desc *led_gpio;
|
||||||
|
|
||||||
|
|
||||||
|
/* 3. 实现对应的open/read/write等函数,填入file_operations结构体 */
|
||||||
|
static ssize_t led_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* write(fd, &val, 1); */
|
||||||
|
static ssize_t led_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
char status;
|
||||||
|
struct inode *inode = file_inode(file);
|
||||||
|
int minor = iminor(inode);
|
||||||
|
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
err = copy_from_user(&status, buf, 1);
|
||||||
|
|
||||||
|
/* 根据次设备号和status控制LED */
|
||||||
|
gpiod_set_value(led_gpio, status);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int led_drv_open (struct inode *node, struct file *file)
|
||||||
|
{
|
||||||
|
int minor = iminor(node);
|
||||||
|
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
/* 根据次设备号初始化LED */
|
||||||
|
gpiod_direction_output(led_gpio, 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int led_drv_close (struct inode *node, struct file *file)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 定义自己的file_operations结构体 */
|
||||||
|
static struct file_operations led_drv = {
|
||||||
|
.owner = THIS_MODULE,
|
||||||
|
.open = led_drv_open,
|
||||||
|
.read = led_drv_read,
|
||||||
|
.write = led_drv_write,
|
||||||
|
.release = led_drv_close,
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 4. 从platform_device获得GPIO
|
||||||
|
* 把file_operations结构体告诉内核:注册驱动程序
|
||||||
|
*/
|
||||||
|
static int chip_demo_gpio_probe(struct platform_device *pdev)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
|
||||||
|
/* 4.1 设备树中定义有: led-gpios=<...>; */
|
||||||
|
led_gpio = gpiod_get(&pdev->dev, "led", 0);
|
||||||
|
if (IS_ERR(led_gpio)) {
|
||||||
|
dev_err(&pdev->dev, "Failed to get GPIO for led\n");
|
||||||
|
return PTR_ERR(led_gpio);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 4.2 注册file_operations */
|
||||||
|
major = register_chrdev(0, "100ask_led", &led_drv); /* /dev/led */
|
||||||
|
|
||||||
|
led_class = class_create(THIS_MODULE, "100ask_led_class");
|
||||||
|
if (IS_ERR(led_class)) {
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
unregister_chrdev(major, "led");
|
||||||
|
gpiod_free(led_gpio);
|
||||||
|
return PTR_ERR(led_class);
|
||||||
|
}
|
||||||
|
|
||||||
|
device_create(led_class, NULL, MKDEV(major, 0), NULL, "100ask_led%d", 0); /* /dev/100ask_led0 */
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static int chip_demo_gpio_remove(struct platform_device *pdev)
|
||||||
|
{
|
||||||
|
device_destroy(led_class, MKDEV(major, 0));
|
||||||
|
class_destroy(led_class);
|
||||||
|
unregister_chrdev(major, "100ask_led");
|
||||||
|
gpiod_free(led_gpio);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const struct of_device_id ask100_leds[] = {
|
||||||
|
{ .compatible = "100ask,leddrv" },
|
||||||
|
{ },
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 1. 定义platform_driver */
|
||||||
|
static struct platform_driver chip_demo_gpio_driver = {
|
||||||
|
.probe = chip_demo_gpio_probe,
|
||||||
|
.remove = chip_demo_gpio_remove,
|
||||||
|
.driver = {
|
||||||
|
.name = "100ask_led",
|
||||||
|
.of_match_table = ask100_leds,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 2. 在入口函数注册platform_driver */
|
||||||
|
static int __init led_init(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
|
||||||
|
err = platform_driver_register(&chip_demo_gpio_driver);
|
||||||
|
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 3. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
|
||||||
|
* 卸载platform_driver
|
||||||
|
*/
|
||||||
|
static void __exit led_exit(void)
|
||||||
|
{
|
||||||
|
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||||
|
|
||||||
|
platform_driver_unregister(&chip_demo_gpio_driver);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* 7. 其他完善:提供设备信息,自动创建设备节点 */
|
||||||
|
|
||||||
|
module_init(led_init);
|
||||||
|
module_exit(led_exit);
|
||||||
|
|
||||||
|
MODULE_LICENSE("GPL");
|
||||||
|
|
||||||
|
|
||||||
50
STM32MP157/source/A7/07_GPIO/01_led/ledtest.c
Normal file
50
STM32MP157/source/A7/07_GPIO/01_led/ledtest.c
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ./ledtest /dev/100ask_led0 on
|
||||||
|
* ./ledtest /dev/100ask_led0 off
|
||||||
|
*/
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
char status;
|
||||||
|
|
||||||
|
/* 1. 判断参数 */
|
||||||
|
if (argc != 3)
|
||||||
|
{
|
||||||
|
printf("Usage: %s <dev> <on | off>\n", argv[0]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 2. 打开文件 */
|
||||||
|
fd = open(argv[1], O_RDWR);
|
||||||
|
if (fd == -1)
|
||||||
|
{
|
||||||
|
printf("can not open file %s\n", argv[1]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 3. 写文件 */
|
||||||
|
if (0 == strcmp(argv[2], "on"))
|
||||||
|
{
|
||||||
|
status = 1;
|
||||||
|
write(fd, &status, 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
status = 0;
|
||||||
|
write(fd, &status, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user