LED灯驱动编写实验
驱动文件
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include "head.h"
gpio_t *vir_gpioe;
gpio_t *vir_gpiof;
unsigned int *vir_rcc;
char kbuf[256] = {};
//定义一个变量保存主设备号
unsigned int major;
//封装操作方法
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
unsigned long ret;
ret = copy_to_user(ubuf, kbuf, size);
if(ret)
{
printk("copy_to_user filed\n");
return -EIO;
}
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
unsigned long ret;
ret = copy_from_user(kbuf, ubuf, size);
if(ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
if(strcmp(kbuf, "led1on") == 0)
vir_gpioe->ODR |= (0x1 << 10);
else if(strcmp(kbuf, "led1off") == 0)
vir_gpioe->ODR &= (~(0x1 << 10));
else if(strcmp(kbuf, "led2on") == 0)
vir_gpiof->ODR |= (0x1 << 10);
else if(strcmp(kbuf, "led2off") == 0)
vir_gpiof->ODR &= (~(0x1 << 10));
else if(strcmp(kbuf, "led3on") == 0)
vir_gpioe->ODR |= (0x1 << 8);
else if(strcmp(kbuf, "led3off") == 0)
vir_gpioe->ODR &= (~(0x1 << 8));
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
//定义一个操作方法结构体变量并且初始化
struct file_operations fops={
.open=mycdev_open,
.release=mycdev_close,
.read=mycdev_read,
.write=mycdev_write,
};
static int __init mycdev_init(void)
{
//注册字符设备驱动
major=register_chrdev(0,"mychrdev",&fops);
if(major<0)
{
printk("注册字符设备驱动失败\n");
return major;
}
printk("注册字符设备驱动成功major=%d\n",major);
//进行寄存器地址的映射
vir_rcc = ioremap(RCC, 4);
if(vir_rcc == NULL)
{
printk("映射物理内存失败%d\n",__LINE__);
return -EFAULT;
}
vir_gpioe = ioremap(GPIOE, 24);
if(vir_gpioe == NULL)
{
printk("映射物理内存失败%d\n",__LINE__);
return -EFAULT;
}
vir_gpiof = ioremap(GPIOF, 24);
if(vir_gpiof == NULL)
{
printk("映射物理内存失败%d\n",__LINE__);
return -EFAULT;
}
printk("映射物理内存成功\n");
//硬件寄存器的初始化
(*vir_rcc) |= (0X3 << 4); //rcc使能
(vir_gpioe->MODER) &= (~(0X3 << 20)); //设置PE10为输出
(vir_gpioe->MODER) |= (0X1 << 20);
(vir_gpioe->ODR) &= (~(0X1 << 10)); //输出低电平
(vir_gpiof->MODER) &= (~(0X3 << 20)); //设置PF10
(vir_gpiof->MODER) |= (0X1 << 20);
(vir_gpiof->ODR) &= (~(0X1 << 10));
(vir_gpioe->MODER) &= (~(0X3 << 16)); //设置PE8
(vir_gpioe->MODER) |= (0X1 << 16);
(vir_gpioe->ODR) &= (~(0X1 << 8));
printk("硬件寄存器初始化成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
//注销字符设备驱动
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 <unistd.h>
#include <string.h>
int main(int argc,const char * argv[])
{
int fd = open("/dev/mychrdev", O_RDWR);
if(fd < 0)
{
perror("open");
return -1;
}
while(1)
{
char buf[256] = {0};
printf("请输入(led(n) on/off)>>>");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = '\0';
write(fd, buf, sizeof(buf));
}
close(fd);
return 0;
}
头文件
#ifndef __HEAD_H__
#define __HEAD_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
} gpio_t;
#define GPIOE 0x50006000
#define GPIOF 0x50007000
#define RCC 0X50000A28
#endif