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