STSW-STLKT01: Drivers/BSP/SensorTile/SensorTile_pressure.c Source File

STSW-STLKT01

STSW-STLKT01
SensorTile_pressure.c
Go to the documentation of this file.
1 
39 /* Includes ------------------------------------------------------------------*/
40 #include "SensorTile_pressure.h"
41 
42 
43 
61 static DrvContextTypeDef PRESSURE_SensorHandle[ PRESSURE_SENSORS_MAX_NUM ];
62 static PRESSURE_Data_t PRESSURE_Data[ PRESSURE_SENSORS_MAX_NUM ]; // Pressure - all.
63 static LPS22HB_P_Data_t LPS22HB_P_0_Data; // Pressure - sensor 0.
64 
73 static DrvStatusTypeDef BSP_LPS22HB_PRESSURE_Init( void **handle );
74 
83 /* Sensor IO functions */
84 extern DrvStatusTypeDef Sensor_IO_Init( void );
85 
101 DrvStatusTypeDef BSP_PRESSURE_Init( PRESSURE_ID_t id, void **handle )
102 {
103  *handle = NULL;
104 
105  switch(id)
106  {
107  case PRESSURE_SENSORS_AUTO:
108  default:
109  {
110  if( BSP_LPS22HB_PRESSURE_Init(handle) == COMPONENT_ERROR )
111  {
112  return COMPONENT_ERROR;
113  }
114  break;
115  }
116  case LPS22HB_P_0:
117  {
118  if( BSP_LPS22HB_PRESSURE_Init(handle) == COMPONENT_ERROR )
119  {
120  return COMPONENT_ERROR;
121  }
122  break;
123  }
124  }
125 
126  return COMPONENT_OK;
127 }
128 
129 static DrvStatusTypeDef BSP_LPS22HB_PRESSURE_Init( void **handle )
130 {
131  PRESSURE_Drv_t *driver = NULL;
132  uint8_t data = 0x01;
133 
134  if(PRESSURE_SensorHandle[ LPS22HB_P_0 ].isInitialized == 1)
135  {
136  /* We have reached the max num of instance for this component */
137  return COMPONENT_ERROR;
138  }
139 
140  if ( Sensor_IO_SPI_Init() == COMPONENT_ERROR )
141  {
142  return COMPONENT_ERROR;
143  }
144 
145  /* Setup sensor handle. */
146  PRESSURE_SensorHandle[ LPS22HB_P_0 ].who_am_i = LPS22HB_WHO_AM_I_VAL;
147  PRESSURE_SensorHandle[ LPS22HB_P_0 ].ifType = 1; // SPI interface
148  PRESSURE_SensorHandle[ LPS22HB_P_0 ].address = LPS22HB_ADDRESS_HIGH;
149  PRESSURE_SensorHandle[ LPS22HB_P_0 ].spiDevice = LPS22HB;
150  PRESSURE_SensorHandle[ LPS22HB_P_0 ].instance = LPS22HB_P_0;
151  PRESSURE_SensorHandle[ LPS22HB_P_0 ].isInitialized = 0;
152  PRESSURE_SensorHandle[ LPS22HB_P_0 ].isEnabled = 0;
153  PRESSURE_SensorHandle[ LPS22HB_P_0 ].isCombo = 1;
154  PRESSURE_SensorHandle[ LPS22HB_P_0 ].pData = ( void * )&PRESSURE_Data[ LPS22HB_P_0 ];
155  PRESSURE_SensorHandle[ LPS22HB_P_0 ].pVTable = ( void * )&LPS22HB_P_Drv;
156  PRESSURE_SensorHandle[ LPS22HB_P_0 ].pExtVTable = 0;
157 
158  LPS22HB_P_0_Data.comboData = &LPS22HB_Combo_Data[0];
159  PRESSURE_Data[ LPS22HB_P_0 ].pComponentData = ( void * )&LPS22HB_P_0_Data;
160  PRESSURE_Data[ LPS22HB_P_0 ].pExtData = 0;
161 
162  *handle = (void *)&PRESSURE_SensorHandle[ LPS22HB_P_0 ];
163 
164  Sensor_IO_SPI_CS_Init(*handle);
165 
166  if(LPS22HB_Combo_Data[0].isTempInitialized == 0)
167  {
168  // SPI Serial Interface Mode selection --> 3Wires
169  if( Sensor_IO_Write(*handle, LPS22HB_CTRL_REG1, &data, 1) )
170  {
171  return COMPONENT_ERROR;
172  }
173 
174  if(LPS22HB_SwResetAndMemoryBoot(*handle))
175  {
176  return COMPONENT_ERROR;
177  }
178 
179  HAL_Delay(1000);
180 
181  data = 0x01;
182 
183  if( Sensor_IO_Write(*handle, LPS22HB_CTRL_REG1, &data, 1) )
184  {
185  return COMPONENT_ERROR;
186  }
187  }
188 
189  driver = ( PRESSURE_Drv_t * )((DrvContextTypeDef *)(*handle))->pVTable;
190 
191  if ( driver->Init == NULL )
192  {
193  memset((*handle), 0, sizeof(DrvContextTypeDef));
194  *handle = NULL;
195  return COMPONENT_ERROR;
196  }
197 
198  if ( driver->Init( (DrvContextTypeDef *)(*handle) ) == COMPONENT_ERROR )
199  {
200  memset((*handle), 0, sizeof(DrvContextTypeDef));
201  *handle = NULL;
202  return COMPONENT_ERROR;
203  }
204 
205  /* Disable I2C interface */
206  if ( LPS22HB_Set_I2C( *handle, LPS22HB_DISABLE ) == LPS22HB_ERROR )
207  {
208  return COMPONENT_ERROR;
209  }
210 
211  return COMPONENT_OK;
212 }
213 
214 
221 DrvStatusTypeDef BSP_PRESSURE_DeInit( void **handle )
222 {
223  DrvContextTypeDef *ctx = (DrvContextTypeDef *)(*handle);
224  PRESSURE_Drv_t *driver = NULL;
225 
226  if(ctx == NULL)
227  {
228  return COMPONENT_ERROR;
229  }
230 
231  driver = ( PRESSURE_Drv_t * )ctx->pVTable;
232 
233  if ( driver->DeInit == NULL )
234  {
235  return COMPONENT_ERROR;
236  }
237 
238  if ( driver->DeInit( ctx ) == COMPONENT_ERROR )
239  {
240  return COMPONENT_ERROR;
241  }
242 
243  memset(ctx, 0, sizeof(DrvContextTypeDef));
244 
245  *handle = NULL;
246 
247  return COMPONENT_OK;
248 }
249 
250 
251 
258 DrvStatusTypeDef BSP_PRESSURE_Sensor_Enable( void *handle )
259 {
260 
261  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
262  PRESSURE_Drv_t *driver = NULL;
263 
264  if(ctx == NULL)
265  {
266  return COMPONENT_ERROR;
267  }
268 
269  driver = ( PRESSURE_Drv_t * )ctx->pVTable;
270 
271  if ( driver->Sensor_Enable == NULL )
272  {
273  return COMPONENT_ERROR;
274  }
275 
276  if ( driver->Sensor_Enable( ctx ) == COMPONENT_ERROR )
277  {
278  return COMPONENT_ERROR;
279  }
280 
281  return COMPONENT_OK;
282 }
283 
284 
285 
292 DrvStatusTypeDef BSP_PRESSURE_Sensor_Disable( void *handle )
293 {
294 
295  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
296  PRESSURE_Drv_t *driver = NULL;
297 
298  if(ctx == NULL)
299  {
300  return COMPONENT_ERROR;
301  }
302 
303  driver = ( PRESSURE_Drv_t * )ctx->pVTable;
304 
305  if ( driver->Sensor_Disable == NULL )
306  {
307  return COMPONENT_ERROR;
308  }
309 
310  if ( driver->Sensor_Disable( ctx ) == COMPONENT_ERROR )
311  {
312  return COMPONENT_ERROR;
313  }
314 
315  return COMPONENT_OK;
316 }
317 
318 
326 DrvStatusTypeDef BSP_PRESSURE_IsInitialized( void *handle, uint8_t *status )
327 {
328  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
329 
330  if(ctx == NULL)
331  {
332  return COMPONENT_ERROR;
333  }
334 
335  if ( status == NULL )
336  {
337  return COMPONENT_ERROR;
338  }
339 
340  *status = ctx->isInitialized;
341 
342  return COMPONENT_OK;
343 }
344 
345 
353 DrvStatusTypeDef BSP_PRESSURE_IsEnabled( void *handle, uint8_t *status )
354 {
355  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
356 
357  if(ctx == NULL)
358  {
359  return COMPONENT_ERROR;
360  }
361 
362  if ( status == NULL )
363  {
364  return COMPONENT_ERROR;
365  }
366 
367  *status = ctx->isEnabled;
368 
369  return COMPONENT_OK;
370 }
371 
372 
380 DrvStatusTypeDef BSP_PRESSURE_IsCombo( void *handle, uint8_t *status )
381 {
382  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
383 
384  if(ctx == NULL)
385  {
386  return COMPONENT_ERROR;
387  }
388 
389  if ( status == NULL )
390  {
391  return COMPONENT_ERROR;
392  }
393 
394  *status = ctx->isCombo;
395 
396  return COMPONENT_OK;
397 }
398 
399 
407 DrvStatusTypeDef BSP_PRESSURE_Get_Instance( void *handle, uint8_t *instance )
408 {
409  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
410 
411  if(ctx == NULL)
412  {
413  return COMPONENT_ERROR;
414  }
415 
416  if ( instance == NULL )
417  {
418  return COMPONENT_ERROR;
419  }
420 
421  *instance = ctx->instance;
422 
423  return COMPONENT_OK;
424 }
425 
426 
427 
435 DrvStatusTypeDef BSP_PRESSURE_Get_WhoAmI( void *handle, uint8_t *who_am_i )
436 {
437 
438  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
439  PRESSURE_Drv_t *driver = NULL;
440 
441  if(ctx == NULL)
442  {
443  return COMPONENT_ERROR;
444  }
445 
446  driver = ( PRESSURE_Drv_t * )ctx->pVTable;
447 
448  if ( who_am_i == NULL )
449  {
450  return COMPONENT_ERROR;
451  }
452  if ( driver->Get_WhoAmI == NULL )
453  {
454  return COMPONENT_ERROR;
455  }
456  if ( driver->Get_WhoAmI( ctx, who_am_i ) == COMPONENT_ERROR )
457  {
458  return COMPONENT_ERROR;
459  }
460 
461  return COMPONENT_OK;
462 }
463 
464 
465 
472 DrvStatusTypeDef BSP_PRESSURE_Check_WhoAmI( void *handle )
473 {
474 
475  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
476  PRESSURE_Drv_t *driver = NULL;
477 
478  if(ctx == NULL)
479  {
480  return COMPONENT_ERROR;
481  }
482 
483  driver = ( PRESSURE_Drv_t * )ctx->pVTable;
484 
485  if ( driver->Check_WhoAmI == NULL )
486  {
487  return COMPONENT_ERROR;
488  }
489  if ( driver->Check_WhoAmI( ctx ) == COMPONENT_ERROR )
490  {
491  return COMPONENT_ERROR;
492  }
493 
494  return COMPONENT_OK;
495 }
496 
497 
498 
506 DrvStatusTypeDef BSP_PRESSURE_Get_Press( void *handle, float *pressure )
507 {
508 
509  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
510  PRESSURE_Drv_t *driver = NULL;
511 
512  if(ctx == NULL)
513  {
514  return COMPONENT_ERROR;
515  }
516 
517  driver = ( PRESSURE_Drv_t * )ctx->pVTable;
518 
519  if ( pressure == NULL )
520  {
521  return COMPONENT_ERROR;
522  }
523  if ( driver->Get_Press == NULL )
524  {
525  return COMPONENT_ERROR;
526  }
527  if ( driver->Get_Press( ctx, pressure ) == COMPONENT_ERROR )
528  {
529  return COMPONENT_ERROR;
530  }
531 
532  return COMPONENT_OK;
533 }
534 
535 
536 
544 DrvStatusTypeDef BSP_PRESSURE_Get_ODR( void *handle, float *odr )
545 {
546 
547  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
548  PRESSURE_Drv_t *driver = NULL;
549 
550  if(ctx == NULL)
551  {
552  return COMPONENT_ERROR;
553  }
554 
555  driver = ( PRESSURE_Drv_t * )ctx->pVTable;
556 
557  if ( odr == NULL )
558  {
559  return COMPONENT_ERROR;
560  }
561  if ( driver->Get_ODR == NULL )
562  {
563  return COMPONENT_ERROR;
564  }
565  if ( driver->Get_ODR( ctx, odr ) == COMPONENT_ERROR )
566  {
567  return COMPONENT_ERROR;
568  }
569 
570  return COMPONENT_OK;
571 }
572 
573 
574 
582 DrvStatusTypeDef BSP_PRESSURE_Set_ODR( void *handle, SensorOdr_t odr )
583 {
584 
585  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
586  PRESSURE_Drv_t *driver = NULL;
587 
588  if(ctx == NULL)
589  {
590  return COMPONENT_ERROR;
591  }
592 
593  driver = ( PRESSURE_Drv_t * )ctx->pVTable;
594 
595  if ( driver->Set_ODR == NULL )
596  {
597  return COMPONENT_ERROR;
598  }
599  if ( driver->Set_ODR( ctx, odr ) == COMPONENT_ERROR )
600  {
601  return COMPONENT_ERROR;
602  }
603 
604  return COMPONENT_OK;
605 }
606 
607 
608 
616 DrvStatusTypeDef BSP_PRESSURE_Set_ODR_Value( void *handle, float odr )
617 {
618 
619  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
620  PRESSURE_Drv_t *driver = NULL;
621 
622  if(ctx == NULL)
623  {
624  return COMPONENT_ERROR;
625  }
626 
627  driver = ( PRESSURE_Drv_t * )ctx->pVTable;
628 
629  if ( driver->Set_ODR_Value == NULL )
630  {
631  return COMPONENT_ERROR;
632  }
633  if ( driver->Set_ODR_Value( ctx, odr ) == COMPONENT_ERROR )
634  {
635  return COMPONENT_ERROR;
636  }
637 
638  return COMPONENT_OK;
639 }
640 
641 
650 DrvStatusTypeDef BSP_PRESSURE_Read_Reg( void *handle, uint8_t reg, uint8_t *data )
651 {
652 
653  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
654  PRESSURE_Drv_t *driver = NULL;
655 
656  if(ctx == NULL)
657  {
658  return COMPONENT_ERROR;
659  }
660 
661  driver = ( PRESSURE_Drv_t * )ctx->pVTable;
662 
663  if(data == NULL)
664  {
665  return COMPONENT_ERROR;
666  }
667 
668  if ( driver->Read_Reg == NULL )
669  {
670  return COMPONENT_ERROR;
671  }
672 
673  if ( driver->Read_Reg( ctx, reg, data ) == COMPONENT_ERROR )
674  {
675  return COMPONENT_ERROR;
676  }
677 
678  return COMPONENT_OK;
679 }
680 
681 
682 
691 DrvStatusTypeDef BSP_PRESSURE_Write_Reg( void *handle, uint8_t reg, uint8_t data )
692 {
693 
694  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
695  PRESSURE_Drv_t *driver = NULL;
696 
697  if(ctx == NULL)
698  {
699  return COMPONENT_ERROR;
700  }
701 
702  driver = ( PRESSURE_Drv_t * )ctx->pVTable;
703 
704  if ( driver->Write_Reg == NULL )
705  {
706  return COMPONENT_ERROR;
707  }
708 
709  if ( driver->Write_Reg( ctx, reg, data ) == COMPONENT_ERROR )
710  {
711  return COMPONENT_ERROR;
712  }
713 
714  return COMPONENT_OK;
715 }
716 
717 
718 
726 DrvStatusTypeDef BSP_PRESSURE_Get_DRDY_Status( void *handle, uint8_t *status )
727 {
728 
729  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
730  PRESSURE_Drv_t *driver = NULL;
731 
732  if(ctx == NULL)
733  {
734  return COMPONENT_ERROR;
735  }
736 
737  driver = ( PRESSURE_Drv_t * )ctx->pVTable;
738 
739  if ( driver->Get_DRDY_Status == NULL )
740  {
741  return COMPONENT_ERROR;
742  }
743 
744  if ( driver->Get_DRDY_Status( ctx, status ) == COMPONENT_ERROR )
745  {
746  return COMPONENT_ERROR;
747  }
748 
749  return COMPONENT_OK;
750 }
767 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
DrvStatusTypeDef BSP_PRESSURE_Read_Reg(void *handle, uint8_t reg, uint8_t *data)
Read the data from register.
DrvStatusTypeDef BSP_PRESSURE_Init(PRESSURE_ID_t id, void **handle)
Initialize a pressure sensor.
DrvStatusTypeDef BSP_PRESSURE_Write_Reg(void *handle, uint8_t reg, uint8_t data)
Write the data to register.
DrvStatusTypeDef BSP_PRESSURE_Sensor_Enable(void *handle)
Enable pressure sensor.
DrvStatusTypeDef BSP_PRESSURE_Get_ODR(void *handle, float *odr)
Get the pressure sensor output data rate.
DrvStatusTypeDef BSP_PRESSURE_Set_ODR(void *handle, SensorOdr_t odr)
Set the pressure sensor output data rate.
DrvStatusTypeDef BSP_PRESSURE_Get_WhoAmI(void *handle, uint8_t *who_am_i)
Get the WHO_AM_I ID of the pressure sensor.
uint8_t Sensor_IO_Write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite)
Writes a buffer to the sensor.
Definition: SensorTile.c:427
DrvStatusTypeDef BSP_PRESSURE_Set_ODR_Value(void *handle, float odr)
Set the pressure sensor output data rate.
DrvStatusTypeDef Sensor_IO_SPI_Init(void)
Configures sensor SPI interface.
Definition: SensorTile.c:293
DrvStatusTypeDef BSP_PRESSURE_DeInit(void **handle)
Deinitialize a pressure sensor.
This file contains definitions for SensorTile_pressure.c driver.
DrvStatusTypeDef BSP_PRESSURE_IsCombo(void *handle, uint8_t *status)
Check if the pressure sensor is combo.
DrvStatusTypeDef BSP_PRESSURE_Get_DRDY_Status(void *handle, uint8_t *status)
Get pressure data ready status.
DrvStatusTypeDef BSP_PRESSURE_IsInitialized(void *handle, uint8_t *status)
Check if the pressure sensor is initialized.
DrvStatusTypeDef BSP_PRESSURE_Get_Press(void *handle, float *pressure)
Get the pressure value.
DrvStatusTypeDef BSP_PRESSURE_Sensor_Disable(void *handle)
Disable pressure sensor.
DrvStatusTypeDef BSP_PRESSURE_IsEnabled(void *handle, uint8_t *status)
Check if the pressure sensor is enabled.
DrvStatusTypeDef BSP_PRESSURE_Get_Instance(void *handle, uint8_t *instance)
Get the pressure sensor instance.
DrvStatusTypeDef BSP_PRESSURE_Check_WhoAmI(void *handle)
Check the WHO_AM_I ID of the pressure sensor.
Generated by   doxygen 1.8.13