STM32F401RE with ISK01A2 and MicroSD arduino shield
Dependencies: mbed X_NUCLEO_IKS01A2
main.cpp@18:91a38b13d21d, 2021-10-14 (annotated)
- Committer:
- berajay
- Date:
- Thu Oct 14 12:50:50 2021 +0000
- Revision:
- 18:91a38b13d21d
- Parent:
- 13:fc873da5b445
STM32F401RE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cparata | 0:69566eea0fba | 1 | /** |
cparata | 0:69566eea0fba | 2 | ****************************************************************************** |
cparata | 0:69566eea0fba | 3 | * @file main.cpp |
cparata | 8:8f495e604424 | 4 | * @author CLab |
cparata | 8:8f495e604424 | 5 | * @version V1.0.0 |
cparata | 8:8f495e604424 | 6 | * @date 2-December-2016 |
cparata | 0:69566eea0fba | 7 | * @brief Simple Example application for using the X_NUCLEO_IKS01A1 |
cparata | 0:69566eea0fba | 8 | * MEMS Inertial & Environmental Sensor Nucleo expansion board. |
cparata | 0:69566eea0fba | 9 | ****************************************************************************** |
cparata | 0:69566eea0fba | 10 | * @attention |
cparata | 0:69566eea0fba | 11 | * |
cparata | 0:69566eea0fba | 12 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
cparata | 0:69566eea0fba | 13 | * |
cparata | 0:69566eea0fba | 14 | * Redistribution and use in source and binary forms, with or without modification, |
cparata | 0:69566eea0fba | 15 | * are permitted provided that the following conditions are met: |
cparata | 0:69566eea0fba | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
cparata | 0:69566eea0fba | 17 | * this list of conditions and the following disclaimer. |
cparata | 0:69566eea0fba | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
cparata | 0:69566eea0fba | 19 | * this list of conditions and the following disclaimer in the documentation |
cparata | 0:69566eea0fba | 20 | * and/or other materials provided with the distribution. |
cparata | 0:69566eea0fba | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
cparata | 0:69566eea0fba | 22 | * may be used to endorse or promote products derived from this software |
cparata | 0:69566eea0fba | 23 | * without specific prior written permission. |
cparata | 0:69566eea0fba | 24 | * |
cparata | 0:69566eea0fba | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
cparata | 0:69566eea0fba | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
cparata | 0:69566eea0fba | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
cparata | 0:69566eea0fba | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
cparata | 0:69566eea0fba | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
cparata | 0:69566eea0fba | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
cparata | 0:69566eea0fba | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
cparata | 0:69566eea0fba | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
cparata | 0:69566eea0fba | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
cparata | 0:69566eea0fba | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
cparata | 0:69566eea0fba | 35 | * |
cparata | 0:69566eea0fba | 36 | ****************************************************************************** |
cparata | 0:69566eea0fba | 37 | */ |
cparata | 0:69566eea0fba | 38 | |
cparata | 0:69566eea0fba | 39 | /* Includes */ |
cparata | 0:69566eea0fba | 40 | #include "mbed.h" |
davide.aliprandi@st.com | 13:fc873da5b445 | 41 | #include "XNucleoIKS01A2.h" |
berajay | 18:91a38b13d21d | 42 | #include "SDFileSystem.h" |
berajay | 18:91a38b13d21d | 43 | #include "FATFileSystem.h" |
berajay | 18:91a38b13d21d | 44 | #include "iostream" |
berajay | 18:91a38b13d21d | 45 | |
berajay | 18:91a38b13d21d | 46 | Serial pc(USBTX, USBRX); |
berajay | 18:91a38b13d21d | 47 | // SDFileSystem sd(PA_7, PA_6, PA_5, PB_6, "sd"); // MOSI, MISO, SCK, CS |
berajay | 18:91a38b13d21d | 48 | SDFileSystem sd(D11, D12, D13, D10, "sd");// the pinout on the mbed Cool Components workshop board |
berajay | 18:91a38b13d21d | 49 | FILE *fp; |
cparata | 0:69566eea0fba | 50 | |
cparata | 0:69566eea0fba | 51 | /* Instantiate the expansion board */ |
davide.aliprandi@st.com | 13:fc873da5b445 | 52 | static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); |
cparata | 0:69566eea0fba | 53 | |
cparata | 0:69566eea0fba | 54 | /* Retrieve the composing elements of the expansion board */ |
davide.aliprandi@st.com | 13:fc873da5b445 | 55 | static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer; |
cparata | 0:69566eea0fba | 56 | static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; |
cparata | 0:69566eea0fba | 57 | static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor; |
cparata | 0:69566eea0fba | 58 | static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro; |
davide.aliprandi@st.com | 13:fc873da5b445 | 59 | static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer; |
cparata | 0:69566eea0fba | 60 | |
cparata | 0:69566eea0fba | 61 | /* Helper function for printing floats & doubles */ |
davide.aliprandi@st.com | 13:fc873da5b445 | 62 | static char *print_double(char* str, double v, int decimalDigits=2) |
cparata | 0:69566eea0fba | 63 | { |
cparata | 0:69566eea0fba | 64 | int i = 1; |
cparata | 0:69566eea0fba | 65 | int intPart, fractPart; |
cparata | 0:69566eea0fba | 66 | int len; |
cparata | 0:69566eea0fba | 67 | char *ptr; |
cparata | 0:69566eea0fba | 68 | |
cparata | 0:69566eea0fba | 69 | /* prepare decimal digits multiplicator */ |
cparata | 0:69566eea0fba | 70 | for (;decimalDigits!=0; i*=10, decimalDigits--); |
cparata | 0:69566eea0fba | 71 | |
cparata | 0:69566eea0fba | 72 | /* calculate integer & fractinal parts */ |
cparata | 0:69566eea0fba | 73 | intPart = (int)v; |
cparata | 0:69566eea0fba | 74 | fractPart = (int)((v-(double)(int)v)*i); |
cparata | 0:69566eea0fba | 75 | |
cparata | 0:69566eea0fba | 76 | /* fill in integer part */ |
cparata | 0:69566eea0fba | 77 | sprintf(str, "%i.", intPart); |
cparata | 0:69566eea0fba | 78 | |
cparata | 0:69566eea0fba | 79 | /* prepare fill in of fractional part */ |
cparata | 0:69566eea0fba | 80 | len = strlen(str); |
cparata | 0:69566eea0fba | 81 | ptr = &str[len]; |
cparata | 0:69566eea0fba | 82 | |
cparata | 0:69566eea0fba | 83 | /* fill in leading fractional zeros */ |
cparata | 0:69566eea0fba | 84 | for (i/=10;i>1; i/=10, ptr++) { |
davide.aliprandi@st.com | 13:fc873da5b445 | 85 | if (fractPart >= i) { |
davide.aliprandi@st.com | 13:fc873da5b445 | 86 | break; |
davide.aliprandi@st.com | 13:fc873da5b445 | 87 | } |
cparata | 0:69566eea0fba | 88 | *ptr = '0'; |
cparata | 0:69566eea0fba | 89 | } |
cparata | 0:69566eea0fba | 90 | |
cparata | 0:69566eea0fba | 91 | /* fill in (rest of) fractional part */ |
cparata | 0:69566eea0fba | 92 | sprintf(ptr, "%i", fractPart); |
cparata | 0:69566eea0fba | 93 | |
cparata | 0:69566eea0fba | 94 | return str; |
cparata | 0:69566eea0fba | 95 | } |
cparata | 0:69566eea0fba | 96 | |
cparata | 0:69566eea0fba | 97 | /* Simple main function */ |
cparata | 0:69566eea0fba | 98 | int main() { |
cparata | 0:69566eea0fba | 99 | uint8_t id; |
cparata | 0:69566eea0fba | 100 | float value1, value2; |
cparata | 0:69566eea0fba | 101 | char buffer1[32], buffer2[32]; |
cparata | 0:69566eea0fba | 102 | int32_t axes[3]; |
cparata | 0:69566eea0fba | 103 | |
cparata | 0:69566eea0fba | 104 | /* Enable all sensors */ |
davide.aliprandi@st.com | 13:fc873da5b445 | 105 | hum_temp->enable(); |
davide.aliprandi@st.com | 13:fc873da5b445 | 106 | press_temp->enable(); |
davide.aliprandi@st.com | 13:fc873da5b445 | 107 | magnetometer->enable(); |
davide.aliprandi@st.com | 13:fc873da5b445 | 108 | accelerometer->enable(); |
davide.aliprandi@st.com | 13:fc873da5b445 | 109 | acc_gyro->enable_x(); |
davide.aliprandi@st.com | 13:fc873da5b445 | 110 | acc_gyro->enable_g(); |
cparata | 0:69566eea0fba | 111 | |
cparata | 0:69566eea0fba | 112 | printf("\r\n--- Starting new run ---\r\n"); |
cparata | 0:69566eea0fba | 113 | |
davide.aliprandi@st.com | 13:fc873da5b445 | 114 | hum_temp->read_id(&id); |
cparata | 0:69566eea0fba | 115 | printf("HTS221 humidity & temperature = 0x%X\r\n", id); |
davide.aliprandi@st.com | 13:fc873da5b445 | 116 | press_temp->read_id(&id); |
cparata | 0:69566eea0fba | 117 | printf("LPS22HB pressure & temperature = 0x%X\r\n", id); |
davide.aliprandi@st.com | 13:fc873da5b445 | 118 | magnetometer->read_id(&id); |
cparata | 0:69566eea0fba | 119 | printf("LSM303AGR magnetometer = 0x%X\r\n", id); |
davide.aliprandi@st.com | 13:fc873da5b445 | 120 | accelerometer->read_id(&id); |
cparata | 0:69566eea0fba | 121 | printf("LSM303AGR accelerometer = 0x%X\r\n", id); |
davide.aliprandi@st.com | 13:fc873da5b445 | 122 | acc_gyro->read_id(&id); |
cparata | 0:69566eea0fba | 123 | printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id); |
berajay | 18:91a38b13d21d | 124 | |
berajay | 18:91a38b13d21d | 125 | pc.printf("Check SD\r\n"); |
berajay | 18:91a38b13d21d | 126 | mkdir("/sd/ISK01A2", 0777); |
berajay | 18:91a38b13d21d | 127 | fp = fopen("/sd/ISK01A2/test.txt", "r"); |
berajay | 18:91a38b13d21d | 128 | if (fp != NULL) { |
berajay | 18:91a38b13d21d | 129 | fclose(fp); |
berajay | 18:91a38b13d21d | 130 | remove("/sd/test.txt"); |
berajay | 18:91a38b13d21d | 131 | pc.printf("Remove an existing file with the same name\r\n"); |
berajay | 18:91a38b13d21d | 132 | } |
berajay | 18:91a38b13d21d | 133 | |
berajay | 18:91a38b13d21d | 134 | fp = fopen("/sd/ISK01A2/test.txt", "w"); |
berajay | 18:91a38b13d21d | 135 | if (fp == NULL) { |
berajay | 18:91a38b13d21d | 136 | error("Unable to write the file\r\n"); |
berajay | 18:91a38b13d21d | 137 | } /*else { |
berajay | 18:91a38b13d21d | 138 | fprintf(fp, "mbed SDCard application!"); |
berajay | 18:91a38b13d21d | 139 | fclose(fp); |
berajay | 18:91a38b13d21d | 140 | pc.printf("File successfully written!\r\n"); |
berajay | 18:91a38b13d21d | 141 | }*/ |
berajay | 18:91a38b13d21d | 142 | int n=0; |
berajay | 18:91a38b13d21d | 143 | printf("SD ok, we can start"); |
berajay | 18:91a38b13d21d | 144 | fprintf(fp, " DATA of STM32F401RE with ISK01A2\r\n"); |
berajay | 18:91a38b13d21d | 145 | printf("\r\n"); |
cparata | 0:69566eea0fba | 146 | |
berajay | 18:91a38b13d21d | 147 | while(n<60) { |
berajay | 18:91a38b13d21d | 148 | |
berajay | 18:91a38b13d21d | 149 | |
berajay | 18:91a38b13d21d | 150 | printf("\r\n Number of cycle: %d \r\n", n); |
cparata | 0:69566eea0fba | 151 | printf("\r\n"); |
berajay | 18:91a38b13d21d | 152 | fprintf(fp, "\r\n Number of cycle: %d \r\n", n); |
berajay | 18:91a38b13d21d | 153 | fprintf(fp, "\r\n"); |
berajay | 18:91a38b13d21d | 154 | |
davide.aliprandi@st.com | 13:fc873da5b445 | 155 | hum_temp->get_temperature(&value1); |
davide.aliprandi@st.com | 13:fc873da5b445 | 156 | hum_temp->get_humidity(&value2); |
berajay | 18:91a38b13d21d | 157 | printf("Temperature & Humidity: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); |
berajay | 18:91a38b13d21d | 158 | fprintf(fp, "Temperature & Humidity: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); |
cparata | 0:69566eea0fba | 159 | |
davide.aliprandi@st.com | 13:fc873da5b445 | 160 | press_temp->get_temperature(&value1); |
davide.aliprandi@st.com | 13:fc873da5b445 | 161 | press_temp->get_pressure(&value2); |
berajay | 18:91a38b13d21d | 162 | printf("Temperature & Pressure: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); |
berajay | 18:91a38b13d21d | 163 | fprintf(fp, "Temperature & Pressure: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); |
cparata | 0:69566eea0fba | 164 | |
cparata | 0:69566eea0fba | 165 | printf("---\r\n"); |
berajay | 18:91a38b13d21d | 166 | fprintf(fp, "---\r\n"); |
cparata | 0:69566eea0fba | 167 | |
davide.aliprandi@st.com | 13:fc873da5b445 | 168 | magnetometer->get_m_axes(axes); |
berajay | 18:91a38b13d21d | 169 | printf("Magnetometer [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
berajay | 18:91a38b13d21d | 170 | fprintf(fp,"Magnetometer [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
cparata | 0:69566eea0fba | 171 | |
davide.aliprandi@st.com | 13:fc873da5b445 | 172 | accelerometer->get_x_axes(axes); |
berajay | 18:91a38b13d21d | 173 | printf("Accelerometer [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
berajay | 18:91a38b13d21d | 174 | fprintf(fp, "Accelerometer [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
cparata | 0:69566eea0fba | 175 | |
davide.aliprandi@st.com | 13:fc873da5b445 | 176 | acc_gyro->get_x_axes(axes); |
berajay | 18:91a38b13d21d | 177 | printf("Accelerometer & Gyroscope x [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
berajay | 18:91a38b13d21d | 178 | fprintf(fp, "Accelerometer & Gyroscope x [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
cparata | 0:69566eea0fba | 179 | |
davide.aliprandi@st.com | 13:fc873da5b445 | 180 | acc_gyro->get_g_axes(axes); |
berajay | 18:91a38b13d21d | 181 | printf("Accelerometer & Gyroscope g [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
berajay | 18:91a38b13d21d | 182 | fprintf(fp, "Accelerometer & Gyroscope g [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
berajay | 18:91a38b13d21d | 183 | |
berajay | 18:91a38b13d21d | 184 | n+=1; |
berajay | 18:91a38b13d21d | 185 | wait(1); |
cparata | 0:69566eea0fba | 186 | } |
berajay | 18:91a38b13d21d | 187 | fclose(fp); |
berajay | 18:91a38b13d21d | 188 | pc.printf("File successfully written!\r\n"); |
cparata | 0:69566eea0fba | 189 | } |