Skip to content

Commit

Permalink
Merge pull request #9 from cparata/master
Browse files Browse the repository at this point in the history
Add begin and end APIs
  • Loading branch information
cparata committed Jan 25, 2021
2 parents 960ee9c + fccea8d commit c4f7f9f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 91 deletions.
32 changes: 18 additions & 14 deletions README.md
Expand Up @@ -6,32 +6,36 @@ Arduino library to support the LSM6DSO 3D accelerometer and 3D gyroscope
This sensor uses I2C or SPI to communicate.
For I2C it is then required to create a TwoWire interface before accessing to the sensors:

dev_i2c = new TwoWire(I2C_SDA, I2C_SCL);
dev_i2c->begin();
TwoWire dev_i2c(I2C_SDA, I2C_SCL);
dev_i2c.begin();

For SPI it is then required to create a SPI interface before accessing to the sensors:

dev_spi = new SPIClass(SPI_MOSI, SPI_MISO, SPI_SCK);
dev_spi->begin();
SPIClass dev_spi(SPI_MOSI, SPI_MISO, SPI_SCK);
dev_spi.begin();

An instance can be created and enbaled when the I2C bus is used following the procedure below:
An instance can be created and enabled when the I2C bus is used following the procedure below:

AccGyr = new LSM6DSOSensor(dev_i2c);
AccGyr->Enable_X();
AccGyr->Enable_G();
LSM6DSOSensor AccGyr(&dev_i2c);
AccGyr.begin();
AccGyr.Enable_X();
AccGyr.Enable_G();

An instance can be created and enbaled when the SPI bus is used following the procedure below:
An instance can be created and enabled when the SPI bus is used following the procedure below:

AccGyr = new LSM6DSOSensor(dev_spi, CS_PIN);
AccGyr->Enable_X();
AccGyr->Enable_G();
LSM6DSOSensor AccGyr(&dev_spi, CS_PIN);
AccGyr.begin();
AccGyr.Enable_X();
AccGyr.Enable_G();

The access to the sensor values is done as explained below:

Read accelerometer and gyroscope.

AccGyr->Get_X_Axes(accelerometer);
AccGyr->Get_G_Axes(gyroscope);
int32_t accelerometer[3];
int32_t gyroscope[3];
AccGyr.Get_X_Axes(accelerometer);
AccGyr.Get_G_Axes(gyroscope);

## Documentation

Expand Down
2 changes: 2 additions & 0 deletions keywords.txt
Expand Up @@ -12,6 +12,8 @@ LSM6DSOSensor KEYWORD1
# Methods and Functions (KEYWORD2)
#######################################

begin KEYWORD2
end KEYWORD2
ReadID KEYWORD2
Enable_X KEYWORD2
Disable_X KEYWORD2
Expand Down
2 changes: 1 addition & 1 deletion library.properties
@@ -1,5 +1,5 @@
name=STM32duino LSM6DSO
version=1.1.1
version=2.0.0
author=SRA
maintainer=stm32duino
sentence=Ultra Low Power inertial measurement unit.
Expand Down
129 changes: 53 additions & 76 deletions src/LSM6DSOSensor.cpp
Expand Up @@ -54,67 +54,8 @@ LSM6DSOSensor::LSM6DSOSensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), addr
reg_ctx.write_reg = LSM6DSO_io_write;
reg_ctx.read_reg = LSM6DSO_io_read;
reg_ctx.handle = (void *)this;

/* Disable I3C */
if (lsm6dso_i3c_disable_set(&reg_ctx, LSM6DSO_I3C_DISABLE) != LSM6DSO_OK)
{
return;
}

/* Enable register address automatically incremented during a multiple byte
access with a serial interface. */
if (lsm6dso_auto_increment_set(&reg_ctx, PROPERTY_ENABLE) != LSM6DSO_OK)
{
return;
}

/* Enable BDU */
if (lsm6dso_block_data_update_set(&reg_ctx, PROPERTY_ENABLE) != LSM6DSO_OK)
{
return;
}

/* FIFO mode selection */
if (lsm6dso_fifo_mode_set(&reg_ctx, LSM6DSO_BYPASS_MODE) != LSM6DSO_OK)
{
return;
}

/* Select default output data rate. */
acc_odr = LSM6DSO_XL_ODR_104Hz;

/* Output data rate selection - power down. */
if (lsm6dso_xl_data_rate_set(&reg_ctx, LSM6DSO_XL_ODR_OFF) != LSM6DSO_OK)
{
return;
}

/* Full scale selection. */
if (lsm6dso_xl_full_scale_set(&reg_ctx, LSM6DSO_2g) != LSM6DSO_OK)
{
return;
}

/* Select default output data rate. */
gyro_odr = LSM6DSO_GY_ODR_104Hz;

