底层:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "myled.h"
#define CNAME "myled"
unsigned int major = 0;
char kbuf[128] = "";
unsigned int* rcc_virt = NULL;
/*
unsigned int* gpioe_moder_virt = NULL;
unsigned int* gpioe_odr_virt = NULL;
*/
gpio_t* gpioe = NULL;
gpio_t* gpiof = NULL;
int myled_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t myled_read(struct file *file, char __user *ubuf, size_t size, loff_t *loffs)
{
int ret;
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
if(size > sizeof(kbuf)) size = sizeof(kbuf);
ret = copy_to_user(ubuf,kbuf,size);
if(ret){
printk("copy to user error\n");
return -EIO;
}
return size;
}
ssize_t myled_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loffs)
{
int ret;
//printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
if(size > sizeof(kbuf)) size = sizeof(kbuf);
ret = copy_from_user(kbuf,ubuf,size);
if(ret){
printk("copy from user error\n");
return -EIO;
}
printk("kernel kbuf[0] = %d,kbuf[1] = %d\n",kbuf[0],kbuf[1]);
switch (kbuf[0])
{
case 0:
if(kbuf[1] == 0)
gpioe->ODR &= (~(0x1 << 10));
else
gpioe->ODR |= (0x1 << 10);
break;
case 1:
if(kbuf[1] == 0)
gpiof->ODR &= (~(0x1 << 10));
else
gpiof->ODR |= (0x1 << 10);
break;
case 2:
if(kbuf[1] == 0)
gpioe->ODR &= (~(0x1 << 8));
else
gpioe->ODR |= (0x1 << 8);
}
return size;
}
int myled_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
const struct file_operations fops = {
.open = myled_open,
.read = myled_read,
.write = myled_write,
.release = myled_close,
};
//入口
static int __init mycdev_init(void)
{
//注册字符设备驱动
major = register_chrdev(0,CNAME,&fops);
if(major < 0)
{
printk("register chrdev is error\n");
return -EIO;
}
printk("major = %d\n",major);
//rcc寄存器地址映射
rcc_virt = ioremap(RCC_PHY_ADDR,4);
if(!rcc_virt)
{
printk("ioremap rcc is error\n");
return -EIO;
}
/*
//gpioe寄存器地址映射
gpioe_moder_virt = ioremap(GPIOE_MODER_PHY_ADDR,4);
if(!gpioe_moder_virt)
{
printk("ioremap gpioe_moder is error\n");
return -EIO;
}
//odr寄存器地址映射
gpioe_odr_virt = ioremap(GPIOE_ODR_PHY_ADDR,4);
if(!gpioe_odr_virt)
{
printk("ioremap gpioe_odr is error\n");
return -EIO;
}
*/
//地址映射
gpioe = ioremap(GPIOE,sizeof(gpio_t));
gpiof = ioremap(GPIOF,sizeof(gpio_t));
*rcc_virt |= (0x3 << 4);//设置GPIOE组时钟使能[4,5] = 1
//初始化led1
gpioe->MODER &= (~(0x3 << 20));//设置pe10引脚为输出模式[21:20] = 01
gpioe->MODER |= (0x1 << 20);
gpioe->ODR &= (~(0x1 << 10)); //设置pe10引脚输出低电平[10] = 0
//初始化led2
gpiof->MODER &= (~(0x3 << 20));
gpiof->MODER |= (0x1 << 20);
gpiof->ODR &= (~(0x1 << 10));
//初始化led3
gpioe->MODER &= (~(0x3 << 16));
gpioe->MODER |= (0x1 << 16);
gpioe->ODR &= (~(0x1 << 8));
return 0;
}
//出口
static void __exit mycdev_exit(void)
{
//注销字符设备驱动
unregister_chrdev(major,CNAME);
printk("remove major = %d\n",major);
//取消地址映射
iounmap(rcc_virt);
iounmap(gpioe);
iounmap(gpiof);
}
//入口地址
module_init(mycdev_init);
//出口地址
module_exit(mycdev_exit);
//遵循GPL协议
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>
int i = 0;
int main(int argc, char const *argv[])
{
char buf[128] = "";
int fd = -1;
//fgets(buf,sizeof(buf),stdin);
//buf[strlen(buf)-1] = 0;
fd = open("/dev/myled",O_RDWR);
if(fd == -1){
perror("open error\n");
exit(1);
}
while(1){
buf[0] = i%3;
if(i%3 == 0)
buf[1] = !buf[1];
write(fd, buf, sizeof(buf));
sleep(1);
i++;
}
//bzero(buf,sizeof(buf));
//read(fd, buf, sizeof(buf));
//printf("buf = %s\n",buf);
close(fd);
return 0;
}
结果:
![](https://img-blog.csdnimg.cn/direct/bdbc45f984534734a022918a61c09eb3.png)