From c4cf1eeb2b16c471e41b2fa65e34ae06d9ec5fa9 Mon Sep 17 00:00:00 2001 From: Petro Karashchenko Date: Wed, 25 Jan 2023 23:00:34 +0200 Subject: [PATCH] arch/xtensa/esp32s2: switch from semaphore to mutex for exclusive access Signed-off-by: Petro Karashchenko --- arch/xtensa/src/esp32s2/esp32s2_spi.c | 47 +++++++++------------------ 1 file changed, 16 insertions(+), 31 deletions(-) diff --git a/arch/xtensa/src/esp32s2/esp32s2_spi.c b/arch/xtensa/src/esp32s2/esp32s2_spi.c index 990d91bf4d..36c823a9a3 100644 --- a/arch/xtensa/src/esp32s2/esp32s2_spi.c +++ b/arch/xtensa/src/esp32s2/esp32s2_spi.c @@ -38,7 +38,7 @@ #include #include #include -#include +#include #include #include @@ -150,7 +150,7 @@ struct esp32s2_spi_priv_s const struct esp32s2_spi_config_s *config; int refs; /* Reference count */ - sem_t exclsem; /* Held while chip is selected for mutual exclusion */ + mutex_t lock; /* Held while chip is selected for mutual exclusion */ #if defined(CONFIG_ESP32S2_SPI2_DMA) || defined(CONFIG_ESP32S2_SPI3_DMA) sem_t sem_isr; /* Interrupt wait semaphore */ int cpuint; /* SPI interrupt ID */ @@ -356,7 +356,7 @@ static struct esp32s2_spi_priv_s esp32s2_spi2_priv = }, .config = &esp32s2_spi2_config, .refs = 0, - .exclsem = SEM_INITIALIZER(0), + .lock = NXMUTEX_INITIALIZER, #ifdef CONFIG_ESP32S2_SPI2_DMA .sem_isr = SEM_INITIALIZER(0), .cpuint = -ENOMEM, @@ -441,7 +441,7 @@ static struct esp32s2_spi_priv_s esp32s2_spi3_priv = }, .config = &esp32s2_spi3_config, .refs = 0, - .exclsem = SEM_INITIALIZER(0), + .lock = NXMUTEX_INITIALIZER, #ifdef CONFIG_ESP32S2_SPI3_DMA .sem_isr = SEM_INITIALIZER(0), .cpuint = -ENOMEM, @@ -563,11 +563,11 @@ static int esp32s2_spi_lock(struct spi_dev_s *dev, bool lock) if (lock) { - ret = nxsem_wait_uninterruptible(&priv->exclsem); + ret = nxmutex_lock(&priv->lock); } else { - ret = nxsem_post(&priv->exclsem); + ret = nxmutex_unlock(&priv->lock); } return ret; @@ -895,7 +895,7 @@ static int esp32s2_spi_hwfeatures(struct spi_dev_s *dev, * uint16_t's * * Returned Value: - * None + * None. * ****************************************************************************/ @@ -1365,10 +1365,6 @@ static void esp32s2_spi_init(struct spi_dev_s *dev) const uint32_t id = config->id; uint32_t regval; - /* Initialize the SPI semaphore that enforces mutually exclusive access */ - - nxsem_init(&priv->exclsem, 0, 1); - esp32s2_gpiowrite(config->cs_pin, true); esp32s2_gpiowrite(config->mosi_pin, true); esp32s2_gpiowrite(config->miso_pin, true); @@ -1519,7 +1515,6 @@ struct spi_dev_s *esp32s2_spibus_initialize(int port) { struct spi_dev_s *spi_dev; struct esp32s2_spi_priv_s *priv; - irqstate_t flags; switch (port) { @@ -1539,12 +1534,11 @@ struct spi_dev_s *esp32s2_spibus_initialize(int port) spi_dev = (struct spi_dev_s *)priv; - flags = enter_critical_section(); - + nxmutex_lock(&priv->lock); if (priv->refs != 0) { - leave_critical_section(flags); - + priv->refs++; + nxmutex_unlock(&priv->lock); return spi_dev; } @@ -1571,8 +1565,7 @@ struct spi_dev_s *esp32s2_spibus_initialize(int port) { /* Failed to allocate a CPU interrupt of this type. */ - leave_critical_section(flags); - + nxmutex_unlock(&priv->lock); return NULL; } @@ -1584,7 +1577,7 @@ struct spi_dev_s *esp32s2_spibus_initialize(int port) esp32s2_teardown_irq(priv->config->periph, priv->cpuint); priv->cpuint = -ENOMEM; - leave_critical_section(flags); + nxmutex_unlock(&priv->lock); return NULL; } @@ -1595,11 +1588,9 @@ struct spi_dev_s *esp32s2_spibus_initialize(int port) #endif esp32s2_spi_init(spi_dev); - priv->refs++; - leave_critical_section(flags); - + nxmutex_unlock(&priv->lock); return spi_dev; } @@ -1619,7 +1610,6 @@ struct spi_dev_s *esp32s2_spibus_initialize(int port) int esp32s2_spibus_uninitialize(struct spi_dev_s *dev) { - irqstate_t flags; struct esp32s2_spi_priv_s *priv = (struct esp32s2_spi_priv_s *)dev; DEBUGASSERT(dev); @@ -1629,16 +1619,13 @@ int esp32s2_spibus_uninitialize(struct spi_dev_s *dev) return ERROR; } - flags = enter_critical_section(); - + nxmutex_lock(&priv->lock); if (--priv->refs != 0) { - leave_critical_section(flags); + nxmutex_unlock(&priv->lock); return OK; } - leave_critical_section(flags); - #if defined(CONFIG_ESP32S2_SPI2_DMA) || defined(CONFIG_ESP32S2_SPI3_DMA) up_disable_irq(priv->config->irq); esp32s2_teardown_irq(priv->config->periph, priv->cpuint); @@ -1646,12 +1633,10 @@ int esp32s2_spibus_uninitialize(struct spi_dev_s *dev) priv->cpuint = -ENOMEM; - nxsem_destroy(&priv->sem_isr); #endif esp32s2_spi_deinit(dev); - - nxsem_destroy(&priv->exclsem); + nxmutex_unlock(&priv->lock); return OK; }