76 lines
1.7 KiB
C
76 lines
1.7 KiB
C
|
#include <linux/kernel.h>
|
|||
|
#include <linux/module.h>
|
|||
|
#include <linux/miscdevice.h>
|
|||
|
#include <linux/fs.h>
|
|||
|
#include <linux/types.h>
|
|||
|
#include <linux/moduleparam.h>
|
|||
|
#include <linux/slab.h>
|
|||
|
#include <linux/cdev.h>
|
|||
|
|
|||
|
#include <linux/gpio.h>
|
|||
|
#include <linux/of_gpio.h>
|
|||
|
|
|||
|
#include <asm/uaccess.h>
|
|||
|
#include "ds18b20.h"
|
|||
|
|
|||
|
#define DEVICE_NAME "ds18b20-223"
|
|||
|
#define NODE_NAME "ds18b20-223"
|
|||
|
|
|||
|
#define gpio_num 223
|
|||
|
|
|||
|
static int ds18b20_open(struct inode *inode, struct file *filp)
|
|||
|
{
|
|||
|
//printk (KERN_INFO "Device opened\n");
|
|||
|
int flag = initDs18b20(gpio_num);
|
|||
|
if(flag & 0x01)
|
|||
|
return -1;
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
/*读取数据*/
|
|||
|
static int ds18b20_read(struct file *filp, char __user *buffer, size_t count, loff_t *ppos)
|
|||
|
{
|
|||
|
unsigned char tmp[2];
|
|||
|
readDs18b20Data(tmp);
|
|||
|
return copy_to_user(buffer, &tmp, sizeof(tmp));
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
static struct file_operations ds18b20_fops = {
|
|||
|
.owner = THIS_MODULE,
|
|||
|
.open = ds18b20_open,
|
|||
|
.read = ds18b20_read,
|
|||
|
};
|
|||
|
|
|||
|
static struct miscdevice ds18b20_dev = {
|
|||
|
.minor = MISC_DYNAMIC_MINOR,
|
|||
|
.name = DEVICE_NAME,
|
|||
|
.nodename = NODE_NAME,
|
|||
|
.fops = &ds18b20_fops,
|
|||
|
};
|
|||
|
|
|||
|
static int __init ds18b20_dev_init(void) {
|
|||
|
int ret;
|
|||
|
ret = gpio_request(gpio_num, DEVICE_NAME);
|
|||
|
if (ret) {
|
|||
|
printk("%s: request GPIO %d for GPE0 failed, ret = %d\n", DEVICE_NAME, gpio_num, ret);
|
|||
|
return ret;
|
|||
|
}
|
|||
|
ret = misc_register(&ds18b20_dev);//注册杂设备驱动
|
|||
|
printk(DEVICE_NAME"\tinitialized\n");
|
|||
|
return ret;
|
|||
|
}
|
|||
|
|
|||
|
static void __exit ds18b20_dev_exit(void) {
|
|||
|
freeDs18b20();
|
|||
|
misc_deregister(&ds18b20_dev);
|
|||
|
}
|
|||
|
|
|||
|
module_init(ds18b20_dev_init);
|
|||
|
module_exit(ds18b20_dev_exit);
|
|||
|
|
|||
|
MODULE_LICENSE("GPL");
|
|||
|
MODULE_VERSION("1.0");
|
|||
|
MODULE_AUTHOR("shuixx.");
|
|||
|
|