first commit

This commit is contained in:
Gogs 2019-06-07 20:06:07 +08:00
commit 20377952d4
7 changed files with 442 additions and 0 deletions

25
Makefile Executable file
View File

@ -0,0 +1,25 @@
ifneq ($(KERNELRELEASE),)
obj-m := ds18b20_223.o ds18b20_224.o
ds18b20_223-objs := ds18b20.o t223.o
ds18b20_224-objs := ds18b20.o t224.o
else
CC = gcc
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
$(MAKE) -C $(KDIR) M=$(PWD) modules
install:
\cp ds18b20_223.ko /data/modules/
\cp ds18b20_224.ko /data/modules/
uninstall:
rm -f /data/modules/ds18b20_224.ko
rm -f /data/modules/ds18b20_223.ko
load:
/data/modules/load-modules.sh
clean:
rm -rf *.o *.ko .*.cmd *.mod.* modules.order Module.symvers .tmp_versions
endif

139
ds18b20.c Executable file
View File

@ -0,0 +1,139 @@
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/ioctl.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
unsigned int gpio_num = 223;
#define DQ_IN gpio_direction_input(gpio_num)
#define DQ_OUT gpio_direction_output(gpio_num, gpio_get_value(gpio_num))
#define DQ_H gpio_set_value(gpio_num, 1)
#define DQ_L gpio_set_value(gpio_num, 0)
#define DQ_V gpio_get_value(gpio_num)
spinlock_t lock;
//********************************
//功能:ds18b20复位
//********************************
int init_DS18B20(void)
{
unsigned int result;
DQ_OUT;
DQ_L;
DQ_H;
udelay(2);
DQ_L;
udelay(480); //480~960
DQ_H;
udelay(60); //60-240
DQ_IN;
result = DQ_V;
udelay(420);
DQ_OUT;
printk("ds18b20 init result %d", result);
return result;
}
//*************************************
//功能:从ds18b20读一个字节的数据
//*************************************
unsigned char read_char(void)
{
unsigned int i = 0;
unsigned char dat = 0;
spin_lock(&lock);
for(i=0; i<8; i++) {
DQ_OUT;
DQ_L;
DQ_H;
udelay(2);
DQ_L;
udelay(2);
DQ_H;
udelay(8);
dat >>= 1;
DQ_IN;
if (DQ_V)
dat |= 0x80;
udelay(50);
}
DQ_OUT;
DQ_L;
DQ_H;
spin_unlock(&lock);
return dat;
}
//*************************************
//功能:向ds18b20写一个字节的数据
//*************************************
void write_char(unsigned char dat)
{
unsigned int i = 0;
spin_lock(&lock);
DQ_OUT;
DQ_H;
for(i=0; i<8; i++)
{
DQ_H;
udelay(2);
DQ_L;
if (dat & 0x01)
DQ_H;
else
DQ_L;
udelay(60);
dat >>= 1;
}
DQ_H;
spin_unlock(&lock);
}
int initDs18b20(unsigned int gpioNum)
{
spin_lock_init(&lock);
gpio_num = gpioNum;
if (init_DS18B20())
return 1;
return 0;
}
/*读取数据*/
void readDs18b20Data(unsigned char *value)
{
if (init_DS18B20())
{
return;
}
write_char(0xCC);
write_char(0x44);
mdelay(800);
if (init_DS18B20())
{
return;
}
write_char(0xCC);
write_char(0xBE);
if (sizeof(&value) > 1)
{
value[0] = read_char();
value[1] = read_char();
}
}
void freeDs18b20(void)
{
gpio_free(gpio_num);
}

12
ds18b20.h Executable file
View File

@ -0,0 +1,12 @@
#ifndef DS18B20_H
#define DS18B20_H
int initDs18b20(unsigned int gpioNum);
/*读取数据*/
void readDs18b20Data(unsigned char *value);
void freeDs18b20(void);
#endif

65
gettemp.c Executable file
View File

@ -0,0 +1,65 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
float getTempByDev(char *devName);
int main(int argc, char *argv[])
{
if (argc == 2)
{
char * ch = argv[1];
// int gpio = atoi(ch);
char * name = "/dev/ds18b20-";
char devName[16];
strcpy(devName, name);
strcat(devName, ch);
float t223 = getTempByDev(devName);
printf("%.4f", t223);
}
else
{
printf("-1");
}
return 0;
}
float getTempByDev(char *devName)
{
int fd;
unsigned char result[2] = {0};
unsigned int hightBitValue = 0;
unsigned int lowBitValue = 0;
float p = 0.0625;
float value = 1024.0f;
fd = open(devName, 0);
if (fd >= 0)
{
int i = read(fd, &result, sizeof(&result));
if (i >= 0)
{
// printf("%xH-%xH\n", result[1], result[0]);
hightBitValue = result[1];
lowBitValue = result[0];
hightBitValue <<= 8;
hightBitValue = hightBitValue + lowBitValue;
if ((result[1] & 0xf8))
{
// printf("aaa\n");
hightBitValue = ~hightBitValue + 1;
value = hightBitValue * p * -1;
}
else
{
value = hightBitValue * p;
}
}
close(fd);
}
return value;
}

75
t223.c Executable file
View File

@ -0,0 +1,75 @@
#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.");

75
t224.c Executable file
View File

@ -0,0 +1,75 @@
#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-224"
#define NODE_NAME "ds18b20-224"
#define gpio_num 224
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.");

51
temp.c Executable file
View File

@ -0,0 +1,51 @@
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/select.h>
float getTempByDev(char *devName);
int main(int argc, char *argv[])
{
float t223 = getTempByDev("/dev/ds18b20-223");
float t224 = getTempByDev("/dev/ds18b20-224");
printf("%.4f#%.4f\n", t223, t224);
return 0;
}
float getTempByDev(char *devName)
{
int fd;
unsigned char result[2] = {0};
unsigned int hightBitValue = 0;
unsigned int lowBitValue = 0;
float p = 0.0625;
float value = 1024.0f;
fd = open(devName, 0);
if (fd >= 0)
{
int i = read(fd, &result, sizeof(&result));
if (i >= 0)
{
// printf("%xH-%xH\n", result[1], result[0]);
hightBitValue = result[1];
lowBitValue = result[0];
hightBitValue <<= 8;
hightBitValue = hightBitValue + lowBitValue;
if ((result[1] & 0xf8))
{
// printf("aaa\n");
hightBitValue = ~hightBitValue + 1;
value = hightBitValue * p * -1;
}
else
{
value = hightBitValue * p;
}
}
close(fd);
}
return value;
}