Version with sensors supporting SPI 3-wires

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON_SPI3W

Dependents:   sensor-node-ble

Fork of X_NUCLEO_IKS01A2 by Licio Mapelli

Committer:
mapellil
Date:
Thu Sep 07 14:22:17 2017 +0000
Revision:
24:17e674bd19c8
Parent:
19:bfb25ac47fb5
Code cleanup

Who changed what in which revision?

UserRevisionLine numberNew contents of line
davide.aliprandi@st.com 6:671fd10a51b7 1 /**
davide.aliprandi@st.com 6:671fd10a51b7 2 ******************************************************************************
davide.aliprandi@st.com 6:671fd10a51b7 3 * @file XNucleoIKS01A2.cpp
davide.aliprandi@st.com 6:671fd10a51b7 4 * @author CLab
davide.aliprandi@st.com 6:671fd10a51b7 5 * @version V1.0.0
davide.aliprandi@st.com 6:671fd10a51b7 6 * @date 9-August-2016
davide.aliprandi@st.com 6:671fd10a51b7 7 * @brief Implementation file for the X_NUCLEO_IKS01A2 singleton class
davide.aliprandi@st.com 6:671fd10a51b7 8 ******************************************************************************
davide.aliprandi@st.com 6:671fd10a51b7 9 * @attention
davide.aliprandi@st.com 6:671fd10a51b7 10 *
davide.aliprandi@st.com 6:671fd10a51b7 11 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
davide.aliprandi@st.com 6:671fd10a51b7 12 *
davide.aliprandi@st.com 6:671fd10a51b7 13 * Redistribution and use in source and binary forms, with or without modification,
davide.aliprandi@st.com 6:671fd10a51b7 14 * are permitted provided that the following conditions are met:
davide.aliprandi@st.com 6:671fd10a51b7 15 * 1. Redistributions of source code must retain the above copyright notice,
davide.aliprandi@st.com 6:671fd10a51b7 16 * this list of conditions and the following disclaimer.
davide.aliprandi@st.com 6:671fd10a51b7 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
davide.aliprandi@st.com 6:671fd10a51b7 18 * this list of conditions and the following disclaimer in the documentation
davide.aliprandi@st.com 6:671fd10a51b7 19 * and/or other materials provided with the distribution.
davide.aliprandi@st.com 6:671fd10a51b7 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
davide.aliprandi@st.com 6:671fd10a51b7 21 * may be used to endorse or promote products derived from this software
davide.aliprandi@st.com 6:671fd10a51b7 22 * without specific prior written permission.
davide.aliprandi@st.com 6:671fd10a51b7 23 *
davide.aliprandi@st.com 6:671fd10a51b7 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
davide.aliprandi@st.com 6:671fd10a51b7 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
davide.aliprandi@st.com 6:671fd10a51b7 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
davide.aliprandi@st.com 6:671fd10a51b7 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
davide.aliprandi@st.com 6:671fd10a51b7 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
davide.aliprandi@st.com 6:671fd10a51b7 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
davide.aliprandi@st.com 6:671fd10a51b7 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
davide.aliprandi@st.com 6:671fd10a51b7 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
davide.aliprandi@st.com 6:671fd10a51b7 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
davide.aliprandi@st.com 6:671fd10a51b7 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
davide.aliprandi@st.com 6:671fd10a51b7 34 *
davide.aliprandi@st.com 6:671fd10a51b7 35 ******************************************************************************
davide.aliprandi@st.com 6:671fd10a51b7 36 */
davide.aliprandi@st.com 6:671fd10a51b7 37
davide.aliprandi@st.com 6:671fd10a51b7 38 /* Includes ------------------------------------------------------------------*/
davide.aliprandi@st.com 6:671fd10a51b7 39 #include "mbed.h"
davide.aliprandi@st.com 6:671fd10a51b7 40 #include "XNucleoIKS01A2.h"
davide.aliprandi@st.com 6:671fd10a51b7 41
davide.aliprandi@st.com 6:671fd10a51b7 42 /* Static variables ----------------------------------------------------------*/
davide.aliprandi@st.com 6:671fd10a51b7 43 XNucleoIKS01A2 *XNucleoIKS01A2::_instance = NULL;
davide.aliprandi@st.com 6:671fd10a51b7 44
davide.aliprandi@st.com 6:671fd10a51b7 45
davide.aliprandi@st.com 6:671fd10a51b7 46 /* Methods -------------------------------------------------------------------*/
davide.aliprandi@st.com 6:671fd10a51b7 47 /**
davide.aliprandi@st.com 6:671fd10a51b7 48 * @brief Constructor
davide.aliprandi@st.com 6:671fd10a51b7 49 */
mapellil 11:6c2d5fbdab18 50
davide.aliprandi@st.com 6:671fd10a51b7 51 XNucleoIKS01A2::XNucleoIKS01A2(DevI2C *ext_i2c, PinName int1, PinName int2) : dev_i2c(ext_i2c),
mapellil 11:6c2d5fbdab18 52 ht_sensor(new HTS221Sensor(dev_i2c)),
mapellil 11:6c2d5fbdab18 53 magnetometer(new LSM303AGRMagSensor(dev_i2c)),
mapellil 11:6c2d5fbdab18 54 accelerometer(new LSM303AGRAccSensor(dev_i2c)),
mapellil 11:6c2d5fbdab18 55 pt_sensor(new LPS22HBSensor(dev_i2c)),
mapellil 19:bfb25ac47fb5 56 acc_gyro(new LSM6DSLSensor(dev_i2c, LSM6DSL_ACC_GYRO_I2C_ADDRESS_HIGH, int1, int2))
mapellil 11:6c2d5fbdab18 57 {
davide.aliprandi@st.com 6:671fd10a51b7 58 ht_sensor->init(NULL);
davide.aliprandi@st.com 6:671fd10a51b7 59 magnetometer->init(NULL);
davide.aliprandi@st.com 6:671fd10a51b7 60 accelerometer->init(NULL);
davide.aliprandi@st.com 6:671fd10a51b7 61 pt_sensor->init(NULL);
davide.aliprandi@st.com 6:671fd10a51b7 62 acc_gyro->init(NULL);
davide.aliprandi@st.com 6:671fd10a51b7 63 }
davide.aliprandi@st.com 6:671fd10a51b7 64
davide.aliprandi@st.com 6:671fd10a51b7 65 /**
davide.aliprandi@st.com 6:671fd10a51b7 66 * @brief Get singleton instance
davide.aliprandi@st.com 6:671fd10a51b7 67 * @return a pointer to the initialized singleton instance of class XNucleoIKS01A2.
davide.aliprandi@st.com 6:671fd10a51b7 68 * A return value of NULL indicates an out of memory situation.
davide.aliprandi@st.com 6:671fd10a51b7 69 * @param[in] ext_i2c (optional) pointer to an instance of DevI2C to be used
davide.aliprandi@st.com 6:671fd10a51b7 70 * for communication on the expansion board.
davide.aliprandi@st.com 6:671fd10a51b7 71 * Defaults to NULL.
davide.aliprandi@st.com 6:671fd10a51b7 72 * Taken into account only on the very first call of one of the 'Instance' functions.
davide.aliprandi@st.com 6:671fd10a51b7 73 * If not provided a new DevI2C will be created with standard
davide.aliprandi@st.com 6:671fd10a51b7 74 * configuration parameters.
davide.aliprandi@st.com 6:671fd10a51b7 75 * The used DevI2C object gets saved in instance variable dev_i2c.
davide.aliprandi@st.com 6:671fd10a51b7 76 * @param[in] int1 LSM6DSL INT1 pin.
davide.aliprandi@st.com 6:671fd10a51b7 77 * Taken into account only on the very first call of one of the 'Instance' functions.
davide.aliprandi@st.com 6:671fd10a51b7 78 * It maps the INT1 pin for LSM6DSL. Defaults to IKS01A2_PIN_LSM6DSL_INT1.
davide.aliprandi@st.com 6:671fd10a51b7 79 * @param[in] int2 LSM6DSL INT1 pin.
davide.aliprandi@st.com 6:671fd10a51b7 80 * Taken into account only on the very first call of one of the 'Instance' functions.
davide.aliprandi@st.com 6:671fd10a51b7 81 * It maps the INT2 pin for LSM6DSL. Defaults to IKS01A2_PIN_LSM6DSL_INT2.
davide.aliprandi@st.com 6:671fd10a51b7 82 */
davide.aliprandi@st.com 6:671fd10a51b7 83 XNucleoIKS01A2 *XNucleoIKS01A2::instance(DevI2C *ext_i2c, PinName int1, PinName int2) {
davide.aliprandi@st.com 6:671fd10a51b7 84 if(_instance == NULL) {
davide.aliprandi@st.com 6:671fd10a51b7 85 if(ext_i2c == NULL)
davide.aliprandi@st.com 6:671fd10a51b7 86 ext_i2c = new DevI2C(IKS01A2_PIN_I2C_SDA, IKS01A2_PIN_I2C_SCL);
davide.aliprandi@st.com 6:671fd10a51b7 87
davide.aliprandi@st.com 6:671fd10a51b7 88 if(ext_i2c != NULL)
davide.aliprandi@st.com 6:671fd10a51b7 89 _instance = new XNucleoIKS01A2(ext_i2c, int1, int2);
davide.aliprandi@st.com 6:671fd10a51b7 90 }
davide.aliprandi@st.com 6:671fd10a51b7 91
davide.aliprandi@st.com 6:671fd10a51b7 92 return _instance;
davide.aliprandi@st.com 6:671fd10a51b7 93 }
davide.aliprandi@st.com 6:671fd10a51b7 94
davide.aliprandi@st.com 6:671fd10a51b7 95 /**
davide.aliprandi@st.com 6:671fd10a51b7 96 * @brief Get singleton instance
davide.aliprandi@st.com 6:671fd10a51b7 97 * @return a pointer to the initialized singleton instance of class X_NUCLEO_IKS01A1.
davide.aliprandi@st.com 6:671fd10a51b7 98 * A return value of NULL indicates an out of memory situation.
davide.aliprandi@st.com 6:671fd10a51b7 99 * @param[in] sda I2C data line pin.
davide.aliprandi@st.com 6:671fd10a51b7 100 * Taken into account only on the very first call of one of the 'Instance' functions.
davide.aliprandi@st.com 6:671fd10a51b7 101 * A new DevI2C will be created based on parameters 'sda' and 'scl'.
davide.aliprandi@st.com 6:671fd10a51b7 102 * The used DevI2C object gets saved in instance variable dev_i2c.
davide.aliprandi@st.com 6:671fd10a51b7 103 * @param[in] scl I2C clock line pin.
davide.aliprandi@st.com 6:671fd10a51b7 104 * Taken into account only on the very first call of one of the 'Instance' functions.
davide.aliprandi@st.com 6:671fd10a51b7 105 * A new DevI2C will be created based on parameters 'sda' and 'scl'.
davide.aliprandi@st.com 6:671fd10a51b7 106 * The used DevI2C object gets saved in instance variable dev_i2c.
davide.aliprandi@st.com 6:671fd10a51b7 107 * @param[in] int1 LSM6DSL INT1 pin.
davide.aliprandi@st.com 6:671fd10a51b7 108 * Taken into account only on the very first call of one of the 'Instance' functions.
davide.aliprandi@st.com 6:671fd10a51b7 109 * It maps the INT1 pin for LSM6DSL. Defaults to IKS01A2_PIN_LSM6DSL_INT1.
davide.aliprandi@st.com 6:671fd10a51b7 110 * @param[in] int2 LSM6DSL INT1 pin.
davide.aliprandi@st.com 6:671fd10a51b7 111 * Taken into account only on the very first call of one of the 'Instance' functions.
davide.aliprandi@st.com 6:671fd10a51b7 112 * It maps the INT2 pin for LSM6DSL. Defaults to IKS01A2_PIN_LSM6DSL_INT2.
davide.aliprandi@st.com 6:671fd10a51b7 113 */
davide.aliprandi@st.com 6:671fd10a51b7 114 XNucleoIKS01A2 *XNucleoIKS01A2::instance(PinName sda, PinName scl, PinName int1, PinName int2) {
davide.aliprandi@st.com 6:671fd10a51b7 115 if(_instance == NULL) {
davide.aliprandi@st.com 6:671fd10a51b7 116 DevI2C *ext_i2c = new DevI2C(sda, scl);
davide.aliprandi@st.com 6:671fd10a51b7 117
davide.aliprandi@st.com 6:671fd10a51b7 118 if(ext_i2c != NULL)
davide.aliprandi@st.com 6:671fd10a51b7 119 _instance = new XNucleoIKS01A2(ext_i2c, int1, int2);
davide.aliprandi@st.com 6:671fd10a51b7 120 }
davide.aliprandi@st.com 6:671fd10a51b7 121
davide.aliprandi@st.com 6:671fd10a51b7 122 return _instance;
davide.aliprandi@st.com 6:671fd10a51b7 123 }