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

STSW-STLKT01

main.c
1 
48 /* Includes ------------------------------------------------------------------*/
49 #include "main.h"
50 #include "cmsis_os.h"
51 #include "datalog_application.h"
52 
53 /* Private typedef -----------------------------------------------------------*/
54 /* Private define ------------------------------------------------------------*/
55 
56 #define DATAQUEUE_SIZE ((uint32_t)100)
57 
58 typedef enum
59 {
60  THREAD_1 = 0,
61  THREAD_2
62 } Thread_TypeDef;
63 
64 /* Private variables ---------------------------------------------------------*/
65 
66 osThreadId GetDataThreadId, WriteDataThreadId;
67 
68 osMessageQId dataQueue_id;
69 osMessageQDef(dataqueue, DATAQUEUE_SIZE, int);
70 
71 osPoolId sensorPool_id;
72 osPoolDef(sensorPool, DATAQUEUE_SIZE, T_SensorsData);
73 
74 osSemaphoreId readDataSem_id;
75 osSemaphoreDef(readDataSem);
76 
77 osSemaphoreId doubleTapSem_id;
78 osSemaphoreDef(doubleTapSem);
79 
80 /* LoggingInterface = USB_Datalog --> Save sensors data on SDCard (enable with double click) */
81 /* LoggingInterface = SDCARD_Datalog --> Send sensors data via USB */
82 LogInterface_TypeDef LoggingInterface = USB_Datalog;
83 
84 USBD_HandleTypeDef USBD_Device;
85 static volatile uint8_t MEMSInterrupt = 0;
86 static volatile uint8_t no_H_HTS221 = 0;
87 static volatile uint8_t no_T_HTS221 = 0;
88 
89  void *LSM6DSM_X_0_handle = NULL;
90  void *LSM6DSM_G_0_handle = NULL;
91  void *LSM303AGR_X_0_handle = NULL;
92  void *LSM303AGR_M_0_handle = NULL;
93  void *LPS22HB_P_0_handle = NULL;
94  void *LPS22HB_T_0_handle = NULL;
95  void *HTS221_H_0_handle = NULL;
96  void *HTS221_T_0_handle = NULL;
97 
98 /* Private function prototypes -----------------------------------------------*/
99 static void GetData_Thread(void const *argument);
100 static void WriteData_Thread(void const *argument);
101 
102 static void Error_Handler( void );
103 static void initializeAllSensors( void );
104 void enableAllSensors( void );
105 void disableAllSensors( void );
106 void setOdrAllSensors( void );
107 
108 void dataTimer_Callback(void const *arg);
109 void dataTimerStart(void);
110 void dataTimerStop(void);
111 
112 osTimerId sensorTimId;
113 osTimerDef(SensorTimer, dataTimer_Callback);
114 
115 uint32_t exec;
116 /* Private functions ---------------------------------------------------------*/
117 
123 int main(void)
124 {
125  HAL_Init();
126 
127  /* Configure the System clock to 80 MHz */
129 
130  if(LoggingInterface == USB_Datalog)
131  {
132  /* Initialize LED */
133  BSP_LED_Init(LED1);
134  BSP_LED_Off(LED1);
135  }
136 
137  /* enable USB power on Pwrctrl CR2 register */
138  HAL_PWREx_EnableVddUSB();
139 
140  if(LoggingInterface == USB_Datalog) /* Configure the USB */
141  {
142  /*** USB CDC Configuration ***/
143  /* Init Device Library */
144  USBD_Init(&USBD_Device, &VCP_Desc, 0);
145  /* Add Supported Class */
146  USBD_RegisterClass(&USBD_Device, USBD_CDC_CLASS);
147  /* Add Interface callbacks for AUDIO and CDC Class */
148  USBD_CDC_RegisterInterface(&USBD_Device, &USBD_CDC_fops);
149  /* Start Device Process */
150  USBD_Start(&USBD_Device);
151  }
152  else /* Configure the SDCard */
153  {
154  DATALOG_SD_Init();
155  }
156 
157  /* Thread 1 definition */
158  osThreadDef(THREAD_1, GetData_Thread, osPriorityAboveNormal, 0, configMINIMAL_STACK_SIZE*4);
159 
160  /* Thread 2 definition */
161  osThreadDef(THREAD_2, WriteData_Thread, osPriorityNormal, 0, configMINIMAL_STACK_SIZE*4);
162 
163  /* Start thread 1 */
164  GetDataThreadId = osThreadCreate(osThread(THREAD_1), NULL);
165 
166  /* Start thread 2 */
167  WriteDataThreadId = osThreadCreate(osThread(THREAD_2), NULL);
168 
169  /* Start scheduler */
170  osKernelStart();
171 
172  /* We should never get here as control is now taken by the scheduler */
173  for (;;);
174 
175 }
176 
182 static void GetData_Thread(void const *argument)
183 {
184  (void) argument;
185  uint8_t doubleTap = 0;
186  T_SensorsData *mptr;
187 
188  sensorPool_id = osPoolCreate(osPool(sensorPool));
189  dataQueue_id = osMessageCreate(osMessageQ(dataqueue), NULL);
190 
191  readDataSem_id = osSemaphoreCreate(osSemaphore(readDataSem), 1);
192  osSemaphoreWait(readDataSem_id, osWaitForever);
193 
194  doubleTapSem_id = osSemaphoreCreate(osSemaphore(doubleTapSem), 1);
195  osSemaphoreWait(doubleTapSem_id, osWaitForever);
196 
197  /* Configure and disable all the Chip Select pins */
198  Sensor_IO_SPI_CS_Init_All();
199 
200  /* Initialize and Enable the available sensors */
201  initializeAllSensors();
202  enableAllSensors();
203 
204  if(LoggingInterface == USB_Datalog)
205  {
206  dataTimerStart();
207  }
208 
209  for (;;)
210  {
211  osSemaphoreWait(readDataSem_id, osWaitForever);
212  if(MEMSInterrupt && LoggingInterface == SDCARD_Datalog)
213  {
214  MEMSInterrupt = 0;
215  BSP_ACCELERO_Get_Double_Tap_Detection_Status_Ext(LSM6DSM_X_0_handle, &doubleTap);
216  if(doubleTap)
217  {
218  if(SD_Log_Enabled)
219  {
220  dataTimerStop();
221  osMessagePut(dataQueue_id, 0x00000007, osWaitForever);
222  }
223  else
224  {
225  osMessagePut(dataQueue_id, 0x00000007, osWaitForever);
226  }
227  }
228  }
229  else
230  {
231  /* Try to allocate a memory block and check if is not NULL */
232  mptr = osPoolAlloc(sensorPool_id);
233  if(mptr != NULL)
234  {
235  /* Get Data from Sensors */
236  if(getSensorsData(mptr) == COMPONENT_OK)
237  {
238  /* Push the new memory Block in the Data Queue */
239  if(osMessagePut(dataQueue_id, (uint32_t)mptr, osWaitForever) != osOK)
240  {
241  Error_Handler();
242  }
243  }
244  else
245  {
246  Error_Handler();
247  }
248  }
249  else
250  {
251  Error_Handler();
252  }
253  }
254  }
255 }
256 
257 
263 static void WriteData_Thread(void const *argument)
264 {
265  (void) argument;
266  osEvent evt;
267  T_SensorsData *rptr;
268  int size;
269  char data_s[256];
270 
271  for (;;)
272  {
273  evt = osMessageGet(dataQueue_id, osWaitForever); // wait for message
274  if (evt.status == osEventMessage)
275  {
276  if(evt.value.v == 0x00000007)
277  {
278  if (SD_Log_Enabled)
279  {
281  SD_Log_Enabled=0;
282  }
283  else
284  {
285  while(SD_Log_Enabled != 1)
286  {
288  {
289  SD_Log_Enabled=1;
290  osDelay(100);
291  dataTimerStart();
292  }
293  else
294  {
295  //DATALOG_SD_Log_Disable();
296  DATALOG_SD_DeInit();
297  DATALOG_SD_Init();
298  osDelay(100);
299  }
300  }
301  }
302  }
303  else
304  {
305  rptr = evt.value.p;
306 
307  if(LoggingInterface == USB_Datalog)
308  {
309  size = sprintf(data_s, "TimeStamp: %d\r\n Acc_X: %d, Acc_Y: %d, Acc_Z :%d\r\n Gyro_X:%d, Gyro_Y:%d, Gyro_Z:%d\r\n Magn_X:%d, Magn_Y:%d, Magn_Z:%d\r\n Press:%5.2f, Temp:%5.2f, Hum:%4.1f\r\n",
310  rptr->ms_counter,
311  (int)rptr->acc.AXIS_X, (int)rptr->acc.AXIS_Y, (int)rptr->acc.AXIS_Z,
312  (int)rptr->gyro.AXIS_X, (int)rptr->gyro.AXIS_Y, (int)rptr->gyro.AXIS_Z,
313  (int)rptr->mag.AXIS_X, (int)rptr->mag.AXIS_Y, (int)rptr->mag.AXIS_Z,
314  rptr->pressure, rptr->temperature, rptr->humidity);
315  osPoolFree(sensorPool_id, rptr); // free memory allocated for message
316  BSP_LED_Toggle(LED1);
317  CDC_Fill_Buffer(( uint8_t * )data_s, size);
318  }
319  else
320  {
321  size = sprintf(data_s, "%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %5.2f, %5.2f, %4.1f\r\n",
322  rptr->ms_counter,
323  (int)rptr->acc.AXIS_X, (int)rptr->acc.AXIS_Y, (int)rptr->acc.AXIS_Z,
324  (int)rptr->gyro.AXIS_X, (int)rptr->gyro.AXIS_Y, (int)rptr->gyro.AXIS_Z,
325  (int)rptr->mag.AXIS_X, (int)rptr->mag.AXIS_Y, (int)rptr->mag.AXIS_Z,
326  rptr->pressure, rptr->temperature, rptr->humidity);
327  osPoolFree(sensorPool_id, rptr); // free memory allocated for message
328  DATALOG_SD_writeBuf(data_s, size);
329  }
330  }
331  }
332  }
333 }
334 
335 
336 void dataTimer_Callback(void const *arg)
337 {
338  osSemaphoreRelease(readDataSem_id);
339 }
340 
341 
342 void dataTimerStart(void)
343 {
344  osStatus status;
345 
346  // Create periodic timer
347  exec = 1;
348  sensorTimId = osTimerCreate(osTimer(SensorTimer), osTimerPeriodic, &exec);
349  if (sensorTimId) {
350  status = osTimerStart (sensorTimId, DATA_PERIOD_MS); // start timer
351  if (status != osOK) {
352  // Timer could not be started
353  }
354  }
355 }
356 
357 void dataTimerStop(void)
358 {
359  osTimerStop(sensorTimId);
360 }
361 
362 
363 
364 
370 static void initializeAllSensors( void )
371 {
372  if (BSP_ACCELERO_Init( LSM6DSM_X_0, &LSM6DSM_X_0_handle ) != COMPONENT_OK)
373  {
374  while(1);
375  }
376 
377  if (BSP_GYRO_Init( LSM6DSM_G_0, &LSM6DSM_G_0_handle ) != COMPONENT_OK)
378  {
379  while(1);
380  }
381 
382  if (BSP_ACCELERO_Init( LSM303AGR_X_0, &LSM303AGR_X_0_handle ) != COMPONENT_OK)
383  {
384  while(1);
385  }
386 
387  if (BSP_MAGNETO_Init( LSM303AGR_M_0, &LSM303AGR_M_0_handle ) != COMPONENT_OK)
388  {
389  while(1);
390  }
391 
392  if (BSP_PRESSURE_Init( LPS22HB_P_0, &LPS22HB_P_0_handle ) != COMPONENT_OK)
393  {
394  while(1);
395  }
396 
397  if (BSP_TEMPERATURE_Init( LPS22HB_T_0, &LPS22HB_T_0_handle ) != COMPONENT_OK)
398  {
399  while(1);
400  }
401 
402  if(BSP_TEMPERATURE_Init( HTS221_T_0, &HTS221_T_0_handle ) == COMPONENT_ERROR)
403  {
404  no_T_HTS221 = 1;
405  }
406 
407  if(BSP_HUMIDITY_Init( HTS221_H_0, &HTS221_H_0_handle ) == COMPONENT_ERROR)
408  {
409  no_H_HTS221 = 1;
410  }
411 
412  if(LoggingInterface == SDCARD_Datalog)
413  {
414  /* Enable HW Double Tap detection */
416  BSP_ACCELERO_Set_Tap_Threshold_Ext(LSM6DSM_X_0_handle, LSM6DSM_TAP_THRESHOLD_MID);
417  }
418 }
419 
425 void enableAllSensors( void )
426 {
427  BSP_ACCELERO_Sensor_Enable( LSM6DSM_X_0_handle );
428  BSP_GYRO_Sensor_Enable( LSM6DSM_G_0_handle );
429  BSP_ACCELERO_Sensor_Enable( LSM303AGR_X_0_handle );
430  BSP_MAGNETO_Sensor_Enable( LSM303AGR_M_0_handle );
431  BSP_PRESSURE_Sensor_Enable( LPS22HB_P_0_handle );
432  BSP_TEMPERATURE_Sensor_Enable( LPS22HB_T_0_handle );
433  if(!no_T_HTS221)
434  {
435  BSP_TEMPERATURE_Sensor_Enable( HTS221_T_0_handle );
436  BSP_HUMIDITY_Sensor_Enable( HTS221_H_0_handle );
437  }
438 }
444 void setOdrAllSensors( void )
445 {
446  BSP_ACCELERO_Set_ODR_Value( LSM303AGR_X_0_handle, ACCELERO_ODR);
447  BSP_MAGNETO_Set_ODR_Value( LSM303AGR_M_0_handle, MAGNETO_ODR);
448  BSP_GYRO_Set_ODR_Value(LSM6DSM_G_0_handle, GYRO_ODR);
449  BSP_PRESSURE_Set_ODR_Value( LPS22HB_P_0_handle, PRESSURE_ODR);
450  BSP_TEMPERATURE_Set_ODR_Value( HTS221_T_0_handle, TEMPERATURE_ODR);
451  BSP_HUMIDITY_Set_ODR_Value( HTS221_H_0_handle, TEMPERATURE_ODR );
452 }
453 
454 
460 void disableAllSensors( void )
461 {
462  BSP_ACCELERO_Sensor_Disable( LSM6DSM_X_0_handle );
463  BSP_ACCELERO_Sensor_Disable( LSM303AGR_X_0_handle );
464  BSP_GYRO_Sensor_Disable( LSM6DSM_G_0_handle );
465  BSP_MAGNETO_Sensor_Disable( LSM303AGR_M_0_handle );
466  BSP_HUMIDITY_Sensor_Disable( HTS221_H_0_handle );
467  BSP_TEMPERATURE_Sensor_Disable( HTS221_T_0_handle );
468  BSP_TEMPERATURE_Sensor_Disable( LPS22HB_T_0_handle );
469  BSP_PRESSURE_Sensor_Disable( LPS22HB_P_0_handle );
470 }
471 
477 void HAL_GPIO_EXTI_Callback( uint16_t GPIO_Pin )
478 {
479  MEMSInterrupt=1;
480  osSemaphoreRelease(readDataSem_id);
481 }
482 
488 static void Error_Handler( void )
489 {
490  while (1)
491  {}
492 }
493 
494 
495 #ifdef USE_FULL_ASSERT
496 
504 void assert_failed(uint8_t *file, uint32_t line)
505 {
506  /* User can add his own implementation to report the file name and line number,
507  ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
508 
509  /* Infinite loop */
510  while (1)
511  {}
512 }
513 #endif
514 
515 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
DrvStatusTypeDef BSP_HUMIDITY_Set_ODR_Value(void *handle, float odr)
Set the humidity sensor output data rate.
DrvStatusTypeDef BSP_PRESSURE_Init(PRESSURE_ID_t id, void **handle)
Initialize a pressure sensor.
DrvStatusTypeDef BSP_PRESSURE_Sensor_Enable(void *handle)
Enable pressure sensor.
DrvStatusTypeDef BSP_HUMIDITY_Init(HUMIDITY_ID_t id, void **handle)
Initialize a humidity sensor.
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
void BSP_LED_Toggle(Led_TypeDef Led)
Toggles the selected LED.
Definition: SensorTile.c:263
DrvStatusTypeDef BSP_GYRO_Sensor_Disable(void *handle)
Disable gyroscope sensor.
DrvStatusTypeDef BSP_HUMIDITY_Sensor_Disable(void *handle)
Disable humidity sensor.
DrvStatusTypeDef BSP_GYRO_Init(GYRO_ID_t id, void **handle)
Initialize a gyroscope sensor.
DrvStatusTypeDef BSP_MAGNETO_Set_ODR_Value(void *handle, float odr)
Set the magnetometer sensor output data rate.
DrvStatusTypeDef BSP_ACCELERO_Set_Tap_Threshold_Ext(void *handle, uint8_t thr)
Set the tap threshold (available only for LSM6DS3 sensor)
DrvStatusTypeDef BSP_ACCELERO_Get_Double_Tap_Detection_Status_Ext(void *handle, uint8_t *status)
Get the double tap detection status (available only for LSM6DS3 sensor)
DrvStatusTypeDef BSP_PRESSURE_Set_ODR_Value(void *handle, float odr)
Set the pressure sensor output data rate.
DrvStatusTypeDef BSP_ACCELERO_Enable_Double_Tap_Detection_Ext(void *handle)
Enable the double tap detection (available only for LSM6DS3 sensor)
DrvStatusTypeDef BSP_ACCELERO_Sensor_Disable(void *handle)
Disable accelerometer sensor.
void Error_Handler(void)
This function is executed in case of error occurrence.
Definition: main.c:177
DrvStatusTypeDef BSP_ACCELERO_Set_ODR_Value(void *handle, float odr)
Set the accelerometer sensor output data rate.
DrvStatusTypeDef BSP_MAGNETO_Sensor_Enable(void *handle)
Enable magnetometer sensor.
void BSP_LED_Init(Led_TypeDef Led)
Configures LEDs.
Definition: SensorTile.c:172
DrvStatusTypeDef BSP_PRESSURE_Sensor_Disable(void *handle)
Disable pressure sensor.
Header for datalog_application.c module.
DrvStatusTypeDef BSP_GYRO_Sensor_Enable(void *handle)
Enable gyroscope sensor.
DrvStatusTypeDef BSP_MAGNETO_Init(MAGNETO_ID_t id, void **handle)
Initialize a magnetometer sensor.
void DATALOG_SD_Log_Disable(void)
Disable SDCard Log.
uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev, USBD_CDC_ItfTypeDef *fops)
USBD_CDC_RegisterInterface.
Definition: usbd_cdc.c:790
DrvStatusTypeDef BSP_ACCELERO_Sensor_Enable(void *handle)
Enable accelerometer sensor.
DrvStatusTypeDef BSP_ACCELERO_Init(ACCELERO_ID_t id, void **handle)
Initialize an accelerometer sensor.
DrvStatusTypeDef BSP_TEMPERATURE_Set_ODR_Value(void *handle, float odr)
Set the temperature sensor output data rate.
void DATALOG_SD_Init(void)
Start SD-Card demo.
uint8_t CDC_Fill_Buffer(uint8_t *Buf, uint32_t TotalLen)
Fill the usb tx buffer.
int main(void)
Main program.
Definition: main.c:71
DrvStatusTypeDef BSP_TEMPERATURE_Sensor_Disable(void *handle)
Disable temperature sensor.
void SystemClock_Config(void)
System Clock Configuration.
Definition: cube_hal_l4.c:48
DrvStatusTypeDef BSP_TEMPERATURE_Init(TEMPERATURE_ID_t id, void **handle)
Initialize a temperature sensor.
DrvStatusTypeDef BSP_HUMIDITY_Sensor_Enable(void *handle)
Enable humidity sensor.
DrvStatusTypeDef BSP_TEMPERATURE_Sensor_Enable(void *handle)
Enable temperature sensor.
DrvStatusTypeDef BSP_GYRO_Set_ODR_Value(void *handle, float odr)
Set the gyroscope sensor output data rate.
DrvStatusTypeDef BSP_MAGNETO_Sensor_Disable(void *handle)
Disable magnetometer sensor.
Generated by   doxygen 1.8.13