Skip to content

Commit

Permalink
use small lock in following file
Browse files Browse the repository at this point in the history
arch/or1k/src/mor1kx/mor1kx_serial.c
arch/risc-v/src/bl602/bl602_serial.c
arch/risc-v/src/common/espressif/esp_lowputc.c
arch/risc-v/src/common/espressif/esp_lowputc.h
arch/risc-v/src/common/espressif/esp_tickless.c
arch/risc-v/src/esp32c3-legacy/esp32c3_idle.c
arch/risc-v/src/esp32c3-legacy/esp32c3_lowputc.c
arch/risc-v/src/esp32c3-legacy/esp32c3_lowputc.h
arch/risc-v/src/esp32c3-legacy/esp32c3_rtc_lowerhalf.c
arch/risc-v/src/fe310/fe310_gpio.c

Signed-off-by: hujun5 <[email protected]>
  • Loading branch information
hujun260 committed Dec 20, 2024
1 parent 67b95d0 commit a0763b8
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 32 deletions.
12 changes: 10 additions & 2 deletions arch/or1k/src/mor1kx/mor1kx_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@
#define OR1K_BAUD (115200)
#define OR1K_DIVISOR (OR1K_SYS_CLK / (16*OR1K_BAUD))

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

#ifdef HAVE_SERIAL_CONSOLE
static spinlock_t g_serial_lock = SP_UNLOCKED;
#endif

/****************************************************************************
* Public Functions
****************************************************************************/
Expand Down Expand Up @@ -117,10 +125,10 @@ void up_putc(int ch)
* interrupts from firing in the serial driver code.
*/

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&g_serial_lock);

/* or1k_lowputc(ch); */

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_serial_lock, flags);
#endif
}
6 changes: 4 additions & 2 deletions arch/risc-v/src/bl602/bl602_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ static bool bl602_txempty(struct uart_dev_s *dev);
* Private Data
****************************************************************************/

static spinlock_t g_bl602_serial_lock = SP_UNLOCKED;

static const struct uart_ops_s g_uart_ops =
{
.setup = bl602_setup,
Expand Down Expand Up @@ -891,10 +893,10 @@ void riscv_serialinit(void)
void up_putc(int ch)
{
#ifdef HAVE_SERIAL_CONSOLE
irqstate_t flags = spin_lock_irqsave(NULL);
irqstate_t flags = spin_lock_irqsave(&g_bl602_serial_lock);

riscv_lowputc(ch);
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_bl602_serial_lock, flags);
#endif
}

Expand Down
10 changes: 6 additions & 4 deletions arch/risc-v/src/common/espressif/esp_lowputc.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ struct esp_uart_s g_uart0_config =
.oflow = false, /* output flow control (CTS) disabled */
#endif
#endif
.hal = &g_uart0_hal
.hal = &g_uart0_hal,
.lock = SP_UNLOCKED
};

#endif /* CONFIG_ESPRESSIF_UART0 */
Expand Down Expand Up @@ -147,7 +148,8 @@ struct esp_uart_s g_uart1_config =
.oflow = false, /* output flow control (CTS) disabled */
#endif
#endif
.hal = &g_uart1_hal
.hal = &g_uart1_hal,
.lock = SP_UNLOCKED
};

#endif /* CONFIG_ESPRESSIF_UART1 */
Expand Down Expand Up @@ -210,7 +212,7 @@ void esp_lowputc_disable_all_uart_int(const struct esp_uart_s *priv,
{
irqstate_t flags;

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&priv->lock);

if (current_status != NULL)
{
Expand All @@ -227,7 +229,7 @@ void esp_lowputc_disable_all_uart_int(const struct esp_uart_s *priv,

uart_hal_clr_intsts_mask(priv->hal, UINT32_MAX);

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&priv->lock, flags);
}

/****************************************************************************
Expand Down
2 changes: 2 additions & 0 deletions arch/risc-v/src/common/espressif/esp_lowputc.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

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

#include "chip.h"
#include "esp_irq.h"
Expand Down Expand Up @@ -76,6 +77,7 @@ struct esp_uart_s
bool oflow; /* Output flow control (CTS) enabled */
#endif
uart_hal_context_t *hal; /* HAL context */
spinlock_t lock; /* Spinlock */
};

extern struct esp_uart_s g_uart0_config;
Expand Down
26 changes: 14 additions & 12 deletions arch/risc-v/src/common/espressif/esp_tickless.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
* Private Data
****************************************************************************/

static spinlock_t g_esp_tickless_lock = SP_UNLOCKED;

/* Systimer HAL layer object */

