实现底层实现三盏灯的控制
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define PHY_LED1_MODER 0X50006000
#define PHY_LED1_ODR 0X50006014
#define PHY_RCC 0X50000A28
#define PHY_LED2_MODER 0X50007000
#define PHY_LED2_ODR 0X50007014
#endif
led.c
#include <stdio.h>
#include <stdlib.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[])
{
char buf[128] = {0};
int fd = open("/dev/mychrdev",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;
}
mychrdev.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "head.h"
int major;
char kbuf[128] = {0};
//LED1
unsigned int *vir_moder;
unsigned int *vir_odr;
unsigned int *vir_rcc;
//LED2
unsigned int *vir_moder2;
unsigned int *vir_odr2;
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(sizeof(kbuf) < size)
size = sizeof(kbuf);
ret = copy_to_user(ubuf, kbuf, size);
if(ret)
{
printk("copy_to_user filed\n");
return -EIO;
}
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 *loof)
{
int ret;
if(sizeof(kbuf) < size)
size = sizeof(kbuf);
ret = copy_from_user(kbuf, ubuf, size);
if(ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
if(kbuf[0] == '1')
{
(*vir_odr) |= (0x1<<10);
(*vir_odr2) |= (0x1<<10);
(*vir_odr) |= (0x1<<8);
}
else if(kbuf[0] == '0')
{
(*vir_odr) &= (~(0x1<<10));
(*vir_odr2) &= (~(0x1<<10));
(*vir_odr) &= (~(0x1<<8));
}
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, "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("MODER寄存器地址映射表失败\n");
return -EFAULT;
}
vir_odr = ioremap(PHY_LED1_ODR,4);
if(vir_odr == NULL)
{
printk("ODR寄存器地址映射表失败\n");
return -EFAULT;
}
vir_rcc = ioremap(PHY_RCC,4);
if(vir_rcc == NULL)
{
printk("RCC寄存器地址映射失败\n");
return -EFAULT;
}
//LED2
vir_moder2 = ioremap(PHY_LED2_MODER,4);
if(vir_moder2 == NULL)
{
printk("MODER寄存器地址映射表失败\n");
return -EFAULT;
}
vir_odr2 = ioremap(PHY_LED2_ODR,4);
if(vir_odr2 == NULL)
{
printk("ODR寄存器地址映射表失败\n");
return -EFAULT;
}
printk("寄存器地址映射成功\n");
//led1
(*vir_rcc) |= (0x1<<4);
(*vir_moder) &= (~(0x3<<20));
(*vir_moder) |= (0x1<<20);
(*vir_odr) &= (~(0x1<<10));
//led2
(*vir_rcc) |= (0x1<<5);
(*vir_moder2) &= (~(0x3<<20));
(*vir_moder2) |= (0x1<<20);
(*vir_odr2) &= (~(0x1<<10));
//led3
(*vir_moder) &= (~(0x3<<16));
(*vir_moder) |= (0x1<<16);
(*vir_odr) &= (~(0x1<<8));
printk("硬件寄存器初始化成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
iounmap(vir_moder);
iounmap(vir_odr);
iounmap(vir_rcc);
iounmap(vir_moder2);
iounmap(vir_odr2);
unregister_chrdev(major,"mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
效果图片