mycdev.c
#include
#include
#include
#include
#include
#include "head.h"
unsigned int major;
char kbuf[128]={0};
unsigned int *vir_moder;
unsigned int *vir_odr;
unsigned int *vir_rcc;
//分装驱动中的操作方法
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)
{
int ret;
if(size > sizeof(kbuf))
{
size = sizeof(kbuf);
}
ret=copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy_to_user faild\n");
return ret;
}
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_write(struct file *file,const char *ubuf,size_t size,loff_t *lof)
{
int ret;
if(size > sizeof(kbuf))
{
size = sizeof(kbuf);
}
ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_from_user faild\n");
return ret;
}
if(kbuf[0] == '1')//开灯
{
(*vir_odr) |= (1 << 10);
}
else if(kbuf[0] == '0')//关灯
{
(*vir_odr) &= (~(1 << 10));
}
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
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,
.read=mycdev_read,
.write=mycdev_write,
.release=mycdev_close,
};
static int __init mycdev_init(void)
{
//字符设备驱动注册
major=register_chrdev(0,"mycdev",&fops);
if(major < 0)
{
printk("字符设备驱动失败\n");
return major;
}
printk("字符设备驱动成功,maj,major=%d\n",major);
//对led1相关的寄存器进行映射
vir_moder = ioremap(PHY_LED1_MODER,4);
if(vir_moder == NULL)
{
printk("寄存器映射失败%d\n",__LINE__);
return -ENOMEM;
}
vir_odr = ioremap(PHY_LED1_ODR,4);
if(vir_odr == NULL)
{
printk("寄存器映射失败%d\n",__LINE__);
return -ENOMEM;
}
vir_rcc = ioremap(PHY_LED1_RCC,4);
if(vir_rcc == NULL)
{
printk("寄存器映射失败%d\n",__LINE__);
return -ENOMEM;
}
printk("寄存器映射成功\n");
//寄存器初始化
(*vir_moder) &= (~(3 << 20));//moder寄存器设置为输出模式01
(*vir_moder) |= (1 << 20);
(*vir_odr) &= (~(1 << 10));
(*vir_rcc) |= (1 << 4);
printk("寄存器初始化成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
//取消内存映射
iounmap(vir_moder);
iounmap(vir_odr);
iounmap(vir_rcc);
//字符设备驱动注销
unregister_chrdev(major,"mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
demo.c
#include
#include
#include
#include
#include
#include
#include
#include "head.h"
int main(int argc, char const *argv[])
{
char buf[128] = {0};
int fd = open("/dev/mycdev", O_RDWR);
if(fd < 0)
{
printf("设备文件打开失败\n");
exit(-1);
}
while(1)
{
printf("请输入要进行的操作:1(开灯),0(关灯)>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
write(fd,buf,sizeof(buf));
}
close(fd);
return 0;
}
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define PHY_LED1_MODER 0x50006000
#define PHY_LED1_ODR 0x50006014
#define PHY_LED1_RCC 0x50000A28
#endif
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
typedef struct
{
unsigned int MODER;
unsigned int OTYPER;
unsigned int OSPEEDER;
unsigned int PUPDR;
unsigned int IDR;
unsigned int ODR;
unsigned int BSRR;
unsigned int LCKR;
unsigned int AFRL;
unsigned int AFRH;
unsigned int BRR;
}gpio_t;
#define PHY_LED_RCC 0x50000A28
#define PHY_LED_MODER_E 0x50006000
#define PHY_LED_MODER_F 0x50007000
#endif
mycdev.c
#include
#include
#include
#include
#include
#include "head.h"
unsigned int major;
char kbuf[128]={0};
gpio_t *vir_led1;
gpio_t *vir_led2;
gpio_t *vir_led3;
unsigned int *vir_rcc;
//封装驱动的操作方法
int mycdev_open(struct inode *inode,struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *lof)
{
int ret;
if(size < sizeof(kbuf))
{
size = sizeof(kbuf);
}
ret = copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy_to_user faild %d\n",__LINE__);
return ret;
}
printk("%s:%s:%d",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *lof)
{
int ret;
if(size < sizeof(kbuf))
{
size = sizeof(kbuf);
}
ret = copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_from_user faild %d\n",__LINE__);
return ret;
}
printk("%s:%s:%d",__FILE__,__func__,__LINE__);
if(kbuf[0] == '1')//控制led1
{
if(kbuf[1] == '1')//点亮led1---->PE10
{
(vir_led1->ODR) |= (1 << 10);
}
else if(kbuf[1] == '0')//关闭led1
{
(vir_led1->ODR) &= (~(1 << 10));
}
}
else if(kbuf[0] == '2')
{
if(kbuf[1] == '1')//点亮led2---->PF10
{
(vir_led2->ODR) |= (1 << 10);
}
else if(kbuf[1] == '0')//关闭led2
{
(vir_led2->ODR) &= (~(1 << 10));
}
}
else if(kbuf[0] == '3')
{
if(kbuf[1] == '1')//点亮led3---->PE8
{
(vir_led3->ODR) |= (1 << 8);
}
else if(kbuf[1] == '0')//关闭led3
{
(vir_led3->ODR) &= (~(1 << 8));
}
}
return 0;
}
struct file_operations fops={
.open=mycdev_open,
.read=mycdev_read,
.write=mycdev_write,
.release=mycdev_close,
};
int all_led_init(void)
{
//1.字符设备驱动的注册
major = register_chrdev(0,"mycdev",&fops);
if(major < 0)
{
printk("字符设备驱动注册失败%d\n",__LINE__);
return major;
}
printk("字符设备驱动注册成功 major=%d\n",major);
//2.进行LED相关寄存器的映射
//led1----->PE10
//led2----->PF10
//led3----->PE8
vir_led1 = ioremap(PHY_LED_MODER_E,sizeof(gpio_t));
vir_led2 = ioremap(PHY_LED_MODER_F,sizeof(gpio_t));
vir_led3 = ioremap(PHY_LED_MODER_E,sizeof(gpio_t));
if(vir_led1 == NULL || vir_led2 == NULL || vir_led3 == NULL)
{
printk("led寄存器映射失败!%d\n",__LINE__);
return -ENOMEM;
}
printk("MODER寄存器映射成功!%d\n",__LINE__);
vir_rcc = ioremap(PHY_LED_RCC,4);
if(vir_rcc == NULL)
{
printk("rcc寄存器映射失败!%d\n",__LINE__);
return -ENOMEM;
}
printk("rcc寄存器映射成功!%d\n",__LINE__);
//3.寄存器的初始化
//RCC寄存器初始化
(*vir_rcc) |= (3 << 4);
//GPIO寄存器初始化
(vir_led1->MODER) &= (~(3 << 20));
(vir_led1->MODER) |=(1 << 20);
(vir_led2->MODER) &= (~(3 << 20));
(vir_led2->MODER) |=(1 << 20);
(vir_led3->MODER) &= (~(3 << 16));
(vir_led3->MODER) |=(1 << 16);
(vir_led1->ODR) &= (~(1 << 10));
(vir_led2->ODR) &= (~(1 << 10));
(vir_led3->ODR) &= (~(1 << 8));
printk("寄存器初始化成功!%d\n",__LINE__);
return 0;
}
static int __init mycdev_init(void)
{
if(all_led_init())
{
printk("led灯初始化失败!\n");
}
printk("led灯初始化成功!\n");
return 0;
}
static void __exit mycdev_exit(void)
{
//取消内存映射
iounmap(vir_led1);
iounmap(vir_led2);
iounmap(vir_led3);
iounmap(vir_rcc);
//字符设备驱动的注销
unregister_chrdev(major,"mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
demo.c
#include
#include
#include
#include
#include
#include
#include
#include "head.h"
int main(int argc, char const *argv[])
{
char buf[128] = {0};
int fd = open("/dev/mycdev", O_RDWR);
if(fd < 0)
{
printf("设备文件打开失败\n");
exit(-1);
}
while(1)
{
//led1----->PE10
//led2----->PF10
//led3----->PE8
printf("请输入要进行的操作:例如(点亮led1输入11,关闭为10)>",buf);
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
write(fd,buf,sizeof(buf));
}
close(fd);
return 0;
}