Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gpio: use small lock to protect configgpio #15219

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions arch/arm/src/at32/at32_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,19 @@
#include <errno.h>
#include <debug.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock.h>

#include "arm_internal.h"
#include "chip.h"
#include "at32_syscfg.h"
#include "at32_gpio.h"

/****************************************************************************
* Private Data
****************************************************************************/

static spinlock_t g_configgpio_lock = SP_UNLOCKED;

/****************************************************************************
* Public Data
****************************************************************************/
Expand Down Expand Up @@ -199,7 +206,7 @@ int at32_configgpio(uint32_t cfgset)
* exclusive access to all of the GPIO configuration registers.
*/

flags = enter_critical_section();
flags = spin_lock_irqsave(&g_configgpio_lock);

/* Determine the alternate function (Only alternate function pins) */

Expand Down Expand Up @@ -355,7 +362,7 @@ int at32_configgpio(uint32_t cfgset)
putreg32(regval, regaddr);
}

leave_critical_section(flags);
spin_unlock_irqrestore(&g_configgpio_lock, flags);
return OK;
}
#endif
Expand Down
11 changes: 7 additions & 4 deletions arch/arm/src/eoss3/eoss3_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

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

#include "arm_internal.h"
#include "chip.h"
Expand Down Expand Up @@ -57,6 +58,8 @@
* Private Data
****************************************************************************/

static spinlock_t g_configgpio_lock = SP_UNLOCKED;

/****************************************************************************
* Private Functions
****************************************************************************/
Expand All @@ -82,7 +85,7 @@ int eoss3_configgpio(gpio_pinset_t cfgset)
uint16_t sel_idx = \
(input & EOSS3_PAD_SEL_IDX_MASK) >> EOSS3_PAD_SEL_IDX_SHIFT;

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

/* Check select index, if it is 0 we are not working with an input */

Expand Down Expand Up @@ -111,7 +114,7 @@ int eoss3_configgpio(gpio_pinset_t cfgset)
}

putreg32(ctrl, EOSS3_PAD_X_CTRL(pad));
leave_critical_section(flags);
spin_unlock_irqrestore(&g_configgpio_lock, flags);
return OK;
}

Expand Down Expand Up @@ -141,7 +144,7 @@ void eoss3_gpiowrite(gpio_pinset_t cfgset, bool value)
uint8_t iobit = (cfgset & GPIO_REG_BIT_MASK) >> GPIO_REG_BIT_SHIFT;
if (cfgset & GPIO_REG_EN_MASK)
{
irqstate_t flags = enter_critical_section();
irqstate_t flags = spin_lock_irqsave(&g_configgpio_lock);
if (value)
{
putreg32(
Expand All @@ -155,7 +158,7 @@ void eoss3_gpiowrite(gpio_pinset_t cfgset, bool value)
EOSS3_MISC_IO_OUTPUT);
}

leave_critical_section(flags);
spin_unlock_irqrestore(&g_configgpio_lock, flags);
}
}

Expand Down
11 changes: 9 additions & 2 deletions arch/arm/src/nuc1xx/nuc_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <debug.h>

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

#include "arm_internal.h"
Expand All @@ -43,6 +44,12 @@
* Pre-processor Definitions
****************************************************************************/

/****************************************************************************
* Private Data
****************************************************************************/

static spinlock_t g_configgpio_lock = SP_UNLOCKED;

/****************************************************************************
* Private Functions
****************************************************************************/
Expand Down Expand Up @@ -240,7 +247,7 @@ void nuc_gpiowrite(gpio_cfgset_t pinset, bool value)

/* Disable interrupts -- the following operations must be atomic */

flags = enter_critical_section();
flags = spin_lock_irqsave(&g_configgpio_lock);

/* Allow writing only to the selected pin in the DOUT register */

Expand All @@ -249,7 +256,7 @@ void nuc_gpiowrite(gpio_cfgset_t pinset, bool value)
/* Set the pin to the selected value and re-enable interrupts */

