Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions drivers/analog/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
{
Expand All @@ -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);
}
}

Expand All @@ -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;

Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 */

Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion include/nuttx/analog/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mutex_t ad_lock; /* Locks use in adc open、close and read */
mutex_t ad_lock; /* Locks used 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 */
Expand Down