STSW-STLKT01: Projects/SensorTile/Applications/DataLog/Src/datalog_application.c Source File

STSW-STLKT01

datalog_application.c
Go to the documentation of this file.
1 
29 /* Includes ------------------------------------------------------------------*/
30 #include "datalog_application.h"
31 #include "main.h"
32 #include "usbd_cdc_interface.h"
33 #include "string.h"
34 #include "SensorTile.h"
35 #include <math.h>
36 
37 /* FatFs includes component */
38 #include "ff_gen_drv.h"
39 #include "sd_diskio.h"
40 
41 FRESULT res; /* FatFs function common result code */
42 uint32_t byteswritten, bytesread; /* File write/read counts */
43 FATFS SDFatFs; /* File system object for SD card logical drive */
44 FIL MyFile; /* File object */
45 char SDPath[4]; /* SD card logical drive path */
46 
47 volatile uint8_t SD_Log_Enabled = 0;
48 
49 char newLine[] = "\r\n";
50 
51 extern void *LSM6DSM_X_0_handle;
52 extern void *LSM6DSM_G_0_handle;
53 extern void *LSM303AGR_M_0_handle;
54 extern void *LSM303AGR_X_0_handle;
55 extern void *LPS22HB_P_0_handle;
56 extern void *LPS22HB_T_0_handle;
57 extern void *HTS221_H_0_handle;
58 extern void *HTS221_T_0_handle;
59 //extern void *GG_handle;
60 
66 void DATALOG_SD_Init(void)
67 {
68  if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0)
69  {
70  /* Register the file system object to the FatFs module */
71  if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) != FR_OK)
72  {
73  /* FatFs Initialization Error */
74  while(1)
75  {
76  BSP_LED_On(LED1);
77  HAL_Delay(500);
78  BSP_LED_Off(LED1);
79  HAL_Delay(100);
80  }
81  }
82  }
83 }
84 
90 uint8_t DATALOG_SD_Log_Enable(void)
91 {
92  static uint16_t sdcard_file_counter = 0;
93  char header[] = "T [ms],AccX [mg],AccY [mg],AccZ [mg],GyroX [mdps],GyroY [mdps],GyroZ [mdps],MagX [mgauss],MagY [mgauss],MagZ [mgauss],P [mB],T [�C],H [%]\r\n";
94  uint32_t byteswritten; /* written byte count */
95  char file_name[30] = {0};
96 
97  /* SD SPI CS Config */
98  SD_IO_CS_Init();
99 
100  sprintf(file_name, "%s%.3d%s", "SensorTile_Log_N", sdcard_file_counter, ".csv");
101  sdcard_file_counter++;
102 
103  HAL_Delay(100);
104 
105  if(f_open(&MyFile, (char const*)file_name, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
106  {
107  sdcard_file_counter--;
108  return 0;
109  }
110 
111  if(f_write(&MyFile, (const void*)&header, sizeof(header)-1, (void *)&byteswritten) != FR_OK)
112  {
113  return 0;
114  }
115  return 1;
116 }
117 
118 uint8_t DATALOG_SD_writeBuf(char *s, uint32_t size)
119 {
120  uint32_t byteswritten;
121 
122  if(f_write(&MyFile, s, size, (void *)&byteswritten) != FR_OK)
123  {
124  return 0;
125  }
126  return 1;
127 }
128 
129 
136 {
137  f_close(&MyFile);
138 
139  /* SD SPI Config */
140  SD_IO_CS_DeInit();
141 }
142 
143 void DATALOG_SD_DeInit(void)
144 {
145  FATFS_UnLinkDriver(SDPath);
146 }
147 
154 {
155  uint32_t byteswritten; /* written byte count */
156  f_write(&MyFile, (const void*)&newLine, 2, (void *)&byteswritten);
157 }
158 
159 
160 DrvStatusTypeDef getSensorsData( T_SensorsData *mptr)
161 {
162  DrvStatusTypeDef ret = COMPONENT_OK;
163 
164  mptr->ms_counter = HAL_GetTick();
165 
166  if ( BSP_ACCELERO_Get_Axes( LSM303AGR_X_0_handle, &mptr->acc ) == COMPONENT_ERROR )
167  {
168  mptr->acc.AXIS_X = 0;
169  mptr->acc.AXIS_Y = 0;
170  mptr->acc.AXIS_Z = 0;
171  ret = COMPONENT_ERROR;
172  }
173 
174  if ( BSP_GYRO_Get_Axes( LSM6DSM_G_0_handle, &mptr->gyro ) == COMPONENT_ERROR )
175  {
176  mptr->gyro.AXIS_X = 0;
177  mptr->gyro.AXIS_Y = 0;
178  mptr->gyro.AXIS_Z = 0;
179  ret = COMPONENT_ERROR;
180  }
181 
182  if ( BSP_MAGNETO_Get_Axes( LSM303AGR_M_0_handle, &mptr->mag ) == COMPONENT_ERROR )
183  {
184  mptr->mag.AXIS_X = 0;
185  mptr->mag.AXIS_Y = 0;
186  mptr->mag.AXIS_Z = 0;
187  ret = COMPONENT_ERROR;
188  }
189 
190  if ( BSP_PRESSURE_Get_Press( LPS22HB_P_0_handle, &mptr->pressure ) == COMPONENT_ERROR )
191  {
192  mptr->pressure = 0.0f;
193  ret = COMPONENT_ERROR;
194  }
195 
196  if ( BSP_HUMIDITY_Get_Hum( HTS221_H_0_handle, &mptr->humidity ) == COMPONENT_ERROR )
197  {
198  mptr->humidity = 0.0f;
199  ret = COMPONENT_ERROR;
200  }
201 
202  if ( BSP_TEMPERATURE_Get_Temp( HTS221_T_0_handle, &mptr->temperature ) == COMPONENT_ERROR )
203  {
204  mptr->temperature = 0.0f;
205  ret = COMPONENT_ERROR;
206  }
207 
208  return ret;
209 }
210 
219 void floatToInt( float in, int32_t *out_int, int32_t *out_dec, int32_t dec_prec )
220 {
221  *out_int = (int32_t)in;
222  if(in >= 0.0f)
223  {
224  in = in - (float)(*out_int);
225  }
226  else
227  {
228  in = (float)(*out_int) - in;
229  }
230  *out_dec = (int32_t)trunc(in * pow(10, dec_prec));
231 }
232 
249 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
uint8_t DATALOG_SD_Log_Enable(void)
Start SD-Card demo.
void BSP_LED_Off(Led_TypeDef Led)
Turns selected LED Off.
Definition: SensorTile.c:241
DrvStatusTypeDef BSP_HUMIDITY_Get_Hum(void *handle, float *humidity)
Get the humidity value.
DrvStatusTypeDef BSP_GYRO_Get_Axes(void *handle, SensorAxes_t *angular_velocity)
Get the gyroscope sensor axes.
DrvStatusTypeDef BSP_PRESSURE_Get_Press(void *handle, float *pressure)
Get the pressure value.
This file contains definitions for SensorTile.c file.
void DATALOG_SD_NewLine(void)
Write New Line to file.
Header for datalog_application.c module.
DrvStatusTypeDef BSP_MAGNETO_Get_Axes(void *handle, SensorAxes_t *magnetic_field)
Get the magnetometer sensor axes.
void DATALOG_SD_Log_Disable(void)
Disable SDCard Log.
DrvStatusTypeDef BSP_ACCELERO_Get_Axes(void *handle, SensorAxes_t *acceleration)
Get the accelerometer sensor axes.
void BSP_LED_On(Led_TypeDef Led)
Turns selected LED On.
Definition: SensorTile.c:219
DrvStatusTypeDef BSP_TEMPERATURE_Get_Temp(void *handle, float *temperature)
Get the temperature value.
void DATALOG_SD_Init(void)
Start SD-Card demo.
void floatToInt(float in, int32_t *out_int, int32_t *out_dec, int32_t dec_prec)
Splits a float into two integer values.
Header for usbd_cdc_interface.c file.
Generated by   doxygen 1.8.13