putreg32(((uint32_t)value << pin), base + NUC_GPIO_DOUT_OFFSET);
leave_critical_section(flags);
spin_unlock_irqrestore(&g_configgpio_lock, flags);
#endif
}

Expand Down
7 changes: 5 additions & 2 deletions arch/arm/src/sam34/sam4l_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

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

#include "arm_internal.h"
Expand All @@ -45,6 +46,8 @@
****************************************************************************/

#ifdef CONFIG_DEBUG_GPIO_INFO
static spinlock_t g_configgpio_lock = SP_UNLOCKED;

static const char g_portchar[4] =
{
'A', 'B', 'C', 'D'
Expand Down Expand Up @@ -529,7 +532,7 @@ int sam_dumpgpio(uint32_t pinset, const char *msg)

/* The following requires exclusive access to the GPIO registers */

flags = enter_critical_section();
flags = spin_lock_irqsave(&g_configgpio_lock);

gpioinfo("GPIO%c pinset: %08x base: %08x -- %s\n",
g_portchar[port], pinset, base, msg);
Expand Down Expand Up @@ -559,7 +562,7 @@ int sam_dumpgpio(uint32_t pinset, const char *msg)
getreg32(base + SAM_GPIO_PARAMETER_OFFSET),
getreg32(base + SAM_GPIO_VERSION_OFFSET));

leave_critical_section(flags);
spin_unlock_irqrestore(&g_configgpio_lock, flags);
return OK;
}
#endif
11 changes: 7 additions & 4 deletions arch/arm/src/sam34/sam_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

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

#include "arm_internal.h"
Expand All @@ -55,6 +56,8 @@
* Private Data
****************************************************************************/

static spinlock_t g_configgpio_lock = SP_UNLOCKED;

#ifdef CONFIG_DEBUG_GPIO_INFO
static const char g_portchar[4] =
{
Expand Down Expand Up @@ -476,7 +479,7 @@ int sam_configgpio(gpio_pinset_t cfgset)

/* Disable interrupts to prohibit re-entrance. */

flags = enter_critical_section();
flags = spin_lock_irqsave(&g_configgpio_lock);

/* Enable writing to GPIO registers */

Expand Down Expand Up @@ -511,7 +514,7 @@ int sam_configgpio(gpio_pinset_t cfgset)
/* Disable writing to GPIO registers */

putreg32(PIO_WPMR_WPEN | PIO_WPMR_WPKEY, base + SAM_PIO_WPMR_OFFSET);
leave_critical_section(flags);
spin_unlock_irqrestore(&g_configgpio_lock, flags);

return ret;
}
Expand Down Expand Up @@ -588,7 +591,7 @@ int sam_dumpgpio(uint32_t pinset, const char *msg)

/* The following requires exclusive access to the GPIO registers */

flags = enter_critical_section();
flags = spin_lock_irqsave(&g_configgpio_lock);

gpioinfo("PIO%c pinset: %08x base: %08x -- %s\n",
g_portchar[port], pinset, base, msg);
Expand Down Expand Up @@ -646,7 +649,7 @@ int sam_dumpgpio(uint32_t pinset, const char *msg)
#endif
#endif

leave_critical_section(flags);
spin_unlock_irqrestore(&g_configgpio_lock, flags);
return OK;
}
#endif
7 changes: 5 additions & 2 deletions arch/arm/src/samv7/sam_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

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

#include "arm_internal.h"
Expand Down Expand Up @@ -75,6 +76,8 @@
* Private Data
****************************************************************************/

static spinlock_t g_configgpio_lock = SP_UNLOCKED;

