drivers/sensors/msa301: switch from semaphore to mutex for exclusive access

Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
This commit is contained in:
Petro Karashchenko 2023-01-25 22:57:46 +02:00 committed by Xiang Xiao
parent a8f4d8447f
commit 6e3cb04531

View File

@ -30,6 +30,7 @@
#include <stdio.h>
#include <nuttx/kmalloc.h>
#include <nuttx/signal.h>
#include <nuttx/mutex.h>
#include <nuttx/fs/fs.h>
#include <nuttx/i2c/i2c_master.h>
#include <nuttx/sensors/msa301.h>
@ -46,7 +47,7 @@ struct msa301_dev_s
uint8_t addr; /* I2C address */
msa301_range_t range;
FAR const struct msa301_ops_s *ops;
sem_t exclsem;
mutex_t lock;
struct msa301_sensor_data_s sensor_data; /* Sensor data container */
};
@ -99,9 +100,8 @@ static int msa301_ioctl(FAR struct file *filep, int cmd,
/* Common Register Function */
static int msa301_register(FAR const char * devpath,
FAR struct i2c_master_s * i2c,
uint8_t addr,
static int msa301_register(FAR const char *devpath,
FAR struct i2c_master_s *i2c, uint8_t addr,
FAR const struct msa301_ops_s *ops);
/****************************************************************************
@ -487,7 +487,7 @@ static int msa301_open(FAR struct file *filep)
DEBUGASSERT(priv != NULL);
nxsem_wait(&priv->exclsem);
nxmutex_lock(&priv->lock);
priv->ops->start(priv);
@ -517,7 +517,7 @@ static int msa301_close(FAR struct file *filep)
priv->ops->stop(priv);
nxsem_post(&priv->exclsem);
nxmutex_unlock(&priv->lock);
return OK;
}
@ -651,9 +651,8 @@ static int msa301_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
*
****************************************************************************/
static int msa301_register(FAR const char * devpath,
FAR struct i2c_master_s * i2c,
uint8_t addr,
static int msa301_register(FAR const char *devpath,
FAR struct i2c_master_s *i2c, uint8_t addr,
FAR const struct msa301_ops_s *ops)
{
FAR struct msa301_dev_s *priv;
@ -698,7 +697,7 @@ static int msa301_register(FAR const char * devpath,
return ret;
}
nxsem_init(&priv->exclsem, 0, 1);
nxmutex_init(&priv->lock);
/* Register the character driver */
@ -706,7 +705,7 @@ static int msa301_register(FAR const char * devpath,
if (ret < 0)
{
snerr("ERROR: Failed to register driver: %d\n", ret);
nxsem_destroy(&priv->exclsem);
nxmutex_destroy(&priv->lock);
kmm_free(priv);
return ret;
}
@ -738,8 +737,8 @@ static int msa301_register(FAR const char * devpath,
int msa301_sensor_register(FAR const char * devpath,
FAR struct i2c_master_s *i2c)
{
return msa301_register(devpath, i2c,
MSA301_ACCEL_ADDR0, &g_msa301_sensor_ops);
return msa301_register(devpath, i2c, MSA301_ACCEL_ADDR0,
&g_msa301_sensor_ops);
}
#endif /* CONFIG_I2C && CONFIG_SENSORS_MSA301 */