From 9ca61be337f7c9738f446d5dbc92df594d85f16f Mon Sep 17 00:00:00 2001 From: shuixx Date: Mon, 10 Jun 2019 20:34:40 +0800 Subject: [PATCH] add mutex lock --- ds18b20.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/ds18b20.c b/ds18b20.c index a820f09..20083ba 100755 --- a/ds18b20.c +++ b/ds18b20.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -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();