2019-2020 Multisensor project using the X_NUCLEO_IKS01A3 sensor platform

Dependencies:   X_NUCLEO_IKS01A3

Committer:
martlefebvre94
Date:
Tue Nov 26 11:33:10 2019 +0000
Revision:
18:17f5509ac69b
Parent:
17:930b91883e6b
Child:
19:35f7b17d4bb5
Addition of GPIOs for controlling the LEDs

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 13:f4ad8550374a 41 M. Lefebvre - 2019-2020
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>
martlefebvre94 17:930b91883e6b 47 #include <math.h>
cparata 0:535249dc4bf5 48 #include "mbed.h"
cparata 0:535249dc4bf5 49 #include "XNucleoIKS01A3.h"
martlefebvre94 6:b2e247935342 50 #include "stm32l073xx.h"
martlefebvre94 18:17f5509ac69b 51 #include "stm32l0xx_hal.h"
martlefebvre94 6:b2e247935342 52 #include "stm32l0xx_hal_flash.h"
martlefebvre94 18:17f5509ac69b 53 #include "main.h"
martlefebvre94 18:17f5509ac69b 54 #include "adc.h"
martlefebvre94 18:17f5509ac69b 55 #include "gpio.h"
martlefebvre94 6:b2e247935342 56
martlefebvre94 6:b2e247935342 57 /* Defines */
martlefebvre94 16:566c4e5f090e 58 #define VDD 3.3 // Supply voltage (V)
martlefebvre94 15:77dec0c4ecba 59 #define FS 10.0 // Readout frequency (Hz) - /!\ Must be below 19Hz
martlefebvre94 15:77dec0c4ecba 60 #define DATA_SIZE 64 // Number of bytes to store in Flash memory
martlefebvre94 15:77dec0c4ecba 61 #define FLASH_WRITE_TIME 0.00328 // Flash write time (s)
martlefebvre94 15:77dec0c4ecba 62 #define TS (1/FS)-((DATA_SIZE/4)*FLASH_WRITE_TIME)
martlefebvre94 8:fa346d946e7e 63
martlefebvre94 18:17f5509ac69b 64 // GPIO
martlefebvre94 18:17f5509ac69b 65 #define RED_LED GPIO_PIN_5
martlefebvre94 18:17f5509ac69b 66 #define IR_LED GPIO_PIN_6
martlefebvre94 18:17f5509ac69b 67 #define GREEN_LED GPIO_PIN_7
martlefebvre94 18:17f5509ac69b 68
martlefebvre94 8:fa346d946e7e 69 // LIS2MDL Magnetometer
martlefebvre94 7:4a3b6202963e 70 #define LIS2MDL_ODR 50.0 // Output data rate (10, 20, 50 or 100 Hz)
martlefebvre94 7:4a3b6202963e 71 #define LIS2MDL_LP 0 // Power mode (0 for high-resolution mode, 1 for low-power mode)
martlefebvre94 7:4a3b6202963e 72 #define LIS2MDL_LPF 0 // Bandwidth (0 for ODR/2, 1 for ODR/4)
martlefebvre94 7:4a3b6202963e 73 #define LIS2MDL_COMP_TEMP_EN 1 // Temperature compensation (0 disabled, 1 enabled)
martlefebvre94 7:4a3b6202963e 74 #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 75 #define LIS2MDL_DATA_SIZE 12 // Number of bytes for LIS2MDL magnetometer data
martlefebvre94 8:fa346d946e7e 76
martlefebvre94 8:fa346d946e7e 77 // LPS22HH Pressure sensor
martlefebvre94 8:fa346d946e7e 78 #define P0 1013.26 // Sea level pressure (hPa)
martlefebvre94 8:fa346d946e7e 79 #define LPS22HH_ODR 50.0 // Output data rate (one-shot, 1, 10, 25, 50, 75, 100, 200 Hz)
martlefebvre94 8:fa346d946e7e 80 #define LPS22HH_LOW_NOISE_EN 1 // Low-noise (0 disabled, 1 enabled)
martlefebvre94 8:fa346d946e7e 81 #define LPS22HH_LPF_CFG 3 // Device bandwidth (0 for ODR/2, 2 for ODR/9, 3 for ODR/20)
martlefebvre94 13:f4ad8550374a 82 #define LPS22HH_DATA_SIZE 8 // Number of bytes for LPS22HH pressure sensor data
martlefebvre94 8:fa346d946e7e 83
martlefebvre94 8:fa346d946e7e 84 // LIS2DW12 Accelerometer
martlefebvre94 8:fa346d946e7e 85 #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 86 #define LIS2DW12_FS 4 // Full-scale +-(2, 4, 8 or 16 g)
martlefebvre94 8:fa346d946e7e 87 #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 88 #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 89 #define LIS2DW12_MODE 1 // Mode (0 for low-power, 1 for high-performance, 2 for single data conversion)
martlefebvre94 15:77dec0c4ecba 90 #define LIS2DW12_LOW_NOISE 1 // Low-noise (0 disabled, 1 enabled)
martlefebvre94 8:fa346d946e7e 91 #define LIS2DW12_POWER_MODE LIS2DW12_LP_MODE + (LIS2DW12_MODE << 2) + (LIS2DW12_LOW_NOISE << 4)
martlefebvre94 15:77dec0c4ecba 92 #define LIS2DW12_DATA_SIZE 12 // Number of bytes for LIS2DW12 accelerometer sensor data
martlefebvre94 8:fa346d946e7e 93
martlefebvre94 8:fa346d946e7e 94 // HTS221 Relative humidity and temperature sensor
martlefebvre94 8:fa346d946e7e 95 #define HTS221_ODR 1 // Output data rate (one-shot, 1Hz, 7Hz, 12.5Hz)
martlefebvre94 8:fa346d946e7e 96 #define HTS221_HEATER 0 // Heater configuration (0 disabled, 1 enabled)
martlefebvre94 8:fa346d946e7e 97 #define HTS221_AVGH 32 // Humidity averaging (4 to 512)
martlefebvre94 8:fa346d946e7e 98 #define HTS221_AVGT 16 // Temperature averaging (2 to 256)
martlefebvre94 8:fa346d946e7e 99
martlefebvre94 8:fa346d946e7e 100 // LSM6DSO Accelerometer + gyroscope
martlefebvre94 8:fa346d946e7e 101 #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 102 #define LSM6DSO_FS_XL 4 // Accelerometer full scale (2, 4, 8, 16g)
martlefebvre94 8:fa346d946e7e 103 #define LSM6DSO_XL_HM_MODE 1 // Accelerometer high-performance mode (0 enabled, 1 disabled)
martlefebvre94 8:fa346d946e7e 104 #define LSM6DSO_XL_ULP_EN 0 // Accelerometer ultra-low-power configuration (0 disabled, 1 enabled)
martlefebvre94 12:2c129618b350 105 #define LSM6DSO_LPF2_XL_EN 1 // Accelerometer high-resolution selection (0 for 1st stage of digital filtering, 1 for 2nd stage)
martlefebvre94 12:2c129618b350 106 #define LSM6DSO_HP_SLOPE_XL_EN 0 // Accelerometer high-pass filter selection (0 for low-pass, 1 for high-pass)
martlefebvre94 12:2c129618b350 107 #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 108 #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 109 #define LSM6DSO_FS_G 1000 // Gyroscope full scale (250, 500, 1000, 2000dps)
martlefebvre94 12:2c129618b350 110 #define LSM6DSO_G_HM_MODE 1 // Gyroscope high-performance mode (0 enabled, 1 disabled)
martlefebvre94 12:2c129618b350 111 #define LSM6DSO_LPF1_SEL_G 1 // Gyroscope digital LPF1 enable (0 disabled, 1 enabled)
martlefebvre94 12:2c129618b350 112 #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 113 #define LSM6DSO_HP_EN_G 1 // Gyroscope digital HPF enable (0 HPF disabled, 1 HPF enabled)
martlefebvre94 12:2c129618b350 114 #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 115
martlefebvre94 8:fa346d946e7e 116 /* Functions definition */
martlefebvre94 8:fa346d946e7e 117 bool acquisition_task(bool verbose);
martlefebvre94 8:fa346d946e7e 118 void read_task();
martlefebvre94 8:fa346d946e7e 119 void print_flash_info();
martlefebvre94 8:fa346d946e7e 120 bool erase_flash(bool verbose);
martlefebvre94 8:fa346d946e7e 121 bool write_flash(uint32_t Flash_addr, uint32_t* Flash_wdata, int32_t n_words, bool verbose);
martlefebvre94 8:fa346d946e7e 122 void read_flash(uint32_t Flash_addr, uint32_t* Flash_rdata, uint32_t n_bytes);
martlefebvre94 8:fa346d946e7e 123 void button1_enabled_cb(void);
martlefebvre94 8:fa346d946e7e 124 void button1_onpressed_cb(void);
martlefebvre94 8:fa346d946e7e 125 static char *print_double(char *str, double v);
martlefebvre94 8:fa346d946e7e 126 float pressure_to_altitude(double pressure);
martlefebvre94 13:f4ad8550374a 127 uint32_t FloatToUint(float n);
martlefebvre94 13:f4ad8550374a 128 float UintToFloat(uint32_t n);
martlefebvre94 6:b2e247935342 129
martlefebvre94 16:566c4e5f090e 130 static void SystemClock_Config(void);
martlefebvre94 16:566c4e5f090e 131
martlefebvre94 18:17f5509ac69b 132 /* ADC */
martlefebvre94 18:17f5509ac69b 133 uint32_t adcValue=0;
martlefebvre94 16:566c4e5f090e 134
martlefebvre94 6:b2e247935342 135 /* Serial link */
martlefebvre94 6:b2e247935342 136 Serial pc(SERIAL_TX, SERIAL_RX);
martlefebvre94 6:b2e247935342 137
martlefebvre94 6:b2e247935342 138 /* Button */
martlefebvre94 6:b2e247935342 139 InterruptIn button1(USER_BUTTON);
martlefebvre94 6:b2e247935342 140 volatile bool button1_pressed = false; // Used in the main loop
martlefebvre94 6:b2e247935342 141 volatile bool button1_enabled = true; // Used for debouncing
martlefebvre94 6:b2e247935342 142 Timeout button1_timeout; // Used for debouncing
cparata 0:535249dc4bf5 143
cparata 0:535249dc4bf5 144 /* Instantiate the expansion board */
cparata 0:535249dc4bf5 145 static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4);
cparata 0:535249dc4bf5 146
cparata 0:535249dc4bf5 147 /* Retrieve the composing elements of the expansion board */
cparata 0:535249dc4bf5 148 static LIS2MDLSensor *magnetometer = mems_expansion_board->magnetometer;
cparata 0:535249dc4bf5 149 static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
cparata 0:535249dc4bf5 150 static LPS22HHSensor *press_temp = mems_expansion_board->pt_sensor;
cparata 0:535249dc4bf5 151 static LSM6DSOSensor *acc_gyro = mems_expansion_board->acc_gyro;
cparata 0:535249dc4bf5 152 static LIS2DW12Sensor *accelerometer = mems_expansion_board->accelerometer;
cparata 0:535249dc4bf5 153 static STTS751Sensor *temp = mems_expansion_board->t_sensor;
cparata 0:535249dc4bf5 154
martlefebvre94 8:fa346d946e7e 155 /* Main */
martlefebvre94 8:fa346d946e7e 156 int main()
martlefebvre94 8:fa346d946e7e 157 {
martlefebvre94 16:566c4e5f090e 158 // Reset of all peripherals
martlefebvre94 16:566c4e5f090e 159 HAL_Init();
martlefebvre94 16:566c4e5f090e 160
martlefebvre94 16:566c4e5f090e 161 // Configure the system clock
martlefebvre94 16:566c4e5f090e 162 SystemClock_Config();
martlefebvre94 16:566c4e5f090e 163
martlefebvre94 8:fa346d946e7e 164 uint8_t id;
martlefebvre94 8:fa346d946e7e 165 float read_reg, read_reg_1;
martlefebvre94 8:fa346d946e7e 166 uint8_t read_reg_int, read_reg_int_1, read_reg_int_2;
martlefebvre94 8:fa346d946e7e 167
martlefebvre94 8:fa346d946e7e 168 bool save_data = false;
martlefebvre94 8:fa346d946e7e 169 uint32_t Flash_addr = FLASH_BANK2_BASE;
martlefebvre94 8:fa346d946e7e 170
martlefebvre94 8:fa346d946e7e 171 /* Serial link configuration */
martlefebvre94 8:fa346d946e7e 172 pc.baud(115200);
martlefebvre94 8:fa346d946e7e 173
martlefebvre94 18:17f5509ac69b 174 /* GPIO configuration */
martlefebvre94 18:17f5509ac69b 175 MX_GPIO_Init();
martlefebvre94 18:17f5509ac69b 176
martlefebvre94 18:17f5509ac69b 177 /* ADC configuration */
martlefebvre94 16:566c4e5f090e 178 MX_ADC_Init();
martlefebvre94 16:566c4e5f090e 179 HAL_ADC_Start(&hadc);
martlefebvre94 16:566c4e5f090e 180
martlefebvre94 8:fa346d946e7e 181 /* Button configuration */
martlefebvre94 8:fa346d946e7e 182 button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
martlefebvre94 8:fa346d946e7e 183
martlefebvre94 8:fa346d946e7e 184 /* Reset message */
martlefebvre94 8:fa346d946e7e 185 printf("\n\r**************************************************\n\r");
martlefebvre94 8:fa346d946e7e 186 printf("LELEC2811 IKS01A3 Multisensor Program\n\r");
martlefebvre94 8:fa346d946e7e 187 printf("**************************************************\n\r");
martlefebvre94 8:fa346d946e7e 188
martlefebvre94 8:fa346d946e7e 189 /* LIS2MDL magnetometer sensor configuration */
martlefebvre94 8:fa346d946e7e 190 magnetometer->enable();
martlefebvre94 8:fa346d946e7e 191 printf("/***** LIS2MDL magnetometer configuration *****/\r\n");
martlefebvre94 8:fa346d946e7e 192
martlefebvre94 8:fa346d946e7e 193 magnetometer->read_id(&id);
martlefebvre94 8:fa346d946e7e 194 printf("LIS2MDL magnetometer = 0x%X\r\n", id);
martlefebvre94 8:fa346d946e7e 195
martlefebvre94 8:fa346d946e7e 196 magnetometer->set_m_odr(LIS2MDL_ODR);
martlefebvre94 8:fa346d946e7e 197 magnetometer->get_m_odr(&read_reg);
martlefebvre94 8:fa346d946e7e 198 printf("LIS2MDL ODR = %1.1f [Hz]\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 199
martlefebvre94 8:fa346d946e7e 200 magnetometer->set_m_lp(LIS2MDL_LP);
martlefebvre94 8:fa346d946e7e 201 magnetometer->get_m_lp(&read_reg_int);
martlefebvre94 8:fa346d946e7e 202 printf("LIS2MDL LP = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 203
martlefebvre94 8:fa346d946e7e 204 magnetometer->set_m_lpf(LIS2MDL_LPF);
martlefebvre94 8:fa346d946e7e 205 magnetometer->get_m_lpf(&read_reg_int);
martlefebvre94 8:fa346d946e7e 206 printf("LIS2MDL LPF = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 207
martlefebvre94 8:fa346d946e7e 208 magnetometer->set_m_comp_temp_en(LIS2MDL_COMP_TEMP_EN);
martlefebvre94 8:fa346d946e7e 209 magnetometer->get_m_comp_temp_en(&read_reg_int);
martlefebvre94 8:fa346d946e7e 210 printf("LIS2MDL COMP_TEMP_EN = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 211
martlefebvre94 8:fa346d946e7e 212 magnetometer->set_m_off_canc(LIS2MDL_OFF_CANC);
martlefebvre94 8:fa346d946e7e 213 magnetometer->get_m_off_canc(&read_reg_int);
martlefebvre94 8:fa346d946e7e 214 printf("LIS2MDL OFF_CANC = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 215
martlefebvre94 8:fa346d946e7e 216 /* LPS22HH pressure sensor configuration */
martlefebvre94 8:fa346d946e7e 217 press_temp->enable();
martlefebvre94 8:fa346d946e7e 218 printf("/***** LPS22HH pressure sensor configuration *****/\r\n");
martlefebvre94 8:fa346d946e7e 219
martlefebvre94 8:fa346d946e7e 220 press_temp->read_id(&id);
martlefebvre94 8:fa346d946e7e 221 printf("LPS22HH pressure = 0x%X\r\n", id);
martlefebvre94 8:fa346d946e7e 222
martlefebvre94 8:fa346d946e7e 223 press_temp->set_odr(LPS22HH_ODR, LPS22HH_LOW_NOISE_EN);
martlefebvre94 8:fa346d946e7e 224 press_temp->get_odr(&read_reg, &read_reg_int);
martlefebvre94 8:fa346d946e7e 225 printf("LPS22HH ODR = %1.1f [Hz]\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 226 printf("LPS22HH LOW_NOISE_EN = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 227
martlefebvre94 8:fa346d946e7e 228 press_temp->set_lpfp_cfg(LPS22HH_LPF_CFG);
martlefebvre94 8:fa346d946e7e 229 press_temp->get_lpfp_cfg(&read_reg_int);
martlefebvre94 8:fa346d946e7e 230 printf("LPS22HH LPF_CFG = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 231
martlefebvre94 8:fa346d946e7e 232 /* LIS2DW12 accelerometer sensor configuration */
martlefebvre94 8:fa346d946e7e 233 accelerometer->enable_x();
martlefebvre94 8:fa346d946e7e 234 printf("/***** LIS2DW12 accelerometer sensor configuration *****/\r\n");
martlefebvre94 8:fa346d946e7e 235
martlefebvre94 8:fa346d946e7e 236 accelerometer->read_id(&id);
martlefebvre94 8:fa346d946e7e 237 printf("LIS2DW12 accelerometer = 0x%X\r\n", id);
martlefebvre94 8:fa346d946e7e 238
martlefebvre94 8:fa346d946e7e 239 accelerometer->set_x_odr(LIS2DW12_ODR);
martlefebvre94 8:fa346d946e7e 240 accelerometer->get_x_odr(&read_reg);
martlefebvre94 8:fa346d946e7e 241 printf("LIS2DW12 ODR = %1.3f [Hz]\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 242
martlefebvre94 8:fa346d946e7e 243 accelerometer->set_x_fs(LIS2DW12_FS);
martlefebvre94 8:fa346d946e7e 244 accelerometer->get_x_fs(&read_reg);
martlefebvre94 8:fa346d946e7e 245 printf("LIS2DW12 FS = %1.3f [g]\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 246
martlefebvre94 8:fa346d946e7e 247 accelerometer->set_x_bw_filt(LIS2DW12_BW_FILT);
martlefebvre94 8:fa346d946e7e 248 accelerometer->get_x_bw_filt(&read_reg_int);
martlefebvre94 8:fa346d946e7e 249 printf("LIS2DW12 BW_FILT = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 250
martlefebvre94 8:fa346d946e7e 251 accelerometer->set_x_power_mode(LIS2DW12_POWER_MODE);
martlefebvre94 8:fa346d946e7e 252 accelerometer->get_x_power_mode(&read_reg_int, &read_reg_int_1, &read_reg_int_2);
martlefebvre94 8:fa346d946e7e 253 printf("LIS2DW12 LP_MODE = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 254 printf("LIS2DW12 MODE = %1d\r\n", read_reg_int_1);
martlefebvre94 8:fa346d946e7e 255 printf("LIS2DW12 LOW_NOISE = %1d\r\n", read_reg_int_2);
martlefebvre94 8:fa346d946e7e 256
martlefebvre94 8:fa346d946e7e 257 /* HTS221 relative humidity and temperature sensor configuration */
martlefebvre94 8:fa346d946e7e 258 hum_temp->enable();
martlefebvre94 8:fa346d946e7e 259 printf("/***** HTS221 humidity sensor configuration *****/\r\n");
martlefebvre94 8:fa346d946e7e 260
martlefebvre94 8:fa346d946e7e 261 hum_temp->read_id(&id);
martlefebvre94 8:fa346d946e7e 262 printf("HTS221 humidity & temperature = 0x%X\r\n", id);
martlefebvre94 8:fa346d946e7e 263
martlefebvre94 8:fa346d946e7e 264 hum_temp->set_odr(HTS221_ODR);
martlefebvre94 8:fa346d946e7e 265 hum_temp->get_odr(&read_reg);
martlefebvre94 8:fa346d946e7e 266 printf("HTS221 ODR = %1.3f [Hz]\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 267
martlefebvre94 8:fa346d946e7e 268 hum_temp->set_heater(HTS221_HEATER);
martlefebvre94 8:fa346d946e7e 269 hum_temp->get_heater(&read_reg_int);
martlefebvre94 8:fa346d946e7e 270 printf("HTS221 HEATER = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 271
martlefebvre94 8:fa346d946e7e 272 hum_temp->set_avg(HTS221_AVGH, HTS221_AVGT);
martlefebvre94 8:fa346d946e7e 273 hum_temp->get_avg(&read_reg, &read_reg_1);
martlefebvre94 8:fa346d946e7e 274 printf("HTS221 AVGH = %1.0f\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 275 printf("HTS221 AVGT = %1.0f\r\n", read_reg_1);
martlefebvre94 8:fa346d946e7e 276
martlefebvre94 8:fa346d946e7e 277 /* STTS751 Temperature sensor configuration */
martlefebvre94 8:fa346d946e7e 278 temp->enable();
martlefebvre94 8:fa346d946e7e 279 printf("/***** STTS751 temperature sensor configuration *****/\r\n");
martlefebvre94 8:fa346d946e7e 280
martlefebvre94 8:fa346d946e7e 281 temp->read_id(&id);
martlefebvre94 8:fa346d946e7e 282 printf("STTS751 temperature = 0x%X\r\n", id);
martlefebvre94 8:fa346d946e7e 283
martlefebvre94 8:fa346d946e7e 284 /* LSM6DSO Accelerometer and gyroscope configuration */
martlefebvre94 8:fa346d946e7e 285 acc_gyro->enable_x();
martlefebvre94 8:fa346d946e7e 286 acc_gyro->enable_g();
martlefebvre94 8:fa346d946e7e 287 printf("/***** LSM6DSO accelerometer and gyroscope sensor configuration *****/\r\n");
martlefebvre94 8:fa346d946e7e 288
martlefebvre94 8:fa346d946e7e 289 acc_gyro->read_id(&id);
martlefebvre94 8:fa346d946e7e 290 printf("LSM6DSO accelerometer & gyroscope = 0x%X\r\n", id);
martlefebvre94 8:fa346d946e7e 291
martlefebvre94 8:fa346d946e7e 292 acc_gyro->set_x_odr(LSM6DSO_ODR_XL);
martlefebvre94 8:fa346d946e7e 293 acc_gyro->get_x_odr(&read_reg);
martlefebvre94 8:fa346d946e7e 294 printf("LSM6DSO ODR_XL = %1.3f [Hz]\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 295
martlefebvre94 8:fa346d946e7e 296 acc_gyro->set_x_fs(LSM6DSO_FS_XL);
martlefebvre94 8:fa346d946e7e 297 acc_gyro->get_x_fs(&read_reg);
martlefebvre94 8:fa346d946e7e 298 printf("LSM6DSO FS_XL = %1.3f [g]\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 299
martlefebvre94 8:fa346d946e7e 300 acc_gyro->set_x_power_mode(LSM6DSO_XL_HM_MODE, LSM6DSO_XL_ULP_EN);
martlefebvre94 8:fa346d946e7e 301 acc_gyro->get_x_power_mode(&read_reg_int, &read_reg_int_1);
martlefebvre94 8:fa346d946e7e 302 printf("LSM6DSO XL_HM_MODE = %1d\r\n", read_reg_int);
martlefebvre94 8:fa346d946e7e 303 printf("LSM6DSO XL_ULP_EN = %1d\r\n", read_reg_int_1);
martlefebvre94 8:fa346d946e7e 304
martlefebvre94 12:2c129618b350 305 acc_gyro->set_x_lpf2_en(LSM6DSO_LPF2_XL_EN);
martlefebvre94 12:2c129618b350 306 acc_gyro->get_x_lpf2_en(&read_reg_int);
martlefebvre94 12:2c129618b350 307 printf("LSM6DSO LPF2_XL_EN = %1d\r\n", read_reg_int);
martlefebvre94 12:2c129618b350 308
martlefebvre94 12:2c129618b350 309 acc_gyro->set_x_filter_config(LSM6DSO_HP_SLOPE_XL_EN, LSM6DSO_HPCF_XL);
martlefebvre94 12:2c129618b350 310 acc_gyro->get_x_filter_config(&read_reg_int, &read_reg_int_1);
martlefebvre94 12:2c129618b350 311 printf("LSM6DSO HP_SLOPE_XL_EN = %1d\r\n", read_reg_int);
martlefebvre94 12:2c129618b350 312 printf("LSM6DSO HPCF_XL = %1d\r\n", read_reg_int_1);
martlefebvre94 12:2c129618b350 313
martlefebvre94 8:fa346d946e7e 314 acc_gyro->set_g_odr(LSM6DSO_ODR_G);
martlefebvre94 8:fa346d946e7e 315 acc_gyro->get_g_odr(&read_reg);
martlefebvre94 8:fa346d946e7e 316 printf("LSM6DSO ODR_G = %1.3f [Hz]\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 317
martlefebvre94 8:fa346d946e7e 318 acc_gyro->set_g_fs(LSM6DSO_FS_XL);
martlefebvre94 8:fa346d946e7e 319 acc_gyro->get_g_fs(&read_reg);
martlefebvre94 8:fa346d946e7e 320 printf("LSM6DSO FS_G = %1.3f [dps]\r\n", read_reg);
martlefebvre94 8:fa346d946e7e 321
martlefebvre94 12:2c129618b350 322 acc_gyro->set_g_power_mode(LSM6DSO_G_HM_MODE);
martlefebvre94 12:2c129618b350 323 acc_gyro->get_g_power_mode(&read_reg_int);
martlefebvre94 12:2c129618b350 324 printf("LSM6DSO G_HM_MODE = %1d\r\n", read_reg_int);
martlefebvre94 12:2c129618b350 325
martlefebvre94 12:2c129618b350 326 acc_gyro->set_g_lpf_config(LSM6DSO_LPF1_SEL_G, LSM6DSO_FTYPE);
martlefebvre94 12:2c129618b350 327 acc_gyro->get_g_lpf_config(&read_reg_int, &read_reg_int_1);
martlefebvre94 12:2c129618b350 328 printf("LSM6DSO LPF1_SEL_G = %1d\r\n", read_reg_int);
martlefebvre94 12:2c129618b350 329 printf("LSM6DSO FTYPE = %1d\r\n", read_reg_int_1);
martlefebvre94 12:2c129618b350 330
martlefebvre94 12:2c129618b350 331 acc_gyro->set_g_hpf_config(LSM6DSO_HP_EN_G, LSM6DSO_HPM_G);
martlefebvre94 12:2c129618b350 332 acc_gyro->get_g_hpf_config(&read_reg_int, &read_reg_int_1);
martlefebvre94 12:2c129618b350 333 printf("LSM6DSO HP_EN_G = %1d\r\n", read_reg_int);
martlefebvre94 12:2c129618b350 334 printf("LSM6DSO HPM_G = %1d\r\n", read_reg_int_1);
martlefebvre94 12:2c129618b350 335
martlefebvre94 8:fa346d946e7e 336 /* Print Flash memory information */
martlefebvre94 8:fa346d946e7e 337 print_flash_info();
martlefebvre94 8:fa346d946e7e 338
martlefebvre94 8:fa346d946e7e 339 /* Information for the user */
martlefebvre94 8:fa346d946e7e 340 printf("Press blue button to start data acquisition\r\n");
martlefebvre94 8:fa346d946e7e 341 printf("Press 'R' to read previously measured data\r\n");
martlefebvre94 8:fa346d946e7e 342
martlefebvre94 18:17f5509ac69b 343 /* GPIO setting */
martlefebvre94 18:17f5509ac69b 344 HAL_GPIO_WritePin(GPIOA, RED_LED, GPIO_PIN_RESET);
martlefebvre94 18:17f5509ac69b 345 HAL_GPIO_WritePin(GPIOA, IR_LED, GPIO_PIN_RESET);
martlefebvre94 18:17f5509ac69b 346 HAL_GPIO_WritePin(GPIOA, GREEN_LED, GPIO_PIN_SET);
martlefebvre94 18:17f5509ac69b 347
martlefebvre94 8:fa346d946e7e 348 /* Acquisition loop */
martlefebvre94 8:fa346d946e7e 349 while(1) {
martlefebvre94 8:fa346d946e7e 350 // Start saving data when button is pushed
martlefebvre94 8:fa346d946e7e 351 if (button1_pressed) {
martlefebvre94 8:fa346d946e7e 352 button1_pressed = false;
martlefebvre94 8:fa346d946e7e 353 save_data = true;
martlefebvre94 8:fa346d946e7e 354 erase_flash(false);
martlefebvre94 8:fa346d946e7e 355 printf("Acquiring data...\r\n");
martlefebvre94 8:fa346d946e7e 356 printf("Press blue button to stop data acquisition\r\n");
martlefebvre94 8:fa346d946e7e 357 Flash_addr = FLASH_BANK2_BASE;
martlefebvre94 8:fa346d946e7e 358 }
martlefebvre94 8:fa346d946e7e 359
martlefebvre94 8:fa346d946e7e 360 if (save_data) {
martlefebvre94 8:fa346d946e7e 361 // Acquisition task
martlefebvre94 8:fa346d946e7e 362 save_data = acquisition_task(true);
martlefebvre94 8:fa346d946e7e 363 }
martlefebvre94 8:fa346d946e7e 364 else {
martlefebvre94 8:fa346d946e7e 365 // Read task
martlefebvre94 8:fa346d946e7e 366 read_task();
martlefebvre94 8:fa346d946e7e 367 }
martlefebvre94 8:fa346d946e7e 368 }
martlefebvre94 8:fa346d946e7e 369 }
martlefebvre94 8:fa346d946e7e 370
martlefebvre94 8:fa346d946e7e 371 /* Acquisition task */
martlefebvre94 8:fa346d946e7e 372 bool acquisition_task(bool verbose)
martlefebvre94 8:fa346d946e7e 373 {
martlefebvre94 8:fa346d946e7e 374 int32_t m_axes[3];
martlefebvre94 8:fa346d946e7e 375 int32_t acc_axes[3];
martlefebvre94 8:fa346d946e7e 376 int32_t acc_axes_1[3];
martlefebvre94 8:fa346d946e7e 377 int32_t gyro_axes[3];
martlefebvre94 8:fa346d946e7e 378 float pressure_value, hum_value, temp_value, temp_value_1;
martlefebvre94 15:77dec0c4ecba 379 int32_t data_buffer[DATA_SIZE/4];
martlefebvre94 8:fa346d946e7e 380
martlefebvre94 8:fa346d946e7e 381 uint32_t Flash_addr = FLASH_BANK2_BASE;
martlefebvre94 18:17f5509ac69b 382
martlefebvre94 18:17f5509ac69b 383 // Start ADC
martlefebvre94 18:17f5509ac69b 384 //HAL_ADC_Start_IT(&hadc);
martlefebvre94 18:17f5509ac69b 385
martlefebvre94 15:77dec0c4ecba 386 while (Flash_addr <= FLASH_BANK2_END-DATA_SIZE+1) {
martlefebvre94 8:fa346d946e7e 387 // Read sensors data
martlefebvre94 8:fa346d946e7e 388 magnetometer->get_m_axes(m_axes);
martlefebvre94 8:fa346d946e7e 389 press_temp->get_pressure(&pressure_value);
martlefebvre94 8:fa346d946e7e 390 accelerometer->get_x_axes(acc_axes);
martlefebvre94 8:fa346d946e7e 391 hum_temp->get_temperature(&temp_value);
martlefebvre94 8:fa346d946e7e 392 hum_temp->get_humidity(&hum_value);
martlefebvre94 8:fa346d946e7e 393 temp->get_temperature(&temp_value_1);
martlefebvre94 8:fa346d946e7e 394 acc_gyro->get_x_axes(acc_axes_1);
martlefebvre94 8:fa346d946e7e 395 acc_gyro->get_g_axes(gyro_axes);
martlefebvre94 8:fa346d946e7e 396
martlefebvre94 8:fa346d946e7e 397 // Save data to Flash memory
martlefebvre94 15:77dec0c4ecba 398 data_buffer[0] = m_axes[0];
martlefebvre94 15:77dec0c4ecba 399 data_buffer[1] = m_axes[1];
martlefebvre94 15:77dec0c4ecba 400 data_buffer[2] = m_axes[2];
martlefebvre94 15:77dec0c4ecba 401 data_buffer[3] = acc_axes[0];
martlefebvre94 15:77dec0c4ecba 402 data_buffer[4] = acc_axes[1];
martlefebvre94 15:77dec0c4ecba 403 data_buffer[5] = acc_axes[2];
martlefebvre94 15:77dec0c4ecba 404 data_buffer[6] = acc_axes_1[0];
martlefebvre94 15:77dec0c4ecba 405 data_buffer[7] = acc_axes_1[1];
martlefebvre94 15:77dec0c4ecba 406 data_buffer[8] = acc_axes_1[2];
martlefebvre94 15:77dec0c4ecba 407 data_buffer[9] = gyro_axes[0];
martlefebvre94 15:77dec0c4ecba 408 data_buffer[10] = gyro_axes[1];
martlefebvre94 15:77dec0c4ecba 409 data_buffer[11] = gyro_axes[2];
martlefebvre94 15:77dec0c4ecba 410 data_buffer[12] = (int32_t) FloatToUint(pressure_value);
martlefebvre94 15:77dec0c4ecba 411 data_buffer[13] = (int32_t) FloatToUint(hum_value);
martlefebvre94 15:77dec0c4ecba 412 data_buffer[14] = (int32_t) FloatToUint(temp_value);
martlefebvre94 16:566c4e5f090e 413 if (HAL_ADC_PollForConversion(&hadc, 5) == HAL_OK)
martlefebvre94 16:566c4e5f090e 414 {
martlefebvre94 16:566c4e5f090e 415 adcValue = HAL_ADC_GetValue(&hadc);
martlefebvre94 16:566c4e5f090e 416 }
martlefebvre94 16:566c4e5f090e 417 data_buffer[15] = (int32_t) FloatToUint(((float) adcValue)/4096 * VDD);
martlefebvre94 15:77dec0c4ecba 418
martlefebvre94 15:77dec0c4ecba 419 write_flash(Flash_addr, (uint32_t*) &data_buffer[0], DATA_SIZE/4, false);
martlefebvre94 15:77dec0c4ecba 420
martlefebvre94 15:77dec0c4ecba 421 // Increase Flash address
martlefebvre94 15:77dec0c4ecba 422 Flash_addr += DATA_SIZE;
martlefebvre94 8:fa346d946e7e 423
martlefebvre94 8:fa346d946e7e 424 // Print data in terminal
martlefebvre94 8:fa346d946e7e 425 if (verbose) {
martlefebvre94 8:fa346d946e7e 426 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 427 printf("LPS22HH: [press/mbar] %1.3f, [alt/m] %1.3f\r\n", pressure_value, pressure_to_altitude(pressure_value));
martlefebvre94 8:fa346d946e7e 428 printf("HTS221: [temp/deg C] %1.3f, [hum/%%] %1.3f\r\n", temp_value, hum_value);
martlefebvre94 8:fa346d946e7e 429 printf("STTS751 [temp/deg C] %1.3f\r\n", temp_value_1);
martlefebvre94 8:fa346d946e7e 430 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 431 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 432 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 433 }
martlefebvre94 8:fa346d946e7e 434
martlefebvre94 8:fa346d946e7e 435 // Wait for acquisition period
martlefebvre94 15:77dec0c4ecba 436 wait(TS);
martlefebvre94 8:fa346d946e7e 437
martlefebvre94 8:fa346d946e7e 438 // Stop saving data when button is pushed
martlefebvre94 8:fa346d946e7e 439 if (button1_pressed) {
martlefebvre94 8:fa346d946e7e 440 button1_pressed = false;
martlefebvre94 8:fa346d946e7e 441 printf("Data acquisition stopped\r\n");
martlefebvre94 8:fa346d946e7e 442 printf("Press 'R' to read the data\r\n");
martlefebvre94 8:fa346d946e7e 443 return false;
martlefebvre94 8:fa346d946e7e 444 }
martlefebvre94 8:fa346d946e7e 445 }
martlefebvre94 8:fa346d946e7e 446 printf("Data acquisition stopped\r\n");
martlefebvre94 8:fa346d946e7e 447 printf("Press 'R' to read the data\r\n");
martlefebvre94 8:fa346d946e7e 448 return false;
martlefebvre94 8:fa346d946e7e 449 }
martlefebvre94 8:fa346d946e7e 450
martlefebvre94 8:fa346d946e7e 451 /* Read task */
martlefebvre94 8:fa346d946e7e 452 void read_task()
martlefebvre94 8:fa346d946e7e 453 {
martlefebvre94 8:fa346d946e7e 454 char pc_input;
martlefebvre94 15:77dec0c4ecba 455 uint32_t Flash_rdata[DATA_SIZE/4];
martlefebvre94 8:fa346d946e7e 456 bool flash_empty = false;
martlefebvre94 8:fa346d946e7e 457
martlefebvre94 8:fa346d946e7e 458 // Read terminal input
martlefebvre94 8:fa346d946e7e 459 if (pc.readable()) {
martlefebvre94 8:fa346d946e7e 460 pc_input = pc.getc();
martlefebvre94 8:fa346d946e7e 461 }
martlefebvre94 8:fa346d946e7e 462 else {
martlefebvre94 8:fa346d946e7e 463 pc_input = ' ';
martlefebvre94 8:fa346d946e7e 464 }
martlefebvre94 8:fa346d946e7e 465
martlefebvre94 8:fa346d946e7e 466 // Read Flash memory if 'R' is pressed
martlefebvre94 8:fa346d946e7e 467 if ((pc_input == 'r') || (pc_input == 'R')) {
martlefebvre94 8:fa346d946e7e 468 // Data labels
martlefebvre94 15:77dec0c4ecba 469 printf("mag_X\tmag_Y\tmag_Z\tacc_X\tacc_Y\tacc_Z\tacc_X_1\tacc_Y_1\tacc_Z_1\tgyr_X\tgyr_Y\tgyr_Z\tpress\thum\ttemp\ttemp_1\r\n");
martlefebvre94 8:fa346d946e7e 470
martlefebvre94 8:fa346d946e7e 471 // Read 1st Flash data
martlefebvre94 8:fa346d946e7e 472 uint32_t Flash_addr_temp = FLASH_BANK2_BASE;
martlefebvre94 15:77dec0c4ecba 473 read_flash(Flash_addr_temp, &Flash_rdata[0], DATA_SIZE);
martlefebvre94 8:fa346d946e7e 474
martlefebvre94 8:fa346d946e7e 475 // Read Flash data
martlefebvre94 15:77dec0c4ecba 476 while ((Flash_addr_temp <= FLASH_BANK2_END-DATA_SIZE+1) && !flash_empty) {
martlefebvre94 8:fa346d946e7e 477 // Print read data in the terminal
martlefebvre94 15:77dec0c4ecba 478 printf("%6d\t%6d\t%6d\t%6d\t%6d\t%6d\t%6d\t%6d\t%6d\t%6d\t%6d\t%6d\t%1.6f\t%1.6f\t%1.6f\t%1.6f\r\n", Flash_rdata[0], Flash_rdata[1], Flash_rdata[2], Flash_rdata[3], Flash_rdata[4], Flash_rdata[5], Flash_rdata[6], Flash_rdata[7], Flash_rdata[8], Flash_rdata[9], Flash_rdata[10], Flash_rdata[11], UintToFloat(Flash_rdata[12]), UintToFloat(Flash_rdata[13]), UintToFloat(Flash_rdata[14]), UintToFloat(Flash_rdata[15]));
martlefebvre94 15:77dec0c4ecba 479 Flash_addr_temp += DATA_SIZE;
martlefebvre94 8:fa346d946e7e 480
martlefebvre94 8:fa346d946e7e 481 // Check if the next address is not empty (erased Flash only contains 0)
martlefebvre94 15:77dec0c4ecba 482 if (Flash_addr_temp <= FLASH_BANK2_END-DATA_SIZE+1) {
martlefebvre94 15:77dec0c4ecba 483 read_flash(Flash_addr_temp, &Flash_rdata[0], DATA_SIZE);
martlefebvre94 8:fa346d946e7e 484 if ((Flash_rdata[0] == 0) && (Flash_rdata[1] == 0) && (Flash_rdata[2] == 0)) {
martlefebvre94 8:fa346d946e7e 485 flash_empty = true;
martlefebvre94 8:fa346d946e7e 486 }
martlefebvre94 8:fa346d946e7e 487 }
martlefebvre94 8:fa346d946e7e 488 }
martlefebvre94 8:fa346d946e7e 489 }
martlefebvre94 8:fa346d946e7e 490 }
martlefebvre94 8:fa346d946e7e 491
martlefebvre94 8:fa346d946e7e 492 /* Print Flash memory info */
martlefebvre94 8:fa346d946e7e 493 void print_flash_info()
martlefebvre94 8:fa346d946e7e 494 {
martlefebvre94 8:fa346d946e7e 495 printf("**************************************************\n\r");
martlefebvre94 8:fa346d946e7e 496 printf("/***** Flash memory info *****/\r\n");
martlefebvre94 8:fa346d946e7e 497 printf("Flash size: %d [B]\r\n", FLASH_SIZE);
martlefebvre94 8:fa346d946e7e 498 printf("Flash page size: %d [B]\r\n", FLASH_PAGE_SIZE);
martlefebvre94 8:fa346d946e7e 499 printf("Flash nb of pages: %d \r\n", FLASH_SIZE/FLASH_PAGE_SIZE);
martlefebvre94 8:fa346d946e7e 500 printf("Flash bank 1 base address: 0x%X\r\n", FLASH_BASE);
martlefebvre94 8:fa346d946e7e 501 printf("Flash bank 1 end address: 0x%X\r\n", FLASH_BANK1_END);
martlefebvre94 8:fa346d946e7e 502 printf("Flash bank 2 base address: 0x%X\r\n", FLASH_BANK2_BASE);
martlefebvre94 8:fa346d946e7e 503 printf("Flash bank 2 end address: 0x%X\r\n", FLASH_BANK2_END);
martlefebvre94 8:fa346d946e7e 504 printf("**************************************************\n\r");
martlefebvre94 8:fa346d946e7e 505 }
martlefebvre94 8:fa346d946e7e 506
martlefebvre94 6:b2e247935342 507 /* Erase content of Flash memory */
martlefebvre94 6:b2e247935342 508 bool erase_flash(bool verbose)
cparata 0:535249dc4bf5 509 {
martlefebvre94 6:b2e247935342 510 printf("Erasing Flash memory...\r\n");
martlefebvre94 6:b2e247935342 511
martlefebvre94 6:b2e247935342 512 // Unlock Flash memory
martlefebvre94 6:b2e247935342 513 HAL_FLASH_Unlock();
cparata 0:535249dc4bf5 514
martlefebvre94 6:b2e247935342 515 // Erase Flash memory
martlefebvre94 6:b2e247935342 516 FLASH_EraseInitTypeDef eraser;
martlefebvre94 6:b2e247935342 517 uint32_t Flash_addr = FLASH_BANK2_BASE;
martlefebvre94 6:b2e247935342 518 uint32_t page_error = 0;
martlefebvre94 6:b2e247935342 519 int32_t page = 1;
martlefebvre94 6:b2e247935342 520
martlefebvre94 6:b2e247935342 521 while (Flash_addr < FLASH_BANK2_END) {
martlefebvre94 6:b2e247935342 522 eraser.TypeErase = FLASH_TYPEERASE_PAGES;
martlefebvre94 6:b2e247935342 523 eraser.PageAddress = Flash_addr;
martlefebvre94 6:b2e247935342 524 eraser.NbPages = 1;
martlefebvre94 6:b2e247935342 525 if(HAL_OK != HAL_FLASHEx_Erase(&eraser, &page_error)) {
martlefebvre94 6:b2e247935342 526 if (verbose) {printf("Flash erase failed!\r\n");}
martlefebvre94 6:b2e247935342 527 printf("Error 0x%X\r\n", page_error);
martlefebvre94 6:b2e247935342 528 HAL_FLASH_Lock();
martlefebvre94 6:b2e247935342 529 return false;
martlefebvre94 6:b2e247935342 530 }
martlefebvre94 6:b2e247935342 531 if (verbose) {printf("Erased page %d at address: 0x%X\r\n", page, Flash_addr);}
martlefebvre94 6:b2e247935342 532 Flash_addr += FLASH_PAGE_SIZE;
martlefebvre94 6:b2e247935342 533 page++;
martlefebvre94 6:b2e247935342 534 }
martlefebvre94 6:b2e247935342 535
martlefebvre94 6:b2e247935342 536 if (verbose) {printf("Flash erase succesful!\r\n");}
martlefebvre94 6:b2e247935342 537 return true;
martlefebvre94 6:b2e247935342 538 }
cparata 0:535249dc4bf5 539
martlefebvre94 6:b2e247935342 540 /* Write Flash memory */
martlefebvre94 8:fa346d946e7e 541 bool write_flash(uint32_t Flash_addr, uint32_t* Flash_wdata, int32_t n_words, bool verbose)
martlefebvre94 6:b2e247935342 542 {
martlefebvre94 8:fa346d946e7e 543 clock_t time;
martlefebvre94 8:fa346d946e7e 544 if (verbose) {time = clock();}
martlefebvre94 8:fa346d946e7e 545
martlefebvre94 6:b2e247935342 546 // Unlock Flash memory
martlefebvre94 6:b2e247935342 547 HAL_FLASH_Unlock();
martlefebvre94 6:b2e247935342 548
martlefebvre94 6:b2e247935342 549 // Write Flash memory
martlefebvre94 8:fa346d946e7e 550 for (int i=0; i<n_words; i++) {
martlefebvre94 8:fa346d946e7e 551 if (HAL_OK != HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Flash_addr, Flash_wdata[i])) {
martlefebvre94 8:fa346d946e7e 552 if (verbose) {printf("Flash write failed!\r\n");}
martlefebvre94 8:fa346d946e7e 553 HAL_FLASH_Lock();
martlefebvre94 8:fa346d946e7e 554 return false;
martlefebvre94 8:fa346d946e7e 555 }
martlefebvre94 8:fa346d946e7e 556 Flash_addr += 4;
martlefebvre94 6:b2e247935342 557 }
martlefebvre94 6:b2e247935342 558 if (verbose) {printf("Flash write succesful!\r\n");}
martlefebvre94 8:fa346d946e7e 559
martlefebvre94 6:b2e247935342 560 HAL_FLASH_Lock();
martlefebvre94 8:fa346d946e7e 561
martlefebvre94 8:fa346d946e7e 562 if (verbose) {
martlefebvre94 8:fa346d946e7e 563 time = clock() - time;
martlefebvre94 8:fa346d946e7e 564 printf("Time to write: %1.6f [s]\r\n", (((double) time)/CLOCKS_PER_SEC));
martlefebvre94 8:fa346d946e7e 565 }
martlefebvre94 8:fa346d946e7e 566
martlefebvre94 6:b2e247935342 567 return true;
martlefebvre94 6:b2e247935342 568 }
cparata 0:535249dc4bf5 569
martlefebvre94 6:b2e247935342 570 /* Read Flash memory */
martlefebvre94 6:b2e247935342 571 void read_flash(uint32_t Flash_addr, uint32_t* Flash_rdata, uint32_t n_bytes)
martlefebvre94 6:b2e247935342 572 {
martlefebvre94 6:b2e247935342 573 memcpy(Flash_rdata, (uint32_t*) Flash_addr, n_bytes);
martlefebvre94 6:b2e247935342 574 }
cparata 0:535249dc4bf5 575
martlefebvre94 6:b2e247935342 576 /* Enables button when bouncing is over */
martlefebvre94 6:b2e247935342 577 void button1_enabled_cb(void)
martlefebvre94 6:b2e247935342 578 {
martlefebvre94 6:b2e247935342 579 button1_enabled = true;
cparata 0:535249dc4bf5 580 }
cparata 0:535249dc4bf5 581
martlefebvre94 6:b2e247935342 582 /* ISR handling button pressed event */
martlefebvre94 6:b2e247935342 583 void button1_onpressed_cb(void)
martlefebvre94 6:b2e247935342 584 {
martlefebvre94 6:b2e247935342 585 if (button1_enabled) { // Disabled while the button is bouncing
martlefebvre94 6:b2e247935342 586 button1_enabled = false;
martlefebvre94 6:b2e247935342 587 button1_pressed = true; // To be read by the main loop
martlefebvre94 6:b2e247935342 588 button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms
martlefebvre94 6:b2e247935342 589 }
martlefebvre94 6:b2e247935342 590 }
martlefebvre94 6:b2e247935342 591
martlefebvre94 8:fa346d946e7e 592 /* Helper function for printing floats & doubles */
martlefebvre94 8:fa346d946e7e 593 static char *print_double(char *str, double v)
martlefebvre94 6:b2e247935342 594 {
martlefebvre94 8:fa346d946e7e 595 int decimalDigits = 6;
martlefebvre94 8:fa346d946e7e 596 int i = 1;
martlefebvre94 8:fa346d946e7e 597 int intPart, fractPart;
martlefebvre94 8:fa346d946e7e 598 int len;
martlefebvre94 8:fa346d946e7e 599 char *ptr;
martlefebvre94 8:fa346d946e7e 600
martlefebvre94 8:fa346d946e7e 601 /* prepare decimal digits multiplicator */
martlefebvre94 8:fa346d946e7e 602 for (; decimalDigits != 0; i *= 10, decimalDigits--);
martlefebvre94 8:fa346d946e7e 603
martlefebvre94 8:fa346d946e7e 604 /* calculate integer & fractinal parts */
martlefebvre94 8:fa346d946e7e 605 intPart = (int)v;
martlefebvre94 8:fa346d946e7e 606 fractPart = (int)((v - (double)(int)v) * i);
martlefebvre94 8:fa346d946e7e 607
martlefebvre94 8:fa346d946e7e 608 /* fill in integer part */
martlefebvre94 8:fa346d946e7e 609 sprintf(str, "%i.", intPart);
martlefebvre94 8:fa346d946e7e 610
martlefebvre94 8:fa346d946e7e 611 /* prepare fill in of fractional part */
martlefebvre94 8:fa346d946e7e 612 len = strlen(str);
martlefebvre94 8:fa346d946e7e 613 ptr = &str[len];
martlefebvre94 8:fa346d946e7e 614
martlefebvre94 8:fa346d946e7e 615 /* fill in leading fractional zeros */
martlefebvre94 8:fa346d946e7e 616 for (i /= 10; i > 1; i /= 10, ptr++) {
martlefebvre94 8:fa346d946e7e 617 if (fractPart >= i) {
martlefebvre94 8:fa346d946e7e 618 break;
martlefebvre94 6:b2e247935342 619 }
martlefebvre94 8:fa346d946e7e 620 *ptr = '0';
martlefebvre94 6:b2e247935342 621 }
martlefebvre94 6:b2e247935342 622
martlefebvre94 8:fa346d946e7e 623 /* fill in (rest of) fractional part */
martlefebvre94 8:fa346d946e7e 624 sprintf(ptr, "%i", fractPart);
martlefebvre94 8:fa346d946e7e 625
martlefebvre94 8:fa346d946e7e 626 return str;
martlefebvre94 6:b2e247935342 627 }
martlefebvre94 6:b2e247935342 628
martlefebvre94 8:fa346d946e7e 629 /* Pressure to altitude conversion */
martlefebvre94 8:fa346d946e7e 630 float pressure_to_altitude(double pressure)
cparata 5:7c883cce2bc4 631 {
martlefebvre94 8:fa346d946e7e 632 return 44330.77 * (1-pow(pressure/P0, 0.1902632));
martlefebvre94 8:fa346d946e7e 633 }
martlefebvre94 13:f4ad8550374a 634
martlefebvre94 13:f4ad8550374a 635 uint32_t FloatToUint(float n)
martlefebvre94 13:f4ad8550374a 636 {
martlefebvre94 13:f4ad8550374a 637 return (uint32_t)(*(uint32_t*)&n);
martlefebvre94 13:f4ad8550374a 638 }
martlefebvre94 13:f4ad8550374a 639
martlefebvre94 13:f4ad8550374a 640 float UintToFloat(uint32_t n)
martlefebvre94 13:f4ad8550374a 641 {
martlefebvre94 13:f4ad8550374a 642 return (float)(*(float*)&n);
martlefebvre94 16:566c4e5f090e 643 }
martlefebvre94 16:566c4e5f090e 644
martlefebvre94 16:566c4e5f090e 645 static void SystemClock_Config(void)
martlefebvre94 16:566c4e5f090e 646 {
martlefebvre94 16:566c4e5f090e 647 RCC_OscInitTypeDef RCC_OscInitStruct;
martlefebvre94 16:566c4e5f090e 648 RCC_ClkInitTypeDef RCC_ClkInitStruct;
martlefebvre94 16:566c4e5f090e 649
martlefebvre94 16:566c4e5f090e 650 /* Configure the main internal regulator output voltage */
martlefebvre94 16:566c4e5f090e 651 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
martlefebvre94 16:566c4e5f090e 652
martlefebvre94 16:566c4e5f090e 653 /* Initializes the CPU, AHB and APB busses clocks */
martlefebvre94 16:566c4e5f090e 654 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
martlefebvre94 16:566c4e5f090e 655 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
martlefebvre94 16:566c4e5f090e 656 RCC_OscInitStruct.HSICalibrationValue = 16;
martlefebvre94 16:566c4e5f090e 657 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
martlefebvre94 16:566c4e5f090e 658 if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
martlefebvre94 16:566c4e5f090e 659 {
martlefebvre94 16:566c4e5f090e 660 printf("Error in oscillator configuration\r\n");
martlefebvre94 16:566c4e5f090e 661 }
martlefebvre94 16:566c4e5f090e 662
martlefebvre94 16:566c4e5f090e 663 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
martlefebvre94 16:566c4e5f090e 664 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
martlefebvre94 16:566c4e5f090e 665 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
martlefebvre94 16:566c4e5f090e 666 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
martlefebvre94 16:566c4e5f090e 667 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
martlefebvre94 16:566c4e5f090e 668 if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
martlefebvre94 16:566c4e5f090e 669 {
martlefebvre94 16:566c4e5f090e 670 printf("Error in clock configuration\r\n");
martlefebvre94 16:566c4e5f090e 671 }
martlefebvre94 16:566c4e5f090e 672 }
martlefebvre94 16:566c4e5f090e 673
martlefebvre94 18:17f5509ac69b 674 /**
martlefebvre94 18:17f5509ac69b 675 * @brief Conversion complete callback in non blocking mode
martlefebvre94 18:17f5509ac69b 676 * @param AdcHandle : AdcHandle handle
martlefebvre94 18:17f5509ac69b 677 * @note This example shows a simple way to report end of conversion, and
martlefebvre94 18:17f5509ac69b 678 * you can add your own implementation.
martlefebvre94 18:17f5509ac69b 679 * @retval None
martlefebvre94 18:17f5509ac69b 680 */
martlefebvre94 18:17f5509ac69b 681 void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *AdcHandle)
martlefebvre94 16:566c4e5f090e 682 {
martlefebvre94 18:17f5509ac69b 683 adcValue = HAL_ADC_GetValue(AdcHandle);
martlefebvre94 16:566c4e5f090e 684 }
martlefebvre94 16:566c4e5f090e 685
martlefebvre94 18:17f5509ac69b 686 /**
martlefebvre94 18:17f5509ac69b 687 * @brief This function is executed in case of error occurrence.
martlefebvre94 18:17f5509ac69b 688 * @param file: The file name as string.
martlefebvre94 18:17f5509ac69b 689 * @param line: The line in file as a number.
martlefebvre94 18:17f5509ac69b 690 * @retval None
martlefebvre94 18:17f5509ac69b 691 */
martlefebvre94 18:17f5509ac69b 692 void _Error_Handler(char *file, int line)
martlefebvre94 16:566c4e5f090e 693 {
martlefebvre94 18:17f5509ac69b 694 while(1)
martlefebvre94 16:566c4e5f090e 695 {
martlefebvre94 16:566c4e5f090e 696 }
martlefebvre94 16:566c4e5f090e 697 }