1.ioctl控制led
//头文件
#ifndef __HEAD_H__
#define __HEAD_H__
#define GPIOE_BAS 0x50006000
#define GPIOF_BAS 0x50007000
#define RCC_AHB4_ENSETR 0x50000A28
typedef struct{
volatile unsigned int MODER;
volatile unsigned int OTYPER;
volatile unsigned int OSPEEDR;
volatile unsigned int PUPDR;
volatile unsigned int IDR;
volatile unsigned int ODR;
}gpio_t;
//功能码
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',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 major;//定义一个变量保存主设备号
struct class *cls;
struct device *dev;
char kbuf[128]={0};
volatile unsigned int* RCC;
volatile gpio_t* GPIOE;
volatile gpio_t* GPIOF;
//封装操作方法
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 LED_ON:
switch (arg)
{
case 1: // LED1
GPIOE->ODR |= (0x1 << 10); // LED1开灯
break;
case 2: // LED2
GPIOF->ODR |= (0x1 << 10); // LED2开灯
break;
case 3: // LED3
GPIOE->ODR |= (0x1 << 8); // LED3开灯
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;
}
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,
};
static int __init mycdev_init(void)
{
int i;
//注册字符设备驱动
major=register_chrdev(0,"mychrdev",&fops);
if(major<0)
{
printk("注册字符设备驱动失败\n");
return major;
}
printk("注册字符设备驱动成功major=%d\n",major);
//向上提交目录
cls = class_create(THIS_MODULE, "myled");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录成功\n");
//向上提交设备节点信息
for( i=0;i<3;i++)
{
dev=device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
}
if(IS_ERR(dev))
{
printk("向上提交节点失败\n");
return -PTR_ERR(dev);
}
printk("向上提交节点成功\n");
//进行寄存器地址的映射
GPIOF=ioremap(GPIOF_BAS,4);
if(GPIOF==NULL)
{
printk("映射物理内存失败%d\n",__LINE__);
return -EFAULT;
}
GPIOE=ioremap(GPIOE_BAS,4);
if(GPIOE==NULL)
{
printk("映射物理内存失败%d\n",__LINE__);
return -EFAULT;
}
RCC=ioremap(RCC_AHB4_ENSETR,4);
if(RCC==NULL)
{
printk("映射物理内存失败%d\n",__LINE__);
return -EFAULT;
}
printk("映射物理内存成功\n");
//硬件寄存器的初始化
*RCC|=(0x1<<4);
*RCC|=(0x1<<5);
GPIOE->MODER&=(~(0x3<<20));
GPIOE->MODER|=(0x1<<20);
GPIOF->MODER&=(~(0x3<<20));
GPIOF->MODER|=(0x1<<20);
GPIOE->MODER&=(~(0x3<<16));
GPIOE->MODER|=(0x1<<16);
printk("硬件寄存器初始化成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
iounmap(GPIOE);
iounmap(GPIOF);
iounmap(RCC);
//销毁节点信息
int i;
for(i=0;i<3;i++)
{
device_destroy(cls,MKDEV(major,i));
}
//销毁目录信息
class_destroy(cls);
//注销字符设备驱动
unregister_chrdev(major,"mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
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 a,b;
int fd = open("/dev/myled0", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
//从终端读取
printf("请输入指令\n");
printf("0(关灯) 1(开灯)\n");
printf("请输入>");
scanf("%d",&a);
printf("请输入要控制的灯 1(LED1) 2(LED2) 3(LED3)>");
scanf("%d",&b);
switch(a)
{
case 1:
ioctl(fd,LED_ON,b);//开灯
break;
case 0:
ioctl(fd,LED_OFF,b);
break;
}
}
close(fd);
return 0;
}
2.ioctl控制风扇
#ifndef __HEAD_H__
#define __HEAD_H__
#define GPIOE_BAS 0x50006000
#define GPIOF_BAS 0x50007000
#define GPIOB_BAS 0x50003000
#define RCC_AHB4_ENSETR 0x50000A28
typedef struct{
volatile unsigned int MODER;
volatile unsigned int OTYPER;
volatile unsigned int OSPEEDR;
volatile unsigned int PUPDR;
volatile unsigned int IDR;
volatile unsigned int ODR;
}gpio_t;
//功能码
#define FAN_ON _IO('l',1)
#define FAN_OFF _IO('l',0)
#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 major; // 定义一个变量保存主设备号
struct class *cls;
struct device *dev;
volatile unsigned int *RCC;
volatile gpio_t *GPIOE;
// 封装操作方法
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;
}
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 my_all_init(void)
{
// 进行寄存器地址的映射
GPIOE = ioremap(GPIOE_BAS, 4);
if (GPIOE == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
RCC = ioremap(RCC_AHB4_ENSETR, 4);
if (RCC == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
printk("映射物理内存成功\n");
// 硬件寄存器的初始化
*RCC |= (0x1 << 4);
GPIOE->MODER &= (~(0x3 << 18));
GPIOE->MODER |= (0x1 << 18);
printk("硬件寄存器初始化成功\n");
return 0;
}
static int __init mycdev_init(void)
{
// 注册字符设备驱动
major = register_chrdev(0, "mychrdev_fan", &fops);
if (major < 0)
{
printk("注册字符设备驱动失败\n");
return major;
}
printk("注册字符设备驱动成功major=%d\n", major);
// 向上提交目录
cls = class_create(THIS_MODULE, "myfan");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录成功\n");
// 向上提交设备节点信息
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "myfan");
if (IS_ERR(dev))
{
printk("向上提交节点失败\n");
return -PTR_ERR(dev);
}
printk("向上提交节点成功\n");
my_all_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, "mychrdev_fan");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
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[])
{
int a,b;
int fd = open("/dev/myfan", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
//从终端读取
printf("请输入指令\n");
printf("0(关) 1(开)\n");
printf("请输入>");
scanf("%d",&a);
switch(a)
{
case 1:
ioctl(fd,FAN_ON);
break;
case 0:
ioctl(fd,FAN_OFF);
break;
}
}
close(fd);
return 0;
}
3.ioctl控制蜂鸣器
#ifndef __HEAD_H__
#define __HEAD_H__
#define GPIOE_BAS 0x50006000
#define GPIOF_BAS 0x50007000
#define GPIOB_BAS 0x50003000
#define RCC_AHB4_ENSETR 0x50000A28
typedef struct{
volatile unsigned int MODER;
volatile unsigned int OTYPER;
volatile unsigned int OSPEEDR;
volatile unsigned int PUPDR;
volatile unsigned int IDR;
volatile unsigned int ODR;
}gpio_t;
//功能码
#define FAN_ON _IO('l',1)
#define FAN_OFF _IO('l',0)
#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 major; // 定义一个变量保存主设备号
struct class *cls;
struct device *dev;
volatile unsigned int *RCC;
volatile gpio_t *GPIOB;
// 封装操作方法
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:
GPIOB->ODR |= (0x1 << 6);
break;
case FAN_OFF:
GPIOB->ODR &= (~(0X1 << 6));
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 my_all_init(void)
{
// 进行寄存器地址的映射
GPIOB = ioremap(GPIOB_BAS, 4);
if (GPIOB == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
RCC = ioremap(RCC_AHB4_ENSETR, 4);
if (RCC == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
printk("映射物理内存成功\n");
// 硬件寄存器的初始化
*RCC |= (0x1 << 1);
GPIOB->MODER &= (~(0x3 << 12));
GPIOB->MODER |= (0x1 << 12);
printk("硬件寄存器初始化成功\n");
return 0;
}
static int __init mycdev_init(void)
{
// 注册字符设备驱动
major = register_chrdev(0, "mychrdev_beep", &fops);
if (major < 0)
{
printk("注册字符设备驱动失败\n");
return major;
}
printk("注册字符设备驱动成功major=%d\n", major);
// 向上提交目录
cls = class_create(THIS_MODULE, "mybeep");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录成功\n");
// 向上提交设备节点信息
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "mybeep");
if (IS_ERR(dev))
{
printk("向上提交节点失败\n");
return -PTR_ERR(dev);
}
printk("向上提交节点成功\n");
my_all_init();
return 0;
}
static void __exit mycdev_exit(void)
{
iounmap(RCC);
iounmap(GPIOB);
// 销毁节点信息
device_destroy(cls, MKDEV(major, 0));
// 销毁目录信息
class_destroy(cls);
// 注销字符设备驱动
unregister_chrdev(major, "mychrdev_beep");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
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[])
{
int a,b;
int fd = open("/dev/mybeep", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
//从终端读取
printf("请输入指令\n");
printf("0(关) 1(开)\n");
printf("请输入>");
scanf("%d",&a);
switch(a)
{
case 1:
ioctl(fd,FAN_ON);
break;
case 0:
ioctl(fd,FAN_OFF);
break;
}
}
close(fd);
return 0;
}
4.ioctl控制马达
#ifndef __HEAD_H__
#define __HEAD_H__
#define GPIOE_BAS 0x50006000
#define GPIOF_BAS 0x50007000
#define GPIOB_BAS 0x50003000
#define RCC_AHB4_ENSETR 0x50000A28
typedef struct{
volatile unsigned int MODER;
volatile unsigned int OTYPER;
volatile unsigned int OSPEEDR;
volatile unsigned int PUPDR;
volatile unsigned int IDR;
volatile unsigned int ODR;
}gpio_t;
//功能码
#define FAN_ON _IO('l',1)
#define FAN_OFF _IO('l',0)
#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 major; // 定义一个变量保存主设备号
struct class *cls;
struct device *dev;
volatile unsigned int *RCC;
volatile gpio_t *GPIOF;
// 封装操作方法
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:
GPIOF->ODR |= (0x1 << 6);
break;
case FAN_OFF:
GPIOF->ODR &= (~(0X1 << 6));
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 my_all_init(void)
{
// 进行寄存器地址的映射
GPIOF = ioremap(GPIOF_BAS, 4);
if (GPIOF == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
RCC = ioremap(RCC_AHB4_ENSETR, 4);
if (RCC == NULL)
{
printk("映射物理内存失败%d\n", __LINE__);
return -EFAULT;
}
printk("映射物理内存成功\n");
// 硬件寄存器的初始化
*RCC |= (0x1 << 5);
GPIOF->MODER &= (~(0x3 << 12));
GPIOF->MODER |= (0x1 << 12);
printk("硬件寄存器初始化成功\n");
return 0;
}
static int __init mycdev_init(void)
{
// 注册字符设备驱动
major = register_chrdev(0, "mychrdev_motor", &fops);
if (major < 0)
{
printk("注册字符设备驱动失败\n");
return major;
}
printk("注册字符设备驱动成功major=%d\n", major);
// 向上提交目录
cls = class_create(THIS_MODULE, "mymotor");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录成功\n");
// 向上提交设备节点信息
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "mymotor");
if (IS_ERR(dev))
{
printk("向上提交节点失败\n");
return -PTR_ERR(dev);
}
printk("向上提交节点成功\n");
my_all_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, "mychrdev_motor");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
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[])
{
int a,b;
int fd = open("/dev/mymotor", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
//从终端读取
printf("请输入指令\n");
printf("0(关) 1(开)\n");
printf("请输入>");
scanf("%d",&a);
switch(a)
{
case 1:
ioctl(fd,FAN_ON);
break;
case 0:
ioctl(fd,FAN_OFF);
break;
}
}
close(fd);
return 0;
}