diff --git a/README.md b/README.md index 5dbad3a..260a80c 100644 --- a/README.md +++ b/README.md @@ -6,30 +6,34 @@ Arduino library to support the LSM6DSR automotive 3D accelerometer and 3D gyrosc 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 LSM6DSRSensor(dev_i2c); - AccGyr->Enable_X(); - AccGyr->Enable_G(); + LSM6DSRSensor 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 LSM6DSRSensor(dev_spi, CS_PIN); - AccGyr->Enable_X(); - AccGyr->Enable_G(); + LSM6DSRSensor 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. + int32_t accelerometer[3]; + int32_t gyroscope[3]; AccGyr->Get_X_Axes(accelerometer); AccGyr->Get_G_Axes(gyroscope); diff --git a/examples/LSM6DSR_I2C_HelloWorld/LSM6DSR_I2C_HelloWorld.ino b/examples/LSM6DSR_I2C_HelloWorld/LSM6DSR_I2C_HelloWorld.ino index 110b350..890d654 100644 --- a/examples/LSM6DSR_I2C_HelloWorld/LSM6DSR_I2C_HelloWorld.ino +++ b/examples/LSM6DSR_I2C_HelloWorld/LSM6DSR_I2C_HelloWorld.ino @@ -56,7 +56,7 @@ #define INT_1 A5 // Components -LSM6DSRSensor *AccGyr; +LSM6DSRSensor AccGyr(&DEV_I2C, LSM6DSR_I2C_ADD_L); void setup() { // Led. @@ -75,9 +75,9 @@ void setup() { // Initialize I2C bus. DEV_I2C.begin(); - AccGyr = new LSM6DSRSensor (&DEV_I2C, LSM6DSR_I2C_ADD_L); - AccGyr->Enable_X(); - AccGyr->Enable_G(); + AccGyr.begin(); + AccGyr.Enable_X(); + AccGyr.Enable_G(); } void loop() { @@ -90,8 +90,8 @@ void loop() { // Read accelerometer and gyroscope. int32_t accelerometer[3]; int32_t gyroscope[3]; - AccGyr->Get_X_Axes(accelerometer); - AccGyr->Get_G_Axes(gyroscope); + AccGyr.Get_X_Axes(accelerometer); + AccGyr.Get_G_Axes(gyroscope); // Output data. SerialPort.print("LSM6DSR: | Acc[mg]: "); diff --git a/examples/LSM6DSR_SPI_HelloWorld/LSM6DSR_SPI_HelloWorld.ino b/examples/LSM6DSR_SPI_HelloWorld/LSM6DSR_SPI_HelloWorld.ino index 9afc8f1..75109f6 100644 --- a/examples/LSM6DSR_SPI_HelloWorld/LSM6DSR_SPI_HelloWorld.ino +++ b/examples/LSM6DSR_SPI_HelloWorld/LSM6DSR_SPI_HelloWorld.ino @@ -46,10 +46,10 @@ #define SerialPort Serial // SPI -SPIClass *dev_spi; +SPIClass dev_spi(D11, D12, D3); // Components -LSM6DSRSensor *AccGyr; +LSM6DSRSensor AccGyr(&dev_spi, D10); void setup() { // Led. @@ -59,12 +59,11 @@ void setup() { SerialPort.begin(115200); // Initialize SPI bus. - dev_spi = new SPIClass(D11, D12, D3); - dev_spi->begin(); + dev_spi.begin(); - AccGyr = new LSM6DSRSensor (dev_spi, D10); - AccGyr->Enable_X(); - AccGyr->Enable_G(); + AccGyr.begin(); + AccGyr.Enable_X(); + AccGyr.Enable_G(); } void loop() { @@ -77,8 +76,8 @@ void loop() { // Read accelerometer and gyroscope. int32_t accelerometer[3]; int32_t gyroscope[3]; - AccGyr->Get_X_Axes(accelerometer); - AccGyr->Get_G_Axes(gyroscope); + AccGyr.Get_X_Axes(accelerometer); + AccGyr.Get_G_Axes(gyroscope); // Output data. SerialPort.print("LSM6DSR: | Acc[mg]: "); diff --git a/keywords.txt b/keywords.txt index eebba9a..13985cd 100644 --- a/keywords.txt +++ b/keywords.txt @@ -12,6 +12,8 @@ LSM6DSRSensor KEYWORD1 # Methods and Functions (KEYWORD2) ####################################### +begin KEYWORD2 +end KEYWORD2 ReadID KEYWORD2 Enable_X KEYWORD2 Disable_X KEYWORD2 diff --git a/library.properties b/library.properties index d01f280..fc87b7b 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=STM32duino LSM6DSR -version=1.0.1 +version=2.0.0 author=SRA maintainer=stm32duino sentence=iNEMO inertial measurement unit. diff --git a/src/LSM6DSRSensor.cpp b/src/LSM6DSRSensor.cpp index c4345e1..ae76998 100644 --- a/src/LSM6DSRSensor.cpp +++ b/src/LSM6DSRSensor.cpp @@ -54,66 +54,8 @@ LSM6DSRSensor::LSM6DSRSensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), addr reg_ctx.write_reg = LSM6DSR_io_write; reg_ctx.read_reg = LSM6DSR_io_read; reg_ctx.handle = (void *)this; - - /* Disable I3C */ - if (lsm6dsr_i3c_disable_set(®_ctx, LSM6DSR_I3C_DISABLE) != LSM6DSR_OK) - { - return; - } - - /* Enable register address automatically incremented during a multiple byte - access with a serial interface. */ - if (lsm6dsr_auto_increment_set(®_ctx, PROPERTY_ENABLE) != LSM6DSR_OK) - { - return; - } - - /* Enable BDU */ - if (lsm6dsr_block_data_update_set(®_ctx, PROPERTY_ENABLE) != LSM6DSR_OK) - { - return; - } - - /* FIFO mode selection */ - if (lsm6dsr_fifo_mode_set(®_ctx, LSM6DSR_BYPASS_MODE) != LSM6DSR_OK) - { - return; - } - - /* Select default output data rate. */ - acc_odr = LSM6DSR_XL_ODR_104Hz; - - /* Output data rate selection - power down. */ - if (lsm6dsr_xl_data_rate_set(®_ctx, LSM6DSR_XL_ODR_OFF) != LSM6DSR_OK) - { - return; - } - - /* Full scale selection. */ - if (lsm6dsr_xl_full_scale_set(®_ctx, LSM6DSR_2g) != LSM6DSR_OK) - { - return; - } - - /* Select default output data rate. */ - gyro_odr = LSM6DSR_GY_ODR_104Hz; - - /* Output data rate selection - power down. */ - if (lsm6dsr_gy_data_rate_set(®_ctx, LSM6DSR_GY_ODR_OFF) != LSM6DSR_OK) - { - return; - } - - /* Full scale selection. */ - if (lsm6dsr_gy_full_scale_set(®_ctx, LSM6DSR_2000dps) != LSM6DSR_OK) - { - return; - } - - acc_is_enabled = 0; - gyro_is_enabled = 0; - - return; + acc_is_enabled = 0U; + gyro_is_enabled = 0U; } /** Constructor @@ -126,36 +68,48 @@ LSM6DSRSensor::LSM6DSRSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : de reg_ctx.write_reg = LSM6DSR_io_write; reg_ctx.read_reg = LSM6DSR_io_read; reg_ctx.handle = (void *)this; - - // Configure CS pin - pinMode(cs_pin, OUTPUT); - digitalWrite(cs_pin, HIGH); dev_i2c = NULL; - address = 0; + address = 0U; + 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 + */ +LSM6DSRStatusTypeDef LSM6DSRSensor::begin() +{ + if(dev_spi) + { + // Configure CS pin + pinMode(cs_pin, OUTPUT); + digitalWrite(cs_pin, HIGH); + } /* Disable I3C */ if (lsm6dsr_i3c_disable_set(®_ctx, LSM6DSR_I3C_DISABLE) != LSM6DSR_OK) { - return; + return LSM6DSR_ERROR; } /* Enable register address automatically incremented during a multiple byte access with a serial interface. */ if (lsm6dsr_auto_increment_set(®_ctx, PROPERTY_ENABLE) != LSM6DSR_OK) { - return; + return LSM6DSR_ERROR; } /* Enable BDU */ if (lsm6dsr_block_data_update_set(®_ctx, PROPERTY_ENABLE) != LSM6DSR_OK) { - return; + return LSM6DSR_ERROR; } /* FIFO mode selection */ if (lsm6dsr_fifo_mode_set(®_ctx, LSM6DSR_BYPASS_MODE) != LSM6DSR_OK) { - return; + return LSM6DSR_ERROR; } /* Select default output data rate. */ @@ -164,13 +118,13 @@ LSM6DSRSensor::LSM6DSRSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : de /* Output data rate selection - power down. */ if (lsm6dsr_xl_data_rate_set(®_ctx, LSM6DSR_XL_ODR_OFF) != LSM6DSR_OK) { - return; + return LSM6DSR_ERROR; } /* Full scale selection. */ if (lsm6dsr_xl_full_scale_set(®_ctx, LSM6DSR_2g) != LSM6DSR_OK) { - return; + return LSM6DSR_ERROR; } /* Select default output data rate. */ @@ -179,21 +133,47 @@ LSM6DSRSensor::LSM6DSRSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : de /* Output data rate selection - power down. */ if (lsm6dsr_gy_data_rate_set(®_ctx, LSM6DSR_GY_ODR_OFF) != LSM6DSR_OK) { - return; + return LSM6DSR_ERROR; } /* Full scale selection. */ if (lsm6dsr_gy_full_scale_set(®_ctx, LSM6DSR_2000dps) != LSM6DSR_OK) { - return; + return LSM6DSR_ERROR; } acc_is_enabled = 0; gyro_is_enabled = 0; - return; + return LSM6DSR_OK; } +/** + * @brief Disable the sensor and relative resources + * @retval 0 in case of success, an error code otherwise + */ +LSM6DSRStatusTypeDef LSM6DSRSensor::end() +{ + /* Disable both acc and gyro */ + if (Disable_X() != LSM6DSR_OK) + { + return LSM6DSR_ERROR; + } + + if (Disable_G() != LSM6DSR_OK) + { + return LSM6DSR_ERROR; + } + + /* Reset CS configuration */ + if(dev_spi) + { + // Configure CS pin + pinMode(cs_pin, INPUT); + } + + return LSM6DSR_OK; +} /** * @brief Read component ID diff --git a/src/LSM6DSRSensor.h b/src/LSM6DSRSensor.h index b8a0bbb..035e38e 100644 --- a/src/LSM6DSRSensor.h +++ b/src/LSM6DSRSensor.h @@ -96,6 +96,8 @@ class LSM6DSRSensor public: LSM6DSRSensor(TwoWire *i2c, uint8_t address=LSM6DSR_I2C_ADD_H); LSM6DSRSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed=2000000); + LSM6DSRStatusTypeDef begin(); + LSM6DSRStatusTypeDef end(); LSM6DSRStatusTypeDef ReadID(uint8_t *Id); LSM6DSRStatusTypeDef Enable_X(); LSM6DSRStatusTypeDef Disable_X();