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:
zhangkai25
2024-12-11 16:24:26 +08:00
committed by Xiang Xiao
parent 0721dfd171
commit daf5715810
2 changed files with 16 additions and 11 deletions
+15 -10
View File
@@ -115,7 +115,7 @@ static int adc_open(FAR struct file *filep)
* finished. * finished.
*/ */
ret = nxmutex_lock(&dev->ad_closelock); ret = nxmutex_lock(&dev->ad_lock);
if (ret >= 0) if (ret >= 0)
{ {
/* Increment the count of references to the device. If this is the /* 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; dev->ad_ocount = tmp;
} }
nxmutex_unlock(&dev->ad_closelock); nxmutex_unlock(&dev->ad_lock);
} }
return ret; return ret;
@@ -188,7 +188,7 @@ static int adc_close(FAR struct file *filep)
irqstate_t flags; irqstate_t flags;
int ret; int ret;
ret = nxmutex_lock(&dev->ad_closelock); ret = nxmutex_lock(&dev->ad_lock);
if (ret >= 0) if (ret >= 0)
{ {
/* Decrement the references to the driver. If the reference count will /* 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) if (dev->ad_ocount > 1)
{ {
dev->ad_ocount--; dev->ad_ocount--;
nxmutex_unlock(&dev->ad_closelock); nxmutex_unlock(&dev->ad_lock);
} }
else else
{ {
@@ -212,7 +212,7 @@ static int adc_close(FAR struct file *filep)
dev->ad_ops->ao_shutdown(dev); /* Disable the ADC */ dev->ad_ops->ao_shutdown(dev); /* Disable the ADC */
leave_critical_section(flags); 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_dev_s *dev = inode->i_private;
FAR struct adc_fifo_s *fifo = &dev->ad_recv; FAR struct adc_fifo_s *fifo = &dev->ad_recv;
size_t nread; size_t nread;
irqstate_t flags;
int ret = 0; int ret = 0;
int msglen; 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 */ /* 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) while (fifo->af_head == fifo->af_tail)
{ {
/* Check if there was an overrun, if set we need to return EIO */ /* 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; ret = nread;
return_with_irqdisabled: return_with_irqdisabled:
leave_critical_section(flags); nxmutex_unlock(&dev->ad_lock);
} }
ainfo("Returning: %d\n", ret); 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 */ /* Initialize semaphores & mutex */
nxsem_init(&dev->ad_recv.af_sem, 0, 0); nxsem_init(&dev->ad_recv.af_sem, 0, 0);
nxmutex_init(&dev->ad_closelock); nxmutex_init(&dev->ad_lock);
/* Reset the ADC hardware */ /* 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); nxsem_destroy(&dev->ad_recv.af_sem);
nxmutex_destroy(&dev->ad_closelock); nxmutex_destroy(&dev->ad_lock);
return ret; return ret;
} }
+1 -1
View File
@@ -213,7 +213,7 @@ struct adc_dev_s
uint8_t ad_ocount; /* The number of times the device has been opened */ 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 */ 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 */ sem_t ad_recvsem; /* Used to wakeup user waiting for space in ad_recv.buffer */
struct adc_fifo_s ad_recv; /* Describes receive FIFO */ struct adc_fifo_s ad_recv; /* Describes receive FIFO */
bool ad_isovr; /* Flag to indicate an ADC overrun */ bool ad_isovr; /* Flag to indicate an ADC overrun */