2019-2020 LIS2DW12 Accelerometer Project

Dependencies:   X_NUCLEO_IKS01A3

Committer:
martlefebvre94
Date:
Tue Sep 17 12:33:08 2019 +0000
Revision:
12:2c129618b350
Parent:
10:3a7580daadda
Child:
13:f4ad8550374a
LSM6DSO accelerometer and gyroscope

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cparata 0:535249dc4bf5 1 /**
cparata 0:535249dc4bf5 2 ******************************************************************************
cparata 0:535249dc4bf5 3 * @file main.cpp
cparata 0:535249dc4bf5 4 * @author SRA
cparata 0:535249dc4bf5 5 * @version V1.0.0
cparata 0:535249dc4bf5 6 * @date 5-March-2019
cparata 5:7c883cce2bc4 7 * @brief Simple Example application for using the X_NUCLEO_IKS01A3
cparata 0:535249dc4bf5 8 * MEMS Inertial & Environmental Sensor Nucleo expansion board.
cparata 0:535249dc4bf5 9 ******************************************************************************
cparata 0:535249dc4bf5 10 * @attention
cparata 0:535249dc4bf5 11 *
cparata 0:535249dc4bf5 12 * <h2><center>&copy; COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
cparata 0:535249dc4bf5 13 *
cparata 0:535249dc4bf5 14 * Redistribution and use in source and binary forms, with or without modification,
cparata 0:535249dc4bf5 15 * are permitted provided that the following conditions are met:
cparata 0:535249dc4bf5 16 * 1. Redistributions of source code must retain the above copyright notice,
cparata 0:535249dc4bf5 17 * this list of conditions and the following disclaimer.
cparata 0:535249dc4bf5 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
cparata 0:535249dc4bf5 19 * this list of conditions and the following disclaimer in the documentation
cparata 0:535249dc4bf5 20 * and/or other materials provided with the distribution.
cparata 0:535249dc4bf5 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
cparata 0:535249dc4bf5 22 * may be used to endorse or promote products derived from this software
cparata 0:535249dc4bf5 23 * without specific prior written permission.
cparata 0:535249dc4bf5 24 *
cparata 0:535249dc4bf5 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
cparata 0:535249dc4bf5 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
cparata 0:535249dc4bf5 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
cparata 0:535249dc4bf5 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
cparata 0:535249dc4bf5 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
cparata 0:535249dc4bf5 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
cparata 0:535249dc4bf5 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
cparata 0:535249dc4bf5 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
cparata 0:535249dc4bf5 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
cparata 0:535249dc4bf5 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cparata 0:535249dc4bf5 35 *
cparata 0:535249dc4bf5 36 ******************************************************************************
cparata 5:7c883cce2bc4 37 */
cparata 0:535249dc4bf5 38
martlefebvre94 6:b2e247935342 39 /*
martlefebvre94 8:fa346d946e7e 40 LELEC2811 Multisensor IKS01A3 Project
martlefebvre94 6:b2e247935342 41 M. Lefebvre - 2019
martlefebvre94 6:b2e247935342 42 */
martlefebvre94 6:b2e247935342 43
cparata 0:535249dc4bf5 44 /* Includes */
martlefebvre94 6:b2e247935342 45 #include <stdlib.h>
martlefebvre94 8:fa346d946e7e 46 #include <time.h>
cparata 0:535249dc4bf5 47 #include "mbed.h"
cparata 0:535249dc4bf5 48 #include "XNucleoIKS01A3.h"
martlefebvre94 6:b2e247935342 49 #include "stm32l073xx.h"
martlefebvre94 6:b2e247935342 50 #include "stm32l0xx_hal_flash.h"
martlefebvre94 6:b2e247935342 51
martlefebvre94 6:b2e247935342 52 /* Defines */
martlefebvre94 8:fa346d946e7e 53 #define FS 25.0 // Sampling frequency (Hz)
martlefebvre94 8:fa346d946e7e 54
martlefebvre94 8:fa346d946e7e 55 // LIS2MDL Magnetometer
martlefebvre94 7:4a3b6202963e 56 #define LIS2MDL_ODR 50.0 // Output data rate (10, 20, 50 or 100 Hz)
martlefebvre94 7:4a3b6202963e 57 #define LIS2MDL_LP 0 // Power mode (0 for high-resolution mode, 1 for low-power mode)
martlefebvre94 7:4a3b6202963e 58 #define LIS2MDL_LPF 0 // Bandwidth (0 for ODR/2, 1 for ODR/4)
martlefebvre94 7:4a3b6202963e 59 #define LIS2MDL_COMP_TEMP_EN 1 // Temperature compensation (0 disabled, 1 enabled)
martlefebvre94 7:4a3b6202963e 60 #define LIS2MDL_OFF_CANC 1 // Offset cancellation (0 for no offset cancellation, 1 for offset cancellation, 2 for set pulse only at power-on)
martlefebvre94 8:fa346d946e7e 61 #define LIS2MDL_DATA_SIZE 12 // Number of bytes for LIS2MDL magnetometer data
martlefebvre94 8:fa346d946e7e 62
martlefebvre94 8:fa346d946e7e 63 // LPS22HH Pressure sensor
martlefebvre94 8:fa346d946e7e 64 #define P0 1013.26 // Sea level pressure (hPa)
martlefebvre94 8:fa346d946e7e 65 #define LPS22HH_ODR 50.0 // Output data rate (one-shot, 1, 10, 25, 50, 75, 100, 200 Hz)
martlefebvre94 8:fa346d946e7e 66 #define LPS22HH_LOW_NOISE_EN 1 // Low-noise (0 disabled, 1 enabled)
martlefebvre94 8:fa346d946e7e 67 #define LPS22HH_LPF_CFG 3 // Device bandwidth (0 for ODR/2, 2 for ODR/9, 3 for ODR/20)
martlefebvre94 8:fa346d946e7e 68
martlefebvre94 8:fa346d946e7e 69 // LIS2DW12 Accelerometer
martlefebvre94 8:fa346d946e7e 70 #define LIS2DW12_ODR 4 // Output data rate (0 power down, 1 HP 12.5Hz/LP 1.6Hz, 2 for 12.5Hz, 3 for 25Hz, 4 for 50Hz, 5 for 100Hz, 6 for 200Hz, 7 for HP 400Hz/LP 200Hz, 8 for HP 800Hz/LP 200Hz, 9 for HP 1600Hz/LP 200Hz)
martlefebvre94 8:fa346d946e7e 71 #define LIS2DW12_FS 4 // Full-scale +-(2, 4, 8 or 16 g)
martlefebvre94 8:fa346d946e7e 72 #define LIS2DW12_BW_FILT 2 // Filter bandwidth (0 for ODR/2, 1 for ODR/4, 2 for ODR/10, 3 for ODR/20)
martlefebvre94 8:fa346d946e7e 73 #define LIS2DW12_LP_MODE 0 // Low-power modes 1 to 4 (1 gives the max. rms noise, 4 gives the min. rms noise)
martlefebvre94 8:fa346d946e7e 74 #define LIS2DW12_MODE 1 // Mode (0 for low-power, 1 for high-performance, 2 for single data conversion)
martlefebvre94 8:fa346d946e7e 75 #define LIS2DW12_LOW_NOISE 1 // Low-noise (0 disable, 1 enabled)
martlefebvre94 8:fa346d946e7e 76 #define LIS2DW12_POWER_MODE LIS2DW12_LP_MODE + (LIS2DW12_MODE << 2) + (LIS2DW12_LOW_NOISE << 4)
martlefebvre94 8:fa346d946e7e 77
martlefebvre94 8:fa346d946e7e 78 // HTS221 Relative humidity and temperature sensor
martlefebvre94 8:fa346d946e7e 79 #define HTS221_ODR 1 // Output data rate (one-shot, 1Hz, 7Hz, 12.5Hz)
martlefebvre94 8:fa346d946e7e 80 #define HTS221_HEATER 0 // Heater configuration (0 disabled, 1 enabled)
martlefebvre94 8:fa346d946e7e 81 #define HTS221_AVGH 32 // Humidity averaging (4 to 512)
martlefebvre94 8:fa346d946e7e 82 #define HTS221_AVGT 16 // Temperature averaging (2 to 256)
martlefebvre94 8:fa346d946e7e 83
martlefebvre94 8:fa346d946e7e 84 // LSM6DSO Accelerometer + gyroscope
martlefebvre94 8:fa346d946e7e 85 #define LSM6DSO_ODR_XL 12.5 // Accelerometer output data rate (12.5, 26, 52, 104, 208, 416, 833, 1.66k, 3.33k, 6.66kHz)
martlefebvre94 8:fa346d946e7e 86 #define LSM6DSO_FS_XL 4 // Accelerometer full scale (2, 4, 8, 16g)
martlefebvre94 8:fa346d946e7e 87 #define LSM6DSO_XL_HM_MODE 1 // Accelerometer high-performance mode (0 enabled, 1 disabled)
martlefebvre94 8:fa346d946e7e 88 #define LSM6DSO_XL_ULP_EN 0 // Accelerometer ultra-low-power configuration (0 disabled, 1 enabled)
martlefebvre94 12:2c129618b350 89 #define LSM6DSO_LPF2_XL_EN 1 // Accelerometer high-resolution selection (0 for 1st stage of digital filtering, 1 for 2nd stage)
martlefebvre94 12:2c129618b350 90 #define LSM6DSO_HP_SLOPE_XL_EN 0 // Accelerometer high-pass filter selection (0 for low-pass, 1 for high-pass)
martlefebvre94 12:2c129618b350 91 #define LSM6DSO_HPCF_XL 2 // Accelerometer filter configuration and cutoff setting (0 for ODR/4, 1 for ODR/10, 2 for ODR/20, 3 for ODR/45, 4 for ODR/100, 5 for ODR/200, 6 for ODR/400, 7 for ODR/800)
martlefebvre94 8:fa346d946e7e 92 #define LSM6DSO_ODR_G 16 // Gyroscope output data rate (12.5, 26, 52, 104, 208, 416, 833, 1.66k, 3.33k, 6.66kHz)
martlefebvre94 8:fa346d946e7e 93 #define LSM6DSO_FS_G 1000 // Gyroscope full scale (250, 500, 1000, 2000dps)
martlefebvre94 12:2c129618b350 94 #define LSM6DSO_G_HM_MODE 1 // Gyroscope high-performance mode (0 enabled, 1 disabled)
martlefebvre94 12:2c129618b350 95 #define LSM6DSO_LPF1_SEL_G 1 // Gyroscope digital LPF1 enable (0 disabled, 1 enabled)
martlefebvre94 12:2c129618b350 96 #define LSM6DSO_FTYPE 3 // Gyroscope LPF1 bandwidth selection (0 ultra light, 1 very light, 2 light, 3 medium, 4 strong, 5 very strong, 6 aggressive, 7 xtreme)
martlefebvre94 12:2c129618b350 97 #define LSM6DSO_HP_EN_G 1 // Gyroscope digital HPF enable (0 HPF disabled, 1 HPF enabled)
martlefebvre94 12:2c129618b350 98 #define LSM6DSO_HPM_G 2 // Gyroscope HPF cutoff selection (0 for 16mHz, 1 for 65mHz, 10 for 260 mHz, 11 for 1.04Hz)
martlefebvre94 8:fa346d946e7e 99
martlefebvre94 8:fa346d946e7e 100 /* Functions definition */
martlefebvre94 8:fa346d946e7e 101 bool acquisition_task(bool verbose);
martlefebvre94 8:fa346d946e7e 102 void read_task();
martlefebvre94 8:fa346d946e7e 103 void print_flash_info();
martlefebvre94 8:fa346d946e7e 104 bool erase_flash(bool verbose);
martlefebvre94 8:fa346d946e7e 105 bool write_flash(uint32_t Flash_addr, uint32_t* Flash_wdata, int32_t n_words, bool verbose);
martlefebvre94 8:fa346d946e7e 106 void read_flash(uint32_t Flash_addr, uint32_t* Flash_rdata, uint32_t n_bytes);
martlefebvre94 8:fa346d946e7e 107 void button1_enabled_cb(void);
martlefebvre94 8:fa346d946e7e 108 void button1_onpressed_cb(void);
martlefebvre94 8:fa346d946e7e 109 static char *print_double(char *str, double v);
martlefebvre94 8:fa346d946e7e 110 float pressure_to_altitude(double pressure);
martlefebvre94 6:b2e247935342 111
martlefebvre94 6:b2e247935342 112 /* Serial link */
martlefebvre94 6:b2e247935342 113 Serial pc(SERIAL_TX, SERIAL_RX);
martlefebvre94 6:b2e247935342 114
martlefebvre94 6:b2e247935342 115 /* Button */
martlefebvre94 6:b2e247935342 116 InterruptIn button1(USER_BUTTON);
martlefebvre94 6:b2e247935342 117 volatile bool button1_pressed = false; // Used in the main loop
martlefebvre94 6:b2e247935342 118 volatile bool button1_enabled = true; // Used for debouncing
martlefebvre94 6:b2e247935342 119 Timeout button1_timeout; // Used for debouncing
cparata 0:535249dc4bf5 120
cparata 0:535249dc4bf5 121 /* Instantiate the expansion board */
cparata 0:535249dc4bf5 122 static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4);
cparata 0:535249dc4bf5 123
cparata 0:535249dc4bf5 124 /* Retrieve the composing elements of the expansion board */
cparata 0:535249dc4bf5 125 static LIS2MDLSensor *magnetometer = mems_expansion_board->magnetometer;
cparata 0:535249dc4bf5 126 static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
cparata 0:535249dc4bf5 127 static LPS22HHSensor *press_temp = mems_expansion_board->pt_sensor;
cparata 0:535249dc4bf5 128 static LSM6DSOSensor *acc_gyro = mems_expansion_board->acc_gyro;
cparata 0:535249dc4bf5 129 static LIS2DW12Sensor *accelerometer = mems_expansion_board->accelerometer;
cparata 0:535249dc4bf5 130 static STTS751Sensor *temp = mems_expansion_board->t_sensor;
cparata 0:535249dc4bf5 131
martlefebvre94 8:fa346d946e7e 132 /* Main */
martlefebvre94 8:fa346d946e7e 133 int main()
martlefebvre94 8:fa346d946e7e 134 {
martlefebvre94 8:fa346d946e7e 135 uint8_t id;
martlefebvre94 8:fa346d946e7e 136 float read_reg, read_reg_1;
martlefebvre94 8:fa346d946e7e 137 uint8_t read_reg_int, read_reg_int_1, read_reg_int_2;
martlefebvre94 8:fa346d946e7e 138
martlefebvre94 8:fa346d946e7e 139 bool save_data = false;
martlefebvre94 8:fa346d946e7e 140 uint32_t Flash_addr = FLASH_BANK2_BASE;
martlefebvre94 8:fa346d946e7e 141
martlefebvre94 8:fa346d946e7e 142 /* Serial link configuration */
martlefebvre94 8:fa346d946e7e 143 pc.baud(115200);
martlefebvre94 8:fa346d946e7e 144
martlefebvre94 8:fa346d946e7e 145 /* Button configuration */
martlefebvre94 8:fa346d946e7e 146 button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
martlefebvre94 8:fa346d946e7e 147
martlefebvre94 8:fa346d946e7e 148 /* Reset message */
martlefebvre94 8:fa346d946e7e 149 printf("\n\r**************************************************\n\r");
martlefebvre94 8:fa346d946e7e 150 printf("LELEC2811 IKS01A3 Multisensor Program\n\r");
martlefebvre94 8:fa346d946e7e 151 printf("**************************************************\n\r");
martlefebvre94 8:fa346d946e7e 152
martlefebvre94 8:fa346d946e7e 153 /* LIS2MDL magnetometer sensor configuration */
martlefebvre94 8:fa346d946e7e 154 magnetometer->enable();
martlefebvre94 8:fa346d946e7e 155 printf("/***** LIS2MDL magnetometer configuration *****/\r\n");
martlefebvre94 8:fa346d946e7e 156
martlefebvre94 8:fa346d946e7e 157 magnetometer->read_id(&id);
martlefebvre94 8:fa346d946e7e 158 printf("LIS2MDL magnetometer = 0x%X\r\n", id);
martlefebvre94 8:fa346d946e7e 159
martlefebvre94 8:fa346d946e7e 160 magnetometer->set_m_odr(LIS2MDL_ODR);
martlefebvre94 8:fa346d946e7e 161 magnetometer->get_m_odr(&read_reg);
martlefebvre94 8:fa346d946e7e 162 printf("LIS2MDL ODR = %1.1f [Hz]\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 163
martlefebvre94 8:fa346d946e7e 164 magnetometer->set_m_lp(LIS2MDL_LP);
martlefebvre94 8:fa346d946e7e 165 magnetometer->get_m_lp(&read_reg_int);
martlefebvre94 8:fa346d946e7e 166 printf("LIS2MDL LP = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 167
martlefebvre94 8:fa346d946e7e 168 magnetometer->set_m_lpf(LIS2MDL_LPF);
martlefebvre94 8:fa346d946e7e 169 magnetometer->get_m_lpf(&read_reg_int);
martlefebvre94 8:fa346d946e7e 170 printf("LIS2MDL LPF = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 171
martlefebvre94 8:fa346d946e7e 172 magnetometer->set_m_comp_temp_en(LIS2MDL_COMP_TEMP_EN);
martlefebvre94 8:fa346d946e7e 173 magnetometer->get_m_comp_temp_en(&read_reg_int);
martlefebvre94 8:fa346d946e7e 174 printf("LIS2MDL COMP_TEMP_EN = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 175
martlefebvre94 8:fa346d946e7e 176 magnetometer->set_m_off_canc(LIS2MDL_OFF_CANC);
martlefebvre94 8:fa346d946e7e 177 magnetometer->get_m_off_canc(&read_reg_int);
martlefebvre94 8:fa346d946e7e 178 printf("LIS2MDL OFF_CANC = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 179
martlefebvre94 8:fa346d946e7e 180 /* LPS22HH pressure sensor configuration */
martlefebvre94 8:fa346d946e7e 181 press_temp->enable();
martlefebvre94 8:fa346d946e7e 182 printf("/***** LPS22HH pressure sensor configuration *****/\r\n");
martlefebvre94 8:fa346d946e7e 183
martlefebvre94 8:fa346d946e7e 184 press_temp->read_id(&id);
martlefebvre94 8:fa346d946e7e 185 printf("LPS22HH pressure = 0x%X\r\n", id);
martlefebvre94 8:fa346d946e7e 186
martlefebvre94 8:fa346d946e7e 187 press_temp->set_odr(LPS22HH_ODR, LPS22HH_LOW_NOISE_EN);
martlefebvre94 8:fa346d946e7e 188 press_temp->get_odr(&read_reg, &read_reg_int);
martlefebvre94 8:fa346d946e7e 189 printf("LPS22HH ODR = %1.1f [Hz]\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 190 printf("LPS22HH LOW_NOISE_EN = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 191
martlefebvre94 8:fa346d946e7e 192 press_temp->set_lpfp_cfg(LPS22HH_LPF_CFG);
martlefebvre94 8:fa346d946e7e 193 press_temp->get_lpfp_cfg(&read_reg_int);
martlefebvre94 8:fa346d946e7e 194 printf("LPS22HH LPF_CFG = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 195
martlefebvre94 8:fa346d946e7e 196 /* LIS2DW12 accelerometer sensor configuration */
martlefebvre94 8:fa346d946e7e 197 accelerometer->enable_x();
martlefebvre94 8:fa346d946e7e 198 printf("/***** LIS2DW12 accelerometer sensor configuration *****/\r\n");
martlefebvre94 8:fa346d946e7e 199
martlefebvre94 8:fa346d946e7e 200 accelerometer->read_id(&id);
martlefebvre94 8:fa346d946e7e 201 printf("LIS2DW12 accelerometer = 0x%X\r\n", id);
martlefebvre94 8:fa346d946e7e 202
martlefebvre94 8:fa346d946e7e 203 accelerometer->set_x_odr(LIS2DW12_ODR);
martlefebvre94 8:fa346d946e7e 204 accelerometer->get_x_odr(&read_reg);
martlefebvre94 8:fa346d946e7e 205 printf("LIS2DW12 ODR = %1.3f [Hz]\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 206
martlefebvre94 8:fa346d946e7e 207 accelerometer->set_x_fs(LIS2DW12_FS);
martlefebvre94 8:fa346d946e7e 208 accelerometer->get_x_fs(&read_reg);
martlefebvre94 8:fa346d946e7e 209 printf("LIS2DW12 FS = %1.3f [g]\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 210
martlefebvre94 8:fa346d946e7e 211 accelerometer->set_x_bw_filt(LIS2DW12_BW_FILT);
martlefebvre94 8:fa346d946e7e 212 accelerometer->get_x_bw_filt(&read_reg_int);
martlefebvre94 8:fa346d946e7e 213 printf("LIS2DW12 BW_FILT = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 214
martlefebvre94 8:fa346d946e7e 215 accelerometer->set_x_power_mode(LIS2DW12_POWER_MODE);
martlefebvre94 8:fa346d946e7e 216 accelerometer->get_x_power_mode(&read_reg_int, &read_reg_int_1, &read_reg_int_2);
martlefebvre94 8:fa346d946e7e 217 printf("LIS2DW12 LP_MODE = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 218 printf("LIS2DW12 MODE = %1d\r\n", read_reg_int_1);
martlefebvre94 8:fa346d946e7e 219 printf("LIS2DW12 LOW_NOISE = %1d\r\n", read_reg_int_2);
martlefebvre94 8:fa346d946e7e 220
martlefebvre94 8:fa346d946e7e 221 /* HTS221 relative humidity and temperature sensor configuration */
martlefebvre94 8:fa346d946e7e 222 hum_temp->enable();
martlefebvre94 8:fa346d946e7e 223 printf("/***** HTS221 humidity sensor configuration *****/\r\n");
martlefebvre94 8:fa346d946e7e 224
martlefebvre94 8:fa346d946e7e 225 hum_temp->read_id(&id);
martlefebvre94 8:fa346d946e7e 226 printf("HTS221 humidity & temperature = 0x%X\r\n", id);
martlefebvre94 8:fa346d946e7e 227
martlefebvre94 8:fa346d946e7e 228 hum_temp->set_odr(HTS221_ODR);
martlefebvre94 8:fa346d946e7e 229 hum_temp->get_odr(&read_reg);
martlefebvre94 8:fa346d946e7e 230 printf("HTS221 ODR = %1.3f [Hz]\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 231
martlefebvre94 8:fa346d946e7e 232 hum_temp->set_heater(HTS221_HEATER);
martlefebvre94 8:fa346d946e7e 233 hum_temp->get_heater(&read_reg_int);
martlefebvre94 8:fa346d946e7e 234 printf("HTS221 HEATER = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 235
martlefebvre94 8:fa346d946e7e 236 hum_temp->set_avg(HTS221_AVGH, HTS221_AVGT);
martlefebvre94 8:fa346d946e7e 237 hum_temp->get_avg(&read_reg, &read_reg_1);
martlefebvre94 8:fa346d946e7e 238 printf("HTS221 AVGH = %1.0f\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 239 printf("HTS221 AVGT = %1.0f\r\n", read_reg_1);
martlefebvre94 8:fa346d946e7e 240
martlefebvre94 8:fa346d946e7e 241 /* STTS751 Temperature sensor configuration */
martlefebvre94 8:fa346d946e7e 242 temp->enable();
martlefebvre94 8:fa346d946e7e 243 printf("/***** STTS751 temperature sensor configuration *****/\r\n");
martlefebvre94 8:fa346d946e7e 244
martlefebvre94 8:fa346d946e7e 245 temp->read_id(&id);
martlefebvre94 8:fa346d946e7e 246 printf("STTS751 temperature = 0x%X\r\n", id);
martlefebvre94 8:fa346d946e7e 247
martlefebvre94 8:fa346d946e7e 248 /* LSM6DSO Accelerometer and gyroscope configuration */
martlefebvre94 8:fa346d946e7e 249 acc_gyro->enable_x();
martlefebvre94 8:fa346d946e7e 250 acc_gyro->enable_g();
martlefebvre94 8:fa346d946e7e 251 printf("/***** LSM6DSO accelerometer and gyroscope sensor configuration *****/\r\n");
martlefebvre94 8:fa346d946e7e 252
martlefebvre94 8:fa346d946e7e 253 acc_gyro->read_id(&id);
martlefebvre94 8:fa346d946e7e 254 printf("LSM6DSO accelerometer & gyroscope = 0x%X\r\n", id);
martlefebvre94 8:fa346d946e7e 255
martlefebvre94 8:fa346d946e7e 256 acc_gyro->set_x_odr(LSM6DSO_ODR_XL);
martlefebvre94 8:fa346d946e7e 257 acc_gyro->get_x_odr(&read_reg);
martlefebvre94 8:fa346d946e7e 258 printf("LSM6DSO ODR_XL = %1.3f [Hz]\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 259
martlefebvre94 8:fa346d946e7e 260 acc_gyro->set_x_fs(LSM6DSO_FS_XL);
martlefebvre94 8:fa346d946e7e 261 acc_gyro->get_x_fs(&read_reg);
martlefebvre94 8:fa346d946e7e 262 printf("LSM6DSO FS_XL = %1.3f [g]\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 263
martlefebvre94 8:fa346d946e7e 264 acc_gyro->set_x_power_mode(LSM6DSO_XL_HM_MODE, LSM6DSO_XL_ULP_EN);
martlefebvre94 8:fa346d946e7e 265 acc_gyro->get_x_power_mode(&read_reg_int, &read_reg_int_1);
martlefebvre94 8:fa346d946e7e 266 printf("LSM6DSO XL_HM_MODE = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 267 printf("LSM6DSO XL_ULP_EN = %1d\r\n", read_reg_int_1);
martlefebvre94 8:fa346d946e7e 268
martlefebvre94 12:2c129618b350 269 acc_gyro->set_x_lpf2_en(LSM6DSO_LPF2_XL_EN);
martlefebvre94 12:2c129618b350 270 acc_gyro->get_x_lpf2_en(&read_reg_int);
martlefebvre94 12:2c129618b350 271 printf("LSM6DSO LPF2_XL_EN = %1d\r\n", read_reg_int);
martlefebvre94 12:2c129618b350 272
martlefebvre94 12:2c129618b350 273 acc_gyro->set_x_filter_config(LSM6DSO_HP_SLOPE_XL_EN, LSM6DSO_HPCF_XL);
martlefebvre94 12:2c129618b350 274 acc_gyro->get_x_filter_config(&read_reg_int, &read_reg_int_1);
martlefebvre94 12:2c129618b350 275 printf("LSM6DSO HP_SLOPE_XL_EN = %1d\r\n", read_reg_int);
martlefebvre94 12:2c129618b350 276 printf("LSM6DSO HPCF_XL = %1d\r\n", read_reg_int_1);
martlefebvre94 12:2c129618b350 277
martlefebvre94 8:fa346d946e7e 278 acc_gyro->set_g_odr(LSM6DSO_ODR_G);
martlefebvre94 8:fa346d946e7e 279 acc_gyro->get_g_odr(&read_reg);
martlefebvre94 8:fa346d946e7e 280 printf("LSM6DSO ODR_G = %1.3f [Hz]\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 281
martlefebvre94 8:fa346d946e7e 282 acc_gyro->set_g_fs(LSM6DSO_FS_XL);
martlefebvre94 8:fa346d946e7e 283 acc_gyro->get_g_fs(&read_reg);
martlefebvre94 8:fa346d946e7e 284 printf("LSM6DSO FS_G = %1.3f [dps]\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 285
martlefebvre94 12:2c129618b350 286 acc_gyro->set_g_power_mode(LSM6DSO_G_HM_MODE);
martlefebvre94 12:2c129618b350 287 acc_gyro->get_g_power_mode(&read_reg_int);
martlefebvre94 12:2c129618b350 288 printf("LSM6DSO G_HM_MODE = %1d\r\n", read_reg_int);
martlefebvre94 12:2c129618b350 289
martlefebvre94 12:2c129618b350 290 acc_gyro->set_g_lpf_config(LSM6DSO_LPF1_SEL_G, LSM6DSO_FTYPE);
martlefebvre94 12:2c129618b350 291 acc_gyro->get_g_lpf_config(&read_reg_int, &read_reg_int_1);
martlefebvre94 12:2c129618b350 292 printf("LSM6DSO LPF1_SEL_G = %1d\r\n", read_reg_int);
martlefebvre94 12:2c129618b350 293 printf("LSM6DSO FTYPE = %1d\r\n", read_reg_int_1);
martlefebvre94 12:2c129618b350 294
martlefebvre94 12:2c129618b350 295 acc_gyro->set_g_hpf_config(LSM6DSO_HP_EN_G, LSM6DSO_HPM_G);
martlefebvre94 12:2c129618b350 296 acc_gyro->get_g_hpf_config(&read_reg_int, &read_reg_int_1);
martlefebvre94 12:2c129618b350 297 printf("LSM6DSO HP_EN_G = %1d\r\n", read_reg_int);
martlefebvre94 12:2c129618b350 298 printf("LSM6DSO HPM_G = %1d\r\n", read_reg_int_1);
martlefebvre94 12:2c129618b350 299
martlefebvre94 8:fa346d946e7e 300 /* Print Flash memory information */
martlefebvre94 8:fa346d946e7e 301 print_flash_info();
martlefebvre94 8:fa346d946e7e 302
martlefebvre94 8:fa346d946e7e 303 /* Information for the user */
martlefebvre94 8:fa346d946e7e 304 printf("Press blue button to start data acquisition\r\n");
martlefebvre94 8:fa346d946e7e 305 printf("Press 'R' to read previously measured data\r\n");
martlefebvre94 8:fa346d946e7e 306
martlefebvre94 8:fa346d946e7e 307 /* Acquisition loop */
martlefebvre94 8:fa346d946e7e 308 while(1) {
martlefebvre94 8:fa346d946e7e 309 // Start saving data when button is pushed
martlefebvre94 8:fa346d946e7e 310 if (button1_pressed) {
martlefebvre94 8:fa346d946e7e 311 button1_pressed = false;
martlefebvre94 8:fa346d946e7e 312 save_data = true;
martlefebvre94 8:fa346d946e7e 313 erase_flash(false);
martlefebvre94 8:fa346d946e7e 314 printf("Acquiring data...\r\n");
martlefebvre94 8:fa346d946e7e 315 printf("Press blue button to stop data acquisition\r\n");
martlefebvre94 8:fa346d946e7e 316 Flash_addr = FLASH_BANK2_BASE;
martlefebvre94 8:fa346d946e7e 317 }
martlefebvre94 8:fa346d946e7e 318
martlefebvre94 8:fa346d946e7e 319 if (save_data) {
martlefebvre94 8:fa346d946e7e 320 // Acquisition task
martlefebvre94 8:fa346d946e7e 321 save_data = acquisition_task(true);
martlefebvre94 8:fa346d946e7e 322 }
martlefebvre94 8:fa346d946e7e 323 else {
martlefebvre94 8:fa346d946e7e 324 // Read task
martlefebvre94 8:fa346d946e7e 325 read_task();
martlefebvre94 8:fa346d946e7e 326 }
martlefebvre94 8:fa346d946e7e 327 }
martlefebvre94 8:fa346d946e7e 328 }
martlefebvre94 8:fa346d946e7e 329
martlefebvre94 8:fa346d946e7e 330 /* Acquisition task */
martlefebvre94 8:fa346d946e7e 331 bool acquisition_task(bool verbose)
martlefebvre94 8:fa346d946e7e 332 {
martlefebvre94 8:fa346d946e7e 333 int32_t m_axes[3];
martlefebvre94 8:fa346d946e7e 334 int32_t acc_axes[3];
martlefebvre94 8:fa346d946e7e 335 int32_t acc_axes_1[3];
martlefebvre94 8:fa346d946e7e 336 int32_t gyro_axes[3];
martlefebvre94 8:fa346d946e7e 337 float pressure_value, hum_value, temp_value, temp_value_1;
martlefebvre94 8:fa346d946e7e 338 char buffer[32];
martlefebvre94 8:fa346d946e7e 339
martlefebvre94 8:fa346d946e7e 340 uint32_t buffer_size = FLASH_PAGE_SIZE/4;
martlefebvre94 8:fa346d946e7e 341 uint32_t data_ind = 0;
martlefebvre94 8:fa346d946e7e 342 uint32_t data_buffer[buffer_size];
martlefebvre94 8:fa346d946e7e 343
martlefebvre94 8:fa346d946e7e 344 uint32_t Flash_addr = FLASH_BANK2_BASE;
martlefebvre94 8:fa346d946e7e 345
martlefebvre94 8:fa346d946e7e 346 while (Flash_addr <= FLASH_BANK2_END-FLASH_PAGE_SIZE+1) {
martlefebvre94 8:fa346d946e7e 347 // Read sensors data
martlefebvre94 8:fa346d946e7e 348 magnetometer->get_m_axes(m_axes);
martlefebvre94 8:fa346d946e7e 349 press_temp->get_pressure(&pressure_value);
martlefebvre94 8:fa346d946e7e 350 accelerometer->get_x_axes(acc_axes);
martlefebvre94 8:fa346d946e7e 351 hum_temp->get_temperature(&temp_value);
martlefebvre94 8:fa346d946e7e 352 hum_temp->get_humidity(&hum_value);
martlefebvre94 8:fa346d946e7e 353 temp->get_temperature(&temp_value_1);
martlefebvre94 8:fa346d946e7e 354 acc_gyro->get_x_axes(acc_axes_1);
martlefebvre94 8:fa346d946e7e 355 acc_gyro->get_g_axes(gyro_axes);
martlefebvre94 8:fa346d946e7e 356
martlefebvre94 8:fa346d946e7e 357 // Save data to Flash memory
martlefebvre94 8:fa346d946e7e 358 for (int i=0; i<3; i++) {
martlefebvre94 8:fa346d946e7e 359 // Write page in Flash memory
martlefebvre94 8:fa346d946e7e 360 if (data_ind >= buffer_size) {
martlefebvre94 8:fa346d946e7e 361 write_flash(Flash_addr, &data_buffer[0], buffer_size, false);
martlefebvre94 8:fa346d946e7e 362 Flash_addr += FLASH_PAGE_SIZE;
martlefebvre94 8:fa346d946e7e 363 data_ind = 0;
martlefebvre94 8:fa346d946e7e 364 }
martlefebvre94 8:fa346d946e7e 365
martlefebvre94 8:fa346d946e7e 366 // Write data to buffer
martlefebvre94 8:fa346d946e7e 367 data_buffer[data_ind] = (uint32_t) m_axes[i];
martlefebvre94 8:fa346d946e7e 368 data_ind++;
martlefebvre94 8:fa346d946e7e 369 }
martlefebvre94 8:fa346d946e7e 370
martlefebvre94 8:fa346d946e7e 371 // Print data in terminal
martlefebvre94 8:fa346d946e7e 372 if (verbose) {
martlefebvre94 8:fa346d946e7e 373 printf("LIS2MDL: [mag/mgauss] %6d, %6d, %6d\r\n", ((uint32_t) m_axes[0]), ((uint32_t) m_axes[1]), ((uint32_t) m_axes[2]));
martlefebvre94 8:fa346d946e7e 374 printf("LPS22HH: [press/mbar] %1.3f, [alt/m] %1.3f\r\n", pressure_value, pressure_to_altitude(pressure_value));
martlefebvre94 8:fa346d946e7e 375 printf("HTS221: [temp/deg C] %1.3f, [hum/%%] %1.3f\r\n", temp_value, hum_value);
martlefebvre94 8:fa346d946e7e 376 printf("STTS751 [temp/deg C] %1.3f\r\n", temp_value_1);
martlefebvre94 8:fa346d946e7e 377 printf("LIS2DW12: [acc/mg] %6d, %6d, %6d\r\n", ((uint32_t) acc_axes[0]), ((uint32_t) acc_axes[1]), ((uint32_t) acc_axes[2]));
martlefebvre94 8:fa346d946e7e 378 printf("LSM6DSO: [acc/mg] %6d, %6d, %6d\r\n", ((uint32_t) acc_axes_1[0]), ((uint32_t) acc_axes_1[1]), ((uint32_t) acc_axes_1[2]));
martlefebvre94 8:fa346d946e7e 379 printf("LSM6DSO: [gyro/mdps] %6d, %6d, %6d\r\n", ((uint32_t) gyro_axes[0]), ((uint32_t) gyro_axes[1]), ((uint32_t) gyro_axes[2]));
martlefebvre94 8:fa346d946e7e 380 }
martlefebvre94 8:fa346d946e7e 381
martlefebvre94 8:fa346d946e7e 382 // Wait for acquisition period
martlefebvre94 8:fa346d946e7e 383 wait(1/FS);
martlefebvre94 8:fa346d946e7e 384
martlefebvre94 8:fa346d946e7e 385 // Stop saving data when button is pushed
martlefebvre94 8:fa346d946e7e 386 if (button1_pressed) {
martlefebvre94 8:fa346d946e7e 387 button1_pressed = false;
martlefebvre94 8:fa346d946e7e 388 // Save remaining data to Flash memory
martlefebvre94 8:fa346d946e7e 389 write_flash(Flash_addr, &data_buffer[0], data_ind, false);
martlefebvre94 8:fa346d946e7e 390 printf("Data acquisition stopped\r\n");
martlefebvre94 8:fa346d946e7e 391 printf("Press 'R' to read the data\r\n");
martlefebvre94 8:fa346d946e7e 392 return false;
martlefebvre94 8:fa346d946e7e 393 }
martlefebvre94 8:fa346d946e7e 394 }
martlefebvre94 8:fa346d946e7e 395 printf("Data acquisition stopped\r\n");
martlefebvre94 8:fa346d946e7e 396 printf("Press 'R' to read the data\r\n");
martlefebvre94 8:fa346d946e7e 397 return false;
martlefebvre94 8:fa346d946e7e 398 }
martlefebvre94 8:fa346d946e7e 399
martlefebvre94 8:fa346d946e7e 400 /* Read task */
martlefebvre94 8:fa346d946e7e 401 void read_task()
martlefebvre94 8:fa346d946e7e 402 {
martlefebvre94 8:fa346d946e7e 403 char pc_input;
martlefebvre94 8:fa346d946e7e 404 uint32_t Flash_rdata[3];
martlefebvre94 8:fa346d946e7e 405 bool flash_empty = false;
martlefebvre94 8:fa346d946e7e 406
martlefebvre94 8:fa346d946e7e 407 // Read terminal input
martlefebvre94 8:fa346d946e7e 408 if (pc.readable()) {
martlefebvre94 8:fa346d946e7e 409 pc_input = pc.getc();
martlefebvre94 8:fa346d946e7e 410 }
martlefebvre94 8:fa346d946e7e 411 else {
martlefebvre94 8:fa346d946e7e 412 pc_input = ' ';
martlefebvre94 8:fa346d946e7e 413 }
martlefebvre94 8:fa346d946e7e 414
martlefebvre94 8:fa346d946e7e 415 // Read Flash memory if 'R' is pressed
martlefebvre94 8:fa346d946e7e 416 if ((pc_input == 'r') || (pc_input == 'R')) {
martlefebvre94 8:fa346d946e7e 417 // Data labels
martlefebvre94 8:fa346d946e7e 418 printf("mag_X\tmag_Y\tmag_Z\r\n");
martlefebvre94 8:fa346d946e7e 419
martlefebvre94 8:fa346d946e7e 420 // Read 1st Flash data
martlefebvre94 8:fa346d946e7e 421 uint32_t Flash_addr_temp = FLASH_BANK2_BASE;
martlefebvre94 8:fa346d946e7e 422 read_flash(Flash_addr_temp, &Flash_rdata[0], LIS2MDL_DATA_SIZE);
martlefebvre94 8:fa346d946e7e 423
martlefebvre94 8:fa346d946e7e 424 // Read Flash data
martlefebvre94 8:fa346d946e7e 425 while ((Flash_addr_temp <= FLASH_BANK2_END-LIS2MDL_DATA_SIZE+1) && !flash_empty) {
martlefebvre94 8:fa346d946e7e 426 // Print read data in the terminal
martlefebvre94 8:fa346d946e7e 427 printf("%6d\t%6d\t%6d\r\n", Flash_rdata[0], Flash_rdata[1], Flash_rdata[2]);
martlefebvre94 8:fa346d946e7e 428 Flash_addr_temp += LIS2MDL_DATA_SIZE;
martlefebvre94 8:fa346d946e7e 429
martlefebvre94 8:fa346d946e7e 430 // Check if the next address is not empty (erased Flash only contains 0)
martlefebvre94 8:fa346d946e7e 431 if (Flash_addr_temp <= FLASH_BANK2_END-LIS2MDL_DATA_SIZE+1) {
martlefebvre94 8:fa346d946e7e 432 read_flash(Flash_addr_temp, &Flash_rdata[0], LIS2MDL_DATA_SIZE);
martlefebvre94 8:fa346d946e7e 433 if ((Flash_rdata[0] == 0) && (Flash_rdata[1] == 0) && (Flash_rdata[2] == 0)) {
martlefebvre94 8:fa346d946e7e 434 flash_empty = true;
martlefebvre94 8:fa346d946e7e 435 }
martlefebvre94 8:fa346d946e7e 436 }
martlefebvre94 8:fa346d946e7e 437 }
martlefebvre94 8:fa346d946e7e 438 }
martlefebvre94 8:fa346d946e7e 439 }
martlefebvre94 8:fa346d946e7e 440
martlefebvre94 8:fa346d946e7e 441 /* Print Flash memory info */
martlefebvre94 8:fa346d946e7e 442 void print_flash_info()
martlefebvre94 8:fa346d946e7e 443 {
martlefebvre94 8:fa346d946e7e 444 printf("**************************************************\n\r");
martlefebvre94 8:fa346d946e7e 445 printf("/***** Flash memory info *****/\r\n");
martlefebvre94 8:fa346d946e7e 446 printf("Flash size: %d [B]\r\n", FLASH_SIZE);
martlefebvre94 8:fa346d946e7e 447 printf("Flash page size: %d [B]\r\n", FLASH_PAGE_SIZE);
martlefebvre94 8:fa346d946e7e 448 printf("Flash nb of pages: %d \r\n", FLASH_SIZE/FLASH_PAGE_SIZE);
martlefebvre94 8:fa346d946e7e 449 printf("Flash bank 1 base address: 0x%X\r\n", FLASH_BASE);
martlefebvre94 8:fa346d946e7e 450 printf("Flash bank 1 end address: 0x%X\r\n", FLASH_BANK1_END);
martlefebvre94 8:fa346d946e7e 451 printf("Flash bank 2 base address: 0x%X\r\n", FLASH_BANK2_BASE);
martlefebvre94 8:fa346d946e7e 452 printf("Flash bank 2 end address: 0x%X\r\n", FLASH_BANK2_END);
martlefebvre94 8:fa346d946e7e 453 printf("**************************************************\n\r");
martlefebvre94 8:fa346d946e7e 454 }
martlefebvre94 8:fa346d946e7e 455
martlefebvre94 6:b2e247935342 456 /* Erase content of Flash memory */
martlefebvre94 6:b2e247935342 457 bool erase_flash(bool verbose)
cparata 0:535249dc4bf5 458 {
martlefebvre94 6:b2e247935342 459 printf("Erasing Flash memory...\r\n");
martlefebvre94 6:b2e247935342 460
martlefebvre94 6:b2e247935342 461 // Unlock Flash memory
martlefebvre94 6:b2e247935342 462 HAL_FLASH_Unlock();
cparata 0:535249dc4bf5 463
martlefebvre94 6:b2e247935342 464 // Erase Flash memory
martlefebvre94 6:b2e247935342 465 FLASH_EraseInitTypeDef eraser;
martlefebvre94 6:b2e247935342 466 uint32_t Flash_addr = FLASH_BANK2_BASE;
martlefebvre94 6:b2e247935342 467 uint32_t page_error = 0;
martlefebvre94 6:b2e247935342 468 int32_t page = 1;
martlefebvre94 6:b2e247935342 469
martlefebvre94 6:b2e247935342 470 while (Flash_addr < FLASH_BANK2_END) {
martlefebvre94 6:b2e247935342 471 eraser.TypeErase = FLASH_TYPEERASE_PAGES;
martlefebvre94 6:b2e247935342 472 eraser.PageAddress = Flash_addr;
martlefebvre94 6:b2e247935342 473 eraser.NbPages = 1;
martlefebvre94 6:b2e247935342 474 if(HAL_OK != HAL_FLASHEx_Erase(&eraser, &page_error)) {
martlefebvre94 6:b2e247935342 475 if (verbose) {printf("Flash erase failed!\r\n");}
martlefebvre94 6:b2e247935342 476 printf("Error 0x%X\r\n", page_error);
martlefebvre94 6:b2e247935342 477 HAL_FLASH_Lock();
martlefebvre94 6:b2e247935342 478 return false;
martlefebvre94 6:b2e247935342 479 }
martlefebvre94 6:b2e247935342 480 if (verbose) {printf("Erased page %d at address: 0x%X\r\n", page, Flash_addr);}
martlefebvre94 6:b2e247935342 481 Flash_addr += FLASH_PAGE_SIZE;
martlefebvre94 6:b2e247935342 482 page++;
martlefebvre94 6:b2e247935342 483 }
martlefebvre94 6:b2e247935342 484
martlefebvre94 6:b2e247935342 485 if (verbose) {printf("Flash erase succesful!\r\n");}
martlefebvre94 6:b2e247935342 486 return true;
martlefebvre94 6:b2e247935342 487 }
cparata 0:535249dc4bf5 488
martlefebvre94 6:b2e247935342 489 /* Write Flash memory */
martlefebvre94 8:fa346d946e7e 490 bool write_flash(uint32_t Flash_addr, uint32_t* Flash_wdata, int32_t n_words, bool verbose)
martlefebvre94 6:b2e247935342 491 {
martlefebvre94 8:fa346d946e7e 492 clock_t time;
martlefebvre94 8:fa346d946e7e 493 if (verbose) {time = clock();}
martlefebvre94 8:fa346d946e7e 494
martlefebvre94 6:b2e247935342 495 // Unlock Flash memory
martlefebvre94 6:b2e247935342 496 HAL_FLASH_Unlock();
martlefebvre94 6:b2e247935342 497
martlefebvre94 6:b2e247935342 498 // Write Flash memory
martlefebvre94 8:fa346d946e7e 499 for (int i=0; i<n_words; i++) {
martlefebvre94 8:fa346d946e7e 500 if (HAL_OK != HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Flash_addr, Flash_wdata[i])) {
martlefebvre94 8:fa346d946e7e 501 if (verbose) {printf("Flash write failed!\r\n");}
martlefebvre94 8:fa346d946e7e 502 HAL_FLASH_Lock();
martlefebvre94 8:fa346d946e7e 503 return false;
martlefebvre94 8:fa346d946e7e 504 }
martlefebvre94 8:fa346d946e7e 505 Flash_addr += 4;
martlefebvre94 6:b2e247935342 506 }
martlefebvre94 6:b2e247935342 507 if (verbose) {printf("Flash write succesful!\r\n");}
martlefebvre94 8:fa346d946e7e 508
martlefebvre94 6:b2e247935342 509 HAL_FLASH_Lock();
martlefebvre94 8:fa346d946e7e 510
martlefebvre94 8:fa346d946e7e 511 if (verbose) {
martlefebvre94 8:fa346d946e7e 512 time = clock() - time;
martlefebvre94 8:fa346d946e7e 513 printf("Time to write: %1.6f [s]\r\n", (((double) time)/CLOCKS_PER_SEC));
martlefebvre94 8:fa346d946e7e 514 }
martlefebvre94 8:fa346d946e7e 515
martlefebvre94 6:b2e247935342 516 return true;
martlefebvre94 6:b2e247935342 517 }
cparata 0:535249dc4bf5 518
martlefebvre94 6:b2e247935342 519 /* Read Flash memory */
martlefebvre94 6:b2e247935342 520 void read_flash(uint32_t Flash_addr, uint32_t* Flash_rdata, uint32_t n_bytes)
martlefebvre94 6:b2e247935342 521 {
martlefebvre94 6:b2e247935342 522 memcpy(Flash_rdata, (uint32_t*) Flash_addr, n_bytes);
martlefebvre94 6:b2e247935342 523 }
cparata 0:535249dc4bf5 524
martlefebvre94 6:b2e247935342 525 /* Enables button when bouncing is over */
martlefebvre94 6:b2e247935342 526 void button1_enabled_cb(void)
martlefebvre94 6:b2e247935342 527 {
martlefebvre94 6:b2e247935342 528 button1_enabled = true;
cparata 0:535249dc4bf5 529 }
cparata 0:535249dc4bf5 530
martlefebvre94 6:b2e247935342 531 /* ISR handling button pressed event */
martlefebvre94 6:b2e247935342 532 void button1_onpressed_cb(void)
martlefebvre94 6:b2e247935342 533 {
martlefebvre94 6:b2e247935342 534 if (button1_enabled) { // Disabled while the button is bouncing
martlefebvre94 6:b2e247935342 535 button1_enabled = false;
martlefebvre94 6:b2e247935342 536 button1_pressed = true; // To be read by the main loop
martlefebvre94 6:b2e247935342 537 button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms
martlefebvre94 6:b2e247935342 538 }
martlefebvre94 6:b2e247935342 539 }
martlefebvre94 6:b2e247935342 540
martlefebvre94 8:fa346d946e7e 541 /* Helper function for printing floats & doubles */
martlefebvre94 8:fa346d946e7e 542 static char *print_double(char *str, double v)
martlefebvre94 6:b2e247935342 543 {
martlefebvre94 8:fa346d946e7e 544 int decimalDigits = 6;
martlefebvre94 8:fa346d946e7e 545 int i = 1;
martlefebvre94 8:fa346d946e7e 546 int intPart, fractPart;
martlefebvre94 8:fa346d946e7e 547 int len;
martlefebvre94 8:fa346d946e7e 548 char *ptr;
martlefebvre94 8:fa346d946e7e 549
martlefebvre94 8:fa346d946e7e 550 /* prepare decimal digits multiplicator */
martlefebvre94 8:fa346d946e7e 551 for (; decimalDigits != 0; i *= 10, decimalDigits--);
martlefebvre94 8:fa346d946e7e 552
martlefebvre94 8:fa346d946e7e 553 /* calculate integer & fractinal parts */
martlefebvre94 8:fa346d946e7e 554 intPart = (int)v;
martlefebvre94 8:fa346d946e7e 555 fractPart = (int)((v - (double)(int)v) * i);
martlefebvre94 8:fa346d946e7e 556
martlefebvre94 8:fa346d946e7e 557 /* fill in integer part */
martlefebvre94 8:fa346d946e7e 558 sprintf(str, "%i.", intPart);
martlefebvre94 8:fa346d946e7e 559
martlefebvre94 8:fa346d946e7e 560 /* prepare fill in of fractional part */
martlefebvre94 8:fa346d946e7e 561 len = strlen(str);
martlefebvre94 8:fa346d946e7e 562 ptr = &str[len];
martlefebvre94 8:fa346d946e7e 563
martlefebvre94 8:fa346d946e7e 564 /* fill in leading fractional zeros */
martlefebvre94 8:fa346d946e7e 565 for (i /= 10; i > 1; i /= 10, ptr++) {
martlefebvre94 8:fa346d946e7e 566 if (fractPart >= i) {
martlefebvre94 8:fa346d946e7e 567 break;
martlefebvre94 6:b2e247935342 568 }
martlefebvre94 8:fa346d946e7e 569 *ptr = '0';
martlefebvre94 6:b2e247935342 570 }
martlefebvre94 6:b2e247935342 571
martlefebvre94 8:fa346d946e7e 572 /* fill in (rest of) fractional part */
martlefebvre94 8:fa346d946e7e 573 sprintf(ptr, "%i", fractPart);
martlefebvre94 8:fa346d946e7e 574
martlefebvre94 8:fa346d946e7e 575 return str;
martlefebvre94 6:b2e247935342 576 }
martlefebvre94 6:b2e247935342 577
martlefebvre94 8:fa346d946e7e 578 /* Pressure to altitude conversion */
martlefebvre94 8:fa346d946e7e 579 float pressure_to_altitude(double pressure)
cparata 5:7c883cce2bc4 580 {
martlefebvre94 8:fa346d946e7e 581 return 44330.77 * (1-pow(pressure/P0, 0.1902632));
martlefebvre94 8:fa346d946e7e 582 }