esp32/esp32_i2c.c: Use device specific locks.

Signed-off-by: Abdelatif Guettouche <abdelatif.guettouche@espressif.com>
This commit is contained in:
Abdelatif Guettouche 2021-08-30 11:44:01 +02:00 committed by Xiang Xiao
parent 0af9a49d9c
commit 0123243f9a

View File

@ -41,6 +41,7 @@
#include <nuttx/irq.h>
#include <nuttx/clock.h>
#include <nuttx/semaphore.h>
#include <nuttx/spinlock.h>
#include <nuttx/i2c/i2c_master.h>
#include <arch/board/board.h>
@ -219,6 +220,8 @@ struct esp32_i2c_priv_s
uint32_t clk_freq; /* Current I2C Clock frequency */
spinlock_t lock; /* Device specific lock */
/* I2C trace support */
#ifdef CONFIG_I2C_TRACE
@ -1172,7 +1175,7 @@ static int esp32_i2c_reset(struct i2c_master_s *dev)
DEBUGASSERT(priv->refs > 0);
flags = enter_critical_section();
flags = spin_lock_irqsave(&priv->lock);
esp32_i2c_deinit(priv);
@ -1185,7 +1188,7 @@ static int esp32_i2c_reset(struct i2c_master_s *dev)
priv->bytes = 0;
priv->ready_read = 0;
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
return OK;
}
@ -1428,7 +1431,7 @@ static inline void esp32_i2c_process(struct esp32_i2c_priv_s *priv,
esp32_i2c_traceevent(priv, I2CEVENT_ERROR, priv->error,
esp32_i2c_get_reg(priv, I2C_SR_OFFSET));
esp32_i2c_set_reg(priv, I2C_INT_ENA_OFFSET, 0);
#ifndef CONFIG_I2C_POLLED
#ifndef CONFIG_I2C_POLLED
nxsem_post(&priv->sem_isr);
#endif
}
@ -1553,11 +1556,11 @@ struct i2c_master_s *esp32_i2cbus_initialize(int port)
return NULL;
}
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 (struct i2c_master_s *)priv;
}
@ -1574,7 +1577,7 @@ struct i2c_master_s *esp32_i2cbus_initialize(int port)
{
/* Failed to allocate a CPU interrupt of this type */
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
return NULL;
}
@ -1584,7 +1587,7 @@ struct i2c_master_s *esp32_i2cbus_initialize(int port)
{
esp32_teardown_irq(priv->cpu, config->periph, priv->cpuint);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
return NULL;
}
@ -1596,7 +1599,7 @@ struct i2c_master_s *esp32_i2cbus_initialize(int port)
esp32_i2c_init(priv);
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
return (struct i2c_master_s *)priv;
}
@ -1621,15 +1624,15 @@ int esp32_i2cbus_uninitialize(struct i2c_master_s *dev)
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;
}
leave_critical_section(flags);
spin_unlock_irqrestore(&priv->lock, flags);
#ifndef CONFIG_I2C_POLLED
up_disable_irq(priv->config->irq);