mirror of
https://github.com/apache/nuttx.git
synced 2026-03-27 02:29:15 +08:00
drivers/adc: Use mutexlock to replace critical section protection
Use mutexlock to replace critical section protection when using adc_isr_thread Signed-off-by: zhangkai25 <zhangkai25@xiaomi.com>
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user