头文件1.h
#ifndef __HEAD_H__
#define __HEAD_H__
//PE10
#define PHY_LED1_MODER 0x50006000
#define PHY_LED1_ODR 0x50006014
#define PHY_LED1_RCC 0x50000A28
//pf10
#define PHY_LED2_MODER 0x50007000
#define PHY_LED2_ODR 0x50007014
#define PHY_LED2_RCC 0x50000A28
//PE8
#define PHY_LED3_MODER 0x50006000
#define PHY_LED3_ODR 0x50006014
#define PHY_LED3_RCC 0x50000A28
#endif
test.c
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char const *argv[])
{
char buf[128] = {0};
int fd = open("/dev/mychrdev",O_RDWR);
if(fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while(1)
{
memset(buf,0,sizeof(buf));
printf("请输入控制命令:LEDX_ON(开灯)LEDX_OFF(关灯)>>");
fgets(buf,sizeof(buf),stdin);//从终端输入数据到buf
buf[strlen(buf)-1]='\0'; //将buf末尾的'\n'切换成'\0'
write(fd,buf,sizeof(buf));
}
close(fd);
return 0;
}
mychrdev.c
#include
#include
#include
#include
#include "1.h"
char kbuf[128]={0};
unsigned int major; //定义一个变量保存主设备号
unsigned int* vir_moder;
unsigned int* vir_odr;
unsigned int* vir_rcc;
unsigned int* vir2_moder;
unsigned int* vir2_odr;
unsigned int* vir2_rcc;
unsigned int* vir3_moder;
unsigned int* vir3_odr;
unsigned int* vir3_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)
{
printk("%s : %s : %d\n",__FILE__,__func__,__LINE__);
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__);
long ret;
ret = copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
if(strcmp(kbuf,"LED1_ON")==0){
(*vir_odr)|=(0x1<<10);
}
else if(strcmp(kbuf,"LED1_OFF")==0){
(*vir_odr)&=(~(0x1<<10));
}
if(strcmp(kbuf,"LED2_ON")==0){
(*vir2_odr)|=(0x1<<10);
}
else if(strcmp(kbuf,"LED2_OFF")==0){
(*vir2_odr)&=(~(0x1<<10));
}
if(strcmp(kbuf,"LED3_ON")==0){
(*vir3_odr)|=(0x1<<8);
}
else if(strcmp(kbuf,"LED3_OFF")==0){
(*vir3_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 mydev_init(void)
{
major=register_chrdev(0,"mychrdev",&fops);
if(major < 0)
{
printk("注册字符设备驱动失败\n");
return major;
}
printk("注册字符设备驱动文件成功major = %d\n",major);
//进行LED1寄存器地址的映射
vir_moder = ioremap(PHY_LED1_MODER,4);
if(vir_moder == NULL)
{
printk("映射物理内存失败%d\n",__LINE__);
return -EFAULT;
}
vir_odr = ioremap(PHY_LED1_ODR,4);
if(vir_odr == NULL)
{
printk("映射物理内存失败%d\n",__LINE__);
return -EFAULT;
}
vir_rcc = ioremap(PHY_LED1_RCC,4);
if(vir_rcc == NULL)
{
printk("映射物理内存失败%d\n",__LINE__);
return -EFAULT;
}
printk("映射物理内存成功\n");
//硬件LED1寄存器初始化
(*vir_rcc) |= (0x1 << 4);
(*vir_moder) &= (~(0x3 << 20));
(*vir_moder) |= (0x1 << 20);
(*vir_odr) &= (~(0x1 << 10));
printk("硬件初始化成功\n");
//进行LED2寄存器地址的映射
vir2_moder = ioremap(PHY_LED2_MODER,4);
if(vir2_moder == NULL)
{
printk("映射物理内存失败%d\n",__LINE__);
return -EFAULT;
}
vir2_odr = ioremap(PHY_LED2_ODR,4);
if(vir2_odr == NULL)
{
printk("映射物理内存失败%d\n",__LINE__);
return -EFAULT;
}
vir2_rcc = ioremap(PHY_LED2_RCC,4);
if(vir2_rcc == NULL)
{
printk("映射物理内存失败%d\n",__LINE__);
return -EFAULT;
}
printk("映射物理内存成功\n");
//硬件LED2寄存器初始化
(*vir2_rcc) |= (0x1 << 5);
(*vir2_moder) &= (~(0x3 << 20));
(*vir2_moder) |= (0x1 << 20);
(*vir2_odr) &= (~(0x1 << 10));
printk("硬件初始化成功\n");
//进行LED3寄存器地址的映射
vir3_moder = ioremap(PHY_LED3_MODER,4);
if(vir3_moder == NULL)
{
printk("映射物理内存失败%d\n",__LINE__);
return -EFAULT;
}
vir3_odr = ioremap(PHY_LED3_ODR,4);
if(vir3_odr == NULL)
{
printk("映射物理内存失败%d\n",__LINE__);
return -EFAULT;
}
vir3_rcc = ioremap(PHY_LED3_RCC,4);
if(vir3_rcc == NULL)
{
printk("映射物理内存失败%d\n",__LINE__);
return -EFAULT;
}
printk("映射物理内存成功\n");
//硬件LED3寄存器初始化
(*vir3_rcc) |= (0x1 << 4);
(*vir3_moder) &= (~(0x3 << 16));
(*vir3_moder) |= (0x1 << 16);
(*vir3_odr) &= (~(0x1 << 8));
printk("硬件初始化成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
iounmap(vir_moder);
iounmap(vir_odr);
iounmap(vir_rcc);
iounmap(vir2_moder);
iounmap(vir2_odr);
iounmap(vir2_rcc);
iounmap(vir3_moder);
iounmap(vir3_odr);
iounmap(vir3_rcc);
unregister_chrdev(major,"mychrdev");
}
module_init(mydev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
编译过程:
arm-linux-gnueabihf-gcc test.c
cp a.out ~/nfs/rootfs
cp mychrdev.ko ~/nfs/rootfs
在secure上:insmod mychrdev.ko
insmod mychrdev.ko
[ 31.627387] 注册字符设备驱动文件成功major = 242
[ 31.631591] 映射物理内存成功
[ 31.635141] 硬件初始化成功
[ 31.638567] 映射物理内存成功
[ 31.642021] 硬件初始化成功
[ 31.645383] 映射物理内存成功
[ 31.648952] 硬件初始化成功
mknod /dev/mychrdev c 242 0
./a.out