static systimer_hal_context_t systimer_hal;
Expand Down Expand Up @@ -126,10 +128,10 @@ uint32_t up_get_idletime(void)
uint64_t counter;
irqstate_t flags;

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&g_esp_tickless_lock);
if (!g_timer_started)
{
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_esp_tickless_lock, flags);

return 0;
}
Expand All @@ -147,7 +149,7 @@ uint32_t up_get_idletime(void)
us = 0;
}

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_esp_tickless_lock, flags);

return us;
}
Expand All @@ -172,12 +174,12 @@ void up_step_idletime(uint32_t idletime_us)

DEBUGASSERT(g_timer_started);

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&g_esp_tickless_lock);

systimer_hal_counter_value_advance(&systimer_hal, SYSTIMER_COUNTER_OS_TICK,
idletime_us);

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_esp_tickless_lock, flags);
}

/****************************************************************************
Expand Down Expand Up @@ -216,13 +218,13 @@ void up_step_idletime(uint32_t idletime_us)
int IRAM_ATTR up_timer_gettime(struct timespec *ts)
{
uint64_t time_us;
irqstate_t flags = spin_lock_irqsave(NULL);
irqstate_t flags = spin_lock_irqsave(&g_esp_tickless_lock);

time_us = systimer_hal_get_time(&systimer_hal, SYSTIMER_COUNTER_OS_TICK);
ts->tv_sec = time_us / USEC_PER_SEC;
ts->tv_nsec = (time_us % USEC_PER_SEC) * NSEC_PER_USEC;

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_esp_tickless_lock, flags);

return OK;
}
Expand Down Expand Up @@ -267,7 +269,7 @@ int IRAM_ATTR up_timer_cancel(struct timespec *ts)
{
irqstate_t flags;

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&g_esp_tickless_lock);

if (ts != NULL)
{
Expand Down Expand Up @@ -314,7 +316,7 @@ int IRAM_ATTR up_timer_cancel(struct timespec *ts)
systimer_ll_clear_alarm_int(systimer_hal.dev,
SYSTIMER_ALARM_OS_TICK_CORE0);

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_esp_tickless_lock, flags);

return OK;
}
Expand Down Expand Up @@ -350,11 +352,11 @@ int IRAM_ATTR up_timer_start(const struct timespec *ts)
uint64_t alarm_ticks;
irqstate_t flags;

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&g_esp_tickless_lock);

if (g_timer_started)
{
up_timer_cancel(NULL);
up_timer_cancel(&g_esp_tickless_lock);
}

target_us = (uint64_t)ts->tv_sec * USEC_PER_SEC +
Expand All @@ -375,7 +377,7 @@ int IRAM_ATTR up_timer_start(const struct timespec *ts)

g_timer_started = true;

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_esp_tickless_lock, flags);

return OK;
}
Expand Down
16 changes: 12 additions & 4 deletions arch/risc-v/src/esp32c3-legacy/esp32c3_idle.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@

#endif

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

#ifdef CONFIG_PM
static spinlock_t g_esp32c3_idle_lock = SP_UNLOCKED;
#endif

/****************************************************************************
* Private Functions
****************************************************************************/
Expand All @@ -94,7 +102,7 @@ static void up_idlepm(void)
irqstate_t flags;

#ifdef CONFIG_ESP32C3_AUTO_SLEEP
flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&g_esp32c3_idle_lock);
if (esp32c3_pm_lockstatus() == 0 &&
(esp32c3_should_skip_light_sleep() == false))
{
Expand Down Expand Up @@ -138,7 +146,7 @@ static void up_idlepm(void)
}
}

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_esp32c3_idle_lock, flags);
#else /* CONFIG_ESP32C3_AUTO_SLEEP */
static enum pm_state_e oldstate = PM_NORMAL;
enum pm_state_e newstate;
Expand All @@ -152,7 +160,7 @@ static void up_idlepm(void)

if (newstate != oldstate)
{
flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&g_esp32c3_idle_lock);

/* Perform board-specific, state-dependent logic here */

Expand All @@ -174,7 +182,7 @@ static void up_idlepm(void)
oldstate = newstate;
}

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&g_esp32c3_idle_lock, flags);

/* MCU-specific power management logic */

