This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 475b7074db2 drivers/analog: replace critical_section to spinlock
475b7074db2 is described below
commit 475b7074db2098cf19d22740322dbec5db4ad139
Author: liaoao <[email protected]>
AuthorDate: Tue Jun 17 21:50:06 2025 +0800
drivers/analog: replace critical_section to spinlock
Protect the ADC and DAC FIFOs with a spinlock instead of the global
critical section and release it around the blocking waits. Call
ao_setup()/ao_shutdown() of the ADC, DAC, comparator and opamp under the
open/close mutex only, and rename ad_closelock of the DAC and opamp to
ad_lock as in the ADC and comparator.
Signed-off-by: liaoao <[email protected]>
---
drivers/analog/adc.c | 38 +++++++++++++++-----------------------
drivers/analog/comp.c | 10 ++--------
drivers/analog/dac.c | 36 +++++++++++++++++-------------------
drivers/analog/opamp.c | 22 ++++++++--------------
include/nuttx/analog/adc.h | 2 ++
include/nuttx/analog/dac.h | 5 +++--
include/nuttx/analog/opamp.h | 2 +-
7 files changed, 48 insertions(+), 67 deletions(-)
diff --git a/drivers/analog/adc.c b/drivers/analog/adc.c
index a26eb75bd6d..ff84eb2b152 100644
--- a/drivers/analog/adc.c
+++ b/drivers/analog/adc.c
@@ -140,8 +140,6 @@ static int adc_open(FAR struct file *filep)
{
/* Yes.. perform one time hardware initialization. */
- irqstate_t flags = enter_critical_section();
-
ret = dev->ad_ops->ao_setup(dev);
if (ret == OK)
{
@@ -158,8 +156,6 @@ static int adc_open(FAR struct file *filep)
dev->ad_ops->ao_rxint(dev, true);
}
-
- leave_critical_section(flags);
}
/* Save the new open count on success */
@@ -186,7 +182,6 @@ static int adc_close(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct adc_dev_s *dev = inode->i_private;
- irqstate_t flags;
int ret;
ret = nxmutex_lock(&dev->ad_lock);
@@ -209,9 +204,7 @@ static int adc_close(FAR struct file *filep)
/* Free the IRQ and disable the ADC device */
- flags = enter_critical_section(); /* Disable interrupts */
- dev->ad_ops->ao_shutdown(dev); /* Disable the ADC */
- leave_critical_section(flags);
+ dev->ad_ops->ao_shutdown(dev);
nxmutex_unlock(&dev->ad_lock);
}
@@ -233,6 +226,7 @@ static ssize_t adc_read(FAR struct file *filep, FAR char
*buffer,
size_t nread;
int ret = 0;
int msglen;
+ irqstate_t flags;
ainfo("buflen: %d\n", (int)buflen);
@@ -271,14 +265,7 @@ static ssize_t adc_read(FAR struct file *filep, FAR char
*buffer,
if (buflen >= msglen)
{
- /* Interrupts must be disabled while accessing the fifo FIFO */
-
- ret = nxmutex_lock(&dev->ad_lock);
-
- if (ret < 0)
- {
- return ret;
- }
+ flags = spin_lock_irqsave(&dev->ad_spinlock);
while (fifo->af_head == fifo->af_tail)
{
@@ -302,7 +289,9 @@ static ssize_t adc_read(FAR struct file *filep, FAR char
*buffer,
/* Wait for a message to be received */
dev->ad_nrxwaiters++;
+ spin_unlock_irqrestore(&dev->ad_spinlock, flags);
ret = nxsem_wait(&fifo->af_sem);
+ flags = spin_lock_irqsave(&dev->ad_spinlock);
dev->ad_nrxwaiters--;
if (ret < 0)
{
@@ -419,7 +408,7 @@ static ssize_t adc_read(FAR struct file *filep, FAR char
*buffer,
ret = nread;
return_with_irqdisabled:
- nxmutex_unlock(&dev->ad_lock);
+ spin_unlock_irqrestore(&dev->ad_spinlock, flags);
}
ainfo("Returning: %d\n", ret);
@@ -618,7 +607,7 @@ static int adc_poll(FAR struct file *filep, FAR struct
pollfd_s *fds,
* and ad_recv FIFO.
*/
- flags = enter_critical_section();
+ flags = spin_lock_irqsave(&dev->ad_spinlock);
if (setup)
{
@@ -659,7 +648,9 @@ static int adc_poll(FAR struct file *filep, FAR struct
pollfd_s *fds,
if (dev->ad_recv.af_head != dev->ad_recv.af_tail)
{
+ spin_unlock_irqrestore(&dev->ad_spinlock, flags);
poll_notify(&fds, 1, POLLIN);
+ return ret;
}
}
else if (fds->priv)
@@ -675,7 +666,7 @@ static int adc_poll(FAR struct file *filep, FAR struct
pollfd_s *fds,
}
return_with_irqdisabled:
- leave_critical_section(flags);
+ spin_unlock_irqrestore(&dev->ad_spinlock, flags);
return ret;
}
@@ -690,11 +681,11 @@ static int adc_reset_fifo(FAR struct adc_dev_s *dev)
/* Interrupts must be disabled while accessing the ad_recv FIFO */
- flags = enter_critical_section();
+ flags = spin_lock_irqsave(&dev->ad_spinlock);
fifo->af_head = fifo->af_tail;
- leave_critical_section(flags);
+ spin_unlock_irqrestore(&dev->ad_spinlock, flags);
return OK;
}
@@ -711,11 +702,11 @@ static int adc_samples_on_read(FAR struct adc_dev_s *dev)
/* Interrupts must be disabled while accessing the ad_recv FIFO */
- flags = enter_critical_section();
+ flags = spin_lock_irqsave(&dev->ad_spinlock);
ret = fifo->af_tail - fifo->af_head;
- leave_critical_section(flags);
+ spin_unlock_irqrestore(&dev->ad_spinlock, flags);
if (ret < 0)
{
@@ -762,6 +753,7 @@ int adc_register(FAR const char *path, FAR struct adc_dev_s
*dev)
nxsem_init(&dev->ad_recv.af_sem, 0, 0);
nxmutex_init(&dev->ad_lock);
+ spin_lock_init(&dev->ad_spinlock);
/* Reset the ADC hardware */
diff --git a/drivers/analog/comp.c b/drivers/analog/comp.c
index 53bf7320dc1..ba1b78df942 100644
--- a/drivers/analog/comp.c
+++ b/drivers/analog/comp.c
@@ -220,10 +220,7 @@ static int comp_open(FAR struct file *filep)
{
/* Yes.. perform one time hardware initialization. */
- irqstate_t flags = enter_critical_section();
-
ret = dev->ad_ops->ao_setup(dev);
- leave_critical_section(flags);
}
/* Save the new open count on success */
@@ -253,7 +250,6 @@ static int comp_close(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct comp_dev_s *dev = inode->i_private;
- irqstate_t flags;
int ret;
ret = nxmutex_lock(&dev->ad_lock);
@@ -274,11 +270,9 @@ static int comp_close(FAR struct file *filep)
dev->ad_ocount = 0;
- /* Free the IRQ and disable the COMP device */
+ /* Disable the IRQ and then disable the COMP device */
- flags = enter_critical_section(); /* Disable interrupts */
- dev->ad_ops->ao_shutdown(dev); /* Disable the COMP */
- leave_critical_section(flags);
+ dev->ad_ops->ao_shutdown(dev);
nxmutex_unlock(&dev->ad_lock);
}
diff --git a/drivers/analog/dac.c b/drivers/analog/dac.c
index edd9a3b0229..013cdb65a1a 100644
--- a/drivers/analog/dac.c
+++ b/drivers/analog/dac.c
@@ -96,7 +96,7 @@ static int dac_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
@@ -121,8 +121,6 @@ static int dac_open(FAR struct file *filep)
{
/* Yes.. perform one time hardware initialization. */
- irqstate_t flags = enter_critical_section();
-
ret = dev->ad_ops->ao_setup(dev);
if (ret == OK)
{
@@ -131,8 +129,6 @@ static int dac_open(FAR struct file *filep)
dev->ad_xmit.af_head = 0;
dev->ad_xmit.af_tail = 0;
}
-
- leave_critical_section(flags);
}
/* Save the new open count on success */
@@ -143,7 +139,7 @@ static int dac_open(FAR struct file *filep)
}
}
- nxmutex_unlock(&dev->ad_closelock);
+ nxmutex_unlock(&dev->ad_lock);
}
return ret;
@@ -162,10 +158,9 @@ static int dac_close(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct dac_dev_s *dev = inode->i_private;
- 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
@@ -175,7 +170,7 @@ static int dac_close(FAR struct file *filep)
if (dev->ad_ocount > 1)
{
dev->ad_ocount--;
- nxmutex_unlock(&dev->ad_closelock);
+ nxmutex_unlock(&dev->ad_lock);
}
else
{
@@ -192,11 +187,9 @@ static int dac_close(FAR struct file *filep)
/* Free the IRQ and disable the DAC device */
- flags = enter_critical_section(); /* Disable interrupts */
- dev->ad_ops->ao_shutdown(dev); /* Disable the DAC */
- leave_critical_section(flags);
+ dev->ad_ops->ao_shutdown(dev);
- nxmutex_unlock(&dev->ad_closelock);
+ nxmutex_unlock(&dev->ad_lock);
}
}
@@ -257,7 +250,7 @@ static ssize_t dac_write(FAR struct file *filep, FAR const
char *buffer,
/* Interrupts must be disabled throughout the following */
- flags = enter_critical_section();
+ flags = spin_lock_irqsave(&dev->ad_spinlock);
/* Check if the TX FIFO was empty when we started. That is a clue that we
* have to kick off a new TX sequence.
@@ -325,7 +318,8 @@ static ssize_t dac_write(FAR struct file *filep, FAR const
char *buffer,
ret = nsent;
}
- goto return_with_irqdisabled;
+ spin_unlock_irqrestore(&dev->ad_spinlock, flags);
+ return ret;
}
/* If the FIFO was empty when we started, then we will have to
@@ -337,14 +331,18 @@ static ssize_t dac_write(FAR struct file *filep, FAR
const char *buffer,
dac_xmit(dev);
}
+ spin_unlock_irqrestore(&dev->ad_spinlock, flags);
+
/* Wait for a message to be sent */
ret = nxsem_wait_uninterruptible(&fifo->af_sem);
if (ret < 0)
{
- goto return_with_irqdisabled;
+ return ret;
}
+ flags = spin_lock_irqsave(&dev->ad_spinlock);
+
/* Re-check the FIFO state */
empty = (fifo->af_head == fifo->af_tail);
@@ -409,8 +407,7 @@ static ssize_t dac_write(FAR struct file *filep, FAR const
char *buffer,
ret = nsent;
-return_with_irqdisabled:
- leave_critical_section(flags);
+ spin_unlock_irqrestore(&dev->ad_spinlock, flags);
return ret;
}
@@ -503,7 +500,8 @@ int dac_register(FAR const char *path, FAR struct dac_dev_s
*dev)
/* Initialize semaphores & mutex */
nxsem_init(&dev->ad_xmit.af_sem, 0, 0);
- nxmutex_init(&dev->ad_closelock);
+ nxmutex_init(&dev->ad_lock);
+ spin_lock_init(&dev->ad_spinlock);
dev->ad_ops->ao_reset(dev);
diff --git a/drivers/analog/opamp.c b/drivers/analog/opamp.c
index b826e47983b..55bf20bd7fb 100644
--- a/drivers/analog/opamp.c
+++ b/drivers/analog/opamp.c
@@ -84,7 +84,7 @@ static int opamp_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
@@ -109,10 +109,7 @@ static int opamp_open(FAR struct file *filep)
{
/* Yes.. perform one time hardware initialization. */
- irqstate_t flags = enter_critical_section();
-
ret = dev->ad_ops->ao_setup(dev);
- leave_critical_section(flags);
}
/* Save the new open count on success */
@@ -123,7 +120,7 @@ static int opamp_open(FAR struct file *filep)
}
}
- nxmutex_unlock(&dev->ad_closelock);
+ nxmutex_unlock(&dev->ad_lock);
}
return ret;
@@ -142,10 +139,9 @@ static int opamp_close(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct opamp_dev_s *dev = inode->i_private;
- 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
@@ -155,7 +151,7 @@ static int opamp_close(FAR struct file *filep)
if (dev->ad_ocount > 1)
{
dev->ad_ocount--;
- nxmutex_unlock(&dev->ad_closelock);
+ nxmutex_unlock(&dev->ad_lock);
}
else
{
@@ -165,11 +161,9 @@ static int opamp_close(FAR struct file *filep)
/* Free the IRQ and disable the OPAMP device */
- flags = enter_critical_section(); /* Disable interrupts */
- dev->ad_ops->ao_shutdown(dev); /* Disable the OPAMP */
- leave_critical_section(flags);
+ dev->ad_ops->ao_shutdown(dev);
- nxmutex_unlock(&dev->ad_closelock);
+ nxmutex_unlock(&dev->ad_lock);
}
}
@@ -208,14 +202,14 @@ int opamp_register(FAR const char *path, FAR struct
opamp_dev_s *dev)
/* Initialize mutex */
- nxmutex_init(&dev->ad_closelock);
+ nxmutex_init(&dev->ad_lock);
/* Register the OPAMP character driver */
ret = register_driver(path, &g_opamp_fops, 0400, dev);
if (ret < 0)
{
- 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 eccf1a8835b..54f1a890d7f 100644
--- a/include/nuttx/analog/adc.h
+++ b/include/nuttx/analog/adc.h
@@ -38,6 +38,7 @@
#include <nuttx/fs/fs.h>
#include <nuttx/mutex.h>
#include <nuttx/semaphore.h>
+#include <nuttx/spinlock.h>
#include <nuttx/spi/spi.h>
#include <nuttx/i2c/i2c_master.h>
@@ -217,6 +218,7 @@ struct adc_dev_s
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 */
+ spinlock_t ad_spinlock; /* Used for data protection
between irq and threads */
/* The following is a list of poll structures of threads waiting for
* driver events. The 'struct pollfd_s' reference for each open is also
diff --git a/include/nuttx/analog/dac.h b/include/nuttx/analog/dac.h
index 4e3bf8531ea..9b05c86d208 100644
--- a/include/nuttx/analog/dac.h
+++ b/include/nuttx/analog/dac.h
@@ -38,6 +38,7 @@
#include <nuttx/i2c/i2c_master.h>
#include <nuttx/mutex.h>
#include <nuttx/semaphore.h>
+#include <nuttx/spinlock.h>
#include <nuttx/spi/spi.h>
#include <nuttx/analog/ioctl.h>
@@ -156,9 +157,9 @@ struct dac_dev_s
uint8_t ad_ocount; /* The number of times the device
has
* been opened */
uint8_t ad_nchannel; /* Number of dac channel */
- mutex_t ad_closelock; /* Locks out new opens while close
is
- * in progress */
+ mutex_t ad_lock; /* Locks used to serialize access
*/
struct dac_fifo_s ad_xmit; /* Describes receive FIFO */
+ spinlock_t ad_spinlock; /* Used for data protection
between irq and threads */
};
/****************************************************************************
diff --git a/include/nuttx/analog/opamp.h b/include/nuttx/analog/opamp.h
index 410b12f7afd..b65f0481949 100644
--- a/include/nuttx/analog/opamp.h
+++ b/include/nuttx/analog/opamp.h
@@ -72,7 +72,7 @@ struct opamp_dev_s
/* Fields managed by common upper half OPAMP logic */
uint8_t ad_ocount; /* The number of times the device has
been opened */
- mutex_t ad_closelock; /* Locks out new opens while close is
in progress */
+ mutex_t ad_lock; /* Lock used to serialize access */
#endif
/* Fields provided by lower half OPAMP logic */