点灯
现象
驱动点灯
.h
#ifndef _MYLED_H_
#define _MYLED_H_
typedef struct {
volatile unsigned int MODER; // 0x00
volatile unsigned int OTYPER; // 0x04
volatile unsigned int OSPEEDR; // 0x08
volatile unsigned int PUPDR; // 0x0C
volatile unsigned int IDR; // 0x10
volatile unsigned int ODR; // 0x14
volatile unsigned int BSRR; // 0x18
volatile unsigned int LCKR; // 0x1C
volatile unsigned int AFRL; // 0x20
volatile unsigned int AFRH; // 0x24
volatile unsigned int BRR; // 0x28
volatile unsigned int res;
volatile unsigned int SECCFGR; // 0x30
}gpio_t;
#define GPIOE (0x50006000)
#define GPIOF (0x50007000)
#define RCC_MP_AHB4ENSETR 0x50000A28
#endif
demo.c
#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[100] = "";
unsigned int* rcc_virt = NULL;
gpio_t* gpioe_virt = NULL;
gpio_t* gpiof_virt = NULL;
#define LED1_ON (gpioe_virt->ODR |= (0x1 << 10))
#define LED1_OFF (gpioe_virt->ODR &= (~(0x1 << 10)))
#define LED2_ON (gpiof_virt->ODR |= (0x1 << 10))
#define LED2_OFF (gpiof_virt->ODR &= (~(0x1 << 10)))
#define LED3_ON (gpioe_virt->ODR |= (0x1 << 8))
#define LED3_OFF (gpioe_virt->ODR &= (~(0x1 << 8)))
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 is error\n");
return -EIO;
}
printk("kbuf = %s\n",kbuf);
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 is error\n");
return -EIO;
}
if(kbuf[0] ==1)
{
LED1_ON;
LED2_OFF;
LED3_OFF;
}else if(kbuf[0] ==2)
{
LED1_OFF;
LED2_ON;
LED3_OFF;
}else if(kbuf[0] ==3)
{
LED1_OFF;
LED2_OFF;
LED3_ON;
}
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,
.write = myled_write,
.read = myled_read,
.release = myled_close,
};
//入口函数
static int __init demo_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_MP_AHB4ENSETR,4);
if(rcc_virt == NULL)
{
printk("ioremap rcc is error\n");
return -EIO;
}
//GPIOE寄存器地址映射
gpioe_virt = ioremap(GPIOE,sizeof(gpio_t));
if(rcc_virt == NULL)
{
printk("ioremap gpioe moder is error\n");
return -EIO;
}
//GPIOF寄存器地址映射
gpiof_virt = ioremap(GPIOF,sizeof(gpio_t));
if(rcc_virt == NULL)
{
printk("ioremap gpioe odr is error\n");
return -EIO;
}
//LED1灯初始化
*rcc_virt |= (0x1 <<4);//使能GPIOE组控制器[4] = 1
gpioe_virt->MODER &= (~(0x3 << 20));
gpioe_virt->MODER |= (0x1 << 20);//设置PE10引脚为输出模式
gpioe_virt->ODR |= (0x1 << 10);//设置PE10引脚输出高电平,LED1亮
//LED2灯初始化
*rcc_virt |= (0x1 <<5);//使能GPIOF组控制器[4] = 1
gpiof_virt->MODER &= (~(0x3 << 20));
gpiof_virt->MODER |= (0x1 << 20);//设置PF10引脚为输出模式
gpiof_virt->ODR |= (0x1 << 10);//设置PF10引脚输出高电平,LED1亮
//LED3灯初始化
*rcc_virt |= (0x1 <<4);//使能GPIOE组控制器[4] = 1
gpioe_virt->MODER &= (~(0x3 << 16));
gpioe_virt->MODER |= (0x1 << 16);//设置PE8引脚为输出模式
gpioe_virt->ODR |= (0x1 << 8);//设置PE8引脚输出高电平,LED1亮
return 0;
}
//出口函数
static void __exit demo_exit(void)
{
iounmap(gpioe_virt);
iounmap(gpiof_virt);
iounmap(rcc_virt);
unregister_chrdev(major,CNAME);
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");
test.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char buf[128] = "";
int fd = -1;
fd = open("/dev/myled",O_RDWR);//打开
if(fd == -1)
{
perror("open is error");
exit(1);
}
//buf[0] = 0 灯熄灭 buf[0] = 1 灯亮
while (1)
{
buf[0] = 1;
write(fd,buf,sizeof(buf));//写
sleep(1);
buf[0] = 2;
write(fd,buf,sizeof(buf));//写
sleep(1);
buf[0] = 3;
write(fd,buf,sizeof(buf));//写
sleep(1);
}
close(fd);//关闭
return 0;
}