Skip to content

Commit bbaf1ff

Browse files
wangzhi16Alan C. Assis
authored andcommitted
use small lock in following files:
arch/arm64/src/a64/a64_twi.c arch/arm64/src/imx9/imx9_gpioirq.c arch/arm64/src/imx9/imx9_lpi2c.c arch/arm64/src/imx9/imx9_usbdev.c arch/arm64/src/imx9/imx9_usdhc.c Signed-off-by: wangzhi16 <[email protected]>
1 parent 7c24ef4 commit bbaf1ff

File tree

5 files changed

+68
-42
lines changed

5 files changed

+68
-42
lines changed

arch/arm64/src/a64/a64_twi.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <nuttx/i2c/i2c_master.h>
4545

4646
#include <nuttx/irq.h>
47+
#include <nuttx/spinlock.h>
4748
#include <arch/board/board.h>
4849

4950
#include "arm64_internal.h"
@@ -166,6 +167,7 @@ struct a64_twi_priv_s
166167

167168
int refs; /* Reference count */
168169
mutex_t lock; /* Mutual exclusion mutex */
170+
spinlock_t spinlock; /* Spinlock */
169171
sem_t waitsem; /* Wait for TWI transfer completion */
170172
struct wdog_s timeout; /* Watchdog to recover from bus hangs */
171173
volatile int result; /* The result of the transfer */
@@ -288,6 +290,7 @@ static struct a64_twi_priv_s a64_twi0_priv =
288290
.config = &a64_twi0_config,
289291
.refs = 0,
290292
.lock = NXMUTEX_INITIALIZER,
293+
.spinlock = SP_UNLOCKED,
291294
.waitsem = SEM_INITIALIZER(0),
292295
.intstate = INTSTATE_IDLE,
293296
.msgc = 0,
@@ -324,6 +327,7 @@ static struct a64_twi_priv_s a64_twi1_priv =
324327
.config = &a64_twi1_config,
325328
.refs = 0,
326329
.lock = NXMUTEX_INITIALIZER,
330+
.spinlock = SP_UNLOCKED,
327331
.waitsem = SEM_INITIALIZER(0),
328332
.intstate = INTSTATE_IDLE,
329333
.msgc = 0,
@@ -360,6 +364,7 @@ static struct a64_twi_priv_s a64_twi2_priv =
360364
.config = &a64_twi2_config,
361365
.refs = 0,
362366
.lock = NXMUTEX_INITIALIZER,
367+
.spinlock = SP_UNLOCKED,
363368
.waitsem = SEM_INITIALIZER(0),
364369
.intstate = INTSTATE_IDLE,
365370
.msgc = 0,
@@ -396,6 +401,7 @@ static struct a64_twi_priv_s a64_rtwi_priv =
396401
.config = &a64_rtwi_config,
397402
.refs = 0,
398403
.lock = NXMUTEX_INITIALIZER,
404+
.spinlock = SP_UNLOCKED,
399405
.waitsem = SEM_INITIALIZER(0),
400406
.intstate = INTSTATE_IDLE,
401407
.msgc = 0,
@@ -1226,7 +1232,7 @@ static int a64_twi_isr_process(struct a64_twi_priv_s *priv)
12261232
*/
12271233

12281234
#ifdef CONFIG_I2C_POLLED
1229-
irqstate_t flags = enter_critical_section();
1235+
irqstate_t flags = spin_lock_irqsave(&priv->spinlock);
12301236
#endif
12311237

12321238
/* Transmit a byte */
@@ -1235,7 +1241,7 @@ static int a64_twi_isr_process(struct a64_twi_priv_s *priv)
12351241
priv->dcnt--;
12361242

12371243
#ifdef CONFIG_I2C_POLLED
1238-
leave_critical_section(flags);
1244+
spin_unlock_irqrestore(&priv->spinlock, flags);
12391245
#endif
12401246