Expand Down
6 changes: 4 additions & 2 deletions arch/risc-v/src/esp32c3-legacy/esp32c3_lowputc.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ struct esp32c3_uart_s g_uart0_config =
.txsig = U0TXD_OUT_IDX,
.rxpin = CONFIG_ESP32C3_UART0_RXPIN,
.rxsig = U0RXD_IN_IDX,
.lock = SP_UNLOCKED,
#ifdef CONFIG_SERIAL_IFLOWCONTROL
.rtspin = CONFIG_ESP32C3_UART0_RTSPIN,
.rtssig = U0RTS_OUT_IDX,
Expand Down Expand Up @@ -118,6 +119,7 @@ struct esp32c3_uart_s g_uart1_config =
.txsig = U1TXD_OUT_IDX,
.rxpin = CONFIG_ESP32C3_UART1_RXPIN,
.rxsig = U1RXD_IN_IDX,
.lock = SP_UNLOCKED,
#ifdef CONFIG_SERIAL_IFLOWCONTROL
.rtspin = CONFIG_ESP32C3_UART1_RTSPIN,
.rtssig = U1RTS_OUT_IDX,
Expand Down Expand Up @@ -713,7 +715,7 @@ void esp32c3_lowputc_disable_all_uart_int(const struct esp32c3_uart_s *priv,
{
irqstate_t flags;

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&priv->lock);

if (current_status != NULL)
{
Expand All @@ -730,7 +732,7 @@ void esp32c3_lowputc_disable_all_uart_int(const struct esp32c3_uart_s *priv,

putreg32(0xffffffff, UART_INT_CLR_REG(priv->id));

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&priv->lock, flags);
}

/****************************************************************************
Expand Down
2 changes: 2 additions & 0 deletions arch/risc-v/src/esp32c3-legacy/esp32c3_lowputc.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <nuttx/config.h>
#include <nuttx/arch.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock.h>

#include <sys/types.h>
#include <stdint.h>
Expand Down Expand Up @@ -102,6 +103,7 @@ struct esp32c3_uart_s
uint8_t txsig; /* TX signal */
uint8_t rxpin; /* RX pin */
uint8_t rxsig; /* RX signal */
spinlock_t lock; /* Spinlock */
#ifdef CONFIG_SERIAL_IFLOWCONTROL
uint8_t rtspin; /* RTS pin number */
uint8_t rtssig; /* RTS signal */
Expand Down
12 changes: 8 additions & 4 deletions arch/risc-v/src/esp32c3-legacy/esp32c3_rtc_lowerhalf.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ struct esp32c3_lowerhalf_s
*/

const struct rtc_ops_s *ops;
spinlock_t lock;
#ifdef CONFIG_RTC_ALARM
/* Alarm callback information */

Expand Down Expand Up @@ -118,6 +119,7 @@ static const struct rtc_ops_s g_rtc_ops =
static struct esp32c3_lowerhalf_s g_rtc_lowerhalf =
{
.ops = &g_rtc_ops,
.lock = SP_UNLOCKED,
};

/****************************************************************************
Expand Down Expand Up @@ -378,6 +380,7 @@ static int rtc_lh_setalarm(struct rtc_lowerhalf_s *lower,
static int rtc_lh_setrelative(struct rtc_lowerhalf_s *lower,
const struct lower_setrelative_s *alarminfo)
{
struct esp32c3_lowerhalf_s *priv = (struct esp32c3_lowerhalf_s *)lower;
struct lower_setalarm_s setalarm;
time_t seconds;
int ret = -EINVAL;
Expand All @@ -389,7 +392,7 @@ static int rtc_lh_setrelative(struct rtc_lowerhalf_s *lower,

if (alarminfo->reltime > 0)
{
flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&priv->lock);

seconds = alarminfo->reltime;
gmtime_r(&seconds, (struct tm *)&setalarm.time);
Expand All @@ -401,7 +404,7 @@ static int rtc_lh_setrelative(struct rtc_lowerhalf_s *lower,
setalarm.priv = alarminfo->priv;
ret = rtc_lh_setalarm(lower, &setalarm);

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&priv->lock, flags);
}

return ret;
Expand Down Expand Up @@ -468,6 +471,7 @@ static int rtc_lh_cancelalarm(struct rtc_lowerhalf_s *lower, int alarmid)
static int rtc_lh_rdalarm(struct rtc_lowerhalf_s *lower,
struct lower_rdalarm_s *alarminfo)
{
struct esp32c3_lowerhalf_s *priv = (struct esp32c3_lowerhalf_s *)lower;
struct timespec ts;
int ret;
irqstate_t flags;
Expand All @@ -476,13 +480,13 @@ static int rtc_lh_rdalarm(struct rtc_lowerhalf_s *lower,
DEBUGASSERT((RTC_ALARM0 <= alarminfo->id) &&
(alarminfo->id < RTC_ALARM_LAST));

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&priv->lock);

ret = up_rtc_rdalarm(&ts, alarminfo->id);
localtime_r((const time_t *)&ts.tv_sec,
(struct tm *)alarminfo->time);

spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&priv->lock, flags);

return ret;
}
Expand Down
Loading

0 comments on commit a0763b8

Please sign in to comment.