上一篇用python点亮LED灯,确实方便简洁(虽然可以屏蔽掉底层细节,加快应用程序的开发进程),但是还是想试一试用C语言方式点灯(毕竟擅长的是mcu裸机开发,主要玩的就是C语言)。在Linux方面,想用C语言点灯,最简明的方式就是以驱动模块方式动态加载进内核来点亮LED。
查看原理图,确认了LED连接在GPIO 85引脚上,且当引脚输出低电平时,LED灯亮。
图1 板载LED威廉希尔官方网站
原理图
1.先将内核源码linux-5.10版本拷贝、解压到虚拟机中
2.编写LED驱动模块(随便起了个文件名led85.c,编译后将以leddev模块进行加载),LED驱动模块代码如下所示。
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#define GPIOLED_CNT 1
#define GPIOLED_NAME "led"
#define OFF 0
#define ON 1
#define GPIO_LED 85
struct my_led_dev{
dev_t dev_id;
struct cdev cdev;
struct class *class;
struct device *device;
int major;
int minor;
struct device_node *nd;
int led;
};
struct my_led_dev led_dev;
static int led_open(struct inode *inode, struct file *filp)
{
filp->private_data = &led_dev;
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 ret;
unsigned char data[1];
unsigned char ledstat;
struct my_led_dev *dev = filp->private_data;
ret = copy_from_user(data, buf, cnt);
if(ret < 0) {
printk("kernel write failed!\r\n");
return -EFAULT;
}
ledstat = data[0];
if(ledstat == ON) {
gpio_set_value(dev->led, 0);
} else if(ledstat == OFF) {
gpio_set_value(dev->led, 1);
}
return 0;
}
static int led_release(struct inode *inode, struct file *filp)
{
return 0;
}
static struct file_operations led_dev_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;
led_dev.led = GPIO_LED;
if(led_dev.led < 0) {
printk("can't get led-gpio");
return -EINVAL;
}
ret = gpio_request(led_dev.led, "LED-GPIO");
if (ret) {
printk(KERN_ERR "led_dev: Failed to request led-gpio\n");
return ret;
}
ret = gpio_direction_output(led_dev.led, 1);
if(ret < 0) {
printk("can't set gpio!\r\n");
}
if (led_dev.major) {
led_dev.dev_id = MKDEV(led_dev.major, 0);
ret = register_chrdev_region(led_dev.dev_id, GPIOLED_CNT, GPIOLED_NAME);
if(ret < 0) {
pr_err("cannot register %s char driver [ret=%d]\n", GPIOLED_NAME, GPIOLED_CNT);
goto free_gpio;
}
} else {
ret = alloc_chrdev_region(&led_dev.dev_id, 0, GPIOLED_CNT, GPIOLED_NAME);
if(ret < 0) {
pr_err("%s Couldn't alloc_chrdev_region, ret=%d\r\n", GPIOLED_NAME, ret);
goto free_gpio;
}
led_dev.major = MAJOR(led_dev.dev_id);
led_dev.minor = MINOR(led_dev.dev_id);
}
led_dev.cdev.owner = THIS_MODULE;
cdev_init(&led_dev.cdev, &led_dev_fops);
cdev_add(&led_dev.cdev, led_dev.dev_id, GPIOLED_CNT);
if(ret < 0)
goto del_unregister;
led_dev.class = class_create(THIS_MODULE, GPIOLED_NAME);
if (IS_ERR(led_dev.class)) {
goto del_cdev;
}
led_dev.device = device_create(led_dev.class, NULL, led_dev.dev_id, NULL, GPIOLED_NAME);
if (IS_ERR(led_dev.device)) {
goto destroy_class;
}
return 0;
destroy_class:
class_destroy(led_dev.class);
del_cdev:
cdev_del(&led_dev.cdev);
del_unregister:
unregister_chrdev_region(led_dev.dev_id, GPIOLED_CNT);
free_gpio:
gpio_free(led_dev.led);
return -EIO;
}
static void __exit led_exit(void)
{
cdev_del(&led_dev.cdev);
unregister_chrdev_region(led_dev.dev_id, GPIOLED_CNT);
device_destroy(led_dev.class, led_dev.dev_id);
class_destroy(led_dev.class);
gpio_free(led_dev.led);
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
3.Makefile文件的编写(Linux下文件的编译,尤其是以工程化方式组织文件的时候,编译命令一般都是以Makefile文件来组织的。)
NAME = leddev
KDIR := /home/loongson/Downloads/linux-5.10
obj-m := $(NAME).o
$(NAME)-objs := led85.o
PWD := $(shell pwd)
all:
$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
-rm -rf *.o *.ko *.order $(NAME).mod.c Module.symvers
4.测试文件(随便起了个文件名testled.c),功能是以半秒为周期的点亮LED灯。相对于裸机操作方式,Linux下的设备均是以文件方式来进行读写操作的,显得有点麻烦。
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"
int main(int argc, char *argv[])
{
int fd, ret;
char *filename;
unsigned char data[1];
if(argc != 2){
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;
}
while(1) {
data[0] = 1;
ret = write(fd, data, sizeof(data));
if(ret < 0){
printf("LED Control Failed!\r\n");
close(fd);
return -1;
}
usleep(500000);
data[0] = 0;
ret = write(fd, data, sizeof(data));
if(ret < 0){
printf("LED Control Failed!\r\n");
close(fd);
return -1;
}
usleep(500000);
}
ret = close(fd);
if(ret < 0){
printf("file %s close failed!\r\n", argv[1]);
return -1;
}
return 0;
}
当然,不需要led模块时,将其卸载即可,操作如图5所示。
图5 卸载驱动模块
实验视频如下所示。点亮LED灯的命令如图4所示。
https://v.youku.com/v_show/id_XNjQxNDIxNjc2MA==.html
更多回帖