2019-06-07 20:06:07 +08:00
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
|
#include <linux/fs.h>
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
|
#include <linux/slab.h>
|
|
|
|
|
#include <linux/ioctl.h>
|
|
|
|
|
#include <linux/delay.h>
|
2019-06-10 20:34:40 +08:00
|
|
|
|
#include <linux/mutex.h>
|
2019-06-07 20:06:07 +08:00
|
|
|
|
|
|
|
|
|
#include <linux/gpio.h>
|
|
|
|
|
#include <linux/of_gpio.h>
|
|
|
|
|
|
2019-06-09 09:09:55 +08:00
|
|
|
|
unsigned int gpio_num = 0;
|
2019-06-07 20:06:07 +08:00
|
|
|
|
|
|
|
|
|
#define DQ_IN gpio_direction_input(gpio_num)
|
2019-06-07 21:27:29 +08:00
|
|
|
|
//#define DQ_OUT gpio_direction_output(gpio_num, gpio_get_value(gpio_num))
|
|
|
|
|
#define DQ_OUT gpio_direction_output(gpio_num, 1)
|
2019-06-07 20:06:07 +08:00
|
|
|
|
#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)
|
|
|
|
|
|
2019-06-10 20:34:40 +08:00
|
|
|
|
//spinlock_t lock;
|
|
|
|
|
//#define DEFINE_MUTEX(LOCK)
|
|
|
|
|
struct mutex mutex;
|
2019-06-07 20:06:07 +08:00
|
|
|
|
|
|
|
|
|
//********************************
|
|
|
|
|
//功能:ds18b20复位
|
|
|
|
|
//********************************
|
|
|
|
|
int init_DS18B20(void)
|
|
|
|
|
{
|
|
|
|
|
unsigned int result;
|
|
|
|
|
DQ_OUT;
|
|
|
|
|
DQ_L;
|
2019-06-09 15:00:27 +08:00
|
|
|
|
udelay(480);
|
2019-06-07 20:06:07 +08:00
|
|
|
|
DQ_H;
|
|
|
|
|
DQ_IN;
|
2019-06-09 15:00:27 +08:00
|
|
|
|
udelay(65);
|
2019-06-07 20:06:07 +08:00
|
|
|
|
result = DQ_V;
|
2019-06-09 15:00:27 +08:00
|
|
|
|
udelay(240);
|
2019-06-10 20:34:40 +08:00
|
|
|
|
//printk("ds18b20 init result %d,%d", result, DQ_V);
|
2019-06-09 15:00:27 +08:00
|
|
|
|
udelay(180);
|
2019-06-07 20:06:07 +08:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//*************************************
|
|
|
|
|
//功能:从ds18b20读一个字节的数据
|
|
|
|
|
//*************************************
|
|
|
|
|
unsigned char read_char(void)
|
|
|
|
|
{
|
|
|
|
|
unsigned int i = 0;
|
|
|
|
|
unsigned char dat = 0;
|
|
|
|
|
|
2019-06-10 20:34:40 +08:00
|
|
|
|
// spin_lock(&lock);
|
2019-06-07 20:06:07 +08:00
|
|
|
|
for(i=0; i<8; i++) {
|
2019-06-16 12:36:11 +08:00
|
|
|
|
DQ_OUT;
|
2019-06-07 20:06:07 +08:00
|
|
|
|
DQ_L;
|
|
|
|
|
udelay(2);
|
|
|
|
|
DQ_IN;
|
2019-06-16 12:36:11 +08:00
|
|
|
|
dat >>= 1;
|
|
|
|
|
udelay(2);
|
2019-06-07 20:06:07 +08:00
|
|
|
|
if (DQ_V)
|
|
|
|
|
dat |= 0x80;
|
2019-06-16 12:36:11 +08:00
|
|
|
|
udelay(60);
|
2019-06-07 20:06:07 +08:00
|
|
|
|
}
|
2019-06-10 20:34:40 +08:00
|
|
|
|
//spin_unlock(&lock);
|
2019-06-07 20:06:07 +08:00
|
|
|
|
return dat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//*************************************
|
|
|
|
|
//功能:向ds18b20写一个字节的数据
|
|
|
|
|
//*************************************
|
|
|
|
|
void write_char(unsigned char dat)
|
|
|
|
|
{
|
|
|
|
|
unsigned int i = 0;
|
|
|
|
|
|
2019-06-10 20:34:40 +08:00
|
|
|
|
//spin_lock(&lock);
|
2019-06-07 20:06:07 +08:00
|
|
|
|
DQ_OUT;
|
|
|
|
|
for(i=0; i<8; i++)
|
|
|
|
|
{
|
|
|
|
|
DQ_L;
|
2019-06-09 09:09:55 +08:00
|
|
|
|
udelay(2);
|
2019-06-07 20:06:07 +08:00
|
|
|
|
if (dat & 0x01)
|
|
|
|
|
DQ_H;
|
|
|
|
|
else
|
|
|
|
|
DQ_L;
|
2019-06-09 09:09:55 +08:00
|
|
|
|
udelay(60);
|
|
|
|
|
DQ_H;
|
|
|
|
|
udelay(2);
|
2019-06-07 20:06:07 +08:00
|
|
|
|
dat >>= 1;
|
|
|
|
|
}
|
2019-06-10 20:34:40 +08:00
|
|
|
|
//spin_unlock(&lock);
|
2019-06-07 20:06:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int initDs18b20(unsigned int gpioNum)
|
|
|
|
|
{
|
|
|
|
|
gpio_num = gpioNum;
|
2019-06-10 20:34:40 +08:00
|
|
|
|
//spin_lock_init(&lock);
|
|
|
|
|
mutex_init(&mutex);
|
2019-06-07 20:06:07 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*读取数据*/
|
|
|
|
|
void readDs18b20Data(unsigned char *value)
|
|
|
|
|
{
|
2019-06-09 15:00:27 +08:00
|
|
|
|
if (sizeof(&value) > 1){
|
|
|
|
|
value[0] = 0xff;
|
|
|
|
|
value[1] = 0xff;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-06-10 20:34:40 +08:00
|
|
|
|
mutex_lock(&mutex);
|
|
|
|
|
// local_irq_disable();
|
2019-06-07 20:06:07 +08:00
|
|
|
|
if (init_DS18B20())
|
|
|
|
|
{
|
2019-06-10 20:34:40 +08:00
|
|
|
|
// local_irq_enable();
|
|
|
|
|
mutex_unlock(&mutex);
|
2019-06-07 20:06:07 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
write_char(0xCC);
|
|
|
|
|
write_char(0x44);
|
|
|
|
|
|
2019-06-10 20:34:40 +08:00
|
|
|
|
// local_irq_enable();
|
|
|
|
|
|
2019-06-07 21:27:29 +08:00
|
|
|
|
mdelay(750);
|
2019-06-16 12:36:11 +08:00
|
|
|
|
//msleep(750);
|
2019-06-07 20:06:07 +08:00
|
|
|
|
|
2019-06-10 20:34:40 +08:00
|
|
|
|
// local_irq_disable();
|
|
|
|
|
|
2019-06-07 20:06:07 +08:00
|
|
|
|
if (init_DS18B20())
|
|
|
|
|
{
|
2019-06-10 20:34:40 +08:00
|
|
|
|
// local_irq_enable();
|
|
|
|
|
mutex_unlock(&mutex);
|
2019-06-07 20:06:07 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
write_char(0xCC);
|
|
|
|
|
write_char(0xBE);
|
|
|
|
|
|
2019-06-09 15:00:27 +08:00
|
|
|
|
value[0] = read_char();
|
|
|
|
|
value[1] = read_char();
|
2019-06-16 12:36:11 +08:00
|
|
|
|
// local_irq_enable();
|
|
|
|
|
mutex_unlock(&mutex);
|
|
|
|
|
|
|
|
|
|
//printk("%d: TH:%x, TL:%x, c:%x", gpio_num,read_char(),read_char(),read_char());
|
|
|
|
|
|
|
|
|
|
// mdelay(1);
|
|
|
|
|
|
|
|
|
|
// init_DS18B20();
|
|
|
|
|
//write_char(0xB4);
|
|
|
|
|
//DQ_OUT;
|
|
|
|
|
//printk("%d: EC:%d", gpio_num,DQ_V);
|
|
|
|
|
//write_char(0x4E);
|
|
|
|
|
//write_char(0x64);
|
|
|
|
|
//write_char(0x00);
|
|
|
|
|
//write_char(0x7F);
|
|
|
|
|
|
|
|
|
|
//init_DS18B20();
|
|
|
|
|
//write_char(0x48);
|
2019-06-07 20:06:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void freeDs18b20(void)
|
|
|
|
|
{
|
|
|
|
|
gpio_free(gpio_num);
|
|
|
|
|
}
|
|
|
|
|
|