myled.h
#ifndef __MYLED_H__
#define __MYLED_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
volatile unsigned int BSRR; // 0x18
volatile unsigned int LCKR; // 0x1C
volatile unsigned int AFRL; // 0x20
volatile unsigned int AFRH; // 0x24
volatile unsigned int BRR; // 0x28
volatile unsigned int res;
volatile unsigned int SECCFGR; // 0x30
}gpio_t;
#define RCC_PHY_ADDR 0x50000A28 //rcc物理地址
#define GPIOE 0x50006000
#define GPIOF 0x50007000
#endif
test.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,const char * argv[])
{
int fd = -1;
char buf[128]="";
fd = open("/dev/myled",O_RDWR);
if(fd==-1)
{
perror("open is error\n");
exit(1);
}
while (1)
{
buf[0]=0;
buf[1] = !buf[1];
write(fd,buf,sizeof(buf));
buf[0]=1;
write(fd,buf,sizeof(buf));
buf[0]=2;
write(fd,buf,sizeof(buf));
sleep(1);
}
close(fd);
return 0;
}
mycdev.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"
int major = 0;
char kbuf[128]="";
unsigned int* rcc_virt = NULL;
gpio_t* gpioe = NULL;
gpio_t* gpiof = NULL;
int myled_open (struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t myled_read (struct file *file, 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_to_user(ubuf,kbuf,sizeof(kbuf));
if(ret)
{
printk("copy to user error\n");
return -EIO;
}
return size;
}
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 error\n");
return -EIO;
}
switch (kbuf[0])
{
case 0:
if(kbuf[1])
{
//灭
gpioe->ODR &=(~(0x1<<10));
}else{
//亮
gpioe->ODR |=(0x1<<10);
}
break;
case 1:
if(kbuf[1])
{
//灭
gpioe->ODR &=(~(0x1<<8));
}else{
//亮
gpioe->ODR |=(0x1<<8);
}
break;
case 2:
if(kbuf[1])
{
//灭
gpiof->ODR &=(~(0x1<<10));
}else{
//亮
gpiof->ODR |=(0x1<<10);
}
break;
default:
break;
}
memset(kbuf,0,sizeof(kbuf));
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,
.read = myled_read,
.write = myled_write,
.release = myled_close,
};
//入口
static int __init mycdev_init(void)
{
major = register_chrdev(0,CNAME,&fops);
if(major <=0)
{
printk("register chrdev is error\n");
}
printk("major = %d\n",major);
rcc_virt = ioremap(RCC_PHY_ADDR,4);
if(rcc_virt ==NULL)
{
printk("rcc_virt ioremap is error\n");
return -EIO;
}
gpioe = ioremap(GPIOE,sizeof(gpio_t));
if(gpioe ==NULL)
{
printk("gpioe is error\n");
return -EIO;
}
gpiof = ioremap(GPIOF,sizeof(gpio_t));
if(gpiof ==NULL)
{
printk("gpiof is error\n");
return -EIO;
}
//初始化
*rcc_virt |=(0x3<<4);
gpioe->MODER &=(~(0x3<<20));//PE10
gpioe->MODER |=(0X1<<20);
gpioe->ODR &=(~(0X1<<10));
gpioe->MODER &=(~(0x3<<16));//PE8
gpioe->MODER |=(0X1<<16);
gpioe->ODR &=(~(0X1<<8));
gpiof->MODER &=(~(0x3<<20));//PF10
gpiof->MODER |=(0X1<<20);
gpiof->ODR &=(~(0X1<<10));
return 0;
}
//出口
static void __exit mycdev_exit(void)
{
unregister_chrdev(major,CNAME);
iounmap(rcc_virt);
iounmap(gpioe);
iounmap(gpiof);
}
//指定地址
module_init(mycdev_init);
module_exit(mycdev_exit);
//gpl
MODULE_LICENSE("GPL");