head.h
#ifndef __HEAD_H__
#define __HEAD_H__
typedef struct{
unsigned int MODER;
unsigned int OTYPER;
unsigned int OSPEEDR;
unsigned int PUPDR;
unsigned int IDR;
unsigned int ODR;
}gpio_t;
#define PHY_LED1_ADDR 0X50006000
#define PHY_LED2_ADDR 0X50007000
#define PHY_LED3_ADDR 0X50006000
#define PHY_RCC_ADDR 0X50000A28
#define PHY_BEEP_ADDR 0x50003000
#define PHY_HOUSE_ADDR 0x50007000
#define PHY_FAN_ADDR 0x50006000
//功能码
//LED
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)
//house
#define HOUSE_ON _IO('h',1)
#define HOUSE_OFF _IO('h',0)
//beep
#define BEEP_ON _IO('b',1)
#define BEEP_OFF _IO('b',0)
//fan
#define FAN_ON _IO('f',1)
#define FAN_OFF _IO('f',0)
#endif
beep_driver
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"
int beep_major;
gpio_t *vir_beep;
unsigned int *vir_rcc;
struct class *beep_cls;
struct device *beep_dev;
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 BEEP_ON:
vir_beep->ODR |= (0x1 << 6);
break;
case BEEP_OFF:
vir_beep->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 beep_init(void) //PB6
{
vir_beep = ioremap(PHY_BEEP_ADDR,sizeof(gpio_t));
if (vir_beep == NULL)
{
printk("ioremap filed:%d\n", __LINE__);
return -ENOMEM;
}
//寄存器初始化
vir_beep->MODER &= (~(3 << 12));
vir_beep->MODER |= (1 << 12);
vir_beep->ODR &= (~(1 << 6));
printk("蜂鸣器初始化完成\n");
return 1;
}
int register_dev(char *name)
{
int major;
major = register_chrdev(0, name, &fops);
if (major < 0)
{
printk("%s设备驱动注册失败\n",name);
return major;
}
printk("%s设备驱动注册成功:%s_major=%d\n",name,name,major);
return major;
}
struct class* class_t(char* name)
{
struct class* cls;
// 向上提交目录
cls = class_create(THIS_MODULE, name);
if (IS_ERR(cls))
{
printk("向上提交%s目录失败\n",name);
return NULL;
}
printk("向上提交%s目录信息成功\n",name);
return cls;
}
static int __init mycdev_init(void)
{
vir_rcc = ioremap(PHY_RCC_ADDR, 4);
if (vir_rcc == NULL)
{
printk("ioremap filed:%d\n", __LINE__);
return -ENOMEM;
}
printk("物理地址映射成功\n");
// 寄存器的初始化
// rcc
(*vir_rcc) |= (3 << 4);
(*vir_rcc) |= (0x1 << 1);
// 字符设备驱动注册
beep_major=register_dev("beep");
//上交目录
beep_cls=class_t("mybeep");
// 向上提交设备节点信息
beep_dev = device_create(beep_cls, NULL, MKDEV(beep_major, 0), NULL, "mybeep0");
if (IS_ERR(beep_dev))
{
printk("向上提交设备节点信息失败\n");
return -PTR_ERR(beep_dev);
}
printk("向上提交设备节点成功\n");
// 寄存器映射以及初始化
beep_init();
return 0;
}
static void __exit mycdev_exit(void)
{
// 取消地址映射
iounmap(vir_rcc);
//销毁节点信息
// 注销字符设备驱动
unregister_chrdev(beep_major, "beep");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
led.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"
int led_major;
char kbuf[128] = {0};
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;
struct class *led_cls;
struct device *led_dev;
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
vir_led1->ODR |= (0x1 << 10); // LED1开灯
break;
case 2: // LED2
vir_led2->ODR |= (0x1 << 10); // LED2开灯
break;
case 3: // LED3
vir_led3->ODR |= (0x1 << 8); // LED3开灯
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;
}
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)
{
// led寄存器地址的映射
vir_led1 = ioremap(PHY_LED1_ADDR, sizeof(gpio_t));
if (vir_led1 == NULL)
{
printk("ioremap filed:%d\n", __LINE__);
return -ENOMEM;
}
vir_led2 = ioremap(PHY_LED2_ADDR, sizeof(gpio_t));
if (vir_led2 == NULL)
{
printk("ioremap filed:%d\n", __LINE__);
return -ENOMEM;
}
vir_led3 = vir_led1;
vir_rcc = ioremap(PHY_RCC_ADDR, 4);
if (vir_rcc == NULL)
{
printk("ioremap filed:%d\n", __LINE__);
return -ENOMEM;
}
printk("物理地址映射成功\n");
// 寄存器的初始化
// rcc
(*vir_rcc) |= (3 << 4);
(*vir_rcc) |= (0x1 << 2);
// led1
vir_led1->MODER &= (~(3 << 20));
vir_led1->MODER |= (1 << 20);
vir_led1->ODR &= (~(1 << 10));
// led2
vir_led2->MODER &= (~(3 << 20));
vir_led2->MODER |= (1 << 20);
vir_led2->ODR &= (~(1 << 10));
// led3
vir_led3->MODER &= (~(3 << 16));
vir_led1->MODER |= (1 << 16);
vir_led1->ODR &= (~(1 << 8));
printk("寄存器初始化成功\n");
return 0;
}
int register_dev(char *name)
{
int major;
major = register_chrdev(0, name, &fops);
if (major < 0)
{
printk("%s设备驱动注册失败\n",name);
return major;
}
printk("%s设备驱动注册成功:%s_major=%d\n",name,name,major);
return major;
}
struct class* class_t(char* name)
{
struct class* cls;
// 向上提交目录
cls = class_create(THIS_MODULE, name);
if (IS_ERR(cls))
{
printk("向上提交%s目录失败\n",name);
return NULL;
}
printk("向上提交%s目录信息成功\n",name);
return cls;
}
static int __init mycdev_init(void)
{
// 字符设备驱动注册
led_major=register_dev("led");
//上交目录
led_cls=class_t("myled");
// 向上提交设备节点信息
led_dev = device_create(led_cls, NULL, MKDEV(led_major, 0), NULL, "myled0");
if (IS_ERR(led_dev))
{
printk("向上提交设备节点信息失败\n");
return -PTR_ERR(led_dev);
}
printk("向上提交设备节点成功\n");
// 寄存器映射以及初始化
all_led_init();
return 0;
}
static void __exit mycdev_exit(void)
{
// 取消地址映射
iounmap(vir_led1);
iounmap(vir_led2);
iounmap(vir_rcc);
//销毁节点信息
// 注销字符设备驱动
unregister_chrdev(led_major, "led");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
fan_driver
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"
int fan_major;
char kbuf[128] = {0};
gpio_t *vir_fan;
unsigned int *vir_rcc;
struct class *fan_cls;
struct device *fan_dev;
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:
vir_fan->ODR |= (0x1 << 9);
break;
case FAN_OFF:
vir_fan->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 fan_init(void) //PE9
{
vir_fan = ioremap(PHY_FAN_ADDR,sizeof(gpio_t));
if (vir_fan == NULL)
{
printk("ioremap filed:%d\n", __LINE__);
return -ENOMEM;
}
vir_fan->MODER &= (~(3 << 18));
vir_fan->MODER |= (1 << 18);
vir_fan->ODR &= (~(1 << 9));
printk("风扇初始化完成\n");
return 1;
}
int register_dev(char *name)
{
int major;
major = register_chrdev(0, name, &fops);
if (major < 0)
{
printk("%s设备驱动注册失败\n",name);
return major;
}
printk("%s设备驱动注册成功:%s_major=%d\n",name,name,major);
return major;
}
struct class* class_t(char* name)
{
struct class* cls;
// 向上提交目录
cls = class_create(THIS_MODULE, name);
if (IS_ERR(cls))
{
printk("向上提交%s目录失败\n",name);
return NULL;
}
printk("向上提交%s目录信息成功\n",name);
return cls;
}
static int __init mycdev_init(void)
{
vir_rcc = ioremap(PHY_RCC_ADDR, 4);
if (vir_rcc == NULL)
{
printk("ioremap filed:%d\n", __LINE__);
return -ENOMEM;
}
printk("物理地址映射成功\n");
// 寄存器的初始化
// rcc
(*vir_rcc) |= (3 << 4);
(*vir_rcc) |= (0x1 << 2);
// 字符设备驱动注册
fan_major=register_dev("fan");
//上交目录
fan_cls=class_t("myfan");
// 向上提交设备节点信息
fan_dev = device_create(fan_cls, NULL, MKDEV(fan_major, 0), NULL, "myfan0");
if (IS_ERR(fan_dev))
{
printk("向上提交设备节点信息失败\n");
return -PTR_ERR(fan_dev);
}
printk("向上提交设备节点成功\n");
// 寄存器映射以及初始化
fan_init();
return 0;
}
static void __exit mycdev_exit(void)
{
// 取消地址映射
iounmap(vir_rcc);
//销毁节点信息
// 注销字符设备驱动
unregister_chrdev(fan_major, "fan");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
horse_driver
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"
int house_major;
char kbuf[128] = {0};
gpio_t *vir_house;
unsigned int *vir_rcc;
struct class *house_cls;
struct device *house_dev;
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 HOUSE_ON:
vir_house->ODR |= (0x1 << 6);
break;
case HOUSE_OFF:
vir_house->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 house_init(void) //PF6
{
vir_house = ioremap(PHY_HOUSE_ADDR,sizeof(gpio_t));
if (vir_house == NULL)
{
printk("ioremap filed:%d\n", __LINE__);
return -ENOMEM;
}
vir_house->MODER &= (~(3 << 12));
vir_house->MODER |= (1 << 12);
vir_house->ODR &= (~(1 << 6));
printk("马达初始化完成\n");
return 1;
}
int register_dev(char *name)
{
int major;
major = register_chrdev(0, name, &fops);
if (major < 0)
{
printk("%s设备驱动注册失败\n",name);
return major;
}
printk("%s设备驱动注册成功:%s_major=%d\n",name,name,major);
return major;
}
struct class* class_t(char* name)
{
struct class* cls;
// 向上提交目录
cls = class_create(THIS_MODULE, name);
if (IS_ERR(cls))
{
printk("向上提交%s目录失败\n",name);
return NULL;
}
printk("向上提交%s目录信息成功\n",name);
return cls;
}
static int __init mycdev_init(void)
{
vir_rcc = ioremap(PHY_RCC_ADDR, 4);
if (vir_rcc == NULL)
{
printk("ioremap filed:%d\n", __LINE__);
return -ENOMEM;
}
printk("物理地址映射成功\n");
// 寄存器的初始化
// rcc
(*vir_rcc) |= (3 << 4);
(*vir_rcc) |= (0x1 << 2);
// 字符设备驱动注册
house_major=register_dev("house");
//上交目录
house_cls=class_t("myhouse");
// 向上提交设备节点信息
house_dev = device_create(house_cls, NULL, MKDEV(house_major, 0), NULL, "myhouse0");
if (IS_ERR(house_dev))
{
printk("向上提交设备节点信息失败\n");
return -PTR_ERR(house_dev);
}
printk("向上提交设备节点成功\n");
// 寄存器映射以及初始化
house_init();
return 0;
}
static void __exit mycdev_exit(void)
{
// 取消地址映射
iounmap(vir_rcc);
//销毁节点信息
// 注销字符设备驱动
unregister_chrdev(house_major, "house");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
main.c
#include<stdlib.h>
#include<stdio.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;
int b;
int fd_led=open("/dev/myled0",O_RDWR);
int fd_beep=open("/dev/mybeep0",O_RDWR);
int fd_fan=open("/dev/myfan0",O_RDWR);
int fd_house=open("/dev/myhouse0",O_RDWR);
int choose;
while(1)
{
printf("请输入选择的设备 LED->1 BEEP->2 FAN->3 HOSUE->4\n");
scanf("%d",&choose);
switch(choose)
{
case 1:
printf("请输入指令\n");
printf("0(关灯) 1(开灯)\n");
printf("请输入>");
scanf("%d",&a);
printf("请选择哪个灯\n");
scanf("%d",&b);
switch(a)
{
case 1:
ioctl(fd_led,LED_ON,b);//开灯
break;
case 0:
ioctl(fd_led,LED_OFF,b);
break;
}
break;
case 2:
printf("请输入指令 0 叫 1 不叫\n");
scanf("%d",&a);
switch(a)
{
case 0:
ioctl(fd_beep,BEEP_ON);
break;
case 1:
ioctl(fd_beep,BEEP_OFF);
break;
}
break;
case 3:
printf("请输入指令 0 转 1 不转\n");
scanf("%d",&a);
switch(a)
{
case 0:
ioctl(fd_fan,FAN_ON);
break;
case 1:
ioctl(fd_fan,FAN_OFF);
break;
}
break;
case 4:
printf("请输入指令 0 震动 1 不震\n");
scanf("%d",&a);
switch(a)
{
case 0:
ioctl(fd_house,HOUSE_ON);
break;
case 1:
ioctl(fd_house,HOUSE_OFF);
break;
}
break;
}
}
close(fd_led);
close(fd_beep);
close(fd_fan);
close(fd_house);
return 0;
}