12411247
break;
@@ -1558,7 +1564,7 @@ static int twi_transfer(struct i2c_master_s *dev,
15581564

15591565
twi_setclock(priv, msgs->frequency);
15601566

1561-
flags = enter_critical_section();
1567+
flags = spin_lock_irqsave(&priv->spinlock);
15621568

15631569
/* Initiate the transfer. The rest will be handled from interrupt
15641570
* logic. Interrupts must be disabled to prevent re-entrance from the
@@ -1612,7 +1618,7 @@ static int twi_transfer(struct i2c_master_s *dev,
16121618

16131619
/* Release the port for re-use by other clients */
16141620

1615-
leave_critical_section(flags);
1621+
spin_unlock_irqrestore(&priv->spinlock, flags);
16161622
nxmutex_unlock(&priv->lock);
16171623
return ret;
16181624
}
@@ -1820,7 +1826,7 @@ static void twi_setclock(struct a64_twi_priv_s *priv, uint32_t freq)
18201826

18211827
static void twi_hw_initialize(struct a64_twi_priv_s *priv)
18221828
{
1823-
irqstate_t flags = enter_critical_section();
1829+
irqstate_t flags = spin_lock_irqsave(&priv->spinlock);
18241830

18251831
i2cinfo("TWI%d Initializing\n", priv->config->twi);
18261832

@@ -1856,7 +1862,7 @@ static void twi_hw_initialize(struct a64_twi_priv_s *priv)
18561862

18571863
twi_softreset(priv);
18581864

1859-
leave_critical_section(flags);
1865+
spin_unlock_irqrestore(&priv->spinlock, flags);
18601866
}
18611867

18621868
/****************************************************************************

arch/arm64/src/imx9/imx9_gpioirq.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include <nuttx/arch.h>
3535
#include <nuttx/irq.h>
36+
#include <nuttx/spinlock.h>
3637

3738
#include "arm64_internal.h"
3839
#include "imx9_gpio.h"
@@ -62,6 +63,7 @@ struct imx9_portisr_s
6263
****************************************************************************/
6364

6465
static struct imx9_portisr_s g_isrtab[IMX9_GPIO_NPORTS];
66+
static spinlock_t g_isrlock = SP_UNLOCKED;
6567

6668
/****************************************************************************
6769
* Private Functions
@@ -197,12 +199,12 @@ int imx9_gpioirq_attach(gpio_pinset_t pinset, xcpt_t isr, void *arg)
197199

198200
/* Atomically change the handler */
199201

200-
irqstate_t flags = enter_critical_section();
202+
irqstate_t flags = spin_lock_irqsave(&g_isrlock);
201203

202204
g_isrtab[port].pins[pin].isr = isr;
203205
g_isrtab[port].pins[pin].arg = arg;
204206

205-
leave_critical_section(flags);
207+
spin_unlock_irqrestore(&g_isrlock, flags);
206208
return OK;
207209
}
208210

arch/arm64/src/imx9/imx9_lpi2c.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
#include <nuttx/arch.h>
4040
#include <nuttx/irq.h>
41+
#include <nuttx/spinlock.h>
4142
#include <nuttx/clock.h>
4243
#include <nuttx/mutex.h>
4344
#include <nuttx/semaphore.h>
@@ -206,6 +207,7 @@ struct imx9_lpi2c_priv_s
206207

