Francesco Beraldini / Mbed 2 deprecated SM32_ISK01_SD

Dependencies:   mbed X_NUCLEO_IKS01A2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    main.cpp
00004  * @author  CLab
00005  * @version V1.0.0
00006  * @date    2-December-2016
00007  * @brief   Simple Example application for using the X_NUCLEO_IKS01A1 
00008  *          MEMS Inertial & Environmental Sensor Nucleo expansion board.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037 */ 
00038 
00039 /* Includes */
00040 #include "mbed.h"
00041 #include "XNucleoIKS01A2.h"
00042 #include "SDFileSystem.h"
00043 #include "FATFileSystem.h"
00044 #include "iostream"
00045 
00046 Serial pc(USBTX, USBRX);
00047 // SDFileSystem sd(PA_7, PA_6, PA_5, PB_6, "sd"); // MOSI, MISO, SCK, CS
00048 SDFileSystem sd(D11, D12, D13, D10, "sd");// the pinout on the mbed Cool Components workshop board
00049 FILE *fp;
00050 
00051 /* Instantiate the expansion board */
00052 static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
00053 
00054 /* Retrieve the composing elements of the expansion board */
00055 static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer;
00056 static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
00057 static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor;
00058 static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
00059 static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer;
00060 
00061 /* Helper function for printing floats & doubles */
00062 static char *print_double(char* str, double v, int decimalDigits=2)
00063 {
00064   int i = 1;
00065   int intPart, fractPart;
00066   int len;
00067   char *ptr;
00068 
00069   /* prepare decimal digits multiplicator */
00070   for (;decimalDigits!=0; i*=10, decimalDigits--);
00071 
00072   /* calculate integer & fractinal parts */
00073   intPart = (int)v;
00074   fractPart = (int)((v-(double)(int)v)*i);
00075 
00076   /* fill in integer part */
00077   sprintf(str, "%i.", intPart);
00078 
00079   /* prepare fill in of fractional part */
00080   len = strlen(str);
00081   ptr = &str[len];
00082 
00083   /* fill in leading fractional zeros */
00084   for (i/=10;i>1; i/=10, ptr++) {
00085     if (fractPart >= i) {
00086       break;
00087     }
00088     *ptr = '0';
00089   }
00090 
00091   /* fill in (rest of) fractional part */
00092   sprintf(ptr, "%i", fractPart);
00093 
00094   return str;
00095 }
00096 
00097 /* Simple main function */
00098 int main() {
00099   uint8_t id;
00100   float value1, value2;
00101   char buffer1[32], buffer2[32];
00102   int32_t axes[3];
00103   
00104   /* Enable all sensors */
00105   hum_temp->enable();
00106   press_temp->enable();
00107   magnetometer->enable();
00108   accelerometer->enable();
00109   acc_gyro->enable_x();
00110   acc_gyro->enable_g();
00111   
00112   printf("\r\n--- Starting new run ---\r\n");
00113 
00114   hum_temp->read_id(&id);
00115   printf("HTS221  humidity & temperature    = 0x%X\r\n", id);
00116   press_temp->read_id(&id);
00117   printf("LPS22HB  pressure & temperature   = 0x%X\r\n", id);
00118   magnetometer->read_id(&id);
00119   printf("LSM303AGR magnetometer            = 0x%X\r\n", id);
00120   accelerometer->read_id(&id);
00121   printf("LSM303AGR accelerometer           = 0x%X\r\n", id);
00122   acc_gyro->read_id(&id);
00123   printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id);
00124   
00125   pc.printf("Check SD\r\n");
00126   mkdir("/sd/ISK01A2", 0777);
00127   fp = fopen("/sd/ISK01A2/test.txt", "r");
00128   if (fp != NULL) {
00129         fclose(fp);
00130         remove("/sd/test.txt");
00131         pc.printf("Remove an existing file with the same name\r\n");
00132     }
00133     
00134   fp = fopen("/sd/ISK01A2/test.txt", "w");
00135   if (fp == NULL) {
00136         error("Unable to write the file\r\n");
00137     } /*else {
00138         fprintf(fp, "mbed SDCard application!");
00139         fclose(fp);
00140         pc.printf("File successfully written!\r\n");
00141     }*/
00142  int n=0;
00143  printf("SD ok, we can start");
00144  fprintf(fp, " DATA of STM32F401RE with ISK01A2\r\n");
00145  printf("\r\n");
00146  
00147   while(n<60) {
00148     
00149     
00150     printf("\r\n Number of cycle: %d \r\n", n);
00151     printf("\r\n");
00152     fprintf(fp, "\r\n Number of cycle: %d \r\n", n);
00153     fprintf(fp, "\r\n");
00154     
00155     hum_temp->get_temperature(&value1);
00156     hum_temp->get_humidity(&value2);
00157     printf("Temperature & Humidity: [temp] %7s C,   [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
00158     fprintf(fp, "Temperature & Humidity: [temp] %7s C,   [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
00159     
00160     press_temp->get_temperature(&value1);
00161     press_temp->get_pressure(&value2);
00162     printf("Temperature & Pressure: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
00163     fprintf(fp, "Temperature & Pressure: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
00164 
00165     printf("---\r\n");
00166     fprintf(fp, "---\r\n");
00167 
00168     magnetometer->get_m_axes(axes);
00169     printf("Magnetometer [mag/mgauss]:  %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00170     fprintf(fp,"Magnetometer [mag/mgauss]:  %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00171     
00172     accelerometer->get_x_axes(axes);
00173     printf("Accelerometer [acc/mg]:  %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00174     fprintf(fp, "Accelerometer [acc/mg]:  %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00175 
00176     acc_gyro->get_x_axes(axes);
00177     printf("Accelerometer & Gyroscope x [acc/mg]:      %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00178     fprintf(fp, "Accelerometer & Gyroscope x [acc/mg]:      %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00179 
00180     acc_gyro->get_g_axes(axes);
00181     printf("Accelerometer & Gyroscope g [gyro/mdps]:   %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00182     fprintf(fp, "Accelerometer & Gyroscope g [gyro/mdps]:   %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00183     
00184     n+=1;
00185     wait(1);
00186   }
00187   fclose(fp);
00188   pc.printf("File successfully written!\r\n");
00189 }