Skip to content

Commit

Permalink
use small lock in following files:
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
wangzhi-art authored and acassis committed Jan 5, 2025
1 parent 7c24ef4 commit bbaf1ff
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 42 deletions.
18 changes: 12 additions & 6 deletions arch/arm64/src/a64/a64_twi.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <nuttx/i2c/i2c_master.h>

#include <nuttx/irq.h>
#include <nuttx/spinlock.h>
#include <arch/board/board.h>

#include "arm64_internal.h"
Expand Down Expand Up @@ -166,6 +167,7 @@ struct a64_twi_priv_s

int refs; /* Reference count */
mutex_t lock; /* Mutual exclusion mutex */
spinlock_t spinlock; /* Spinlock */
sem_t waitsem; /* Wait for TWI transfer completion */
struct wdog_s timeout; /* Watchdog to recover from bus hangs */
volatile int result; /* The result of the transfer */
Expand Down Expand Up @@ -288,6 +290,7 @@ static struct a64_twi_priv_s a64_twi0_priv =
.config = &a64_twi0_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
.waitsem = SEM_INITIALIZER(0),
.intstate = INTSTATE_IDLE,
.msgc = 0,
Expand Down Expand Up @@ -324,6 +327,7 @@ static struct a64_twi_priv_s a64_twi1_priv =
.config = &a64_twi1_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
.waitsem = SEM_INITIALIZER(0),
.intstate = INTSTATE_IDLE,
.msgc = 0,
Expand Down Expand Up @@ -360,6 +364,7 @@ static struct a64_twi_priv_s a64_twi2_priv =
.config = &a64_twi2_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
.waitsem = SEM_INITIALIZER(0),
.intstate = INTSTATE_IDLE,
.msgc = 0,
Expand Down Expand Up @@ -396,6 +401,7 @@ static struct a64_twi_priv_s a64_rtwi_priv =
.config = &a64_rtwi_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
.waitsem = SEM_INITIALIZER(0),
.intstate = INTSTATE_IDLE,
.msgc = 0,
Expand Down Expand Up @@ -1226,7 +1232,7 @@ static int a64_twi_isr_process(struct a64_twi_priv_s *priv)
*/

#ifdef CONFIG_I2C_POLLED
irqstate_t flags = enter_critical_section();
irqstate_t flags = spin_lock_irqsave(&priv->spinlock);
#endif

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

#ifdef CONFIG_I2C_POLLED
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);
#endif

break;
Expand Down Expand Up @@ -1558,7 +1564,7 @@ static int twi_transfer(struct i2c_master_s *dev,

twi_setclock(priv, msgs->frequency);

flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->spinlock);

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

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

leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);
nxmutex_unlock(&priv->lock);
return ret;
}
Expand Down Expand Up @@ -1820,7 +1826,7 @@ static void twi_setclock(struct a64_twi_priv_s *priv, uint32_t freq)

static void twi_hw_initialize(struct a64_twi_priv_s *priv)
{
irqstate_t flags = enter_critical_section();
irqstate_t flags = spin_lock_irqsave(&priv->spinlock);

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

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

twi_softreset(priv);

leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);
}

/****************************************************************************
Expand Down
6 changes: 4 additions & 2 deletions arch/arm64/src/imx9/imx9_gpioirq.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include <nuttx/arch.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock.h>

#include "arm64_internal.h"
#include "imx9_gpio.h"
Expand Down Expand Up @@ -62,6 +63,7 @@ struct imx9_portisr_s
****************************************************************************/

static struct imx9_portisr_s g_isrtab[IMX9_GPIO_NPORTS];
static spinlock_t g_isrlock = SP_UNLOCKED;

/****************************************************************************
* Private Functions
Expand Down Expand Up @@ -197,12 +199,12 @@ int imx9_gpioirq_attach(gpio_pinset_t pinset, xcpt_t isr, void *arg)

/* Atomically change the handler */

irqstate_t flags = enter_critical_section();
irqstate_t flags = spin_lock_irqsave(&g_isrlock);

g_isrtab[port].pins[pin].isr = isr;
g_isrtab[port].pins[pin].arg = arg;

