Publish Library to my account
Dependencies: ST_INTERFACES X_NUCLEO_COMMON
Fork of LIS3MDL by
Revision 4:00493226e59f, committed 2017-10-06
- Comitter:
- mapellil
- Date:
- Fri Oct 06 11:48:36 2017 +0000
- Parent:
- 3:a35a16f3da39
- Child:
- 5:1af013474759
- Commit message:
- Added SPI3/4W interface
Changed in this revision
--- a/lis3mdl.h Mon Oct 02 09:28:58 2017 +0200
+++ b/lis3mdl.h Fri Oct 06 11:48:36 2017 +0000
@@ -250,10 +250,11 @@
#define LIS3MDL_I2C_MULTIPLEBYTE_CMD ((uint8_t)0x80)
/**
- * @brief Device Address
+ * @brief I2C Device Address
*/
-
-#define LIS3MDL_M_MEMS_ADDRESS 0x3C // SAD[1] = 1
+//#define LIS3MDL_M_MEMS_ADDRESS 0x3C // SAD[1] = 1
+#define LIS3MDL_M_MEMS_ADDRESS_LOW 0x38 // SAD[1] = 0
+#define LIS3MDL_M_MEMS_ADDRESS_HIGH 0x3C // SAD[1] = 1
/**
* @brief Device Identifier. Default value of the WHO_AM_I register.
@@ -422,7 +423,7 @@
/* extended features of LSM6DS0. See the example of */
/* LSM6DS3 in lsm6ds3.h */
/* ------------------------------------------------------- */
-
+typedef enum { LIS3MDL_SPI_4_WIRE = (uint8_t)0x00, LIS3MDL_SPI_3_WIRE = (uint8_t)0x04} LIS3MDL_SPIMode_t;
/** @addtogroup LIS3MDL_Exported_Variables LIS3MDL_Exported_Variables
* @{
*/
--- a/lis3mdl_class.cpp Mon Oct 02 09:28:58 2017 +0200
+++ b/lis3mdl_class.cpp Fri Oct 06 11:48:36 2017 +0000
@@ -45,6 +45,42 @@
X-CUBE-MEMS1/trunk: revision #416
*/
+LIS3MDL::LIS3MDL(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 LIS3MDL CS MUST NOT BE NC\n\r");
+ _dev_spi = NULL;
+ _dev_i2c=NULL;
+ return;
+ }
+
+ _cs_pin = 0;
+ _dev_i2c=NULL;
+
+ if (_spi_type == SPI3W) LIS3MDL_Set_SpiInterface ((void *)this, LIS3MDL_SPI_3_WIRE);
+ else if (_spi_type == SPI4W) LIS3MDL_Set_SpiInterface ((void *)this, LIS3MDL_SPI_4_WIRE);
+}
+
+LIS3MDL::LIS3MDL(DevI2C *i2c, uint8_t address, PinName int_pin) :
+ _dev_i2c(i2c), _address(address), _cs_pin(NC), _int_pin(int_pin)
+{
+ assert (i2c);
+ _dev_spi = NULL;
+}
+
+
+MAGNETO_StatusTypeDef LIS3MDL::LIS3MDL_Set_SpiInterface (void *handle, LIS3MDL_SPIMode_t spimode)
+{
+ uint8_t tmp=0x03; //deft LIS3MDL_CTRL_REG3 value
+
+ tmp |= (uint8_t)spimode;
+ if (LIS3MDL_IO_Write(&tmp, LIS3MDL_M_CTRL_REG3_M, 1) != MAGNETO_OK) return MAGNETO_ERROR;
+ return MAGNETO_OK;
+}
+
/**
* @brief Set LIS3MDL Initialization
* @param LIS3MDL_Init the configuration setting for the LIS3MDL
--- 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
