ST / LPS22HB

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

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

Files at this revision

API Documentation at this revision

Comitter:
mapellil
Date:
Mon Oct 02 09:29:15 2017 +0200
Parent:
1:33ddb62b31fd
Child:
3:3971e9431200
Commit message:
Added SPI3/3Wires sensor support, some API modifcations

Changed in this revision

LPS22HBSensor.cpp Show annotated file Show diff for this revision Revisions of this file
LPS22HBSensor.h Show annotated file Show diff for this revision Revisions of this file
LPS22HB_driver.c Show annotated file Show diff for this revision Revisions of this file
--- a/LPS22HBSensor.cpp	Wed Sep 27 16:49:30 2017 +0200
+++ b/LPS22HBSensor.cpp	Mon Oct 02 09:29:15 2017 +0200
@@ -44,6 +44,26 @@
 
 /* Class Implementation ------------------------------------------------------*/
 
+LPS22HBSensor::LPS22HBSensor(SPI *spi, PinName cs_pin, PinName int_pin, SPI_type_t spi_type)  : _dev_spi(spi), _cs_pin(cs_pin), _int_pin(int_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) LPS22HB_Set_SpiInterface ((void *)this, LPS22HB_SPI_3_WIRE);
+    else if (_spi_type == SPI4W) LPS22HB_Set_SpiInterface ((void *)this, LPS22HB_SPI_4_WIRE);
+    
+    LPS22HB_Set_I2C ((void *)this, LPS22HB_DISABLE);
+}
+
 /** Constructor
  * @param i2c object of an helper class which handles the I2C peripheral
  * @param address the address of the component's instance
@@ -52,6 +72,8 @@
                             _dev_i2c(i2c), _address(address), _cs_pin(NC), _int_pin(int_pin)
 {
     assert (i2c);
+    _dev_spi = NULL;
+    LPS22HB_Set_I2C ((void *)this, LPS22HB_ENABLE);         
 };
 
 /**
--- a/LPS22HBSensor.h	Wed Sep 27 16:49:30 2017 +0200
+++ b/LPS22HBSensor.h	Mon Oct 02 09:29:15 2017 +0200
@@ -58,6 +58,9 @@
 class LPS22HBSensor : public PressureSensor, public TempSensor
 {
   public:
+  
+    enum SPI_type_t {SPI3W, SPI4W};  
+    LPS22HBSensor(SPI *spi, PinName cs_pin, PinName int_pin=NC, SPI_type_t spi_type=SPI4W);     
     LPS22HBSensor(DevI2C *i2c, uint8_t address=LPS22HB_ADDRESS_HIGH, PinName int_pin=NC);
     virtual int init(void *init);
     virtual int read_id(uint8_t *id);
@@ -80,7 +83,26 @@
      */
     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;
     }
     
     /**
@@ -92,7 +114,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:
@@ -101,11 +133,13 @@
 
     /* Helper classes. */
     DevI2C *_dev_i2c;
+    SPI    *_dev_spi;     
     
     /* Configuration */
     uint8_t _address;
     DigitalOut  _cs_pin; 
     InterruptIn _int_pin;    
+    SPI_type_t _spi_type;    
     
     uint8_t _is_enabled;
     float _last_odr;
--- a/LPS22HB_driver.c	Wed Sep 27 16:49:30 2017 +0200
+++ b/LPS22HB_driver.c	Mon Oct 02 09:29:15 2017 +0200
@@ -341,13 +341,13 @@
 */
 LPS22HB_Error_et LPS22HB_Set_SpiInterface(void *handle, LPS22HB_SPIMode_et spimode)
 {
-  uint8_t tmp;
-
+  uint8_t tmp=0;  //deft LPS22HB_CTRL_REG1 value
 
   LPS22HB_assert_param(IS_LPS22HB_SPIMode(spimode));
 
-  if(LPS22HB_read_reg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
-    return LPS22HB_ERROR;
+/** FIXME could not read before setting SPI mode **/	
+//  if(LPS22HB_read_reg(handle, LPS22HB_CTRL_REG1, 1, &tmp))
+//    return LPS22HB_ERROR;
 
   tmp &= ~LPS22HB_SIM_MASK;
   tmp |= (uint8_t)spimode;