#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include<linux/io.h>
#include <linux/device.h>
#include "led.h"
int major;
char kbuf[256] = {0};
unsigned int *vir_GPIOE;
unsigned int *vir_GPIOF;
unsigned int *vir_Rcc;
GPIO_TypeDef* GPIOE_Init;
GPIO_TypeDef* GPIOF_Init;
struct class* cls;
struct device* dev;
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%d\n",__func__,__LINE__);
return 0;
}
ssize_t mycdev_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 filad\n");
}
return 0;
}
ssize_t mycdev_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 filad\n");
}
switch(kbuf[0])
{
case '0' :
if(kbuf[1] == '1')
{
GPIOE_Init->ODR |= (0x01 << 10);
}else
{
GPIOE_Init->ODR &= (~(0x01 << 10));
}
break;
case '1' :
if(kbuf[1] == '1')
{
GPIOE_Init->ODR |= (0x01 << 8);
}else
{
GPIOE_Init->ODR &= (~(0x01 << 8));
}
break;
case '2' :
if(kbuf[1] == '1')
{
GPIOF_Init->ODR |= (0x01 << 10);
}else
{
GPIOF_Init->ODR &= (~(0x01 << 10));
}
break;
}
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%d\n",__func__,__LINE__);
return 0;
}
struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.release = mycdev_close
};
static int __init mycdev_init(void)
{
int i=0;
major = register_chrdev(0,"mychrdev",&fops);
if(major < 0)
{
printk("字符设备注册失败\n");
return major;
}
printk("字符设备注册完成 major:%d\n",major);
vir_GPIOE = ioremap(GPIOE,4);
if(vir_GPIOE == NULL)
{
printk("地址映射失败:GPIOE!\n");
return -EFAULT;
}
GPIOE_Init = (GPIO_TypeDef*)vir_GPIOE;
vir_GPIOF = ioremap(GPIOF,4);
if(vir_GPIOF == NULL)
{
printk("地址映射失败:GPIOF!\n");
return -EFAULT;
}
GPIOF_Init = (GPIO_TypeDef*)vir_GPIOF;
vir_Rcc = ioremap(RCC_MP_AHB4_ENSETR,4);
if(vir_Rcc == NULL)
{
printk("地址映射失败:RCC_MP_AHB4_ENSETR!\n");
return -EFAULT;
}
cls = class_create(THIS_MODULE,"mycdev");
if(IS_ERR(cls))
{
printk("目录提交失败!\n");
return -PTR_ERR(cls);
}
for(i=0;i<3;i++)
{
dev = device_create(cls,NULL,MKDEV(major,i),NULL,"mycdev%d",i);
if(IS_ERR(dev))
{
printk("设备信息提交失败 devID:%d\n",i);
return -PTR_ERR(dev);
}
}
printk("设备信息提交完成 devID:%d\n",i);
(*vir_Rcc) |= (3 << 4);
GPIOE_Init->MODER &= (~(0x03 << 20));
GPIOE_Init->MODER |= (0x01 << 20);
GPIOE_Init->MODER &= (~(0x03 << 16));
GPIOE_Init->MODER |= (0x01 << 16);
GPIOF_Init->MODER &= (~(0x03 << 20));
GPIOF_Init->MODER |= (0x01 << 20);
GPIOE_Init->ODR &= (~(0x01 << 10));
GPIOE_Init->ODR |= (0x01 << 10);
GPIOF_Init->ODR &= (~(0x01 << 10));
GPIOF_Init->ODR |= (0x01 << 10);
GPIOE_Init->ODR &= (~(0x01 << 8));
GPIOE_Init->ODR |= (0x01 << 8);
return 0;
}
static void __exit mycdev_exit(void)
{
int i=0;
iounmap(vir_GPIOE);
iounmap(vir_GPIOF);
iounmap(vir_Rcc);
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 <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char buf[128] = {0};
int fd0 = open("/dev/mycdev0", O_RDWR);
if(fd0 < 0) {
printf("设备0读取失败\n");
return -1;
}
while(1)
{
printf("请输入LED指令>>\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
write(fd0, buf, sizeof(buf));
}
close(fd0);
return 0;
}