Publish Library to my account
Dependencies: ST_INTERFACES X_NUCLEO_COMMON
Fork of LIS3MDL by
Diff: lis3mdl_class.h
- Revision:
- 4:00493226e59f
- Parent:
- 3:a35a16f3da39
--- a/lis3mdl_class.h Mon Oct 02 09:28:58 2017 +0200 +++ b/lis3mdl_class.h Fri Oct 06 11:48:36 2017 +0000 @@ -45,21 +45,21 @@ #include "MagneticSensor.h" #include <assert.h> + /* Classes -------------------------------------------------------------------*/ /** Class representing a LIS3MDL sensor component */ class LIS3MDL : public MagneticSensor { public: - enum SPI_type_t {SPI3W, SPI4W}; -// LIS3MDL(SPI *spi, PinName cs_pin, PinName int_pin=NC, SPI_type_t spi_type=SPI4W); -// LIS3MDL(DevI2C *i2c, uint8_t address=LPS22HB_ADDRESS_HIGH, PinName int_pin=NC); - + enum SPI_type_t {SPI3W, SPI4W}; + + LIS3MDL(SPI *spi, PinName cs_pin, PinName int_pin=NC, SPI_type_t spi_type=SPI4W); + /** Constructor * @param[in] i2c device I2C to be used for communication - */ - // LIS3MDL(DevI2C *i2c) : MagneticSensor(), dev_i2c(i2c) { - LIS3MDL(DevI2C *i2c) : dev_i2c(i2c) { - } + */ + + LIS3MDL(DevI2C *i2c, uint8_t address=LIS3MDL_M_MEMS_ADDRESS_HIGH, PinName int_pin=NC); /** Destructor */ @@ -88,6 +88,7 @@ MAGNETO_StatusTypeDef LIS3MDL_Read_M_ID(uint8_t *m_id); MAGNETO_StatusTypeDef LIS3MDL_M_GetAxes(int32_t *pData); MAGNETO_StatusTypeDef LIS3MDL_M_GetAxesRaw(int16_t *pData); + MAGNETO_StatusTypeDef LIS3MDL_Set_SpiInterface (void *handle, LIS3MDL_SPIMode_t spimode); /** * @brief Configures LIS3MDL interrupt lines for NUCLEO boards @@ -117,8 +118,27 @@ MAGNETO_StatusTypeDef LIS3MDL_IO_Read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead) { - int ret = dev_i2c->i2c_read(pBuffer, - LIS3MDL_M_MEMS_ADDRESS, + 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 MAGNETO_OK; + } + if (!_dev_i2c) return MAGNETO_ERROR; + int ret = _dev_i2c->i2c_read(pBuffer, + _address, RegisterAddr, NumByteToRead); if(ret != 0) { @@ -138,10 +158,19 @@ MAGNETO_StatusTypeDef LIS3MDL_IO_Write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite) { - int ret = dev_i2c->i2c_write(pBuffer, - LIS3MDL_M_MEMS_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 MAGNETO_OK; + } + + if (!_dev_i2c) return MAGNETO_ERROR; + int ret = _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite); if(ret != 0) { return MAGNETO_ERROR; } @@ -150,7 +179,12 @@ /*** Instance Variables ***/ /* IO Device */ - DevI2C *dev_i2c; + DevI2C *_dev_i2c; + SPI *_dev_spi; + uint8_t _address; + DigitalOut _cs_pin; + InterruptIn _int_pin; + SPI_type_t _spi_type; }; #endif // __LIS3MDL_CLASS_H