diff --git a/drivers/analog/adc.c b/drivers/analog/adc.c index 86dc64e594e79..7b2c77d6e0a62 100644 --- a/drivers/analog/adc.c +++ b/drivers/analog/adc.c @@ -115,7 +115,7 @@ static int adc_open(FAR struct file *filep) * finished. */ - ret = nxmutex_lock(&dev->ad_closelock); + ret = nxmutex_lock(&dev->ad_lock); if (ret >= 0) { /* Increment the count of references to the device. If this is the @@ -166,7 +166,7 @@ static int adc_open(FAR struct file *filep) dev->ad_ocount = tmp; } - nxmutex_unlock(&dev->ad_closelock); + nxmutex_unlock(&dev->ad_lock); } return ret; @@ -188,7 +188,7 @@ static int adc_close(FAR struct file *filep) irqstate_t flags; int ret; - ret = nxmutex_lock(&dev->ad_closelock); + ret = nxmutex_lock(&dev->ad_lock); if (ret >= 0) { /* Decrement the references to the driver. If the reference count will @@ -198,7 +198,7 @@ static int adc_close(FAR struct file *filep) if (dev->ad_ocount > 1) { dev->ad_ocount--; - nxmutex_unlock(&dev->ad_closelock); + nxmutex_unlock(&dev->ad_lock); } else { @@ -212,7 +212,7 @@ static int adc_close(FAR struct file *filep) dev->ad_ops->ao_shutdown(dev); /* Disable the ADC */ leave_critical_section(flags); - nxmutex_unlock(&dev->ad_closelock); + nxmutex_unlock(&dev->ad_lock); } } @@ -230,7 +230,6 @@ static ssize_t adc_read(FAR struct file *filep, FAR char *buffer, FAR struct adc_dev_s *dev = inode->i_private; FAR struct adc_fifo_s *fifo = &dev->ad_recv; size_t nread; - irqstate_t flags; int ret = 0; int msglen; @@ -273,7 +272,13 @@ static ssize_t adc_read(FAR struct file *filep, FAR char *buffer, { /* Interrupts must be disabled while accessing the fifo FIFO */ - flags = enter_critical_section(); + ret = nxmutex_lock(&dev->ad_lock); + + if (ret < 0) + { + return ret; + } + while (fifo->af_head == fifo->af_tail) { /* Check if there was an overrun, if set we need to return EIO */ @@ -412,7 +417,7 @@ static ssize_t adc_read(FAR struct file *filep, FAR char *buffer, ret = nread; return_with_irqdisabled: - leave_critical_section(flags); + nxmutex_unlock(&dev->ad_lock); } ainfo("Returning: %d\n", ret); @@ -754,7 +759,7 @@ int adc_register(FAR const char *path, FAR struct adc_dev_s *dev) /* Initialize semaphores & mutex */ nxsem_init(&dev->ad_recv.af_sem, 0, 0); - nxmutex_init(&dev->ad_closelock); + nxmutex_init(&dev->ad_lock); /* Reset the ADC hardware */ @@ -812,7 +817,7 @@ int adc_register(FAR const char *path, FAR struct adc_dev_s *dev) } nxsem_destroy(&dev->ad_recv.af_sem); - nxmutex_destroy(&dev->ad_closelock); + nxmutex_destroy(&dev->ad_lock); return ret; } diff --git a/include/nuttx/analog/adc.h b/include/nuttx/analog/adc.h index 9a790478d0f92..49faffd0d4435 100644 --- a/include/nuttx/analog/adc.h +++ b/include/nuttx/analog/adc.h @@ -213,7 +213,7 @@ struct adc_dev_s uint8_t ad_ocount; /* The number of times the device has been opened */ uint8_t ad_nrxwaiters; /* Number of threads waiting to enqueue a message */ - mutex_t ad_closelock; /* Locks out new opens while close is in progress */ + mutex_t ad_lock; /* Locks use in adc open、close and read */ sem_t ad_recvsem; /* Used to wakeup user waiting for space in ad_recv.buffer */ struct adc_fifo_s ad_recv; /* Describes receive FIFO */ bool ad_isovr; /* Flag to indicate an ADC overrun */