add mutex lock

This commit is contained in:
shuixx 2019-06-10 20:34:40 +08:00
parent c6e0d4dafa
commit 9ca61be337

View File

@ -4,6 +4,7 @@
#include <linux/slab.h>
#include <linux/ioctl.h>
#include <linux/delay.h>
#include <linux/mutex.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
@ -17,7 +18,9 @@ unsigned int gpio_num = 0;
#define DQ_L gpio_set_value(gpio_num, 0)
#define DQ_V gpio_get_value(gpio_num)
spinlock_t lock;
//spinlock_t lock;
//#define DEFINE_MUTEX(LOCK)
struct mutex mutex;
//********************************
//功能:ds18b20复位
@ -33,7 +36,7 @@ int init_DS18B20(void)
udelay(65);
result = DQ_V;
udelay(240);
printk("ds18b20 init result %d,%d", result, DQ_V);
//printk("ds18b20 init result %d,%d", result, DQ_V);
udelay(180);
return result;
}
@ -46,7 +49,7 @@ unsigned char read_char(void)
unsigned int i = 0;
unsigned char dat = 0;
spin_lock(&lock);
// spin_lock(&lock);
DQ_OUT;
for(i=0; i<8; i++) {
DQ_L;
@ -60,7 +63,7 @@ unsigned char read_char(void)
DQ_OUT;
udelay(2);
}
spin_unlock(&lock);
//spin_unlock(&lock);
return dat;
}
@ -71,7 +74,7 @@ void write_char(unsigned char dat)
{
unsigned int i = 0;
spin_lock(&lock);
//spin_lock(&lock);
DQ_OUT;
for(i=0; i<8; i++)
{
@ -86,17 +89,14 @@ void write_char(unsigned char dat)
udelay(2);
dat >>= 1;
}
spin_unlock(&lock);
//spin_unlock(&lock);
}
int initDs18b20(unsigned int gpioNum)
{
spin_lock_init(&lock);
gpio_num = gpioNum;
if (init_DS18B20())
{
return 1;
}
//spin_lock_init(&lock);
mutex_init(&mutex);
return 0;
}
@ -111,21 +111,33 @@ void readDs18b20Data(unsigned char *value)
{
return;
}
mutex_lock(&mutex);
// local_irq_disable();
if (init_DS18B20())
{
// local_irq_enable();
mutex_unlock(&mutex);
return;
}
write_char(0xCC);
write_char(0x44);
// local_irq_enable();
mdelay(750);
// local_irq_disable();
if (init_DS18B20())
{
// local_irq_enable();
mutex_unlock(&mutex);
return;
}
write_char(0xCC);
write_char(0xBE);
// local_irq_enable();
mutex_unlock(&mutex);
value[0] = read_char();
value[1] = read_char();