iNEMO inertial module: 3D accelerometer and 3D gyroscope.

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   HelloWorld_ST_Sensors MOTENV_Mbed mbed-os-mqtt-client LSM6DSL_JS ... more

Files at this revision

API Documentation at this revision

Comitter:
mapellil
Date:
Mon Oct 02 09:29:50 2017 +0200
Parent:
1:c583f32fe272
Child:
3:20ccff7dd652
Commit message:
Added SPI3/3Wires sensor support, some API modifcations

Changed in this revision

LSM6DSLSensor.cpp Show annotated file Show diff for this revision Revisions of this file
LSM6DSLSensor.h Show annotated file Show diff for this revision Revisions of this file
--- a/LSM6DSLSensor.cpp	Wed Sep 27 16:50:11 2017 +0200
+++ b/LSM6DSLSensor.cpp	Mon Oct 02 09:29:50 2017 +0200
@@ -44,6 +44,26 @@
 
 /* Class Implementation ------------------------------------------------------*/
 
+LSM6DSLSensor::LSM6DSLSensor(SPI *spi, PinName cs_pin, PinName int1_pin, PinName int2_pin, SPI_type_t spi_type ) : 
+                             _dev_spi(spi), _cs_pin(cs_pin), _int1_irq(int1_pin), _int2_irq(int2_pin), _spi_type(spi_type)
+{
+    assert (spi);
+    if (cs_pin == NC) 
+    {
+        printf ("ERROR LPS22HBSensor CS MUST NOT BE NC\n\r");       
+        _dev_spi = NULL;
+        _dev_i2c=NULL;
+        return;
+    }       
+    _cs_pin = 1;    
+    _dev_i2c=NULL;
+    
+    if (_spi_type == SPI3W) LSM6DSL_ACC_GYRO_W_SPI_Mode((void *)this, LSM6DSL_ACC_GYRO_SIM_3_WIRE);
+    else LSM6DSL_ACC_GYRO_W_SPI_Mode((void *)this, LSM6DSL_ACC_GYRO_SIM_4_WIRE);
+    
+    LSM6DSL_ACC_GYRO_W_I2C_MASTER_Enable((void *)this, LSM6DSL_ACC_GYRO_MASTER_ON_DISABLED);    
+}
+
 /** Constructor
  * @param i2c object of an helper class which handles the I2C peripheral
  * @param address the address of the component's instance
@@ -52,7 +72,8 @@
                              _dev_i2c(i2c), _address(address), _cs_pin(NC), _int1_irq(int1_pin), _int2_irq(int2_pin)
 {
     assert (i2c);
-};
+    _dev_spi = NULL;
+}
 
 /**
  * @brief     Initializing the component.
--- a/LSM6DSLSensor.h	Wed Sep 27 16:50:11 2017 +0200
+++ b/LSM6DSLSensor.h	Mon Oct 02 09:29:50 2017 +0200
@@ -118,7 +118,7 @@
 } LSM6DSL_Event_Status_t;
 
 /* Class Declaration ---------------------------------------------------------*/
-
+   
 /**
  * Abstract class of an LSM6DSL Inertial Measurement Unit (IMU) 6 axes
  * sensor.
@@ -126,6 +126,8 @@
 class LSM6DSLSensor : public MotionSensor, public GyroSensor
 {
   public:
+    enum SPI_type_t {SPI3W, SPI4W};      
+    LSM6DSLSensor(SPI *spi, PinName cs_pin, PinName INT1_pin=NC, PinName INT2_pin=NC, SPI_type_t spi_type=SPI4W);  
     LSM6DSLSensor(DevI2C *i2c, uint8_t address=LSM6DSL_ACC_GYRO_I2C_ADDRESS_HIGH, PinName INT1_pin=NC, PinName INT2_pin=NC);
     virtual int init(void *init);
     virtual int read_id(uint8_t *id);
@@ -248,8 +250,27 @@
      * @retval 0 if ok, an error code otherwise.
      */
     uint8_t io_read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead)
-    {
-        return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead);
+    {        
+        if (_dev_spi) {
+        /* Write Reg Address */
+            _dev_spi->lock();
+            _cs_pin = 0;           
+            if (_spi_type == SPI4W) {            
+                _dev_spi->write(RegisterAddr | 0x80);
+                for (int i=0; i<NumByteToRead; i++) {
+                    *(pBuffer+i) = _dev_spi->write(0x00);
+                }
+            } else if (_spi_type == SPI3W){
+                /* Write RD Reg Address with RD bit*/
+                uint8_t TxByte = RegisterAddr | 0x80;    
+                _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead);
+            }            
+            _cs_pin = 1;
+            _dev_spi->unlock(); 
+            return 0;
+        }                       
+        if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead);
+        return 1;
     }
     
     /**
@@ -261,7 +282,18 @@
      */
     uint8_t io_write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite)
     {
-        return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite);
+        int data;   
+        if (_dev_spi) { 
+            _dev_spi->lock();
+            _cs_pin = 0;
+            data = _dev_spi->write(RegisterAddr);                    
+            _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0);                     
+            _cs_pin = 1;                    
+            _dev_spi->unlock();
+            return data;                    
+        }        
+        if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite);    
+        return 1;
     }
 
   private:
@@ -272,7 +304,9 @@
 
     /* Helper classes. */
     DevI2C *_dev_i2c;
-
+    SPI    *_dev_spi;
+    SPI_type_t _spi_type;
+    
     /* Configuration */
     uint8_t _address;
     DigitalOut  _cs_pin;