/* Output data rate selection - power down. */
if (lsm6dso_gy_data_rate_set(&reg_ctx, LSM6DSO_GY_ODR_OFF) != LSM6DSO_OK)
{
return;
}

/* Full scale selection. */
if (lsm6dso_gy_full_scale_set(&reg_ctx, LSM6DSO_2000dps) != LSM6DSO_OK)
{
return;
}

acc_is_enabled = 0;
gyro_is_enabled = 0;


return;
}

/** Constructor
Expand All @@ -126,37 +67,49 @@ LSM6DSOSensor::LSM6DSOSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : de
{
reg_ctx.write_reg = LSM6DSO_io_write;
reg_ctx.read_reg = LSM6DSO_io_read;
reg_ctx.handle = (void *)this;

// Configure CS pin
pinMode(cs_pin, OUTPUT);
digitalWrite(cs_pin, HIGH);
reg_ctx.handle = (void *)this;
dev_i2c = NULL;
address = 0;
acc_is_enabled = 0U;
gyro_is_enabled = 0U;
}

/**
* @brief Configure the sensor in order to be used
* @retval 0 in case of success, an error code otherwise
*/
LSM6DSOStatusTypeDef LSM6DSOSensor::begin()
{
if(dev_spi)
{
// Configure CS pin
pinMode(cs_pin, OUTPUT);
digitalWrite(cs_pin, HIGH);
}

/* Disable I3C */
if (lsm6dso_i3c_disable_set(&reg_ctx, LSM6DSO_I3C_DISABLE) != LSM6DSO_OK)
{
return;
return LSM6DSO_ERROR;
}

/* Enable register address automatically incremented during a multiple byte
access with a serial interface. */
if (lsm6dso_auto_increment_set(&reg_ctx, PROPERTY_ENABLE) != LSM6DSO_OK)
{
return;
return LSM6DSO_ERROR;
}

/* Enable BDU */
if (lsm6dso_block_data_update_set(&reg_ctx, PROPERTY_ENABLE) != LSM6DSO_OK)
{
return;
return LSM6DSO_ERROR;
}

/* FIFO mode selection */
if (lsm6dso_fifo_mode_set(&reg_ctx, LSM6DSO_BYPASS_MODE) != LSM6DSO_OK)
{
return;
return LSM6DSO_ERROR;
}

/* Select default output data rate. */
Expand All @@ -165,13 +118,13 @@ LSM6DSOSensor::LSM6DSOSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : de
/* Output data rate selection - power down. */
if (lsm6dso_xl_data_rate_set(&reg_ctx, LSM6DSO_XL_ODR_OFF) != LSM6DSO_OK)
{
return;
return LSM6DSO_ERROR;
}

/* Full scale selection. */
if (lsm6dso_xl_full_scale_set(&reg_ctx, LSM6DSO_2g) != LSM6DSO_OK)
{
return;
return LSM6DSO_ERROR;
}

/* Select default output data rate. */
Expand All @@ -180,23 +133,47 @@ LSM6DSOSensor::LSM6DSOSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : de
/* Output data rate selection - power down. */
if (lsm6dso_gy_data_rate_set(&reg_ctx, LSM6DSO_GY_ODR_OFF) != LSM6DSO_OK)
{
return;
return LSM6DSO_ERROR;
}

/* Full scale selection. */
if (lsm6dso_gy_full_scale_set(&reg_ctx, LSM6DSO_2000dps) != LSM6DSO_OK)
{
return;
return LSM6DSO_ERROR;
}

acc_is_enabled = 0;
gyro_is_enabled = 0;


return;

return LSM6DSO_OK;
}

/**
* @brief Disable the sensor and relative resources
* @retval 0 in case of success, an error code otherwise
*/
LSM6DSOStatusTypeDef LSM6DSOSensor::end()
{
/* Disable both acc and gyro */
if (Disable_X() != LSM6DSO_OK)
{
return LSM6DSO_ERROR;
}

if (Disable_G() != LSM6DSO_OK)
{
return LSM6DSO_ERROR;
}

/* Reset CS configuration */
if(dev_spi)
{
// Configure CS pin
pinMode(cs_pin, INPUT);
}

return LSM6DSO_OK;
}

/**
* @brief Read component ID
Expand Down
2 changes: 2 additions & 0 deletions src/LSM6DSOSensor.h
Expand Up @@ -114,6 +114,8 @@ class LSM6DSOSensor
public:
LSM6DSOSensor(TwoWire *i2c, uint8_t address=LSM6DSO_I2C_ADD_H);
LSM6DSOSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed=2000000);
LSM6DSOStatusTypeDef begin();
LSM6DSOStatusTypeDef end();
LSM6DSOStatusTypeDef ReadID(uint8_t *Id);
LSM6DSOStatusTypeDef Enable_X();
LSM6DSOStatusTypeDef Disable_X();
Expand Down

0 comments on commit c4f7f9f

Please sign in to comment.