把stm32的光敏模块移植到arm上
使用的是s5p6818的开发板
先找电路的连接
对应的就是gpioB29 我们把光敏模块经行连接
驱动应用编写
second_drv.c
#include<linux/module.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <asm/mach-types.h>
#include <linux/gpio.h>
#include <asm/gpio.h>
#include <asm/delay.h>
#include <linux/clk.h>
#include <mach/gpio.h>
#include <mach/soc.h>
#include <mach/platform.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/errno.h>
#include <linux/serio.h>
#include <linux/wait.h>
#include <linux/cdev.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <mach/gpio.h>
#include <linux/gpio.h>
#define CFG_IO_X6818_Humity (PAD_GPIO_B + 29)
static struct class *seconddrv_class;
static struct class_device *seconddrv_class_dev;
volatile unsigned long *gpbcon;
volatile unsigned long *gpbdat;
static int second_drv_open(struct inode *inode, struct file *file)
{
gpio_direction_input(CFG_IO_X6818_Humity);
return 0;
}
ssize_t second_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
/* 返回4个引脚的电平 */
unsigned int key_vals[1];
int regval;
/* 读GPF0,2 */
regval = gpio_get_value(CFG_IO_X6818_Humity); ;
key_vals[0] = regval;
copy_to_user(buf, key_vals, sizeof(key_vals));
return sizeof(key_vals);
}
static struct file_operations sencod_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = second_drv_open,
.read = second_drv_read,
};
int major;
static int second_drv_init(void)
{
major = register_chrdev(0, "second_drv", &sencod_drv_fops);
seconddrv_class = class_create(THIS_MODULE, "second_drv");
seconddrv_class_dev = device_create(seconddrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */
return 0;
}
static void second_drv_exit(void)
{
unregister_chrdev(major, "second_drv");
device_unregister(seconddrv_class_dev);
class_destroy(seconddrv_class);
return 0;
}
module_init(second_drv_init);
module_exit(second_drv_exit);
MODULE_LICENSE("GPL");
seconddrvtest.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
/* seconddrvtest */
int main(int argc, char **argv)
{
int fd;
unsigned int key_vals[1];
fd = open("/dev/buttons", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
read(fd, key_vals, sizeof(key_vals));
printf(" key pressed: %d \n", key_vals[0]);
return 0;
}
Makefile
#内核要提前编译好
KERN_DIR = /home/scholar/Desktop/6818GEC/kernel
#-C 是转到后面的目录'$(KERN_DIR)',使用这个目录下面的 Makefile 编译
#M是指当前目标是什么
#moudule 是指目标
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += second_drv.o
开始实验
有光源输出为0,低没光源电平为1