Skip to content

Commit daf5715

Browse files
zhangkai25xiaoxiang781216
authored andcommitted
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>
1 parent 0721dfd commit daf5715

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

drivers/analog/adc.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static int adc_open(FAR struct file *filep)
115115
* finished.
116116
*/
117117

118-
ret = nxmutex_lock(&dev->ad_closelock);
118+
ret = nxmutex_lock(&dev->ad_lock);
119119
if (ret >= 0)
120120
{
121121
/* Increment the count of references to the device. If this is the
@@ -166,7 +166,7 @@ static int adc_open(FAR struct file *filep)
166166
dev->ad_ocount = tmp;
167167
}
168168

169-
nxmutex_unlock(&dev->ad_closelock);
169+
nxmutex_unlock(&dev->ad_lock);
170170
}
171171

172172
return ret;
@@ -188,7 +188,7 @@ static int adc_close(FAR struct file *filep)
188188
irqstate_t flags;
189189
int ret;
190190

191-
ret = nxmutex_lock(&dev->ad_closelock);
191+
ret = nxmutex_lock(&dev->ad_lock);
192192
if (ret >= 0)
193193
{
194194
/* Decrement the references to the driver. If the reference count will
@@ -198,7 +198,7 @@ static int adc_close(FAR struct file *filep)
198198
if (dev->ad_ocount > 1)
199199
{
200200
dev->ad_ocount--;
201-
nxmutex_unlock(&dev->ad_closelock);
201+
nxmutex_unlock(&dev->ad_lock);
202202
}
203203
else
204204
{
@@ -212,7 +212,7 @@ static int adc_close(FAR struct file *filep)
212212
dev->ad_ops->ao_shutdown(dev); /* Disable the ADC */
213213
leave_critical_section(flags);
214214

215-
nxmutex_unlock(&dev->ad_closelock);
215+
nxmutex_unlock(&dev->ad_lock);
216216
}
217217
}
218218

@@ -230,7 +230,6 @@ static ssize_t adc_read(FAR struct file *filep, FAR char *buffer,
230230
FAR struct adc_dev_s *dev = inode->i_private;
231231
FAR struct adc_fifo_s *fifo = &dev->ad_recv;
232232
size_t nread;
233-
irqstate_t flags;
234233
int ret = 0;
235234
int msglen;
236235

@@ -273,7 +272,13 @@ static ssize_t adc_read(FAR struct file *filep, FAR char *buffer,
273272
{
274273
/* Interrupts must be disabled while accessing the fifo FIFO */
275274

276-
flags = enter_critical_section();
275+
ret = nxmutex_lock(&dev->ad_lock);
276+
277+
if (ret < 0)
278+
{
279+
return ret;
280+
}
281+
277282
while (fifo->af_head == fifo->af_tail)
278283
{
279284
/* 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,
412417
ret = nread;
413418

414419
return_with_irqdisabled:
415-
leave_critical_section(flags);
420+
nxmutex_unlock(&dev->ad_lock);
416421
}
417422

418423
ainfo("Returning: %d\n", ret);
@@ -754,7 +759,7 @@ int adc_register(FAR const char *path, FAR struct adc_dev_s *dev)
754759
/* Initialize semaphores & mutex */
755760

756761
nxsem_init(&dev->ad_recv.af_sem, 0, 0);
757-
nxmutex_init(&dev->ad_closelock);
762+
nxmutex_init(&dev->ad_lock);
758763

759764
/* Reset the ADC hardware */
760765

@@ -812,7 +817,7 @@ int adc_register(FAR const char *path, FAR struct adc_dev_s *dev)
812817
}
813818

814819
nxsem_destroy(&dev->ad_recv.af_sem);
815-
nxmutex_destroy(&dev->ad_closelock);
820+
nxmutex_destroy(&dev->ad_lock);
816821
return ret;
817822
}
818823

include/nuttx/analog/adc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ struct adc_dev_s
213213

214214
uint8_t ad_ocount; /* The number of times the device has been opened */
215215
uint8_t ad_nrxwaiters; /* Number of threads waiting to enqueue a message */
216-
mutex_t ad_closelock; /* Locks out new opens while close is in progress */
216+
mutex_t ad_lock; /* Locks use in adc open、close and read */
217217
sem_t ad_recvsem; /* Used to wakeup user waiting for space in ad_recv.buffer */
218218
struct adc_fifo_s ad_recv; /* Describes receive FIFO */
219219
bool ad_isovr; /* Flag to indicate an ADC overrun */

0 commit comments

Comments
 (0)