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