leave_critical_section(flags);
spin_unlock_irqrestore(&g_isrlock, flags);
return OK;
}

Expand Down
22 changes: 16 additions & 6 deletions arch/arm64/src/imx9/imx9_lpi2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

#include <nuttx/arch.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock.h>
#include <nuttx/clock.h>
#include <nuttx/mutex.h>
#include <nuttx/semaphore.h>
Expand Down Expand Up @@ -206,6 +207,7 @@ struct imx9_lpi2c_priv_s

int refs; /* Reference count */
mutex_t lock; /* Mutual exclusion mutex */
spinlock_t spinlock; /* Spinlock */
#ifndef CONFIG_I2C_POLLED
sem_t sem_isr; /* Interrupt wait semaphore */
#endif
Expand Down Expand Up @@ -364,6 +366,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c1_priv =
.config = &imx9_lpi2c1_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
#ifndef CONFIG_I2C_POLLED
.sem_isr = SEM_INITIALIZER(0),
#endif
Expand Down Expand Up @@ -412,6 +415,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c2_priv =
.config = &imx9_lpi2c2_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
#ifndef CONFIG_I2C_POLLED
.sem_isr = SEM_INITIALIZER(0),
#endif
Expand Down Expand Up @@ -460,6 +464,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c3_priv =
.config = &imx9_lpi2c3_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
#ifndef CONFIG_I2C_POLLED
.sem_isr = SEM_INITIALIZER(0),
#endif
Expand Down Expand Up @@ -508,6 +513,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c4_priv =
.config = &imx9_lpi2c4_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
#ifndef CONFIG_I2C_POLLED
.sem_isr = SEM_INITIALIZER(0),
#endif
Expand Down Expand Up @@ -556,6 +562,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c5_priv =
.config = &imx9_lpi2c5_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
#ifndef CONFIG_I2C_POLLED
.sem_isr = SEM_INITIALIZER(0),
#endif
Expand Down Expand Up @@ -604,6 +611,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c6_priv =
.config = &imx9_lpi2c6_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
#ifndef CONFIG_I2C_POLLED
.sem_isr = SEM_INITIALIZER(0),
#endif
Expand Down Expand Up @@ -652,6 +660,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c7_priv =
.config = &imx9_lpi2c7_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
#ifndef CONFIG_I2C_POLLED
.sem_isr = SEM_INITIALIZER(0),
#endif
Expand Down Expand Up @@ -700,6 +709,7 @@ static struct imx9_lpi2c_priv_s imx9_lpi2c8_priv =
.config = &imx9_lpi2c8_config,
.refs = 0,
.lock = NXMUTEX_INITIALIZER,
.spinlock = SP_UNLOCKED,
#ifndef CONFIG_I2C_POLLED
.sem_isr = SEM_INITIALIZER(0),
#endif
Expand Down Expand Up @@ -1595,7 +1605,7 @@ static int imx9_lpi2c_isr_process(struct imx9_lpi2c_priv_s *priv)
*/

#ifdef CONFIG_I2C_POLLED
irqstate_t flags = enter_critical_section();
irqstate_t flags = spin_lock_irqsave(&priv->spinlock);
#endif

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

#ifdef CONFIG_I2C_POLLED
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);
#endif
}

Expand Down Expand Up @@ -2376,7 +2386,7 @@ struct i2c_master_s *imx9_i2cbus_initialize(int port)
* power-up hardware and configure GPIOs.
*/

flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->spinlock);

if ((volatile int)priv->refs++ == 0)
{
Expand All @@ -2397,7 +2407,7 @@ struct i2c_master_s *imx9_i2cbus_initialize(int port)
#endif
}

leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);

return (struct i2c_master_s *)priv;
}
Expand All @@ -2424,15 +2434,15 @@ int imx9_i2cbus_uninitialize(struct i2c_master_s *dev)
return ERROR;
}

flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->spinlock);

if (--priv->refs > 0)
{
leave_critical_section(flags);
return OK;
}

leave_critical_section(flags);
spin_unlock_irqrestore(&priv->spinlock, flags);

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

Expand Down
Loading

0 comments on commit bbaf1ff

Please sign in to comment.