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; //定义rcc虚拟地址
gpio_t* gpioe_virt = NULL; //定义gpioe组虚拟地址
gpio_t* gpiof_virt = NULL; //定义gpiof组虚拟地址
#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_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;
}
//kbuf[0] = 0 kbuf[0] = 1 kbuf[0] = 2 操作哪一盏灯
// kbuf[1] = 0 灯熄灭 kbuf[1] = 1 灯点亮
switch(kbuf[0])
{
case LED1: //0
kbuf[1] == 1 ? LED1_ON : LED1_OFF;
break;
case LED2: //1
kbuf[1] == 1 ? LED2_ON : LED2_OFF;
break;
case LED3: //2
kbuf[1] == 1 ? LED3_ON : LED3_OFF;
break;
}
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,
.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(gpioe_virt == NULL){
printk("ioremap gpioe is error\n");
return -EIO;
}
//GPIOF组寄存器地址映射
gpiof_virt = ioremap(GPIOF,sizeof(gpio_t));
if(gpiof_virt == NULL){
printk("ioremap gpiof is error\n");
return -EIO;
}
//LED1灯初始化 PE10
*rcc_virt |= (0x1 << 4);//使能GPIOE组控制器[4] = 1
gpioe_virt->MODER &= (~(0x3 << 20));//设置PE10引脚为输出模式 [21:20] = 01
gpioe_virt->MODER |= (0x1 << 20);
gpioe_virt->ODR |= (0x1 << 10); //设置PE10引脚输出低电平,LED1熄灭 [10] = 0
//LED2灯初始化 PF10
*rcc_virt |= (0x1 << 5);//使能GPIOF组控制器[5] = 1
gpiof_virt->MODER &= (~(0x3 << 20));//设置PF10引脚为输出模式 [21:20] = 01
gpiof_virt->MODER |= (0x1 << 20);
gpiof_virt->ODR |= (0x1 << 10); //设置PF10引脚输出低电平,LED2熄灭 [10] = 0
//LED3灯初始化 PE8
gpioe_virt->MODER &= (~(0x3 << 16));//设置PE8引脚为输出模式 [17:16] = 01
gpioe_virt->MODER |= (0x1 << 16);
gpioe_virt->ODR |= (0x1 << 8); //设置PE8引脚输出低电平,LED1熄灭 [8] = 0
return 0;
}
//出口
static void __exit demo_exit(void)
{
iounmap(gpioe_virt);//取消GPIOE组寄存器地址映射
iounmap(gpiof_virt);//取消GPIOF寄存器地址映射
iounmap(rcc_virt);//取消rcc寄存器地址映射
//注销字符设备驱动
unregister_chrdev(major,CNAME);
}
module_init(demo_init); //指定入口地址
module_exit(demo_exit); //指定出口地址
MODULE_LICENSE("GPL"); //许可证