通过ioctl函数选择不同硬件的控制,LED 蜂鸣器 马达 风扇
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
typedef struct
{
volatile unsigned int MODER; // 0x00
volatile unsigned int OTYPER; // 0x04
volatile unsigned int OSPEEDR; // 0x08
volatile unsigned int PUPDR; // 0x0C
volatile unsigned int IDR; // 0x10
volatile unsigned int ODR; // 0x14
volatile unsigned int BSRR; // 0x18
volatile unsigned int LCKR; // 0x1C
volatile unsigned int AFRL; // 0x20
volatile unsigned int AFRH; // 0x24
volatile unsigned int BRR; // 0x28
volatile unsigned int res;
volatile unsigned int SECCFGR; // 0x30
} gpio_t;
#define GPIOA 0x50002000
#define GPIOB 0x50003000
#define GPIOC 0x50004000
#define GPIOD 0x50005000
#define GPIOE 0x50006000
#define GPIOF 0x50007000
#define GPIOG 0x50008000
#define GPIOH 0x50009000
#define GPIOI 0x5000A000
#define GPIOJ 0x5000B000
#define GPIOK 0x5000C000
#define GPIOZ 0x54004000
#define PHY_RCC 0X50000a28
#define LED_ON _IOW('i', 1, int)
#define LED_OFF _IOW('i', 0, int)
#define FAN_ON _IO('j', 1)
#define FAN_OFF _IO('j', 0)
#define MOTOR_ON _IO('k', 1)
#define MOTOR_OFF _IO('k', 0)
#define BEE_ON _IO('h', 1)
#define BEE_OFF _IO('h', 0)
#endif
test.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include "head.h"
int main(int argc, char const *argv[])
{
int a, b, c;
int fd;
while (1)
{
printf("1:led灯 2:风扇 3:马达 4:蜂鸣器 5(退出)>>>");
scanf("%d", &c);
switch (c)
{
case 1:
fd = open("/dev/led0", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
printf("请选择要实现的功能\n");
printf("0(关灯) 1(开灯) 3(退出)>>>");
scanf("%d", &a);
printf("请选择要控制的灯");
printf("1(led1) 2(led2) 3(led3)>>>");
scanf("%d", &b);
if (1 == a)
{
ioctl(fd, LED_ON, b);
}
else if (0 == a)
{
ioctl(fd, LED_OFF, b);
}
else
{
break;
}
}
close(fd);
getchar();
break;
case 2:
fd = open("/dev/fan", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
printf("请选择要实现的功能\n");
printf("0(关) 1(开) 3(退出)>>>");
scanf("%d", &a);
if (1 == a)
{
ioctl(fd, FAN_ON);
}
else if (0 == a)
{
ioctl(fd, FAN_OFF);
}
else
{
break;
}
}
close(fd);
getchar();
break;
case 3:
fd = open("/dev/motor", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
printf("请选择要实现的功能\n");
printf("0(关) 1(开) 3(退出)>>>");
scanf("%d", &a);
if (1 == a)
{
ioctl(fd, MOTOR_ON);
}
else if (0 == a)
{
ioctl(fd, MOTOR_OFF);
}
else
{
break;
}
}
close(fd);
break;
case 4:
fd = open("/dev/bee", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
printf("请选择要实现的功能\n");
printf("0(关) 1(开) 3(退出)>>>");
scanf("%d", &a);
if (1 == a)
{
ioctl(fd, BEE_ON);
}
else if (0 == a)
{
ioctl(fd, BEE_OFF);
}
else
{
break;
}
}
close(fd);
getchar();
break;
default:
break;
}
if (5 == c)
{
break;
}
}
return 0;
}
led.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/io.h>
#include "head.h"
struct class *cls;
struct device *dev;
int major;
char kbuf[128] = {0};
gpio_t *gpioe;
gpio_t *gpiof;
unsigned int *rcc;
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
printk("11111111111111111111111111111111111111\n");
switch (cmd)
{
case LED_ON:
switch (arg)
{
case 1:
printk("222222222222222222222222222222222222\n");
gpioe->ODR |= (0x1 << 10);
break;
case 2:
gpiof->ODR |= (0x1 << 10);
break;
case 3:
gpioe->ODR |= (0x1 << 8);
break;
}
break;
case LED_OFF:
switch (arg)
{
case 1:
gpioe->ODR &= (~(0x1 << 10));
break;
case 2:
gpiof->ODR &= (~(0x1 << 10));
break;
case 3:
gpioe->ODR &= (~(0x1 << 8));
break;
}
default:
break;
}
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,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
int all_led_init(void)
{
// 映射物理寄存器
gpioe = ioremap(GPIOE, sizeof(gpio_t));
if (gpioe == NULL)
{
printk("GPIOE地址映射表失败\n");
return -EFAULT;
}
gpiof = ioremap(GPIOF, sizeof(gpio_t));
if (gpiof == NULL)
{
printk("GPIOF地址映射表失败\n");
return -EFAULT;
}
rcc = ioremap(PHY_RCC, 4);
if (rcc == NULL)
{
printk("RCC地址映射表失败\n");
return -EFAULT;
}
// 寄存器初始化
(*rcc) |= (0x3 << 4);
gpioe->MODER &= (~(0x3 << 20));
gpioe->MODER |= (0x1 << 20);
gpioe->ODR &= (~(0x1 << 10));
gpioe->MODER &= (~(0x3 << 16));
gpioe->MODER |= (0x1 << 16);
gpioe->ODR &= (~(0x1 << 8));
gpiof->MODER &= (~(0x3 << 20));
gpiof->MODER |= (0x1 << 20);
gpiof->ODR &= (~(0x1 << 10));
printk("硬件寄存器初始化成功\n");
return 0;
}
static int __init mycdev_init(void)
{
int i;
major = register_chrdev(0, "led", &fops);
if (major < 0)
{
printk("字符设备驱动失败\n");
return major;
}
printk("字符设备驱动注册成功major=%d\n", major);
// 向上提交目录
cls = class_create(THIS_MODULE, "led");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
// 向上提交设备节点信息
// 为三盏灯创建三个设备文件
for (i = 0; i < 3; i++)
{
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "led%d", i);
if (IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
return -PTR_ERR(dev);
}
}
// 寄存器映射以及初始化
all_led_init();
return 0;
}
static void __exit mycdev_exit(void)
{
int i;
// 取消地址映射
iounmap(gpioe);
iounmap(gpiof);
iounmap(rcc);
// 销毁设备节点信息
for (i = 0; i < 3; i++)
{
device_destroy(cls, MKDEV(major, i));
}
// 销毁目录信息
class_destroy(cls);
// 注销字符设备驱动
unregister_chrdev(major, "led");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
fan.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/io.h>
#include "head.h"
struct class *cls;
struct device *dev;
int major;
gpio_t *gpioe;
unsigned int *rcc;
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch (cmd)
{
case FAN_ON:
gpioe->ODR |= (0x1 << 9);
break;
case FAN_OFF:
gpioe->ODR &= (~(0x1 << 9));
break;
default:
break;
}
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,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
int all_led_init(void)
{
// 映射物理寄存器
gpioe = ioremap(GPIOE, sizeof(gpio_t));
if (gpioe == NULL)
{
printk("GPIOE地址映射表失败\n");
return -EFAULT;
}
rcc = ioremap(PHY_RCC, 4);
if (rcc == NULL)
{
printk("rcc地址映射表失败\n");
return -EFAULT;
}
// 寄存器初始化
(*rcc) |= (0x1 << 4);
gpioe->MODER &= (~(0x3 << 18));
gpioe->MODER |= (0x1 << 18);
gpioe->ODR &= (~(0x1 << 9));
printk("硬件寄存器初始化成功\n");
return 0;
}
static int __init mycdev_init(void)
{
major = register_chrdev(0, "fan", &fops);
if (major < 0)
{
printk("字符设备驱动失败\n");
return major;
}
printk("字符设备驱动注册成功major=%d\n", major);
// 向上提交目录
cls = class_create(THIS_MODULE, "fan");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
// 向上提交设备节点信息
// 为风扇创建设备文件
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "fan");
if (IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
return -PTR_ERR(dev);
}
// 寄存器映射以及初始化
all_led_init();
return 0;
}
static void __exit mycdev_exit(void)
{
// 取消地址映射
iounmap(gpioe);
iounmap(rcc);
// 销毁设备节点信息
device_destroy(cls, MKDEV(major, 0));
// 销毁目录信息
class_destroy(cls);
// 注销字符设备驱动
unregister_chrdev(major, "fan");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
motor.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/io.h>
#include "head.h"
struct class *cls;
struct device *dev;
int major;
char kbuf[128] = {0};
gpio_t *gpiof;
unsigned int *rcc;
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch (cmd)
{
case MOTOR_ON:
gpiof->ODR |= (0x1 << 6);
break;
case MOTOR_OFF:
gpiof->ODR &= (~(0x1 << 6));
break;
default:
break;
}
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,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
int all_led_init(void)
{
// 映射物理寄存器
gpiof = ioremap(GPIOF, sizeof(gpio_t));
if (gpiof == NULL)
{
printk("GPIOF地址映射表失败\n");
return -EFAULT;
}
rcc = ioremap(PHY_RCC, 4);
if (rcc == NULL)
{
printk("rcc地址映射表失败\n");
return -EFAULT;
}
// 寄存器初始化
(*rcc) |= (0x1 << 5);
gpiof->MODER &= (~(0x3 << 12));
gpiof->MODER |= (0x1 << 12);
gpiof->ODR &= (~(0x1 << 6));
printk("硬件寄存器初始化成功\n");
return 0;
}
static int __init mycdev_init(void)
{
major = register_chrdev(0, "motor", &fops);
if (major < 0)
{
printk("字符设备驱动失败\n");
return major;
}
printk("字符设备驱动注册成功major=%d\n", major);
// 向上提交目录
cls = class_create(THIS_MODULE, "motor");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
// 向上提交设备节点信息
// 为三盏灯创建三个设备文件
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "motor");
if (IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
return -PTR_ERR(dev);
}
// 寄存器映射以及初始化
all_led_init();
return 0;
}
static void __exit mycdev_exit(void)
{
// 取消地址映射
iounmap(gpiof);
iounmap(rcc);
// 销毁设备节点信息
device_destroy(cls, MKDEV(major, 0));
// 销毁目录信息
class_destroy(cls);
// 注销字符设备驱动
unregister_chrdev(major, "motor");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
bee.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/io.h>
#include "head.h"
struct class *cls;
struct device *dev;
int major;
gpio_t *gpiob;
unsigned int *rcc;
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch (cmd)
{
case BEE_ON:
gpiob->ODR |= (0x1 << 6);
break;
case BEE_OFF:
gpiob->ODR &= (~(0x1 << 6));
break;
default:
break;
}
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,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
int all_led_init(void)
{
// 映射物理寄存器
gpiob = ioremap(GPIOB, sizeof(gpio_t));
if (gpiob == NULL)
{
printk("GPIOB地址映射表失败\n");
return -EFAULT;
}
rcc = ioremap(PHY_RCC, 4);
if (rcc == NULL)
{
printk("rcc地址映射表失败\n");
return -EFAULT;
}
// 寄存器初始化
(*rcc) |= (0x1 << 1);
gpiob->MODER &= (~(0x3 << 12));
gpiob->MODER |= (0x1 << 12);
gpiob->ODR &= (~(0x1 << 6));
printk("硬件寄存器初始化成功\n");
return 0;
}
static int __init mycdev_init(void)
{
major = register_chrdev(0, "bee", &fops);
if (major < 0)
{
printk("字符设备驱动失败\n");
return major;
}
printk("字符设备驱动注册成功major=%d\n", major);
// 向上提交目录
cls = class_create(THIS_MODULE, "bee");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
// 向上提交设备节点信息
// 为三盏灯创建三个设备文件
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "bee");
if (IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
return -PTR_ERR(dev);
}
// 寄存器映射以及初始化
all_led_init();
return 0;
}
static void __exit mycdev_exit(void)
{
// 取消地址映射
iounmap(gpiob);
iounmap(rcc);
// 销毁设备节点信息
device_destroy(cls, MKDEV(major, 0));
// 销毁目录信息
class_destroy(cls);
// 注销字符设备驱动
unregister_chrdev(major, "bee");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");