diff --git a/README.md b/README.md index 65f5449..603b6a2 100644 --- a/README.md +++ b/README.md @@ -3,22 +3,37 @@ Arduino library to support the LPS25HB 260-1260 hPa absolute digital output baro ## API -This sensor uses I2C to communicate. It is then required to create a TwoWire interface before accessing to the sensors: +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(I2C2_SDA, I2C2_SCL); - dev_i2c->begin(); + TwoWire dev_i2c(I2C2_SDA, I2C2_SCL); + dev_i2c.begin(); -An instance can be created and enbaled following the procedure below: +For SPI it is then required to create a SPI interface before accessing to the sensors: - PressTemp = new LPS25HBSensor(dev_i2c); - PressTemp->Enable(); + SPIClass dev_spi(SPI_MOSI, SPI_MISO, SPI_SCK); + dev_spi.begin(); + +An instance can be created and enabled when the I2C bus is used following the procedure below: + + LPS25HBSensor PressTemp(&dev_i2c); + PressTemp.begin(); + PressTemp.Enable(); + +An instance can be created and enabled when the SPI bus is used following the procedure below: + + LPS25HBSensor PressTemp(&dev_spi, CS_PIN); + PressTemp.begin(); + PressTemp.Enable(); The access to the sensor values is done as explained below: Read pressure and temperature. - PressTemp->GetPressure(&pressure); - PressTemp->GetTemperature(&temperature); + float pressure; + float temperature; + PressTemp.GetPressure(&pressure); + PressTemp.GetTemperature(&temperature); ## Documentation diff --git a/examples/X_NUCLEO_IKS01A1_LPS25HB_DataLog_Terminal/X_NUCLEO_IKS01A1_LPS25HB_DataLog_Terminal.ino b/examples/X_NUCLEO_IKS01A1_LPS25HB_DataLog_Terminal/X_NUCLEO_IKS01A1_LPS25HB_DataLog_Terminal.ino index 8d6e624..33100b5 100644 --- a/examples/X_NUCLEO_IKS01A1_LPS25HB_DataLog_Terminal/X_NUCLEO_IKS01A1_LPS25HB_DataLog_Terminal.ino +++ b/examples/X_NUCLEO_IKS01A1_LPS25HB_DataLog_Terminal/X_NUCLEO_IKS01A1_LPS25HB_DataLog_Terminal.ino @@ -51,7 +51,7 @@ #endif // Components. -LPS25HBSensor *PressTemp; +LPS25HBSensor PressTemp(&DEV_I2C); void setup() { // Led. @@ -63,9 +63,9 @@ void setup() { // Initialize I2C bus. DEV_I2C.begin(); - // Initlialize components. - PressTemp = new LPS25HBSensor (&DEV_I2C); - PressTemp->Enable(); + // Initialize components. + PressTemp.begin(); + PressTemp.Enable(); } void loop() { @@ -77,8 +77,8 @@ void loop() { // Read pressure and temperature. float pressure, temperature; - PressTemp->GetPressure(&pressure); - PressTemp->GetTemperature(&temperature); + PressTemp.GetPressure(&pressure); + PressTemp.GetTemperature(&temperature); // Output data. SerialPort.print("| Pres[hPa]: "); diff --git a/keywords.txt b/keywords.txt index 1d16840..18cf4b2 100644 --- a/keywords.txt +++ b/keywords.txt @@ -12,18 +12,20 @@ LPS25HBSensor KEYWORD1 # Methods and Functions (KEYWORD2) ####################################### -Enable KEYWORD2 -Disable KEYWORD2 -ReadID KEYWORD2 -Reset KEYWORD2 -GetTemperature KEYWORD2 -GetODR KEYWORD2 -SetODR KEYWORD2 -ReadReg KEYWORD2 -WriteReg KEYWORD2 -IO_Read KEYWORD2 -IO_Write KEYWORD2 -GetPressure KEYWORD2 +begin KEYWORD2 +end KEYWORD2 +Enable KEYWORD2 +Disable KEYWORD2 +ReadID KEYWORD2 +Reset KEYWORD2 +GetTemperature KEYWORD2 +GetODR KEYWORD2 +SetODR KEYWORD2 +ReadReg KEYWORD2 +WriteReg KEYWORD2 +IO_Read KEYWORD2 +IO_Write KEYWORD2 +GetPressure KEYWORD2 ####################################### diff --git a/library.properties b/library.properties index 561e6a6..dafa2e4 100644 --- a/library.properties +++ b/library.properties @@ -1,9 +1,9 @@ name=STM32duino LPS25HB -version=1.0.3 +version=2.0.0 author=AST maintainer=stm32duino sentence=260-1260 hPa absolute digital output barometer. paragraph=This library provides Arduino support for the 260-1260 hPa absolute digital output barometer LPS25HB for STM32 boards. category=Sensors url=https://github.com/stm32duino/LPS25HB -architectures=stm32 +architectures=stm32, avr, sam diff --git a/src/LPS25HBSensor.cpp b/src/LPS25HBSensor.cpp index 4694685..95cfdba 100644 --- a/src/LPS25HBSensor.cpp +++ b/src/LPS25HBSensor.cpp @@ -39,99 +39,102 @@ /* Includes ------------------------------------------------------------------*/ #include "Arduino.h" -#include "Wire.h" #include "LPS25HBSensor.h" /* Class Implementation ------------------------------------------------------*/ - /** Constructor * @param i2c object of an helper class which handles the I2C peripheral * @param address the address of the component's instance */ -LPS25HBSensor::LPS25HBSensor(TwoWire *i2c) : dev_i2c(i2c) +LPS25HBSensor::LPS25HBSensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), address(address) +{ + dev_spi = NULL; +} + +/** Constructor + * @param spi object of an helper class which handles the SPI peripheral + * @param cs_pin the chip select pin + * @param spi_speed the SPI speed + */ +LPS25HBSensor::LPS25HBSensor(SPIClass *spi, int cs_pin, uint32_t spi_speed) : dev_spi(spi), cs_pin(cs_pin), spi_speed(spi_speed) { - address = LPS25HB_ADDRESS_HIGH; + dev_i2c = NULL; + address = 0; +} + +/** + * @brief Configure the sensor in order to be used + * @retval 0 in case of success, an error code otherwise + */ +LPS25HBStatusTypeDef LPS25HBSensor::begin(void) +{ + if(dev_spi) + { + // Configure CS pin + pinMode(cs_pin, OUTPUT); + digitalWrite(cs_pin, HIGH); + } /* Power down the device */ if ( LPS25HB_DeActivate( (void *)this ) == LPS25HB_ERROR ) { - return; + return LPS25HB_STATUS_ERROR; } if ( SetODR( 1.0f ) == LPS25HB_STATUS_ERROR ) { - return; + return LPS25HB_STATUS_ERROR; } /* Enable interrupt circuit */ if ( LPS25HB_Set_InterruptCircuitEnable( (void *)this, LPS25HB_ENABLE ) == LPS25HB_ERROR ) { - return; + return LPS25HB_STATUS_ERROR; } /* Set block data update mode */ if ( LPS25HB_Set_Bdu( (void *)this, LPS25HB_BDU_NO_UPDATE ) == LPS25HB_ERROR ) { - return; + return LPS25HB_STATUS_ERROR; } /* Set SPI mode */ if ( LPS25HB_Set_SpiInterface( (void *)this, LPS25HB_SPI_4_WIRE ) == LPS25HB_ERROR ) { - return; + return LPS25HB_STATUS_ERROR; } /* Set internal averaging sample counts for pressure and temperature */ if ( LPS25HB_Set_Avg( (void *)this, LPS25HB_AVGP_32, LPS25HB_AVGT_16 ) == LPS25HB_ERROR ) { - return; + return LPS25HB_STATUS_ERROR; } -}; + return LPS25HB_STATUS_OK; +} -/** Constructor - * @param i2c object of an helper class which handles the I2C peripheral - * @param address the address of the component's instance +/** + * @brief Disable the sensor and relative resources + * @retval 0 in case of success, an error code otherwise */ -LPS25HBSensor::LPS25HBSensor(TwoWire *i2c, uint8_t address) : dev_i2c(i2c), address(address) +LPS25HBStatusTypeDef LPS25HBSensor::end(void) { - /* Power down the device */ - if ( LPS25HB_DeActivate( (void *)this ) == LPS25HB_ERROR ) - { - return; - } - - if ( SetODR( 1.0f ) == LPS25HB_STATUS_ERROR ) - { - return; - } - - /* Enable interrupt circuit */ - if ( LPS25HB_Set_InterruptCircuitEnable( (void *)this, LPS25HB_ENABLE ) == LPS25HB_ERROR ) - { - return; - } - - /* Set block data update mode */ - if ( LPS25HB_Set_Bdu( (void *)this, LPS25HB_BDU_NO_UPDATE ) == LPS25HB_ERROR ) + /* Disable pressure and temperature sensor */ + if (Disable() != LPS25HB_STATUS_OK) { - return; - } - - /* Set SPI mode */ - if ( LPS25HB_Set_SpiInterface( (void *)this, LPS25HB_SPI_4_WIRE ) == LPS25HB_ERROR ) - { - return; + return LPS25HB_STATUS_ERROR; } - /* Set internal averaging sample counts for pressure and temperature */ - if ( LPS25HB_Set_Avg( (void *)this, LPS25HB_AVGP_32, LPS25HB_AVGT_16 ) == LPS25HB_ERROR ) + /* Reset CS configuration */ + if(dev_spi) { - return; + // Configure CS pin + pinMode(cs_pin, INPUT); } -}; + return LPS25HB_STATUS_OK; +} /** * @brief Enable LPS25HB diff --git a/src/LPS25HBSensor.h b/src/LPS25HBSensor.h index fc68410..6dfa42c 100644 --- a/src/LPS25HBSensor.h +++ b/src/LPS25HBSensor.h @@ -45,6 +45,7 @@ /* Includes ------------------------------------------------------------------*/ #include "Wire.h" +#include "SPI.h" #include "LPS25HB_Driver.h" /* Typedefs ------------------------------------------------------------------*/ @@ -65,8 +66,10 @@ typedef enum class LPS25HBSensor { public: - LPS25HBSensor (TwoWire *i2c); - LPS25HBSensor (TwoWire *i2c, uint8_t address); + LPS25HBSensor (TwoWire *i2c, uint8_t address=LPS25HB_ADDRESS_HIGH); + LPS25HBSensor (SPIClass *spi, int cs_pin, uint32_t spi_speed=2000000); + LPS25HBStatusTypeDef begin (void); + LPS25HBStatusTypeDef end (void); LPS25HBStatusTypeDef Enable (void); LPS25HBStatusTypeDef Disable (void); LPS25HBStatusTypeDef ReadID (uint8_t *ht_id); @@ -87,20 +90,43 @@ class LPS25HBSensor */ uint8_t IO_Read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead) { - dev_i2c->beginTransmission(((uint8_t)(((address) >> 1) & 0x7F))); - dev_i2c->write(RegisterAddr); - dev_i2c->endTransmission(false); + if (dev_spi) { + dev_spi->beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE3)); - dev_i2c->requestFrom(((uint8_t)(((address) >> 1) & 0x7F)), (byte) NumByteToRead); + digitalWrite(cs_pin, LOW); - int i=0; - while (dev_i2c->available()) - { - pBuffer[i] = dev_i2c->read(); - i++; + /* Write Reg Address */ + dev_spi->transfer(RegisterAddr | 0x80); + /* Read the data */ + for (uint16_t i=0; itransfer(0x00); + } + + digitalWrite(cs_pin, HIGH); + + dev_spi->endTransaction(); + + return 0; } - return 0; + if(dev_i2c) { + dev_i2c->beginTransmission(((uint8_t)(((address) >> 1) & 0x7F))); + dev_i2c->write(RegisterAddr); + dev_i2c->endTransmission(false); + + dev_i2c->requestFrom(((uint8_t)(((address) >> 1) & 0x7F)), (byte) NumByteToRead); + + int i=0; + while (dev_i2c->available()) + { + pBuffer[i] = dev_i2c->read(); + i++; + } + + return 0; + } + + return 1; } /** @@ -112,23 +138,49 @@ class LPS25HBSensor */ uint8_t IO_Write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite) { - dev_i2c->beginTransmission(((uint8_t)(((address) >> 1) & 0x7F))); + if (dev_spi) { + dev_spi->beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE3)); + + digitalWrite(cs_pin, LOW); + + /* Write Reg Address */ + dev_spi->transfer(RegisterAddr); + /* Write the data */ + for (uint16_t i=0; itransfer(pBuffer[i]); + } - dev_i2c->write(RegisterAddr); - for (int i = 0 ; i < NumByteToWrite ; i++) - dev_i2c->write(pBuffer[i]); + digitalWrite(cs_pin, HIGH); - dev_i2c->endTransmission(true); + dev_spi->endTransaction(); - return 0; + return 0; + } + + if (dev_i2c) { + dev_i2c->beginTransmission(((uint8_t)(((address) >> 1) & 0x7F))); + + dev_i2c->write(RegisterAddr); + for (int i = 0 ; i < NumByteToWrite ; i++) + dev_i2c->write(pBuffer[i]); + + dev_i2c->endTransmission(true); + + return 0; + } + + return 1; } private: /* Helper classes. */ TwoWire *dev_i2c; - + SPIClass *dev_spi; + /* Configuration */ uint8_t address; + int cs_pin; + uint32_t spi_speed; }; #ifdef __cplusplus