头文件
#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_LED_GPIOE 0x50006000
#define PHY_LED_GPIOF 0x50007000
#define PHY_LED_RCC 0X50000A28
#define LED_ON _IOW('1',1,int)
#define LED_OFF _IOW('0',0,int)
#endif
驱动文件
#include
#include
#include
#include
#include "head.h"
#include
#include
#include
#include
unsigned int major = 0;
unsigned int minor = 0;
char kbuf[128] = {0};
gpio_t *gpioe10;
gpio_t *gpiof10;
gpio_t *gpioe8;
unsigned int *vir_rcc;
struct class *clz;
struct device *dev;
struct cdev *cdev;
dev_t devno;
int myDev_open(struct inode *major, struct file *file)
{
printk("%s:%s,%d\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t myDev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
int ret;
ret = copy_from_user(kbuf, ubuf, size);
if (ret)
{
printk("copy_from_user failed!\n");
return ret;
}
return 0;
}
ssize_t myDev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
int ret;
ret = copy_to_user(ubuf, kbuf, size);
if (ret)
{
printk("copy_to_user failed!\n");
return ret;
}
return 0;
}
int myDev_close(struct inode *major, struct file *file)
{
printk("myDev_close\n");
return 0;
}
long myDev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
printk("%s:%s,%d\n", __FILE__, __func__, __LINE__);
switch (cmd)
{
case LED_ON:
switch (arg)
{
case 1:
gpioe10->ODR |= (0x1 << 10);
break;
case 2:
gpiof10->ODR |= (0x1 << 10);
break;
case 3:
gpioe8->ODR |= (0x1 << 8);
break;
default:
break;
}
break;
case LED_OFF:
switch (arg)
{
case 1:
gpioe10->ODR &= (~(0x1 << 10));
break;
case 2:
gpiof10->ODR &= (~(0x1 << 10));
break;
case 3:
gpioe8->ODR &= (~(0x1 << 8));
break;
default:
break;
}
break;
default:
break;
}
return 0;
}
struct file_operations fop = {
.open = myDev_open,
.release = myDev_close,
.read = myDev_read,
.write = myDev_write,
.unlocked_ioctl = myDev_ioctl};
int myLedInit(void)
{
gpioe10 = ioremap(PHY_LED_GPIOE, sizeof(gpio_t));
if (gpioe10 == NULL)
{
printk("内存映射失败%d\n", __LINE__);
return -EFAULT;
}
gpiof10 = ioremap(PHY_LED_GPIOF, sizeof(gpio_t));
if (gpiof10 == NULL)
{
printk("内存映射失败%d\n", __LINE__);
return -EFAULT;
}
gpioe8 = gpioe10;
vir_rcc = ioremap(PHY_LED_RCC, 4);
if (vir_rcc == NULL)
{
printk("内存映射失败%d\n", __LINE__);
return -EFAULT;
}
// pe10
gpioe10->MODER &= (~(0x3 << 20));
gpioe10->MODER |= (0x1 << 20);
// pe8
gpioe8->MODER &= (~(0x3 << 16));
gpioe8->MODER |= (0x1 << 16);
// pf10
gpiof10->MODER &= (~(0x3 << 20));
gpiof10->MODER |= (0x1 << 20);
(*vir_rcc) |= (0x1 << 4);
(*vir_rcc) |= (0x1 << 5);
(gpioe10->ODR) &= (~(0x1 << 10));
gpioe8->ODR &= (~(0x1 << 8));
gpiof10->ODR &= (~(0x1 << 10));
return 0;
}
static int __init mydev_init(void)
{
int i = 0;
int ret;
cdev = cdev_alloc();
if (cdev == NULL)
{
printk("申请设备对象失败\n");
return -EFAULT;
}
printk("申请设备对象成功\n");
cdev_init(cdev, &fop);
if (major > 0)
{
// 静态申请
ret = register_chrdev_region(MKDEV(major, minor), 3, "mydev");
if (ret)
{
printk("静态指定设备号失败\n");
goto OUT2;
}
}
else
{
ret = alloc_chrdev_region(&devno, minor, 3, "mydev");
if (ret)
{
printk("动态申请设备号失败\n");
goto OUT2;
}
major = MAJOR(devno);
minor = MINOR(devno);
}
printk("申请设备号成功\n");
ret = cdev_add(cdev, MKDEV(major, minor), 3);
if (ret)
{
printk("注册字符设备驱动对象失败\n");
goto OUT3;
}
printk("设备注册成功!major=%d\n", major);
clz = class_create(THIS_MODULE, "mydev");
if (IS_ERR(clz))
{
printk("向上提交目录失败\n");
ret = -PTR_ERR(clz);
goto OUT4;
}
printk("向上提交目录成功\n");
for (i = 0; i < 3; i++)
{
dev = device_create(clz, NULL, MKDEV(major, i), NULL, "mydev%d", i);
if (IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
ret = -PTR_ERR(clz);
goto OUT5;
}
}
printk("向上提交设备节点信息成功\n");
myLedInit();
return 0;
OUT5:
for (--i; i >= 0; i--)
{
device_destroy(clz, MKDEV(major, i));
}
class_destroy(clz);
OUT4:
cdev_del(cdev);
OUT3:
unregister_chrdev_region(MKDEV(major, minor), 0);
OUT2:
kfree(cdev);
OUT1:
return ret;
}
static void __exit mydev_exit(void)
{
printk("mydev_exit\n");
iounmap(gpioe10);
iounmap(vir_rcc);
iounmap(gpiof10);
int i;
for (i = 0; i < 3; i++)
{
device_destroy(clz, MKDEV(major, i));
}
class_destroy(clz);
cdev_del(cdev);
unregister_chrdev_region(MKDEV(major, minor), 3);
kfree(cdev);
}
module_init(mydev_init);
module_exit(mydev_exit);
MODULE_LICENSE("GPL");
测试文件
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "head.h"
int main()
{
char buf[128];
int fd = open("/dev/mydev0", O_RDWR);
if (fd < 0)
{
perror("open failed!\n");
exit(-1);
}
int a, b;
printf("打开设备成功!\n");
while (1)
{
printf("请输入led灯的逻辑》1(开灯)0(关灯)\n");
scanf("%d", &a);
printf("请输入控制的灯:1(LED1) 2(LED2) 3(LED3)\n");
scanf("%d", &b);
switch (a)
{
case 1:
ioctl(fd, LED_ON, b);
break;
case 0:
ioctl(fd, LED_OFF, b);
break;
default:
break;
}
}
close(fd);
return 0;
}