设备树根目录添加
led: gpioled{
device_type = "gpioled";
compatible = "gpio-led";
gpios = <&pio PD 18 GPIO_ACTIVE_HIGH>;
status = "okay";
};
linxu系统操作
编译 Lina :
- 首先执行 source build/envsetup.sh 提示 Setup env done! Please run lunch next.
- 输入 lunch d1-h_nezha-tina
- make -j32
- 执行 pack打包
MQ_pro操作
1.下载至SD卡启动
2.通过ls /proc/device-tree/ 命令查看是否有 “gpioled”节点
gpioled.c 如下 该文件直接复制 正点原子历程
#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>
#define GPIOLED_CNT 1
#define GPIOLED_NAME "gpioled"
#define LEDOFF 0
#define LEDON 1
struct gpioled_dev{
dev_t devid;
struct cdev cdev;
struct class *class;
struct device *device;
int major;
int minor;
struct device_node *nd;
int led_gpio;
};
struct gpioled_dev gpioled;
static int led_open(struct inode *inode, struct file *filp)
{
filp->private_data = &gpioled;
return 0;
}
static ssize_t led_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
return 0;
}
static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
int retvalue;
unsigned char databuf[1];
unsigned char ledstat;
struct gpioled_dev *dev = filp->private_data;
retvalue = copy_from_user(databuf, buf, cnt);
if(retvalue < 0) {
printk("kernel write failed!\r\n");
return -EFAULT;
}
ledstat = databuf[0];
if(ledstat == LEDON) {
gpio_set_value(dev->led_gpio, 0);
} else if(ledstat == LEDOFF) {
gpio_set_value(dev->led_gpio, 1);
}
return 0;
}
static int led_release(struct inode *inode, struct file *filp)
{
return 0;
}
static struct file_operations gpioled_fops = {
.owner = THIS_MODULE,
.open = led_open,
.read = led_read,
.write = led_write,
.release = led_release,
};
static int __init led_init(void)
{
int ret = 0;
ret = ret;
gpioled.nd = of_find_node_by_path("/gpioled");
if(gpioled.nd == NULL) {
printk("gpioled node not find!\r\n");
return -EINVAL;
} else {
printk("gpioled node find!\r\n");
}
gpioled.led_gpio = of_get_named_gpio(gpioled.nd, "gpios", 0);
if(gpioled.led_gpio < 0) {
printk("can't get led-gpio");
return -EINVAL;
}
printk("led-gpio num = %d\r\n", gpioled.led_gpio);
ret = gpio_direction_output(gpioled.led_gpio, 0);
if(ret < 0) {
printk("can't set gpio!\r\n");
}
if (gpioled.major) {
gpioled.devid = MKDEV(gpioled.major, 0);
register_chrdev_region(gpioled.devid, GPIOLED_CNT, GPIOLED_NAME);
} else {
alloc_chrdev_region(&gpioled.devid, 0, GPIOLED_CNT, GPIOLED_NAME);
gpioled.major = MAJOR(gpioled.devid);
gpioled.minor = MINOR(gpioled.devid);
}
printk("gpioled major=%d,minor=%d\r\n",gpioled.major, gpioled.minor);
gpioled.cdev.owner = THIS_MODULE;
cdev_init(&gpioled.cdev, &gpioled_fops);
cdev_add(&gpioled.cdev, gpioled.devid, GPIOLED_CNT);
gpioled.class = class_create(THIS_MODULE, GPIOLED_NAME);
if (IS_ERR(gpioled.class)) {
return PTR_ERR(gpioled.class);
}
gpioled.device = device_create(gpioled.class, NULL, gpioled.devid, NULL, GPIOLED_NAME);
if (IS_ERR(gpioled.device)) {
return PTR_ERR(gpioled.device);
}
return 0;
}
static void __exit led_exit(void)
{
cdev_del(&gpioled.cdev);
unregister_chrdev_region(gpioled.devid, GPIOLED_CNT);
device_destroy(gpioled.class, gpioled.devid);
class_destroy(gpioled.class);
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zuozhongkai");
Makefile 文件编写 如下:
KERNELDIR := /home/tina-d1-h/lichee/linux-5.4/
CURRENT_PATH := $(shell pwd)
CFLAGS= ARCH=riscv CROSS_COMPILE=/home/tina-d1-h/prebuilt/gcc/linux-x86/riscv/toolchain-thead-glibc/riscv64-glibc-gcc-thead_20200702/bin/riscv64-unknown-linux-gnu-
obj-m := gpioled.o
build: kernel_modules
kernel_modules:
$(MAKE) $(CFLAGS) -C $(KERNELDIR) M=$(CURRENT_PATH) modules
clean:
$(MAKE) $(CFLAGS) -C $(KERNELDIR) M=$(CURRENT_PATH) cleanmake
KERNELDIR CFLAGS 路径需要根据自己的路径设置
ledAPP.c 正点原子原封不动的例程
#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!\r\n");
return -1;
}
filename = argv[1];
fd = open(filename, O_RDWR);
if(fd < 0){
printf("file %s open failed!\r\n", argv[1]);
return -1;
}
databuf[0] = atoi(argv[2]);
retvalue = write(fd, databuf, sizeof(databuf));
if(retvalue < 0){
printf("LED Control Failed!\r\n");
close(fd);
return -1;
}
retvalue = close(fd);
if(retvalue < 0){
printf("file %s close failed!\r\n", argv[1]);
return -1;
}
return 0;
}
linux系统操作:
1.通过make 编译出gpioled.ko文件
2.通过 /home/tina-d1-h/prebuilt/gcc/linux-x86/riscv/toolchain-thead-glibc/riscv64-glibc-gcc-
thead_20200702/bin/riscv64-unknown-linux-gnu-gcc -o ledapp ledApp.c 编译出ledgpio 软件
MQpro:
1.通过insmod gpioled.ko加载gpioled驱动,通过ls /dev 查看是否有gpioled
2.通过 chmod 777 ledapp 添加权限
3.通过 ./ledapp /dev/gpioled 0 点亮LED
4.通过 ./ledapp /dev/gpioled 1 熄灭LED
注:以上命令没有跟LED高低电平相对应可以通过修改 gpioled.c led_write函数进行修改就好了