#ifdef CONFIG_DEBUG_GPIO_INFO
static const char g_portchar[SAMV7_NPIO] =
{
Expand Down Expand Up @@ -497,7 +500,7 @@ int sam_configgpio(gpio_pinset_t cfgset)

/* Disable interrupts to prohibit re-entrance. */

flags = enter_critical_section();
flags = spin_lock_irqsave(&g_configgpio_lock);

/* Enable writing to GPIO registers */

Expand Down Expand Up @@ -606,7 +609,7 @@ int sam_dumpgpio(uint32_t pinset, const char *msg)

/* The following requires exclusive access to the GPIO registers */

flags = enter_critical_section();
flags = spin_lock_irqsave(&g_configgpio_lock);

gpioinfo("PIO%c pinset: %08x base: %08x -- %s\n",
g_portchar[port], pinset, base, msg);
Expand Down
19 changes: 13 additions & 6 deletions arch/arm/src/stm32/stm32_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <debug.h>

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

#include "arm_internal.h"
#include "chip.h"
Expand All @@ -43,6 +44,12 @@
# pragma message "CONFIG_STM32_USE_LEGACY_PINMAP will be deprecated migrate board.h see tools/stm32_pinmap_tool.py"
#endif

/****************************************************************************
* Private Data
****************************************************************************/

static spinlock_t g_configgpio_lock = SP_UNLOCKED;

/****************************************************************************
* Public Data
****************************************************************************/
Expand Down Expand Up @@ -299,7 +306,7 @@ int stm32_configgpio(uint32_t cfgset)
* exclusive access to all of the GPIO configuration registers.
*/

flags = enter_critical_section();
flags = spin_lock_irqsave(&g_configgpio_lock);

/* Decode the mode and configuration */

Expand Down Expand Up @@ -339,7 +346,7 @@ int stm32_configgpio(uint32_t cfgset)
{
/* Its an alternate function pin... we can return early */

leave_critical_section(flags);
spin_unlock_irqrestore(&g_configgpio_lock, flags);
return OK;
}
}
Expand All @@ -366,7 +373,7 @@ int stm32_configgpio(uint32_t cfgset)
{
/* Neither... we can return early */

leave_critical_section(flags);
spin_unlock_irqrestore(&g_configgpio_lock, flags);
return OK;
}
}
Expand All @@ -393,7 +400,7 @@ int stm32_configgpio(uint32_t cfgset)
regval |= (1 << pin);
putreg32(regval, regaddr);

leave_critical_section(flags);
spin_unlock_irqrestore(&g_configgpio_lock, flags);
return OK;
}
#endif
Expand Down Expand Up @@ -468,7 +475,7 @@ int stm32_configgpio(uint32_t cfgset)
* exclusive access to all of the GPIO configuration registers.
*/

flags = enter_critical_section();
flags = spin_lock_irqsave(&g_configgpio_lock);

/* Determine the alternate function (Only alternate function pins) */

Expand Down Expand Up @@ -684,7 +691,7 @@ int stm32_configgpio(uint32_t cfgset)
putreg32(regval, regaddr);
}

leave_critical_section(flags);
spin_unlock_irqrestore(&g_configgpio_lock, flags);
return OK;
}
#endif
Expand Down
11 changes: 9 additions & 2 deletions arch/arm/src/stm32f0l0g0/stm32_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

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

#include "arm_internal.h"
#include "chip.h"
Expand All @@ -49,6 +50,12 @@
# pragma message "CONFIG_STM32F0G0L0_USE_LEGACY_PINMAP will be deprecated migrate board.h see tools/stm32_pinmap_tool.py"
#endif

/****************************************************************************
* Private Data
****************************************************************************/

static spinlock_t g_configgpio_lock = SP_UNLOCKED;

/****************************************************************************
* Public Data
****************************************************************************/
Expand Down Expand Up @@ -178,7 +185,7 @@ int stm32_configgpio(uint32_t cfgset)
* exclusive access to all of the GPIO configuration registers.
*/

flags = enter_critical_section();
flags = spin_lock_irqsave(&g_configgpio_lock);

/* Now apply the configuration to the mode register */

Expand Down Expand Up @@ -339,7 +346,7 @@ int stm32_configgpio(uint32_t cfgset)
#endif
}

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

Expand Down
Loading
Loading