ds18b20/ds18b20.c

139 lines
2.4 KiB
C
Raw Normal View History

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>
#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-09 09:09:55 +08:00
spinlock_t lock;
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);
printk("ds18b20 init result %d,%d", result, DQ_V);
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-09 09:09:55 +08:00
spin_lock(&lock);
DQ_OUT;
2019-06-07 20:06:07 +08:00
for(i=0; i<8; i++) {
DQ_L;
udelay(2);
dat >>= 1;
DQ_IN;
2019-06-09 15:00:27 +08:00
udelay(12);
2019-06-07 20:06:07 +08:00
if (DQ_V)
dat |= 0x80;
udelay(50);
2019-06-09 09:09:55 +08:00
DQ_OUT;
udelay(2);
2019-06-07 20:06:07 +08:00
}
2019-06-09 09:09:55 +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-09 09:09:55 +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-09 09:09:55 +08:00
spin_unlock(&lock);
2019-06-07 20:06:07 +08:00
}
int initDs18b20(unsigned int gpioNum)
{
2019-06-09 09:09:55 +08:00
spin_lock_init(&lock);
2019-06-07 20:06:07 +08:00
gpio_num = gpioNum;
if (init_DS18B20())
2019-06-09 09:09:55 +08:00
{
2019-06-07 20:06:07 +08:00
return 1;
2019-06-09 09:09:55 +08:00
}
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-07 20:06:07 +08:00
if (init_DS18B20())
{
return;
}
write_char(0xCC);
write_char(0x44);
2019-06-07 21:27:29 +08:00
mdelay(750);
2019-06-07 20:06:07 +08:00
if (init_DS18B20())
{
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-07 20:06:07 +08:00
}
void freeDs18b20(void)
{
gpio_free(gpio_num);
}