esp32/esp32_spi_slave.c: Use device specific locks.

Signed-off-by: Abdelatif Guettouche <abdelatif.guettouche@espressif.com>
This commit is contained in:
Abdelatif Guettouche
2021-09-08 14:24:55 +02:00
committed by Xiang Xiao
parent 2273684cb1
commit c61009c2cf
+22 -19
View File
@@ -41,6 +41,7 @@
#include <nuttx/irq.h>
#include <nuttx/clock.h>
#include <nuttx/semaphore.h>
#include <nuttx/spinlock.h>
#include <nuttx/spi/spi.h>
#include <nuttx/spi/slave.h>
@@ -163,6 +164,8 @@ struct esp32_spislv_priv_s
/* Copy from config to speed up checking */
bool dma_chan;
spinlock_t lock; /* Device specific lock. */
};
/****************************************************************************
@@ -1009,7 +1012,7 @@ static void esp32_spislv_bind(struct spi_slave_ctrlr_s *ctrlr,
DEBUGASSERT(priv != NULL && priv->dev == NULL && dev != NULL);
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
priv->dev = dev;
@@ -1031,7 +1034,7 @@ static void esp32_spislv_bind(struct spi_slave_ctrlr_s *ctrlr,
esp32_spi_set_regbits(priv, SPI_CMD_OFFSET, SPI_USR_M);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
}
/****************************************************************************
@@ -1061,7 +1064,7 @@ static void esp32_spislv_unbind(struct spi_slave_ctrlr_s *ctrlr)
DEBUGASSERT(priv->dev != NULL);
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
up_disable_irq(priv->config->irq);
@@ -1076,7 +1079,7 @@ static void esp32_spislv_unbind(struct spi_slave_ctrlr_s *ctrlr)
priv->dev = NULL;
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
}
/****************************************************************************
@@ -1114,7 +1117,7 @@ static int esp32_spislv_enqueue(struct spi_slave_ctrlr_s *ctrlr,
ctrlr, data, nwords);
DEBUGASSERT(priv != NULL && priv->dev != NULL);
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
bufsize = SPI_SLAVE_BUFSIZE - priv->txlen;
if (!bufsize)
@@ -1135,7 +1138,7 @@ static int esp32_spislv_enqueue(struct spi_slave_ctrlr_s *ctrlr,
}
}
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
return ret;
}
@@ -1165,9 +1168,9 @@ static bool esp32_spislv_qfull(struct spi_slave_ctrlr_s *ctrlr)
spiinfo("spi_qfull(ctrlr=%p)\n", ctrlr);
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
ret = priv->txlen == SPI_SLAVE_BUFSIZE;
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
return ret;
}
@@ -1195,11 +1198,11 @@ static void esp32_spislv_qflush(struct spi_slave_ctrlr_s *ctrlr)
DEBUGASSERT(priv != NULL && priv->dev != NULL);
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
priv->rxlen = 0;
priv->txlen = 0;
priv->txen = false;
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
}
/****************************************************************************
@@ -1225,12 +1228,12 @@ static size_t esp32_spislv_qpoll(struct spi_slave_ctrlr_s *ctrlr)
DEBUGASSERT(priv != NULL && priv->dev != NULL);
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
esp32_spislv_rx(priv);
n = priv->rxlen;
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
return n;
}
@@ -1274,11 +1277,11 @@ struct spi_slave_ctrlr_s *esp32_spislv_ctrlr_initialize(int port)
spislv_dev = (struct spi_slave_ctrlr_s *)priv;
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
if ((volatile int)priv->refs != 0)
{
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
return spislv_dev;
}
@@ -1307,14 +1310,14 @@ struct spi_slave_ctrlr_s *esp32_spislv_ctrlr_initialize(int port)
{
esp32_teardown_irq(priv->cpu, priv->config->periph, priv->cpuint);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
return NULL;
}
priv->refs++;
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
return spislv_dev;
}
@@ -1345,11 +1348,11 @@ int esp32_spislv_ctrlr_uninitialize(struct spi_slave_ctrlr_s *ctrlr)
return ERROR;
}
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
if (--priv->refs)
{
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
return OK;
}
@@ -1357,7 +1360,7 @@ int esp32_spislv_ctrlr_uninitialize(struct spi_slave_ctrlr_s *ctrlr)
esp32_teardown_irq(priv->cpu, priv->config->periph, priv->cpuint);
esp32_spislv_deinit(ctrlr);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
return OK;
}