头文件
c
#ifndef __HEAD_H__
#define __HEAD_H__
#define PHY_LED_MODER 0X50006000
#define PHY_LED_ODR 0X50006014
#define PHY_LED_RCC 0X50000A28
#define PHY_LED_FMODER 0X50007000
#define PHY_LED_FODR 0X50007014
#endif
驱动代码
c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include "head.h"
#include <linux/io.h>
#include <linux/device.h>
unsigned int major;
char kbuf[128] = {0};
unsigned int *vir_moder;
unsigned int *vir_fmoder;
unsigned int *vir_rcc;
unsigned int *vir_odr;
unsigned int *vir_fodr;
struct class *clz;
struct device *dev;
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;
}
switch (kbuf[0])
{
case '1'://pe10
if (kbuf[1] == '1')
{
(*vir_odr) |= (0x1 << 10);
}
else if (kbuf[1] == '0')
{
(*vir_odr) &= (~(0x1 << 10));
}
break;
case '2'://pe8
if (kbuf[1] == '1')
{
(*vir_fodr) |= (0x1 << 10);
}
else if (kbuf[1] == '0')
{
(*vir_fodr) &= (~(0x1 << 10));
}
break;
case '3'://pf10
if (kbuf[1] == '1')
{
(*vir_odr) |= (0x1 << 8);
}
else if (kbuf[1] == '0')
{
(*vir_odr) &= (~(0x1 << 8));
}
break;
default:
break;
}
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;
}
struct file_operations fop = {
.open = myDev_open,
.release = myDev_close,
.read = myDev_read,
.write = myDev_write};
static int __init mydev_init(void)
{
int i = 0;
major = register_chrdev(0, "myCharDev", &fop);
if (major < 0)
{
printk("设备注册失败\n");
return major;
}
printk("设备注册成功!major=%d\n", major);
vir_moder = ioremap(PHY_LED_MODER, 4);
if (vir_moder == NULL)
{
printk("内存映射失败%d\n", __LINE__);
return -EFAULT;
}
vir_fmoder = ioremap(PHY_LED_FMODER, 4);
if (vir_fmoder == NULL)
{
printk("内存映射失败%d\n", __LINE__);
return -EFAULT;
}
vir_rcc = ioremap(PHY_LED_RCC, 4);
if (vir_rcc == NULL)
{
printk("内存映射失败%d\n", __LINE__);
return -EFAULT;
}
vir_odr = ioremap(PHY_LED_ODR, 4);
if (vir_odr == NULL)
{
printk("内存映射失败%d\n", __LINE__);
return -EFAULT;
}
vir_fodr = ioremap(PHY_LED_FODR, 4);
if (vir_fodr == NULL)
{
printk("内存映射失败%d\n", __LINE__);
return -EFAULT;
}
// pe10
(*vir_moder) &= (~(0x3 << 20));
(*vir_moder) |= (0x1 << 20);
// pe8
(*vir_moder) &= (~(0x3 << 16));
(*vir_moder) |= (0x1 << 16);
// pf10
(*vir_fmoder) &= (~(0x3 << 20));
(*vir_fmoder) |= (0x1 << 20);
(*vir_rcc) |= (0x1 << 4);
(*vir_rcc) |= (0x1 << 5);
(*vir_odr) &= (~(0x1 << 10));
(*vir_odr) &= (~(0x1 << 8));
(*vir_fodr) &= (~(0x1 << 10));
clz = class_create(THIS_MODULE, "mydev");
if (IS_ERR(clz))
{
printk("提交目录失败\n");
return -PTR_ERR(clz);
}
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");
return -PTR_ERR(clz);
}
}
printk("提交设备节点信息成功\n");
return 0;
}
static void __exit mydev_exit(void)
{
printk("mydev_exit\n");
iounmap(vir_moder);
iounmap(vir_rcc);
iounmap(vir_odr);
iounmap(vir_fmoder);
iounmap(vir_fodr);
int i;
for (i = 0; i < 3; i++)
{
device_destroy(clz, MKDEV(major, i));
}
class_destroy(clz);
unregister_chrdev(major, "myCharDev");
}
module_init(mydev_init);
module_exit(mydev_exit);
MODULE_LICENSE("GPL");
测试代码
c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
char buf[128];
int fd = open("/dev/mydev0", O_RDWR);
if (fd < 0)
{
perror("open failed!\n");
exit(-1);
}
printf("打开设备成功!\n");
while (1)
{
printf("请输入led灯的逻辑》\n");
printf("11:led1亮\n");
printf("10:led1灭\n");
printf("21:led2亮\n");
printf("20:led2灭\n");
printf("31:led3亮\n");
printf("30:led3灭\n");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = '\0';
write(fd, buf, sizeof(buf));
}
close(fd);
return 0;
}