//head.h************************************************************
#ifndef __LED_H__
#define __LED_H__
#define PHY_LED1_MODER 0X50006000
#define PHY_LED1_ODR 0X50006014
#define PHY_LED1_RCC 0X50000A28
#define PHY_LED2_MODER 0X50007000
#define PHY_LED2_ODR 0X50007014
#define PHY_LED2_RCC 0X50000A28
#define PHY_LED3_MODER 0X50006000
#define PHY_LED3_ODR 0X50006014
#define PHY_LED3_RCC 0X50000A28
//风扇
#define PHY_FAN_MODER 0X50006000
#define PHY_FAN_ODR 0X50006014
#define PHY_FAN_RCC 0X50000A28
//蜂鸣器
#define PHY_BUZZER_MODER 0X50003000
#define PHY_BUZZER_ODR 0X50003014
#define PHY_BUZZER_RCC 0X50000A28
//马达
#define PHY_MOTOR_MODER 0X50007000
#define PHY_MOTOR_ODR 0X50007014
#define PHY_MOTOR_RCC 0X50000A28
//功能码
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)
#define FAN_ON _IOW('f',1,int)
#define FAN_OFF _IOW('f',0,int)
#define BUZZER_ON _IOW('b',1,int)
#define BUZZER_OFF _IOW('b',0,int)
#define MOTOR_ON _IOW('m',1,int)
#define MOTOR_OFF _IOW('m',0,int)
#endif
//驱动****************************************************************
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"
// 定义一个变量保存主设备号
unsigned int led_major;
unsigned int fan_major;
unsigned int buzzer_major;
unsigned int motor_major;
char kbuf[128] = {0};
struct class *cls;
struct device *dev;
struct class *cls_fan;
struct device *dev_fan;
struct class *cls_buzzer;
struct device *dev_buzzer;
struct class *cls_motor;
struct device *dev_motor;
unsigned int *vir_led1_moder;
unsigned int *vir_led1_odr;
unsigned int *vir_led1_rcc;
unsigned int *vir_led2_moder;
unsigned int *vir_led2_odr;
unsigned int *vir_led2_rcc;
unsigned int *vir_led3_moder;
unsigned int *vir_led3_odr;
unsigned int *vir_led3_rcc;
unsigned int *vir_fan_moder;
unsigned int *vir_fan_odr;
unsigned int *vir_fan_rcc;
unsigned int *vir_buzzer_moder;
unsigned int *vir_buzzer_odr;
unsigned int *vir_buzzer_rcc;
unsigned int *vir_motor_moder;
unsigned int *vir_motor_odr;
unsigned int *vir_motor_rcc;
// 封装操作方法
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
long mychrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch (cmd)
{
case LED_ON:
switch (arg)
{
case 1:
*vir_led1_odr |= (0x1 << 10);
break;
case 2:
*vir_led2_odr |= (0x1 << 10);
break;
case 3:
*vir_led3_odr |= (0x1 << 8);
break;
}
break;
case LED_OFF:
switch (arg)
{
case 1:
*vir_led1_odr &= (~(0x1 << 10));
break;
case 2:
*vir_led2_odr &= (~(0x1 << 10));
break;
case 3:
*vir_led3_odr &= (~(0x1 << 8));
break;
}
break;
case FAN_ON:
switch (arg)
{
case 4:
*vir_fan_odr |= (0x1 << 9);
break;
}
break;
case FAN_OFF:
switch (arg)
{
case 4:
*vir_fan_odr &= (~(0x1 << 9));
break;
}
break;
case BUZZER_ON:
switch (arg)
{
case 5:
*vir_buzzer_odr |= (0x1 << 6);
break;
}
break;
case BUZZER_OFF:
switch (arg)
{
case 5:
*vir_buzzer_odr &= (~(0x1 << 6));
break;
}
break;
case MOTOR_ON:
switch (arg)
{
case 6:
*vir_motor_odr |= (0x1 << 6);
break;
}
break;
case MOTOR_OFF:
switch (arg)
{
case 6:
*vir_motor_odr &= (~(0x1 << 6));
break;
}
break;
}
return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
long ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
ret = copy_to_user(ubuf, kbuf, size);
if (ret)
{
printk("copy_to_user filed\n");
return -EIO;
}
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
long ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
ret = copy_from_user(kbuf, ubuf, size);
if (ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
// 判断用户输入的数据,进行不同的硬件逻辑执行
if (kbuf[0] == '0') // 关灯
{
// 关灯逻辑
(*vir_led1_odr) &= (~(0x1 << 10));
(*vir_led2_odr) &= (~(0x1 << 10));
(*vir_led3_odr) &= (~(0x1 << 8));
}
else if (kbuf[0] == '1')
{
// 开灯
if (kbuf[1] == '1')
{
(*vir_led1_odr) |= (0x1 << 10);
}
else if (kbuf[1] == '0')
{
(*vir_led1_odr) &= (~(0x1 << 10));
}
}
else if (kbuf[0] == '2')
{
// 开灯
if (kbuf[1] == '1')
{
(*vir_led2_odr) |= (0x1 << 10);
}
else if (kbuf[1] == '0')
{
(*vir_led2_odr) &= (~(0x1 << 10));
}
}
else if (kbuf[0] == '3')
{
// 开灯
if (kbuf[1] == '1')
{
(*vir_led3_odr) |= (0x1 << 8);
}
else if (kbuf[1] == '0')
{
(*vir_led3_odr) &= (~(0x1 << 8));
}
}
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.unlocked_ioctl = mychrdev_ioctl,
.release = mycdev_close,
};
int led_init(void)
{
vir_led1_moder = ioremap(PHY_LED1_MODER, 4);
if (vir_led1_moder == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
vir_led1_odr = ioremap(PHY_LED1_ODR, 4);
if (vir_led1_odr == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
vir_led1_rcc = ioremap(PHY_LED1_RCC, 4);
if (vir_led1_rcc == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
(*vir_led1_rcc) |= (0x1 << 4);
(*vir_led1_moder) &= (~(0x3 << 20));
(*vir_led1_moder) |= (0x1 << 20);
(*vir_led1_odr) &= (~(0x1 << 10));
vir_led2_moder = ioremap(PHY_LED2_MODER, 4);
if (vir_led2_moder == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
vir_led2_odr = ioremap(PHY_LED2_ODR, 4);
if (vir_led2_odr == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
vir_led2_rcc = ioremap(PHY_LED2_RCC, 4);
if (vir_led2_rcc == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
(*vir_led2_rcc) |= (0x1 << 5);
(*vir_led2_moder) &= (~(0x3 << 20));
(*vir_led2_moder) |= (0x1 << 20);
(*vir_led2_odr) &= (~(0x1 << 10));
vir_led3_moder = ioremap(PHY_LED3_MODER, 4);
if (vir_led3_moder == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
vir_led3_odr = ioremap(PHY_LED3_ODR, 4);
if (vir_led3_odr == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
vir_led3_rcc = ioremap(PHY_LED3_RCC, 4);
if (vir_led3_rcc == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
printk("映射物理内存成功\n");
// 硬件寄存器的初始化
(*vir_led3_rcc) |= (0x1 << 4);
(*vir_led3_moder) &= (~(0x3 << 16));
(*vir_led3_moder) |= (0x1 << 16);
(*vir_led3_odr) &= (~(0x1 << 8));
return 0;
}
int fan_init(void)
{
vir_fan_moder = ioremap(PHY_FAN_MODER, 4);
if (vir_fan_moder == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
vir_fan_odr = ioremap(PHY_FAN_ODR, 4);
if (vir_fan_odr == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
vir_fan_rcc = ioremap(PHY_FAN_RCC, 4);
if (vir_fan_rcc == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
(*vir_fan_rcc) |= (0x1 << 4);
(*vir_fan_moder) &= (~(0x3 << 18));
(*vir_fan_moder) |= (0x1 << 18);
(*vir_fan_odr) &= (~(0x1 << 9));
return 0;
}
int buzzer_init(void)
{
vir_buzzer_moder = ioremap(PHY_BUZZER_MODER, 4);
if (vir_buzzer_moder == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
vir_buzzer_odr = ioremap(PHY_BUZZER_ODR, 4);
if (vir_buzzer_odr == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
vir_buzzer_rcc = ioremap(PHY_BUZZER_RCC, 4);
if (vir_buzzer_rcc == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
(*vir_buzzer_rcc) |= (0x1 << 1);
(*vir_buzzer_moder) &= (~(0x3 << 12));
(*vir_buzzer_moder) |= (0x1 << 12);
(*vir_buzzer_odr) &= (~(0x1 << 6));
return 0;
}
int motor_init(void)
{
vir_motor_moder = ioremap(PHY_MOTOR_MODER, 4);
if (vir_motor_moder == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
vir_motor_odr = ioremap(PHY_MOTOR_ODR, 4);
if (vir_motor_odr == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
vir_motor_rcc = ioremap(PHY_MOTOR_RCC, 4);
if (vir_motor_rcc == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
(*vir_motor_rcc) |= (0x1 << 5);
(*vir_motor_moder) &= (~(0x3 << 12));
(*vir_motor_moder) |= (0x1 << 12);
(*vir_motor_odr) &= (~(0x1 << 6));
return 0;
}
static int __init mycdev_init(void)
{
int i;
// 注册led字符设备驱动
led_major= register_chrdev(0, "myled", &fops);
if (led_major < 0)
{
printk("注册led字符设备驱动失败\n");
return led_major;
}
printk("注册led字符设备驱动成功%d\n", led_major);
// 注册fan字符设备驱动
fan_major= register_chrdev(0, "myfan", &fops);
if (fan_major < 0)
{
printk("注册fan字符设备驱动失败\n");
return fan_major;
}
printk("注册fan字符设备驱动成功%d\n", fan_major);
// 注册buzzer字符设备驱动
buzzer_major= register_chrdev(0, "mybuzzer", &fops);
if (buzzer_major < 0)
{
printk("注册buzzer字符设备驱动失败\n");
return buzzer_major;
}
printk("注册buzzer字符设备驱动成功%d\n", buzzer_major);
// 注册motor字符设备驱动
motor_major= register_chrdev(0, "mymotor", &fops);
if (motor_major < 0)
{
printk("注册motor字符设备驱动失败\n");
return motor_major;
}
printk("注册motor字符设备驱动成功%d\n", motor_major);
// 创建led设备文件
// 1、向上提交目录
cls = class_create(THIS_MODULE, "myled");
if (IS_ERR(cls))
{
printk("led提交目录失败\n");
return -PTR_ERR(cls);
}
printk("led提交目录成功\n");
// 2、向上提交设备节点信息
for (i = 0; i < 3; i++)
{
dev = device_create(cls, NULL, MKDEV(led_major, i), NULL, "myled%d", i);
if (IS_ERR(dev))
{
printk("提交led设备信息失败\n");
return -PTR_ERR(dev);
}
}
printk("提交led设备信息成功\n");
// 创建fan设备文件
// 1、向上提交目录
cls_fan = class_create(THIS_MODULE, "myfan");
if (IS_ERR(cls_fan))
{
printk("fan提交目录失败\n");
return -PTR_ERR(cls_fan);
}
printk("fan提交目录成功\n");
// 2、向上提交设备节点信息
dev_fan = device_create(cls_fan, NULL, MKDEV(fan_major, 0), NULL, "myfan0");
if (IS_ERR(dev_fan))
{
printk("提交fan设备信息失败\n");
return -PTR_ERR(dev_fan);
}
printk("提交fan设备信息成功\n");
// 创建buzzer设备文件
// 1、向上提交目录
cls_buzzer = class_create(THIS_MODULE, "mybuzzer");
if (IS_ERR(cls_buzzer))
{
printk("buzzer提交目录失败\n");
return -PTR_ERR(cls_buzzer);
}
printk("buzzer提交目录成功\n");
// 2、向上提交设备节点信息
dev_buzzer = device_create(cls_buzzer, NULL, MKDEV(buzzer_major, 0), NULL, "mybuzzer0");
if (IS_ERR(dev_buzzer))
{
printk("提交buzzer设备信息失败\n");
return -PTR_ERR(dev_buzzer);
}
printk("提交buzzer设备信息成功\n");
// 创建motor设备文件
// 1、向上提交目录
cls_motor = class_create(THIS_MODULE, "mymotor");
if (IS_ERR(cls_motor))
{
printk("motor提交目录失败\n");
return -PTR_ERR(cls_motor);
}
printk("motor提交目录成功\n");
// 2、向上提交设备节点信息
dev_motor = device_create(cls_motor, NULL, MKDEV(motor_major, 0), NULL, "mymotor0");
if (IS_ERR(dev_motor))
{
printk("提交motor设备信息失败\n");
return -PTR_ERR(dev_motor);
}
printk("提交motor设备信息成功\n");
// 寄存器地址映射及初始化
led_init();
fan_init();
buzzer_init();
motor_init();
printk("硬件寄存器初始化成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
int i;
iounmap(vir_led1_moder);
iounmap(vir_led1_odr);
iounmap(vir_led1_rcc);
iounmap(vir_led2_moder);
iounmap(vir_led2_odr);
iounmap(vir_led2_rcc);
iounmap(vir_led3_moder);
iounmap(vir_led3_odr);
iounmap(vir_led3_rcc);
iounmap(vir_fan_moder);
iounmap(vir_fan_odr);
iounmap(vir_fan_rcc);
iounmap(vir_buzzer_moder);
iounmap(vir_buzzer_odr);
iounmap(vir_buzzer_rcc);
iounmap(vir_motor_moder);
iounmap(vir_motor_odr);
iounmap(vir_motor_rcc);
printk("取消映射成功\n");
// 销毁节点信息
for (i = 0; i < 3; i++)
{
device_destroy(cls, MKDEV(led_major, i));
}
device_destroy(cls_fan, MKDEV(fan_major, 0));
device_destroy(cls_buzzer, MKDEV(buzzer_major, 0));
device_destroy(cls_motor, MKDEV(motor_major, 0));
// 销毁目录信息
class_destroy(cls);
class_destroy(cls_fan);
class_destroy(cls_buzzer);
class_destroy(cls_motor);
printk("注销字符设备驱动\n");
unregister_chrdev(led_major, "myled");
unregister_chrdev(fan_major, "myfan");
unregister_chrdev(buzzer_major, "buzzer");
unregister_chrdev(motor_major, "motor");
}
// 用于向内核报备当前内核模块入口函数的地址
module_init(mycdev_init);
// 用于向内核报备当前内核模块出口函数的地址
module_exit(mycdev_exit);
// 生命当前内核模块遵循GPL协议
MODULE_LICENSE("GPL");
//应用层****************************************************************
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "head.h"
int main(int argc, char const *argv[])
{
char buf[128] = {0};
int led_fd, fan_fd, buzzer_fd, motor_fd;
// 打开led设备文件
led_fd = open("/dev/myled0", O_RDWR);
if (led_fd < 0)
{
printf("打开led设备文件失败\n");
exit(-1);
}
// 打开风扇设备文件
fan_fd = open("/dev/myfan0", O_RDWR);
if (fan_fd < 0)
{
printf("打开fan设备文件失败\n");
exit(-1);
}
// 打开蜂鸣器设备文件
buzzer_fd = open("/dev/mybuzzer0", O_RDWR);
if (buzzer_fd < 0)
{
printf("打开buzzer设备文件失败\n");
exit(-1);
}
// 打开马达设备文件
motor_fd = open("/dev/mymotor0", O_RDWR);
if (motor_fd < 0)
{
printf("打开motor设备文件失败\n");
exit(-1);
}
int a, b;
while (1)
{
printf("请选择要控制的器件: 1(led灯1) 2 (led灯2) 3 (led灯3) 4 (风扇) 5(蜂鸣器) 6(马达)");
scanf("%d", &a);
printf("请输入指令:0(关闭) 1 (打开)");
scanf("%d", &b);
switch (b)
{
case 1:
switch (a)
{
case 1:
ioctl(led_fd, LED_ON, a);
break;
case 2:
ioctl(led_fd, LED_ON, a);
break;
case 3:
ioctl(led_fd, LED_ON, a);
break;
case 4:
ioctl(fan_fd, FAN_ON, a);
break;
case 5:
ioctl(buzzer_fd, BUZZER_ON, a);
break;
case 6:
ioctl(motor_fd, MOTOR_ON, a);
break;
}
break;
case 0:
switch (a)
{
case 1:
ioctl(led_fd, LED_OFF, a);
break;
case 2:
ioctl(led_fd, LED_OFF, a);
break;
case 3:
ioctl(led_fd, LED_OFF, a);
break;
case 4:
ioctl(fan_fd, FAN_OFF, a);
break;
case 5:
ioctl(buzzer_fd, BUZZER_OFF,a);
break;
case 6:
ioctl(motor_fd, MOTOR_OFF, a);
break;
}
break;
}
}
close(led_fd);
close(fan_fd);
close(buzzer_fd);
close(motor_fd);
return 0;
}
驱动开发(ioctl)
最新推荐文章于 2024-10-15 16:45:19 发布
该代码实现了一个内核模块,用于管理LED、风扇、蜂鸣器和马达的字符设备驱动。通过ioremap进行内存映射,初始化硬件寄存器,并提供了open、read、write、ioctl等文件操作方法。用户空间程序通过ioctl调用对设备进行开关控制。
摘要由CSDN通过智能技术生成