通过ioctl函数选择不同硬件的控制,如实现对LED、蜂鸣器、马达、风扇的控制
1.将GPIO的相关寄存器封装成结构体 ——–> gpio.h
2.LED相关驱动文件 ——–> led.c
3.蜂鸣器相关驱动文件 ——–> beep.c
4.风扇相关驱动文件 ——–> fan.c
5.马达相关驱动文件 ——–> motor.c
6.应用层测试文件 ——–> test.c
gpio.h
/*
* Copyright (c) 2023 by Huijie Xia, All Rights Reserved.
* @Author: Huijie Xia
* @Date: 2023-07-05 16:21:43
* @LastEditTime: 2023-07-05 16:47:21
* @FilePath: /B_Drive/day3/led_beep_fan_motor/gpio.h
* @version:
* @Description:
*/
#ifndef __GPIO_H_
#define __GPIO_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 PHY_RCC_GPIO 0x50000A28
#define PHY_GPIOB_BASE 0x50003000
#define PHY_GPIOE_BASE 0x50006000
#define PHY_GPIOF_BASE 0x50007000
#define LED1_ON _IO('l', 1)
#define LED1_OFF _IO('l', 0)
#define LED_ON _IOW('l', 1, int)
#define LED_OFF _IOW('l', 0, int)
#define FAN_ON _IO('f', 1)
#define FAN_OFF _IO('f', 0)
#define BEEP_ON _IO('b', 1)
#define BEEP_OFF _IO('b', 0)
#define MOTOR_ON _IO('m', 1)
#define MOTOR_OFF _IO('m', 0)
#endif
led.c
/*
* Copyright (c) 2023 by Huijie Xia, All Rights Reserved.
* @Author: Huijie Xia
* @Date: 2023-06-29 08:56:26
* @LastEditTime: 2023-07-05 16:24:06
* @FilePath: /B_Drive/day3/led_beep_fan_motor/led.c
* @version:
* @Description: 驱动控制LED灯
*/
#include "gpio.h"
#include
#include
#include
#include
#include
#include
#define CNAME "myled"
struct class* cls;
struct device* dev;
int major;
char kbuf[128] = { 0 };
unsigned int* KERNEL_RCC_GPIO;
gpio_t* KERNEL_GPIOE_BASE;
gpio_t* KERNEL_GPIOF_BASE;
int mycdev_open(struct inode* inode, struct file* file)
{
printk("%s:%s:%dn", __FILE__, __func__, __LINE__);
/* 地址映射 */
KERNEL_RCC_GPIO = ioremap(PHY_RCC_GPIO, 4);
if (KERNEL_RCC_GPIO == NULL) {
printk("ioremap RCC error!n");
return -ENOMEM;
}
KERNEL_GPIOE_BASE = ioremap(PHY_GPIOE_BASE, sizeof(gpio_t));
if (KERNEL_GPIOE_BASE == NULL) {
printk("ioremap GPIOE BASE error!n");
return -ENOMEM;
}
KERNEL_GPIOF_BASE = ioremap(PHY_GPIOF_BASE, sizeof(gpio_t));
if (KERNEL_GPIOF_BASE == NULL) {
printk("ioremap GPIOF BASE error!n");
return -ENOMEM;
}
/* LED1(PE10)寄存器初始化(读,改,写) */
writel(readl(KERNEL_RCC_GPIO) | (0x1 MODER) & (~(0x3 MODER); /* MODER寄存器21~20bit清零 */
writel(readl(&KERNEL_GPIOE_BASE->MODER) | (0x1 MODER); /* MODER寄存器设置为输出模式 */
writel(readl(&KERNEL_GPIOE_BASE->ODR) & (~(0x1 ODR); /* LED1熄灭 */
/* LED2(PF10)寄存器初始化(读,改,写) */
writel(readl(KERNEL_RCC_GPIO) | (0x1 MODER) & (~(0x3 MODER); /* MODER寄存器21~20bit清零 */
writel(readl(&KERNEL_GPIOF_BASE->MODER) | (0x1 MODER); /* MODER寄存器设置为输出模式 */
writel(readl(&KERNEL_GPIOF_BASE->ODR) & (~(0x1 ODR); /* LED2熄灭 */
/* LED3(PE8)寄存器初始化(读,改,写) */
writel(readl(KERNEL_RCC_GPIO) | (0x1 MODER) & (~(0x3 MODER); /* MODER寄存器21~20bit清零 */
writel(readl(&KERNEL_GPIOE_BASE->MODER) | (0x1 MODER); /* MODER寄存器设置为输出模式 */
writel(readl(&KERNEL_GPIOE_BASE->ODR) & (~(0x1 ODR); /* LED3熄灭 */
return 0;
}
ssize_t mycdev_read(struct file* file, char __user* ubuf,
size_t size, loff_t* offs)
{
int ret;
printk("%s:%s:%dn", __FILE__, __func__, __LINE__);
if (size > sizeof(kbuf)) {
size = sizeof(kbuf);
}
ret = copy_to_user(ubuf, kbuf, size);
if (ret) {
printk("copy data to user error!n");
return -EIO;
}
return size;
}
ssize_t mycdev_write(struct file* file, const char __user* ubuf,
size_t size, loff_t* offs)
{
int ret;
printk("%s:%s:%dn", __FILE__, __func__, __LINE__);
if (size > sizeof(kbuf)) {
size = sizeof(kbuf);
}
ret = copy_from_user(kbuf, ubuf, size);
if (ret) {
printk("copy data from user error!n");
return -EIO;
}
// switch (kbuf[0]) {
// case '1': /* LED1点亮 */
// writel(readl(&KERNEL_GPIOE_BASE->ODR) | (0x1 ODR);
// break;
// case '2': /* LED1熄灭 */
// writel(readl(&KERNEL_GPIOE_BASE->ODR) & (~(0x1 ODR);
// break;
// case '3': /* LED2点亮 */
// writel(readl(&KERNEL_GPIOF_BASE->ODR) | (0x1 ODR);
// break;
// case '4': /* LED2熄灭 */
// writel(readl(&KERNEL_GPIOF_BASE->ODR) & (~(0x1 ODR);
// break;
// case '5': /* LED3点亮 */
// writel(readl(&KERNEL_GPIOE_BASE->ODR) | (0x1 ODR);
// break;
// case '6': /* LED3熄灭 */
// writel(readl(&KERNEL_GPIOE_BASE->ODR) & (~(0x1 ODR);
// break;
// }
// kbuf[0] == 1 ? (writel(readl(&KERNEL_GPIOE_BASE->ODR) | ((1 ODR)) : (writel(readl(&KERNEL_GPIOE_BASE->ODR) & (~(1 ODR));
return size;
}
long mycdev_ioctl(struct file* file, unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case LED_ON:
switch (arg) {
case 1:
writel(readl(&KERNEL_GPIOE_BASE->ODR) | (0x1 ODR);
break;
case 2:
writel(readl(&KERNEL_GPIOF_BASE->ODR) | (0x1 ODR);
break;
case 3:
writel(readl(&KERNEL_GPIOE_BASE->ODR) | (0x1 ODR);
break;
default:
break;
}
break;
case LED_OFF:
switch (arg) {
case 1:
writel(readl(&KERNEL_GPIOE_BASE->ODR) & (~(0x1 ODR);
break;
case 2:
writel(readl(&KERNEL_GPIOF_BASE->ODR) & (~(0x1 ODR);
break;
case 3:
writel(readl(&KERNEL_GPIOE_BASE->ODR) & (~(0x1 ODR);
break;
default:
break;
}
break;
default:
break;
}
return 0;
}
int mycdev_close(struct inode* inode, struct file* file)
{
printk("%s:%s:%dn", __FILE__, __func__, __LINE__);
return 0;
}
struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.release = mycdev_close,
.unlocked_ioctl = mycdev_ioctl,
};
/**
* @description: 入口:在安装驱动的时候入口函数执行,做资源申请的工作
* demo_init:驱动入口的名字
* __init:它是给编译器使用的,告诉编译器将这个函数放在.init.text段中
* @return {*}
*/
static int __init demo_init(void)
{
/* 1. 注册字符设备驱动 */
major = register_chrdev(0, CNAME, &fops);
if (major
beep.c
/*
* Copyright (c) 2023 by Huijie Xia, All Rights Reserved.
* @Author: Huijie Xia
* @Date: 2023-06-29 08:56:26
* @LastEditTime: 2023-07-05 16:36:58
* @FilePath: /B_Drive/day3/led_beep_fan_motor/beep.c
* @version:
* @Description: 驱动控制风扇
*/
#include "gpio.h"
#include
#include
#include
#include
#include
#include
#define CNAME "mybeep"
struct class* cls;
struct device* dev;
int major;
char kbuf[128] = { 0 };
unsigned int* KERNEL_RCC_GPIO;
gpio_t* KERNEL_GPIOB_BASE;
int mycdev_open(struct inode* inode, struct file* file)
{
printk("%s:%s:%dn", __FILE__, __func__, __LINE__);
/* 地址映射 */
KERNEL_RCC_GPIO = ioremap(PHY_RCC_GPIO, 4);
if (KERNEL_RCC_GPIO == NULL) {
printk("ioremap RCC error!n");
return -ENOMEM;
}
KERNEL_GPIOB_BASE = ioremap(PHY_GPIOB_BASE, sizeof(gpio_t));
if (KERNEL_GPIOB_BASE == NULL) {
printk("ioremap GPIOB BASE error!n");
return -ENOMEM;
}
/* 蜂鸣器(PB6)寄存器初始化(读,改,写) */
writel(readl(KERNEL_RCC_GPIO) | (0x1 MODER) & (~(0x3 MODER); /* MODER寄存器13~12bit清零 */
writel(readl(&KERNEL_GPIOB_BASE->MODER) | (0x1 MODER); /* MODER寄存器设置为输出模式 */
writel(readl(&KERNEL_GPIOB_BASE->ODR) & (~(0x1 ODR); /* 蜂鸣器熄灭 */
return 0;
}
ssize_t mycdev_read(struct file* file, char __user* ubuf,
size_t size, loff_t* offs)
{
int ret;
printk("%s:%s:%dn", __FILE__, __func__, __LINE__);
if (size > sizeof(kbuf)) {
size = sizeof(kbuf);
}
ret = copy_to_user(ubuf, kbuf, size);
if (ret) {
printk("copy data to user error!n");
return -EIO;
}
return size;
}
ssize_t mycdev_write(struct file* file, const char __user* ubuf,
size_t size, loff_t* offs)
{
int ret;
printk("%s:%s:%dn", __FILE__, __func__, __LINE__);
if (size > sizeof(kbuf)) {
size = sizeof(kbuf);
}
ret = copy_from_user(kbuf, ubuf, size);
if (ret) {
printk("copy data from user error!n");
return -EIO;
}
return size;
}
long mycdev_ioctl(struct file* file, unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case BEEP_ON:
writel(readl(&KERNEL_GPIOB_BASE->ODR) | (0x1 ODR);
break;
case BEEP_OFF:
writel(readl(&KERNEL_GPIOB_BASE->ODR) & (~(0x1 ODR);
break;
default:
break;
}
return 0;
}
int mycdev_close(struct inode* inode, struct file* file)
{
printk("%s:%s:%dn", __FILE__, __func__, __LINE__);
return 0;
}
struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.release = mycdev_close,
.unlocked_ioctl = mycdev_ioctl,
};
/**
* @description: 入口:在安装驱动的时候入口函数执行,做资源申请的工作
* demo_init:驱动入口的名字
* __init:它是给编译器使用的,告诉编译器将这个函数放在.init.text段中
* @return {*}
*/
static int __init demo_init(void)
{
/* 1. 注册字符设备驱动 */
major = register_chrdev(0, CNAME, &fops);
if (major
fan.c
/*
* Copyright (c) 2023 by Huijie Xia, All Rights Reserved.
* @Author: Huijie Xia
* @Date: 2023-06-29 08:56:26
* @LastEditTime: 2023-07-05 16:59:43
* @FilePath: /B_Drive/day3/led_beep_fan_motor/fan.c
* @version:
* @Description: 驱动控制风扇
*/
#include "gpio.h"
#include
#include
#include
#include
#include
#include
#define CNAME "myfan"
struct class* cls;
struct device* dev;
int major;
char kbuf[128] = { 0 };
unsigned int* KERNEL_RCC_GPIO;
gpio_t* KERNEL_GPIOE_BASE;
gpio_t* KERNEL_GPIOF_BASE;
int mycdev_open(struct inode* inode, struct file* file)
{
printk("%s:%s:%dn", __FILE__, __func__, __LINE__);
/* 地址映射 */
KERNEL_RCC_GPIO = ioremap(PHY_RCC_GPIO, 4);
if (KERNEL_RCC_GPIO == NULL) {
printk("ioremap RCC error!n");
return -ENOMEM;
}
KERNEL_GPIOE_BASE = ioremap(PHY_GPIOE_BASE, sizeof(gpio_t));
if (KERNEL_GPIOE_BASE == NULL) {
printk("ioremap GPIOE BASE error!n");
return -ENOMEM;
}
/* 风扇(PE9)寄存器初始化(读,改,写) */
writel(readl(KERNEL_RCC_GPIO) | (0x1 MODER) & (~(0x3 MODER); /* MODER寄存器19~18bit清零 */
writel(readl(&KERNEL_GPIOE_BASE->MODER) | (0x1 MODER); /* MODER寄存器设置为输出模式 */
writel(readl(&KERNEL_GPIOE_BASE->ODR) & (~(0x1 ODR); /* 风扇熄灭 */
return 0;
}
ssize_t mycdev_read(struct file* file, char __user* ubuf,
size_t size, loff_t* offs)
{
int ret;
printk("%s:%s:%dn", __FILE__, __func__, __LINE__);
if (size > sizeof(kbuf)) {
size = sizeof(kbuf);
}
ret = copy_to_user(ubuf, kbuf, size);
if (ret) {
printk("copy data to user error!n");
return -EIO;
}
return size;
}
ssize_t mycdev_write(struct file* file, const char __user* ubuf,
size_t size, loff_t* offs)
{
int ret;
printk("%s:%s:%dn", __FILE__, __func__, __LINE__);
if (size > sizeof(kbuf)) {
size = sizeof(kbuf);
}
ret = copy_from_user(kbuf, ubuf, size);
if (ret) {
printk("copy data from user error!n");
return -EIO;
}
return size;
}
long mycdev_ioctl(struct file* file, unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case FAN_ON:
writel(readl(&KERNEL_GPIOE_BASE->ODR) | (0x1 ODR);
break;
case FAN_OFF:
writel(readl(&KERNEL_GPIOE_BASE->ODR) & (~(0x1 ODR);
break;
default:
break;
}
return 0;
}
int mycdev_close(struct inode* inode, struct file* file)
{
printk("%s:%s:%dn", __FILE__, __func__, __LINE__);
return 0;
}
struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.release = mycdev_close,
.unlocked_ioctl = mycdev_ioctl,
};
/**
* @description: 入口:在安装驱动的时候入口函数执行,做资源申请的工作
* demo_init:驱动入口的名字
* __init:它是给编译器使用的,告诉编译器将这个函数放在.init.text段中
* @return {*}
*/
static int __init demo_init(void)
{
/* 1. 注册字符设备驱动 */
major = register_chrdev(0, CNAME, &fops);
if (major
motor.c
/*
* Copyright (c) 2023 by Huijie Xia, All Rights Reserved.
* @Author: Huijie Xia
* @Date: 2023-06-29 08:56:26
* @LastEditTime: 2023-07-05 16:53:57
* @FilePath: /B_Drive/day3/led_beep_fan_motor/motor.c
* @version:
* @Description: 驱动控制马达
*/
#include "gpio.h"
#include
#include
#include
#include
#include
#include
#define CNAME "mymotor"
struct class* cls;
struct device* dev;
int major;
char kbuf[128] = { 0 };
unsigned int* KERNEL_RCC_GPIO;
gpio_t* KERNEL_GPIOF_BASE;
int mycdev_open(struct inode* inode, struct file* file)
{
printk("%s:%s:%dn", __FILE__, __func__, __LINE__);
/* 地址映射 */
KERNEL_RCC_GPIO = ioremap(PHY_RCC_GPIO, 4);
if (KERNEL_RCC_GPIO == NULL) {
printk("ioremap RCC error!n");
return -ENOMEM;
}
KERNEL_GPIOF_BASE = ioremap(PHY_GPIOF_BASE, sizeof(gpio_t));
if (KERNEL_GPIOF_BASE == NULL) {
printk("ioremap GPIOB BASE error!n");
return -ENOMEM;
}
/* 马达(PF6)寄存器初始化(读,改,写) */
writel(readl(KERNEL_RCC_GPIO) | (0x1 MODER) & (~(0x3 MODER); /* MODER寄存器13~12bit清零 */
writel(readl(&KERNEL_GPIOF_BASE->MODER) | (0x1 MODER); /* MODER寄存器设置为输出模式 */
writel(readl(&KERNEL_GPIOF_BASE->ODR) & (~(0x1 ODR); /* 马达熄灭 */
return 0;
}
ssize_t mycdev_read(struct file* file, char __user* ubuf,
size_t size, loff_t* offs)
{
int ret;
printk("%s:%s:%dn", __FILE__, __func__, __LINE__);
if (size > sizeof(kbuf)) {
size = sizeof(kbuf);
}
ret = copy_to_user(ubuf, kbuf, size);
if (ret) {
printk("copy data to user error!n");
return -EIO;
}
return size;
}
ssize_t mycdev_write(struct file* file, const char __user* ubuf,
size_t size, loff_t* offs)
{
int ret;
printk("%s:%s:%dn", __FILE__, __func__, __LINE__);
if (size > sizeof(kbuf)) {
size = sizeof(kbuf);
}
ret = copy_from_user(kbuf, ubuf, size);
if (ret) {
printk("copy data from user error!n");
return -EIO;
}
return size;
}
long mycdev_ioctl(struct file* file, unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case MOTOR_ON:
writel(readl(&KERNEL_GPIOF_BASE->ODR) | (0x1 ODR);
break;
case MOTOR_OFF:
writel(readl(&KERNEL_GPIOF_BASE->ODR) & (~(0x1 ODR);
break;
default:
break;
}
return 0;
}
int mycdev_close(struct inode* inode, struct file* file)
{
printk("%s:%s:%dn", __FILE__, __func__, __LINE__);
return 0;
}
struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.release = mycdev_close,
.unlocked_ioctl = mycdev_ioctl,
};
/**
* @description: 入口:在安装驱动的时候入口函数执行,做资源申请的工作
* demo_init:驱动入口的名字
* __init:它是给编译器使用的,告诉编译器将这个函数放在.init.text段中
* @return {*}
*/
static int __init demo_init(void)
{
/* 1. 注册字符设备驱动 */
major = register_chrdev(0, CNAME, &fops);
if (major
test.c
/*
* Copyright (c) 2023 by Huijie Xia, All Rights Reserved.
* @Author: Huijie Xia
* @Date: 2023-07-03 14:44:33
* @LastEditTime: 2023-07-05 16:59:23
* @FilePath: /B_Drive/day3/led_beep_fan_motor/test.c
* @version:
* @Description: 驱动控制硬件的测试文件
*/
#include "gpio.h"
#include
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg)
do {
fprintf(stderr, "line : %dn", __LINE__);
perror(msg);
} while (0)
char buf[128] = { 0 };
int main(int argc, const char* argv[])
{
int ledfd, beepfd, fanfd, motorfd; /* 文件描述符 */
int hardware_which; /* 硬件设备选择 */
int choice; /* 设备选择 */
int whichled; /* LED选择 */
/* 打开LED驱动文件 */
if ((ledfd = open("/dev/myled", O_RDWR)) == -1)
ERR_MSG("myled open error");
/* 打开蜂鸣器驱动文件 */
if ((beepfd = open("/dev/mybeep", O_RDWR)) == -1)
ERR_MSG("mybeep open error");
/* 打开风扇驱动文件 */
if ((fanfd = open("/dev/myfan", O_RDWR)) == -1)
ERR_MSG("myfan open error");
/* 打开马达驱动文件 */
if ((motorfd = open("/dev/mymotor", O_RDWR)) == -1)
ERR_MSG("mymotor open error");
while (1) {
printf("请输入你要执行的硬件设备n");
printf("1(LED灯) 2(蜂鸣器) 3(风扇) 4(马达) > ");
scanf("%d", &hardware_which);
switch (hardware_which) {
case 1:
printf("请选择要控制的LEDn");
printf("1(LED1) 2(LED2) 3(LED3) > ");
scanf("%d", &whichled);
printf("请选择要实现的功能n");
printf("0(关灯) 1(开灯) > ");
scanf("%d", &choice);
if (choice == 0) {
ioctl(ledfd, LED_OFF, whichled);
} else {
ioctl(ledfd, LED_ON, whichled);
}
break;
case 2:
printf("请选择要实现的蜂鸣器功能n");
printf("0(不响) 1(响) > ");
scanf("%d", &choice);
if (choice == 0) {
ioctl(beepfd, BEEP_OFF);
} else {
ioctl(beepfd, BEEP_ON);
}
break;
case 3:
printf("请选择要实现的风扇功能n");
printf("0(停止) 1(转动) > ");
scanf("%d", &choice);
if (choice == 0) {
ioctl(fanfd, FAN_OFF);
} else {
ioctl(fanfd, FAN_ON);
}
break;
case 4:
printf("请选择要实现的马达功能n");
printf("0(停止) 1(振动) > ");
scanf("%d", &choice);
if (choice == 0) {
ioctl(motorfd, MOTOR_OFF);
} else {
ioctl(motorfd, MOTOR_ON);
}
break;
default:
break;
}
}
close(ledfd);
close(beepfd);
close(fanfd);
close(motorfd);
return 0;
}
效果如下图:
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net