Simple application to test ST motion and environmental MEMS sensors. Supports NUCLEO+X-NUCLEO-IKS01A2 , STEVAL-STLKT01V1 (a.k.a. SensorTile) and B-L475E-IOT01A boards.

Dependencies:   HTS221 LIS3MDL LPS22HB LSM303AGR LSM6DSL VL53L0X

Supports NUCLEO+X-NUCLEO-IKS01A2 , STEVAL-STLKT01V1 (a.k.a. SensorTile) and B-L475E-IOT01A boards.

Committer:
mapellil
Date:
Mon Oct 02 09:30:24 2017 +0200
Revision:
4:a876fbe13688
Parent:
3:82e8968382d6
Child:
5:2fdca1624a33
Added SPI3/3Wires sensor support, some API modifcations

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nikapov 0:6a97487a0ab3 1 /**
nikapov 0:6a97487a0ab3 2 ******************************************************************************
nikapov 0:6a97487a0ab3 3 * @file main.cpp
nikapov 0:6a97487a0ab3 4 * @author CLab
nikapov 0:6a97487a0ab3 5 * @version V1.0.0
nikapov 0:6a97487a0ab3 6 * @date 5-September-2017
nikapov 0:6a97487a0ab3 7 * @brief Simple Example application for using X_NUCLEO_IKS01A2
nikapov 0:6a97487a0ab3 8 * MEMS Inertial & Environmental Sensor Nucleo expansion and
nikapov 0:6a97487a0ab3 9 * B-L475E-IOT01A2 boards.
nikapov 0:6a97487a0ab3 10 ******************************************************************************
nikapov 0:6a97487a0ab3 11 * @attention
nikapov 0:6a97487a0ab3 12 *
nikapov 0:6a97487a0ab3 13 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
nikapov 0:6a97487a0ab3 14 *
nikapov 0:6a97487a0ab3 15 * Redistribution and use in source and binary forms, with or without modification,
nikapov 0:6a97487a0ab3 16 * are permitted provided that the following conditions are met:
nikapov 0:6a97487a0ab3 17 * 1. Redistributions of source code must retain the above copyright notice,
nikapov 0:6a97487a0ab3 18 * this list of conditions and the following disclaimer.
nikapov 0:6a97487a0ab3 19 * 2. Redistributions in binary form must reproduce the above copyright notice,
nikapov 0:6a97487a0ab3 20 * this list of conditions and the following disclaimer in the documentation
nikapov 0:6a97487a0ab3 21 * and/or other materials provided with the distribution.
nikapov 0:6a97487a0ab3 22 * 3. Neither the name of STMicroelectronics nor the names of its contributors
nikapov 0:6a97487a0ab3 23 * may be used to endorse or promote products derived from this software
nikapov 0:6a97487a0ab3 24 * without specific prior written permission.
nikapov 0:6a97487a0ab3 25 *
nikapov 0:6a97487a0ab3 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
nikapov 0:6a97487a0ab3 27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
nikapov 0:6a97487a0ab3 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
nikapov 0:6a97487a0ab3 29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
nikapov 0:6a97487a0ab3 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
nikapov 0:6a97487a0ab3 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
nikapov 0:6a97487a0ab3 32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
nikapov 0:6a97487a0ab3 33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
nikapov 0:6a97487a0ab3 34 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
nikapov 0:6a97487a0ab3 35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nikapov 0:6a97487a0ab3 36 *
nikapov 0:6a97487a0ab3 37 ******************************************************************************
nikapov 0:6a97487a0ab3 38 */
nikapov 0:6a97487a0ab3 39
nikapov 0:6a97487a0ab3 40 /* Includes */
nikapov 0:6a97487a0ab3 41 #include "mbed.h"
nikapov 0:6a97487a0ab3 42 #include "HTS221Sensor.h"
nikapov 0:6a97487a0ab3 43 #include "LPS22HBSensor.h"
nikapov 0:6a97487a0ab3 44 #include "LSM6DSLSensor.h"
nikapov 0:6a97487a0ab3 45
nikapov 0:6a97487a0ab3 46 #ifdef TARGET_DISCO_L475VG_IOT01A
nikapov 0:6a97487a0ab3 47 #include "lis3mdl_class.h"
nikapov 0:6a97487a0ab3 48 #else // X-Nucleo-IKS01A2
nikapov 0:6a97487a0ab3 49 #include "LSM303AGRMagSensor.h"
nikapov 0:6a97487a0ab3 50 #endif
nikapov 0:6a97487a0ab3 51
nikapov 0:6a97487a0ab3 52
nikapov 0:6a97487a0ab3 53 /* Retrieve the composing elements of the expansion board */
nikapov 0:6a97487a0ab3 54
nikapov 0:6a97487a0ab3 55 #ifdef TARGET_DISCO_L475VG_IOT01A
mapellil 4:a876fbe13688 56 static DevI2C devI2c(PB_11,PB_10);
nikapov 0:6a97487a0ab3 57 #else // X-Nucleo-IKS01A2
mapellil 4:a876fbe13688 58 //#define SPI3W_TEST // or SPI4W_TEST
mapellil 4:a876fbe13688 59 #ifdef SPI3W_TEST
mapellil 4:a876fbe13688 60 SPI devSPI(PB_15, NC, PB_13); // 3W
mapellil 4:a876fbe13688 61 #else
mapellil 4:a876fbe13688 62 #ifdef SPI4W_TEST
mapellil 4:a876fbe13688 63 SPI devSPI(PB_15, PB_14, PB_13); // 4W
mapellil 4:a876fbe13688 64 #else
mapellil 2:4d4fa82dbc59 65 static DevI2C devI2c(D14,D15);
nikapov 0:6a97487a0ab3 66 #endif
mapellil 4:a876fbe13688 67 #endif
mapellil 4:a876fbe13688 68 #endif
nikapov 0:6a97487a0ab3 69
mapellil 4:a876fbe13688 70 //static DevI2C devI2c(D14,D15); /** FIXME temporary **/
mapellil 4:a876fbe13688 71
mapellil 2:4d4fa82dbc59 72 static HTS221Sensor hum_temp(&devI2c);
mapellil 4:a876fbe13688 73 #if defined (SPI3W_TEST) || defined (SPI4W_TEST)
mapellil 4:a876fbe13688 74 #ifdef SPI3W_TEST
mapellil 4:a876fbe13688 75 #define SPI_TYPE LPS22HBSensor::SPI3W
mapellil 4:a876fbe13688 76 #else
mapellil 4:a876fbe13688 77 #define SPI_TYPE LPS22HBSensor::SPI4W
mapellil 4:a876fbe13688 78 #endif
mapellil 4:a876fbe13688 79 static LPS22HBSensor press_temp(&devSPI, PA_10, NC, SPI_TYPE);
mapellil 4:a876fbe13688 80 #else // target Nucleoxxx + IKS01A2
mapellil 2:4d4fa82dbc59 81 static LPS22HBSensor press_temp(&devI2c);
mapellil 4:a876fbe13688 82 #endif
nikapov 0:6a97487a0ab3 83
nikapov 0:6a97487a0ab3 84 #ifdef TARGET_DISCO_L475VG_IOT01A
nikapov 3:82e8968382d6 85 static LSM6DSLSensor acc_gyro(&devI2c,LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW,PD_11); // low address
mapellil 2:4d4fa82dbc59 86 static LIS3MDL magnetometer(&devI2c);
nikapov 0:6a97487a0ab3 87 #else // X-NUCLEO_IKS01A2
nikapov 3:82e8968382d6 88 static LSM6DSLSensor acc_gyro(&devI2c,LSM6DSL_ACC_GYRO_I2C_ADDRESS_HIGH,D4,D5); // high address
mapellil 2:4d4fa82dbc59 89 static LSM303AGRMagSensor magnetometer(&devI2c);
nikapov 0:6a97487a0ab3 90 #endif
nikapov 0:6a97487a0ab3 91
nikapov 0:6a97487a0ab3 92 /* Helper function for printing floats & doubles */
nikapov 0:6a97487a0ab3 93 static char *print_double(char* str, double v, int decimalDigits=2)
nikapov 0:6a97487a0ab3 94 {
nikapov 0:6a97487a0ab3 95 int i = 1;
nikapov 0:6a97487a0ab3 96 int intPart, fractPart;
nikapov 0:6a97487a0ab3 97 int len;
nikapov 0:6a97487a0ab3 98 char *ptr;
nikapov 0:6a97487a0ab3 99
nikapov 0:6a97487a0ab3 100 /* prepare decimal digits multiplicator */
nikapov 0:6a97487a0ab3 101 for (;decimalDigits!=0; i*=10, decimalDigits--);
nikapov 0:6a97487a0ab3 102
nikapov 0:6a97487a0ab3 103 /* calculate integer & fractinal parts */
nikapov 0:6a97487a0ab3 104 intPart = (int)v;
nikapov 0:6a97487a0ab3 105 fractPart = (int)((v-(double)(int)v)*i);
nikapov 0:6a97487a0ab3 106
nikapov 0:6a97487a0ab3 107 /* fill in integer part */
nikapov 0:6a97487a0ab3 108 sprintf(str, "%i.", intPart);
nikapov 0:6a97487a0ab3 109
nikapov 0:6a97487a0ab3 110 /* prepare fill in of fractional part */
nikapov 0:6a97487a0ab3 111 len = strlen(str);
nikapov 0:6a97487a0ab3 112 ptr = &str[len];
nikapov 0:6a97487a0ab3 113
nikapov 0:6a97487a0ab3 114 /* fill in leading fractional zeros */
nikapov 0:6a97487a0ab3 115 for (i/=10;i>1; i/=10, ptr++) {
nikapov 0:6a97487a0ab3 116 if (fractPart >= i) {
nikapov 0:6a97487a0ab3 117 break;
nikapov 0:6a97487a0ab3 118 }
nikapov 0:6a97487a0ab3 119 *ptr = '0';
nikapov 0:6a97487a0ab3 120 }
nikapov 0:6a97487a0ab3 121
nikapov 0:6a97487a0ab3 122 /* fill in (rest of) fractional part */
nikapov 0:6a97487a0ab3 123 sprintf(ptr, "%i", fractPart);
nikapov 0:6a97487a0ab3 124
nikapov 0:6a97487a0ab3 125 return str;
nikapov 0:6a97487a0ab3 126 }
nikapov 0:6a97487a0ab3 127
nikapov 0:6a97487a0ab3 128 /* Simple main function */
nikapov 0:6a97487a0ab3 129 int main() {
nikapov 0:6a97487a0ab3 130 uint8_t id;
nikapov 0:6a97487a0ab3 131 float value1, value2;
nikapov 0:6a97487a0ab3 132 char buffer1[32], buffer2[32];
nikapov 0:6a97487a0ab3 133 int32_t axes[3];
nikapov 0:6a97487a0ab3 134
nikapov 0:6a97487a0ab3 135 /* Init all sensors with default params */
nikapov 0:6a97487a0ab3 136 hum_temp.init(NULL);
nikapov 0:6a97487a0ab3 137 press_temp.init(NULL);
nikapov 0:6a97487a0ab3 138 magnetometer.init(NULL);
nikapov 0:6a97487a0ab3 139 acc_gyro.init(NULL);
nikapov 0:6a97487a0ab3 140
nikapov 0:6a97487a0ab3 141 /* Enable all sensors */
nikapov 0:6a97487a0ab3 142 hum_temp.enable();
nikapov 0:6a97487a0ab3 143 press_temp.enable();
nikapov 0:6a97487a0ab3 144 #ifndef TARGET_DISCO_L475VG_IOT01A // X-NUCLEO-IKS01A2
nikapov 0:6a97487a0ab3 145 magnetometer.enable();
nikapov 0:6a97487a0ab3 146 #endif
nikapov 0:6a97487a0ab3 147 acc_gyro.enable_x();
nikapov 0:6a97487a0ab3 148 acc_gyro.enable_g();
nikapov 0:6a97487a0ab3 149
nikapov 1:0f7838527fef 150 printf("\033[2J\033[20A");
nikapov 1:0f7838527fef 151 printf ("\r\n--- Starting new run ---\r\n\r\n");
nikapov 0:6a97487a0ab3 152
nikapov 0:6a97487a0ab3 153 hum_temp.read_id(&id);
nikapov 0:6a97487a0ab3 154 printf("HTS221 humidity & temperature = 0x%X\r\n", id);
nikapov 0:6a97487a0ab3 155 press_temp.read_id(&id);
nikapov 0:6a97487a0ab3 156 printf("LPS22HB pressure & temperature = 0x%X\r\n", id);
nikapov 0:6a97487a0ab3 157 magnetometer.read_id(&id);
nikapov 0:6a97487a0ab3 158 #ifdef TARGET_DISCO_L475VG_IOT01A
nikapov 1:0f7838527fef 159 printf("LIS3MDL magnetometer = 0x%X\r\n", id);
nikapov 0:6a97487a0ab3 160 #else // X-NUCLEO-IKS01A2
nikapov 0:6a97487a0ab3 161 printf("LSM303AGR magnetometer = 0x%X\r\n", id);
nikapov 0:6a97487a0ab3 162 #endif
nikapov 0:6a97487a0ab3 163 acc_gyro.read_id(&id);
nikapov 0:6a97487a0ab3 164 printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id);
nikapov 1:0f7838527fef 165
nikapov 1:0f7838527fef 166 printf("\n\r--- Reading sensor values ---\n\r"); ;
nikapov 0:6a97487a0ab3 167
nikapov 0:6a97487a0ab3 168 while(1) {
nikapov 0:6a97487a0ab3 169 printf("\r\n");
nikapov 0:6a97487a0ab3 170
nikapov 0:6a97487a0ab3 171 hum_temp.get_temperature(&value1);
nikapov 0:6a97487a0ab3 172 hum_temp.get_humidity(&value2);
mapellil 4:a876fbe13688 173 printf("HTS221: [temp] %7f C, [hum] %f%%\r\n", value1, value2);
mapellil 4:a876fbe13688 174 value1=value2=0;
nikapov 0:6a97487a0ab3 175 press_temp.get_temperature(&value1);
nikapov 0:6a97487a0ab3 176 press_temp.get_pressure(&value2);
mapellil 4:a876fbe13688 177 printf("LPS22HB: [temp] %7f C, [press] %f mbar\r\n", value1, value2);
nikapov 0:6a97487a0ab3 178
nikapov 0:6a97487a0ab3 179 printf("---\r\n");
nikapov 0:6a97487a0ab3 180
nikapov 0:6a97487a0ab3 181 magnetometer.get_m_axes(axes);
nikapov 0:6a97487a0ab3 182 #ifdef TARGET_DISCO_L475VG_IOT01A
nikapov 1:0f7838527fef 183 printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
nikapov 0:6a97487a0ab3 184 #else // X-NUCLEO-IKS01A2
nikapov 0:6a97487a0ab3 185 printf("LSM303AGR [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
nikapov 0:6a97487a0ab3 186 #endif
nikapov 0:6a97487a0ab3 187
nikapov 0:6a97487a0ab3 188 acc_gyro.get_x_axes(axes);
nikapov 1:0f7838527fef 189 printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
nikapov 0:6a97487a0ab3 190
nikapov 0:6a97487a0ab3 191 acc_gyro.get_g_axes(axes);
nikapov 1:0f7838527fef 192 printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
nikapov 0:6a97487a0ab3 193
nikapov 1:0f7838527fef 194 printf("\033[7A");
nikapov 1:0f7838527fef 195 wait(1);
nikapov 0:6a97487a0ab3 196 }
nikapov 0:6a97487a0ab3 197 }