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

STSW-STLKT01

STSW-STLKT01
SensorTile_temperature.c
Go to the documentation of this file.
1 
39 /* Includes ------------------------------------------------------------------*/
40 #include "SensorTile_temperature.h"
41 
42 
43 
61 static DrvContextTypeDef TEMPERATURE_SensorHandle[ TEMPERATURE_SENSORS_MAX_NUM ];
62 static TEMPERATURE_Data_t TEMPERATURE_Data[ TEMPERATURE_SENSORS_MAX_NUM ]; // Temperature - all.
63 static LPS22HB_T_Data_t LPS22HB_T_0_Data; // Temperature - sensor 0 LPS22HB on SensorTile.
64 static HTS221_T_Data_t HTS221_T_0_Data; // Temperature - sensor 1 HTS221 on motherboard.
65 
74 static DrvStatusTypeDef BSP_LPS22HB_TEMPERATURE_Init( void **handle );
75 static DrvStatusTypeDef BSP_HTS221_TEMPERATURE_Init( void **handle );
76 
92 DrvStatusTypeDef BSP_TEMPERATURE_Init( TEMPERATURE_ID_t id, void **handle )
93 {
94 
95  *handle = NULL;
96 
97  switch(id)
98  {
99  case TEMPERATURE_SENSORS_AUTO:
100  default:
101  {
102  /* Try to init the HTS221 on motherboard before */
103  if(BSP_HTS221_TEMPERATURE_Init(handle) == COMPONENT_ERROR )
104  {
105  if( BSP_LPS22HB_TEMPERATURE_Init(handle) == COMPONENT_ERROR )
106  {
107  return COMPONENT_ERROR;
108  }
109  }
110  break;
111  }
112  case LPS22HB_T_0:
113  {
114  if(BSP_LPS22HB_TEMPERATURE_Init(handle) == COMPONENT_ERROR )
115  {
116  return COMPONENT_ERROR;
117  }
118  break;
119  }
120  case HTS221_T_0:
121  {
122  if(BSP_HTS221_TEMPERATURE_Init(handle) == COMPONENT_ERROR )
123  {
124  return COMPONENT_ERROR;
125  }
126  break;
127  }
128  }
129 
130  return COMPONENT_OK;
131 }
132 
133 
134 
135 static DrvStatusTypeDef BSP_LPS22HB_TEMPERATURE_Init( void **handle )
136 {
137  TEMPERATURE_Drv_t *driver = NULL;
138  uint8_t data = 0x01;
139 
140  if(TEMPERATURE_SensorHandle[ LPS22HB_T_0 ].isInitialized == 1)
141  {
142  /* We have reached the max num of instance for this component */
143  return COMPONENT_ERROR;
144  }
145 
146  if ( Sensor_IO_SPI_Init() == COMPONENT_ERROR )
147  {
148  return COMPONENT_ERROR;
149  }
150 
151  /* Setup sensor handle. */
152  TEMPERATURE_SensorHandle[ LPS22HB_T_0 ].who_am_i = LPS22HB_WHO_AM_I_VAL;
153  TEMPERATURE_SensorHandle[ LPS22HB_T_0 ].ifType = 1; // SPI interface
154  TEMPERATURE_SensorHandle[ LPS22HB_T_0 ].address = LPS22HB_ADDRESS_HIGH;
155  TEMPERATURE_SensorHandle[ LPS22HB_T_0 ].spiDevice = LPS22HB;
156  TEMPERATURE_SensorHandle[ LPS22HB_T_0 ].instance = LPS22HB_T_0;
157  TEMPERATURE_SensorHandle[ LPS22HB_T_0 ].isInitialized = 0;
158  TEMPERATURE_SensorHandle[ LPS22HB_T_0 ].isEnabled = 0;
159  TEMPERATURE_SensorHandle[ LPS22HB_T_0 ].isCombo = 1;
160  TEMPERATURE_SensorHandle[ LPS22HB_T_0 ].pData = ( void * )&TEMPERATURE_Data[ LPS22HB_T_0 ];
161  TEMPERATURE_SensorHandle[ LPS22HB_T_0 ].pVTable = ( void * )&LPS22HB_T_Drv;
162  TEMPERATURE_SensorHandle[ LPS22HB_T_0 ].pExtVTable = 0;
163 
164  LPS22HB_T_0_Data.comboData = &LPS22HB_Combo_Data[0];
165  TEMPERATURE_Data[ LPS22HB_T_0 ].pComponentData = ( void * )&LPS22HB_T_0_Data;
166  TEMPERATURE_Data[ LPS22HB_T_0 ].pExtData = 0;
167 
168  *handle = (void *)&TEMPERATURE_SensorHandle[ LPS22HB_T_0 ];
169 
170  Sensor_IO_SPI_CS_Init(*handle);
171 
172  if(LPS22HB_Combo_Data[0].isPressInitialized == 0)
173  {
174  // SPI Serial Interface Mode selection --> 3Wires
175  if( Sensor_IO_Write(*handle, LPS22HB_CTRL_REG1, &data, 1) )
176  {
177  return COMPONENT_ERROR;
178  }
179 
180  if(LPS22HB_SwResetAndMemoryBoot(*handle))
181  {
182  return COMPONENT_ERROR;
183  }
184 
185  HAL_Delay(100);
186 
187  data = 0x01;
188 
189  if( Sensor_IO_Write(*handle, LPS22HB_CTRL_REG1, &data, 1) )
190  {
191  return COMPONENT_ERROR;
192  }
193  }
194 
195  driver = ( TEMPERATURE_Drv_t * )((DrvContextTypeDef *)(*handle))->pVTable;
196 
197  if ( driver->Init == NULL )
198  {
199  memset((*handle), 0, sizeof(DrvContextTypeDef));
200  *handle = NULL;
201  return COMPONENT_ERROR;
202  }
203 
204  if ( driver->Init( (DrvContextTypeDef *)(*handle) ) == COMPONENT_ERROR )
205  {
206  memset((*handle), 0, sizeof(DrvContextTypeDef));
207  *handle = NULL;
208  return COMPONENT_ERROR;
209  }
210 
211  return COMPONENT_OK;
212 
213 }
214 
215 
216 
217 static DrvStatusTypeDef BSP_HTS221_TEMPERATURE_Init( void **handle )
218 {
219  TEMPERATURE_Drv_t *driver = NULL;
220 
221  if(TEMPERATURE_SensorHandle[ HTS221_T_0 ].isInitialized == 1)
222  {
223  /* We have reached the max num of instance for this component */
224  return COMPONENT_ERROR;
225  }
226 
227  if ( Sensor_IO_I2C_Init() == COMPONENT_ERROR )
228  {
229  return COMPONENT_ERROR;
230  }
231 
232  /* Setup sensor handle. */
233  TEMPERATURE_SensorHandle[ HTS221_T_0 ].who_am_i = HTS221_WHO_AM_I_VAL;
234  TEMPERATURE_SensorHandle[ HTS221_T_0 ].ifType = 0; // I2C interface
235  TEMPERATURE_SensorHandle[ HTS221_T_0 ].address = HTS221_ADDRESS_DEFAULT;
236  TEMPERATURE_SensorHandle[ HTS221_T_0 ].instance = HTS221_T_0;
237  TEMPERATURE_SensorHandle[ HTS221_T_0 ].isInitialized = 0;
238  TEMPERATURE_SensorHandle[ HTS221_T_0 ].isEnabled = 0;
239  TEMPERATURE_SensorHandle[ HTS221_T_0 ].isCombo = 1;
240  TEMPERATURE_SensorHandle[ HTS221_T_0 ].pData = ( void * )&TEMPERATURE_Data[ HTS221_T_0 ];
241  TEMPERATURE_SensorHandle[ HTS221_T_0 ].pVTable = ( void * )&HTS221_T_Drv;
242  TEMPERATURE_SensorHandle[ HTS221_T_0 ].pExtVTable = 0;
243 
244  HTS221_T_0_Data.comboData = &HTS221_Combo_Data[0];
245  TEMPERATURE_Data[ HTS221_T_0 ].pComponentData = ( void * )&HTS221_T_0_Data;
246  TEMPERATURE_Data[ HTS221_T_0 ].pExtData = 0;
247 
248  *handle = (void *)&TEMPERATURE_SensorHandle[ HTS221_T_0 ];
249 
250  driver = ( TEMPERATURE_Drv_t * )((DrvContextTypeDef *)(*handle))->pVTable;
251 
252  if ( driver->Init == NULL )
253  {
254  memset((*handle), 0, sizeof(DrvContextTypeDef));
255  *handle = NULL;
256  return COMPONENT_ERROR;
257  }
258 
259  if ( driver->Init( (DrvContextTypeDef *)(*handle) ) == COMPONENT_ERROR )
260  {
261  memset((*handle), 0, sizeof(DrvContextTypeDef));
262  *handle = NULL;
263  return COMPONENT_ERROR;
264  }
265 
266  return COMPONENT_OK;
267 }
268 
269 
276 DrvStatusTypeDef BSP_TEMPERATURE_DeInit( void **handle )
277 {
278  DrvContextTypeDef *ctx = (DrvContextTypeDef *)(*handle);
279  TEMPERATURE_Drv_t *driver = NULL;
280 
281  if(ctx == NULL)
282  {
283  return COMPONENT_ERROR;
284  }
285 
286  driver = ( TEMPERATURE_Drv_t * )ctx->pVTable;
287 
288  if ( driver->DeInit == NULL )
289  {
290  return COMPONENT_ERROR;
291  }
292 
293  if ( driver->DeInit( ctx ) == COMPONENT_ERROR )
294  {
295  return COMPONENT_ERROR;
296  }
297 
298  memset(ctx, 0, sizeof(DrvContextTypeDef));
299 
300  *handle = NULL;
301 
302  return COMPONENT_OK;
303 }
304 
305 
312 DrvStatusTypeDef BSP_TEMPERATURE_Sensor_Enable( void *handle )
313 {
314 
315  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
316  TEMPERATURE_Drv_t *driver = NULL;
317 
318  if(ctx == NULL)
319  {
320  return COMPONENT_ERROR;
321  }
322 
323  driver = ( TEMPERATURE_Drv_t * )ctx->pVTable;
324 
325  if ( driver->Sensor_Enable == NULL )
326  {
327  return COMPONENT_ERROR;
328  }
329 
330  if ( driver->Sensor_Enable( ctx ) == COMPONENT_ERROR )
331  {
332  return COMPONENT_ERROR;
333  }
334 
335  return COMPONENT_OK;
336 }
337 
338 
339 
346 DrvStatusTypeDef BSP_TEMPERATURE_Sensor_Disable( void *handle )
347 {
348 
349  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
350  TEMPERATURE_Drv_t *driver = NULL;
351 
352  if(ctx == NULL)
353  {
354  return COMPONENT_ERROR;
355  }
356 
357  driver = ( TEMPERATURE_Drv_t * )ctx->pVTable;
358 
359  if ( driver->Sensor_Disable == NULL )
360  {
361  return COMPONENT_ERROR;
362  }
363 
364  if ( driver->Sensor_Disable( ctx ) == COMPONENT_ERROR )
365  {
366  return COMPONENT_ERROR;
367  }
368 
369  return COMPONENT_OK;
370 }
371 
372 
380 DrvStatusTypeDef BSP_TEMPERATURE_IsInitialized( 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->isInitialized;
395 
396  return COMPONENT_OK;
397 }
398 
399 
407 DrvStatusTypeDef BSP_TEMPERATURE_IsEnabled( void *handle, uint8_t *status )
408 {
409  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
410 
411  if(ctx == NULL)
412  {
413  return COMPONENT_ERROR;
414  }
415 
416  if ( status == NULL )
417  {
418  return COMPONENT_ERROR;
419  }
420 
421  *status = ctx->isEnabled;
422 
423  return COMPONENT_OK;
424 }
425 
426 
434 DrvStatusTypeDef BSP_TEMPERATURE_IsCombo( void *handle, uint8_t *status )
435 {
436  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
437 
438  if(ctx == NULL)
439  {
440  return COMPONENT_ERROR;
441  }
442 
443  if ( status == NULL )
444  {
445  return COMPONENT_ERROR;
446  }
447 
448  *status = ctx->isCombo;
449 
450  return COMPONENT_OK;
451 }
452 
453 
461 DrvStatusTypeDef BSP_TEMPERATURE_Get_Instance( void *handle, uint8_t *instance )
462 {
463  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
464 
465  if(ctx == NULL)
466  {
467  return COMPONENT_ERROR;
468  }
469 
470  if ( instance == NULL )
471  {
472  return COMPONENT_ERROR;
473  }
474 
475  *instance = ctx->instance;
476 
477  return COMPONENT_OK;
478 }
479 
480 
481 
489 DrvStatusTypeDef BSP_TEMPERATURE_Get_WhoAmI( void *handle, uint8_t *who_am_i )
490 {
491 
492  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
493  TEMPERATURE_Drv_t *driver = NULL;
494 
495  if(ctx == NULL)
496  {
497  return COMPONENT_ERROR;
498  }
499 
500  driver = ( TEMPERATURE_Drv_t * )ctx->pVTable;
501 
502  if ( who_am_i == NULL )
503  {
504  return COMPONENT_ERROR;
505  }
506  if ( driver->Get_WhoAmI == NULL )
507  {
508  return COMPONENT_ERROR;
509  }
510  if ( driver->Get_WhoAmI( ctx, who_am_i ) == COMPONENT_ERROR )
511  {
512  return COMPONENT_ERROR;
513  }
514 
515  return COMPONENT_OK;
516 }
517 
518 
519 
526 DrvStatusTypeDef BSP_TEMPERATURE_Check_WhoAmI( void *handle )
527 {
528 
529  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
530  TEMPERATURE_Drv_t *driver = NULL;
531 
532  if(ctx == NULL)
533  {
534  return COMPONENT_ERROR;
535  }
536 
537  driver = ( TEMPERATURE_Drv_t * )ctx->pVTable;
538 
539  if ( driver->Check_WhoAmI == NULL )
540  {
541  return COMPONENT_ERROR;
542  }
543  if ( driver->Check_WhoAmI( ctx ) == COMPONENT_ERROR )
544  {
545  return COMPONENT_ERROR;
546  }
547 
548  return COMPONENT_OK;
549 }
550 
551 
552 
560 DrvStatusTypeDef BSP_TEMPERATURE_Get_Temp( void *handle, float *temperature )
561 {
562 
563  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
564  TEMPERATURE_Drv_t *driver = NULL;
565 
566  if(ctx == NULL)
567  {
568  return COMPONENT_ERROR;
569  }
570 
571  driver = ( TEMPERATURE_Drv_t * )ctx->pVTable;
572 
573  if ( temperature == NULL )
574  {
575  return COMPONENT_ERROR;
576  }
577  if ( driver->Get_Temp == NULL )
578  {
579  return COMPONENT_ERROR;
580  }
581  if ( driver->Get_Temp( ctx, temperature ) == COMPONENT_ERROR )
582  {
583  return COMPONENT_ERROR;
584  }
585 
586  return COMPONENT_OK;
587 }
588 
589 
590 
598 DrvStatusTypeDef BSP_TEMPERATURE_Get_ODR( void *handle, float *odr )
599 {
600 
601  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
602  TEMPERATURE_Drv_t *driver = NULL;
603 
604  if(ctx == NULL)
605  {
606  return COMPONENT_ERROR;
607  }
608 
609  driver = ( TEMPERATURE_Drv_t * )ctx->pVTable;
610 
611  if ( odr == NULL )
612  {
613  return COMPONENT_ERROR;
614  }
615  if ( driver->Get_ODR == NULL )
616  {
617  return COMPONENT_ERROR;
618  }
619  if ( driver->Get_ODR( ctx, odr ) == COMPONENT_ERROR )
620  {
621  return COMPONENT_ERROR;
622  }
623 
624  return COMPONENT_OK;
625 }
626 
627 
628 
636 DrvStatusTypeDef BSP_TEMPERATURE_Set_ODR( void *handle, SensorOdr_t odr )
637 {
638 
639  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
640  TEMPERATURE_Drv_t *driver = NULL;
641 
642  if(ctx == NULL)
643  {
644  return COMPONENT_ERROR;
645  }
646 
647  driver = ( TEMPERATURE_Drv_t * )ctx->pVTable;
648 
649  if ( driver->Set_ODR == NULL )
650  {
651  return COMPONENT_ERROR;
652  }
653  if ( driver->Set_ODR( ctx, odr ) == COMPONENT_ERROR )
654  {
655  return COMPONENT_ERROR;
656  }
657 
658  return COMPONENT_OK;
659 }
660 
661 
662 
670 DrvStatusTypeDef BSP_TEMPERATURE_Set_ODR_Value( void *handle, float odr )
671 {
672 
673  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
674  TEMPERATURE_Drv_t *driver = NULL;
675 
676  if(ctx == NULL)
677  {
678  return COMPONENT_ERROR;
679  }
680 
681  driver = ( TEMPERATURE_Drv_t * )ctx->pVTable;
682 
683  if ( driver->Set_ODR_Value == NULL )
684  {
685  return COMPONENT_ERROR;
686  }
687  if ( driver->Set_ODR_Value( ctx, odr ) == COMPONENT_ERROR )
688  {
689  return COMPONENT_ERROR;
690  }
691 
692  return COMPONENT_OK;
693 }
694 
695 
696 
705 DrvStatusTypeDef BSP_TEMPERATURE_Read_Reg( void *handle, uint8_t reg, uint8_t *data )
706 {
707 
708  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
709  TEMPERATURE_Drv_t *driver = NULL;
710 
711  if(ctx == NULL)
712  {
713  return COMPONENT_ERROR;
714  }
715 
716  driver = ( TEMPERATURE_Drv_t * )ctx->pVTable;
717 
718  if(data == NULL)
719  {
720  return COMPONENT_ERROR;
721  }
722 
723  if ( driver->Read_Reg == NULL )
724  {
725  return COMPONENT_ERROR;
726  }
727 
728  if ( driver->Read_Reg( ctx, reg, data ) == COMPONENT_ERROR )
729  {
730  return COMPONENT_ERROR;
731  }
732 
733  return COMPONENT_OK;
734 }
735 
736 
745 DrvStatusTypeDef BSP_TEMPERATURE_Write_Reg( void *handle, uint8_t reg, uint8_t data )
746 {
747 
748  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
749  TEMPERATURE_Drv_t *driver = NULL;
750 
751  if(ctx == NULL)
752  {
753  return COMPONENT_ERROR;
754  }
755 
756  driver = ( TEMPERATURE_Drv_t * )ctx->pVTable;
757 
758  if ( driver->Write_Reg == NULL )
759  {
760  return COMPONENT_ERROR;
761  }
762 
763  if ( driver->Write_Reg( ctx, reg, data ) == COMPONENT_ERROR )
764  {
765  return COMPONENT_ERROR;
766  }
767 
768  return COMPONENT_OK;
769 }
770 
771 
779 DrvStatusTypeDef BSP_TEMPERATURE_Get_DRDY_Status( void *handle, uint8_t *status )
780 {
781 
782  DrvContextTypeDef *ctx = (DrvContextTypeDef *)handle;
783  TEMPERATURE_Drv_t *driver = NULL;
784 
785  if(ctx == NULL)
786  {
787  return COMPONENT_ERROR;
788  }
789 
790  driver = ( TEMPERATURE_Drv_t * )ctx->pVTable;
791 
792  if ( driver->Get_DRDY_Status == NULL )
793  {
794  return COMPONENT_ERROR;
795  }
796 
797  if ( driver->Get_DRDY_Status( ctx, status ) == COMPONENT_ERROR )
798  {
799  return COMPONENT_ERROR;
800  }
801 
802  return COMPONENT_OK;
803 }
804 
821 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
DrvStatusTypeDef BSP_TEMPERATURE_IsInitialized(void *handle, uint8_t *status)
Check if the temperature sensor is initialized.
DrvStatusTypeDef BSP_TEMPERATURE_DeInit(void **handle)
Deinitialize a temperature sensor.
DrvStatusTypeDef BSP_TEMPERATURE_Set_ODR(void *handle, SensorOdr_t odr)
Set the temperature sensor output data rate.
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 Sensor_IO_SPI_Init(void)
Configures sensor SPI interface.
Definition: SensorTile.c:293
DrvStatusTypeDef BSP_TEMPERATURE_Check_WhoAmI(void *handle)
Check the WHO_AM_I ID of the temperature sensor.
DrvStatusTypeDef BSP_TEMPERATURE_Write_Reg(void *handle, uint8_t reg, uint8_t data)
Write the data to register.
DrvStatusTypeDef BSP_TEMPERATURE_Read_Reg(void *handle, uint8_t reg, uint8_t *data)
Read the data from register.
DrvStatusTypeDef BSP_TEMPERATURE_Get_Instance(void *handle, uint8_t *instance)
Get the temperature sensor instance.
DrvStatusTypeDef BSP_TEMPERATURE_IsEnabled(void *handle, uint8_t *status)
Check if the temperature sensor is enabled.
DrvStatusTypeDef BSP_TEMPERATURE_Get_ODR(void *handle, float *odr)
Get the temperature sensor output data rate.
DrvStatusTypeDef Sensor_IO_I2C_Init(void)
Configures sensor SPI interface.
Definition: SensorTile.c:274
DrvStatusTypeDef BSP_TEMPERATURE_Set_ODR_Value(void *handle, float odr)
Set the temperature sensor output data rate.
DrvStatusTypeDef BSP_TEMPERATURE_IsCombo(void *handle, uint8_t *status)
Check if the temperature sensor is combo.
DrvStatusTypeDef BSP_TEMPERATURE_Get_Temp(void *handle, float *temperature)
Get the temperature value.
This file contains definitions for SensorTile_temperature.c driver.
DrvStatusTypeDef BSP_TEMPERATURE_Get_WhoAmI(void *handle, uint8_t *who_am_i)
Get the WHO_AM_I ID of the temperature sensor.
DrvStatusTypeDef BSP_TEMPERATURE_Sensor_Disable(void *handle)
Disable temperature sensor.
DrvStatusTypeDef BSP_TEMPERATURE_Init(TEMPERATURE_ID_t id, void **handle)
Initialize a temperature sensor.
DrvStatusTypeDef BSP_TEMPERATURE_Get_DRDY_Status(void *handle, uint8_t *status)
Get temperature data ready status.
DrvStatusTypeDef BSP_TEMPERATURE_Sensor_Enable(void *handle)
Enable temperature sensor.
Generated by   doxygen 1.8.13