207208
int refs; /* Reference count */
208209
mutex_t lock; /* Mutual exclusion mutex */
210+
spinlock_t spinlock; /* Spinlock */
209211
#ifndef CONFIG_I2C_POLLED
210212
sem_t sem_isr; /* Interrupt wait semaphore */
211213
#endif
@@ -364,6 +366,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c1_priv =
364366
.config = &imx9_lpi2c1_config,
365367
.refs = 0,
366368
.lock = NXMUTEX_INITIALIZER,
369+
.spinlock = SP_UNLOCKED,
367370
#ifndef CONFIG_I2C_POLLED
368371
.sem_isr = SEM_INITIALIZER(0),
369372
#endif
@@ -412,6 +415,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c2_priv =
412415
.config = &imx9_lpi2c2_config,
413416
.refs = 0,
414417
.lock = NXMUTEX_INITIALIZER,
418+
.spinlock = SP_UNLOCKED,
415419
#ifndef CONFIG_I2C_POLLED
416420
.sem_isr = SEM_INITIALIZER(0),
417421
#endif
@@ -460,6 +464,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c3_priv =
460464
.config = &imx9_lpi2c3_config,
461465
.refs = 0,
462466
.lock = NXMUTEX_INITIALIZER,
467+
.spinlock = SP_UNLOCKED,
463468
#ifndef CONFIG_I2C_POLLED
464469
.sem_isr = SEM_INITIALIZER(0),
465470
#endif
@@ -508,6 +513,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c4_priv =
508513
.config = &imx9_lpi2c4_config,
509514
.refs = 0,
510515
.lock = NXMUTEX_INITIALIZER,
516+
.spinlock = SP_UNLOCKED,
511517
#ifndef CONFIG_I2C_POLLED
512518
.sem_isr = SEM_INITIALIZER(0),
513519
#endif
@@ -556,6 +562,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c5_priv =
556562
.config = &imx9_lpi2c5_config,
557563
.refs = 0,
558564
.lock = NXMUTEX_INITIALIZER,
565+
.spinlock = SP_UNLOCKED,
559566
#ifndef CONFIG_I2C_POLLED
560567
.sem_isr = SEM_INITIALIZER(0),
561568
#endif
@@ -604,6 +611,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c6_priv =
604611
.config = &imx9_lpi2c6_config,
605612
.refs = 0,
606613
.lock = NXMUTEX_INITIALIZER,
614+
.spinlock = SP_UNLOCKED,
607615
#ifndef CONFIG_I2C_POLLED
608616
.sem_isr = SEM_INITIALIZER(0),
609617
#endif
@@ -652,6 +660,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c7_priv =
652660
.config = &imx9_lpi2c7_config,
653661
.refs = 0,
654662
.lock = NXMUTEX_INITIALIZER,
663+
.spinlock = SP_UNLOCKED,
655664
#ifndef CONFIG_I2C_POLLED
656665
.sem_isr = SEM_INITIALIZER(0),
657666
#endif
@@ -700,6 +709,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c8_priv =
700709
.config = &imx9_lpi2c8_config,
701710
.refs = 0,
702711
.lock = NXMUTEX_INITIALIZER,
712+
.spinlock = SP_UNLOCKED,
703713
#ifndef CONFIG_I2C_POLLED
704714
.sem_isr = SEM_INITIALIZER(0),
705715
#endif
@@ -1595,7 +1605,7 @@ static int imx9_lpi2c_isr_process(struct imx9_lpi2c_priv_s *priv)
15951605
*/
15961606

15971607
#ifdef CONFIG_I2C_POLLED
1598-
irqstate_t flags = enter_critical_section();
1608+
irqstate_t flags = spin_lock_irqsave(&priv->spinlock);
15991609
#endif
16001610

16011611
/* Receive a byte */
@@ -1610,7 +1620,7 @@ static int imx9_lpi2c_isr_process(struct imx9_lpi2c_priv_s *priv)
16101620
}
16111621

16121622
#ifdef CONFIG_I2C_POLLED
1613-
leave_critical_section(flags);
1623+
spin_unlock_irqrestore(&priv->spinlock, flags);
16141624
#endif
16151625
}
16161626

@@ -2376,7 +2386,7 @@ struct i2c_master_s *imx9_i2cbus_initialize(int port)
23762386
* power-up hardware and configure GPIOs.
23772387
*/
23782388

2379-
flags = enter_critical_section();
2389+
flags = spin_lock_irqsave(&priv->spinlock);
23802390

23812391
if ((volatile int)priv->refs++ == 0)
23822392
{
@@ -2397,7 +2407,7 @@ struct i2c_master_s *imx9_i2cbus_initialize(int port)
23972407
#endif
23982408
}
23992409

2400-
leave_critical_section(flags);
2410+
spin_unlock_irqrestore(&priv->spinlock, flags);
24012411

24022412
return (struct i2c_master_s *)priv;
24032413
}
@@ -2424,15 +2434,15 @@ int imx9_i2cbus_uninitialize(struct i2c_master_s *dev)
24242434
return ERROR;
24252435
}
24262436

2427-
flags = enter_critical_section();
2437+
flags = spin_lock_irqsave(&priv->spinlock);
24282438

24292439
if (--priv->refs > 0)
24302440
{
24312441
leave_critical_section(flags);
24322442
return OK;
24332443
}
24342444

2435-
leave_critical_section(flags);
2445+
spin_unlock_irqrestore(&priv->spinlock, flags);
24362446

24372447
/* Disable power and other HW resource (GPIO's) */
24382448

0 commit comments

Comments
 (0)