Ultra-compact high-performance eCompass module: ultra-low power 3D accelerometer and 3D magnetometer.

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

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

Revision:
2:e37be5550633
Parent:
1:86d530a7f949
Child:
4:6c7e83ae885e
--- a/LSM303AGRAccSensor.h	Wed Sep 27 16:49:51 2017 +0200
+++ b/LSM303AGRAccSensor.h	Mon Oct 02 09:29:33 2017 +0200
@@ -72,6 +72,7 @@
 class LSM303AGRAccSensor : public MotionSensor
 {
   public:
+    LSM303AGRAccSensor(SPI *spi, PinName cs_pin, PinName int1_pin=NC, PinName int2_pin=NC); // SPI3W ONLY
     LSM303AGRAccSensor(DevI2C *i2c, uint8_t address=LSM303AGR_ACC_I2C_ADDRESS, PinName int1_pin=NC, PinName int2_pin=NC);
     virtual int init(void *init);
     virtual int read_id(uint8_t *id);
@@ -96,7 +97,19 @@
      */
     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;           
+            /* 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;
     }
     
     /**
@@ -108,7 +121,17 @@
      */
     uint8_t io_write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite)
     {
-        return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite);
+        if (_dev_spi) { 
+            _dev_spi->lock();
+            _cs_pin = 0;
+            int 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:
@@ -120,6 +143,7 @@
 
     /* Helper classes. */
     DevI2C *_dev_i2c;
+    SPI    *_dev_spi;    
     
     /* Configuration */
     uint8_t _address;