linux系统之字符设备驱动——点灯

linux系统之字符设备驱动——点灯

1. 原理图

在这里插入图片描述

底板核心板
RGB-REDGPIOA28
RGB-GREENGPIOE13
RGB-BLUEGPIOB12

2. 驱动代码

  1. 驱动代码程序 led_dev.c
/*
 * @Author: your name
 * @Date: 2021-02-06 00:06:43
 * @LastEditTime: 2021-02-23 20:06:05
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \2-led\led_dev.c
 */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>

#include "led.h"

#define GPIONO(m,n) (m*32+n)
#define GPIOA28 GPIONO(0,28)
#define GPIOB12 GPIONO(1,12)
#define GPIOE13 GPIONO(4,13)

#define NAME "RGB_IO"	// 设备节点名字
struct led_dev{
	unsigned int major;
    struct class *class;	// 设备节点目录
    struct device *dev; 	// 设备
}LedDevice;

int led_open (struct inode *inode, struct file *file)
{
	return 0;
}
int led_close (struct inode *inode, struct file *file)
{
	return 0;
}
long led_ioctl (struct file *file, unsigned int cmd, unsigned long agrs)
{
	switch(cmd){
		case RED_ON:
			gpio_set_value(GPIOA28, 1);
			break;
			
		case RED_OFF:
			gpio_set_value(GPIOA28, 0);
			break;

		case GREEN_ON:
			gpio_set_value(GPIOE13, 1);
			break;

		case GREEN_OFF:
			gpio_set_value(GPIOE13, 0);
			break;

		case BLUE_ON:
			gpio_set_value(GPIOB12, 1);
			break;

		case BLUE_OFF:
			gpio_set_value(GPIOB12, 0);
			break;

	}
	return 0;
}
const struct file_operations fops = 
{
	.open = led_open,
    .release = led_close,
    .unlocked_ioctl = led_ioctl,  
};
static int __init myRGB_Init(void)
{
	int ret = -1;

	// 1. 注册字符设备驱动
	LedDevice.major = register_chrdev(LedDevice.major, NAME, &fops);
	if(LedDevice.major < 0)
	{
		printk("%s, %s, register_chrdev failed\n", __FILE__, __func__);
		goto ERR1;
	}
	// 2. 创建设备驱动目录
    LedDevice.class = class_create(THIS_MODULE, "cleddev");
	if(IS_ERR(LedDevice.class)){
		printk("%s, %s, class_create failed\n", __FILE__, __func__);
		goto ERR2;
	}
    // 3. 创建设备驱动节点
    LedDevice.dev = device_create(LedDevice.class, NULL, MKDEV(LedDevice.major, 0), NULL, NAME);
	if(IS_ERR(LedDevice.dev)){
		printk("%s, %s, device_create failed\n", __FILE__, __func__);
		ret = PTR_ERR(LedDevice.dev);
		goto ERR3;
	}
	gpio_set_value(GPIOA28, 0);
	gpio_set_value(GPIOB12, 0);
	gpio_set_value(GPIOE13, 0);

	printk("%s, %s, myRGB_Init succeed \n", __FILE__, __func__);

	return 0;

ERR3:
	class_destroy(LedDevice.class);
ERR2:
	unregister_chrdev(LedDevice.major, NAME);
ERR1:
	return ret;
}

static void __exit myRGB_Exit(void)
{
	gpio_set_value(GPIOA28, 1);
	gpio_set_value(GPIOB12, 1);
	gpio_set_value(GPIOE13, 1);

	device_destroy(LedDevice.class, MKDEV(LedDevice.major, 0));
    // 3. 销毁设备驱动目录
	class_destroy(LedDevice.class);
    // 4. 注销字符设备驱动
    unregister_chrdev(LedDevice.major, NAME);

	printk("%s, %s, myRGB_Exit succeed \n", __FILE__, __func__);
}
module_init(myRGB_Init);
module_exit(myRGB_Exit);
MODULE_LICENSE("GPL");
  1. 中间文件 led.h
/*
 * @Author: your name
 * @Date: 2021-02-23 20:01:02
 * @LastEditTime: 2021-02-23 20:01:35
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \2-led\led.h
 */

#ifndef __LED_H__
#define __LED_H__

#define TYPE 'L'

#define RED_ON 		_IO(TYPE, 0)
#define RED_OFF		_IO(TYPE, 1)
#define GREEN_ON 	_IO(TYPE, 2)
#define GREEN_OFF 	_IO(TYPE, 3)
#define BLUE_ON 	_IO(TYPE, 4)
#define BLUE_OFF 	_IO(TYPE, 5)

#endif /* __LED_H__ */
  1. 应用程序 main.c
/*
 * @Author: your name
 * @Date: 2021-02-23 20:09:13
 * @LastEditTime: 2021-02-23 20:15:03
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \2-led\main.c
 */
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "led.h"

int main(int argc, char const *argv[])
{
    int fd = open("/dev/RGB_IO", O_RDWR);
    if(fd < 0)
    {
         perror("open");
         return -1;  
    }

    while(1)
    {
        ioctl(fd, RED_ON);
		sleep(1);
		ioctl(fd, RED_OFF);
		ioctl(fd, GREEN_ON);
		sleep(1);
		ioctl(fd, GREEN_OFF);
		ioctl(fd, BLUE_ON);
		sleep(1);
		ioctl(fd, BLUE_OFF);
    }
    close(fd);

    return 0;
}

3. 实验现象

红、绿、蓝三个灯交替闪烁。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值