-
Notifications
You must be signed in to change notification settings - Fork 0
/
stts751.c
718 lines (603 loc) · 16.3 KB
/
stts751.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/**
******************************************************************************
* @file stts751.c
* @author MCD Application Team
* @brief stts751 driver file
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stts751.h"
#include "cmsis_iar.h"
/** @addtogroup BSP BSP
* @{
*/
/** @addtogroup Component Component
* @{
*/
/** @defgroup STTS751 STTS751
* @{
*/
/** @defgroup STTS751_Exported_Variables STTS751 Exported Variables
* @{
*/
STTS751_CommonDrv_t STTS751_COMMON_Driver =
{
STTS751_Init,
STTS751_DeInit,
STTS751_ReadID,
STTS751_GetCapabilities,
};
STTS751_TEMP_Drv_t STTS751_TEMP_Driver =
{
STTS751_TEMP_Enable,
STTS751_TEMP_Disable,
STTS751_TEMP_GetOutputDataRate,
STTS751_TEMP_SetOutputDataRate,
STTS751_TEMP_GetTemperature,
};
/**
* @}
*/
/** @defgroup STTS751_Private_Function_Prototypes STTS751 Private Function Prototypes
* @{
*/
static int32_t ReadRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length);
static int32_t WriteRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length);
/**
* @}
*/
/** @defgroup STTS751_Exported_Functions STTS751 Exported Functions
* @{
*/
/**
* @brief Register Component Bus IO operations
* @param pObj the device pObj
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_RegisterBusIO(STTS751_Object_t *pObj, STTS751_IO_t *pIO)
{
int32_t ret;
if (pObj == NULL)
{
ret = STTS751_ERROR;
}
else
{
pObj->IO.Init = pIO->Init;
pObj->IO.DeInit = pIO->DeInit;
pObj->IO.BusType = pIO->BusType;
pObj->IO.Address = pIO->Address;
pObj->IO.WriteReg = pIO->WriteReg;
pObj->IO.ReadReg = pIO->ReadReg;
pObj->IO.GetTick = pIO->GetTick;
pObj->Ctx.read_reg = ReadRegWrap;
pObj->Ctx.write_reg = WriteRegWrap;
pObj->Ctx.handle = pObj;
if (pObj->IO.Init != NULL)
{
ret = pObj->IO.Init();
}
else
{
ret = STTS751_ERROR;
}
}
return ret;
}
/**
* @brief Initialize the STTS751 sensor
* @param pObj the device pObj
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_Init(STTS751_Object_t *pObj)
{
if (pObj->is_initialized == 0U)
{
/* Disable EVENT pin of SMBus. */
if (stts751_pin_event_route_set(&(pObj->Ctx), PROPERTY_ENABLE) != STTS751_OK)
{
return STTS751_ERROR;
}
/* Set default ODR */
pObj->temp_odr = 1.0f;
/* Set the resolution to the maximum allowed value */
if (stts751_resolution_set(&(pObj->Ctx), STTS751_12bit) != STTS751_OK)
{
return STTS751_ERROR;
}
/* Put the component in standby mode. */
if (stts751_temp_data_rate_set(&(pObj->Ctx), STTS751_TEMP_ODR_OFF) != STTS751_OK)
{
return STTS751_ERROR;
}
}
pObj->is_initialized = 1;
return STTS751_OK;
}
/**
* @brief Deinitialize the STTS751 sensor
* @param pObj the device pObj
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_DeInit(STTS751_Object_t *pObj)
{
if (pObj->is_initialized == 1U)
{
/* Put the component in standby mode */
if (STTS751_TEMP_Disable(pObj) != STTS751_OK)
{
return STTS751_ERROR;
}
}
pObj->is_initialized = 0;
return STTS751_OK;
}
/**
* @brief Get WHO_AM_I value
* @param pObj the device pObj
* @param Id the WHO_AM_I value
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_ReadID(STTS751_Object_t *pObj, uint8_t *Id)
{
stts751_id_t buf;
if (stts751_device_id_get(&(pObj->Ctx), &buf) != STTS751_OK)
{
return STTS751_ERROR;
}
*Id = buf.manufacturer_id;
return STTS751_OK;
}
/**
* @brief Get STTS751 sensor capabilities
* @param pObj Component object pointer
* @param Capabilities pointer to STTS751 sensor capabilities
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_GetCapabilities(STTS751_Object_t *pObj, STTS751_Capabilities_t *Capabilities)
{
/* Prevent unused argument(s) compilation warning */
(void)(pObj);
Capabilities->Humidity = 0;
Capabilities->Pressure = 0;
Capabilities->Temperature = 1;
Capabilities->LowPower = 0;
Capabilities->HumMaxOdr = 0.0f;
Capabilities->TempMaxOdr = 32.0f;
Capabilities->PressMaxOdr = 0.0f;
return STTS751_OK;
}
/**
* @brief Get the STTS751 initialization status
* @param pObj the device pObj
* @param Status 1 if initialized, 0 otherwise
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_Get_Init_Status(STTS751_Object_t *pObj, uint8_t *Status)
{
if (pObj == NULL)
{
return STTS751_ERROR;
}
*Status = pObj->is_initialized;
return STTS751_OK;
}
/**
* @brief Enable the STTS751 temperature sensor
* @param pObj the device pObj
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_TEMP_Enable(STTS751_Object_t *pObj)
{
/* Check if the component is already enabled */
if (pObj->temp_is_enabled == 1U)
{
return STTS751_OK;
}
/* Power on the component and set the odr. */
if (STTS751_TEMP_SetOutputDataRate(pObj, pObj->temp_odr) != STTS751_OK)
{
return STTS751_ERROR;
}
pObj->temp_is_enabled = 1;
return STTS751_OK;
}
/**
* @brief Disable the STTS751 temperature sensor
* @param pObj the device pObj
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_TEMP_Disable(STTS751_Object_t *pObj)
{
/* Check if the component is already disabled */
if (pObj->temp_is_enabled == 0U)
{
return STTS751_OK;
}
/* Save the current odr. */
if (STTS751_TEMP_GetOutputDataRate(pObj, &pObj->temp_odr) != STTS751_OK)
{
return STTS751_ERROR;
}
/* Put the component in standby mode. */
if (stts751_temp_data_rate_set(&(pObj->Ctx), STTS751_TEMP_ODR_OFF) != STTS751_OK)
{
return STTS751_ERROR;
}
pObj->temp_is_enabled = 0;
return STTS751_OK;
}
/**
* @brief Get the STTS751 temperature sensor output data rate
* @param pObj the device pObj
* @param Odr pointer where the output data rate is written
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_TEMP_GetOutputDataRate(STTS751_Object_t *pObj, float *Odr)
{
int32_t ret = STTS751_OK;
stts751_odr_t odr_low_level;
if (stts751_temp_data_rate_get(&(pObj->Ctx), &odr_low_level) != STTS751_OK)
{
return STTS751_ERROR;
}
switch (odr_low_level)
{
case STTS751_TEMP_ODR_OFF:
case STTS751_TEMP_ODR_ONE_SHOT:
*Odr = 0.0f;
break;
case STTS751_TEMP_ODR_62mHz5:
*Odr = 0.0625f;
break;
case STTS751_TEMP_ODR_125mHz:
*Odr = 0.125f;
break;
case STTS751_TEMP_ODR_250mHz:
*Odr = 0.250f;
break;
case STTS751_TEMP_ODR_500mHz:
*Odr = 0.500f;
break;
case STTS751_TEMP_ODR_1Hz:
*Odr = 1.0f;
break;
case STTS751_TEMP_ODR_2Hz:
*Odr = 2.0f;
break;
case STTS751_TEMP_ODR_4Hz:
*Odr = 4.0f;
break;
case STTS751_TEMP_ODR_8Hz:
*Odr = 8.0f;
break;
case STTS751_TEMP_ODR_16Hz:
*Odr = 16.0f;
break;
case STTS751_TEMP_ODR_32Hz:
*Odr = 32.0f;
break;
default:
ret = STTS751_ERROR;
break;
}
return ret;
}
/**
* @brief Set the STTS751 temperature sensor output data rate
* @param pObj the device pObj
* @param Odr the output data rate value to be set
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_TEMP_SetOutputDataRate(STTS751_Object_t *pObj, float Odr)
{
stts751_odr_t new_odr;
stts751_tres_t res;
/* Get the current resolution */
if (stts751_resolution_get(&(pObj->Ctx), &res) != STTS751_OK)
{
return STTS751_ERROR;
}
/* If the requested odr is 16Hz we cannot use the 12 bits resolution */
if(Odr == 16.0f && res == STTS751_12bit)
{
/* We force resolution to the maximum allowed value */
if (stts751_resolution_set(&(pObj->Ctx), STTS751_11bit) != STTS751_OK)
{
return STTS751_ERROR;
}
}
/* If the requested odr is 32Hz we cannot use the 12 bits and 11 bits resolutions */
if(Odr == 32.0f && (res == STTS751_12bit || res == STTS751_11bit))
{
/* We force resolution to the maximum allowed value */
if (stts751_resolution_set(&(pObj->Ctx), STTS751_10bit) != STTS751_OK)
{
return STTS751_ERROR;
}
}
new_odr = (Odr <= 0.0625f) ? STTS751_TEMP_ODR_62mHz5
: (Odr <= 0.125f ) ? STTS751_TEMP_ODR_125mHz
: (Odr <= 0.25f ) ? STTS751_TEMP_ODR_250mHz
: (Odr <= 0.5f ) ? STTS751_TEMP_ODR_500mHz
: (Odr <= 1.0f ) ? STTS751_TEMP_ODR_1Hz
: (Odr <= 2.0f ) ? STTS751_TEMP_ODR_2Hz
: (Odr <= 4.0f ) ? STTS751_TEMP_ODR_4Hz
: (Odr <= 8.0f ) ? STTS751_TEMP_ODR_8Hz
: (Odr <= 16.0f ) ? STTS751_TEMP_ODR_16Hz
: STTS751_TEMP_ODR_32Hz;
if (stts751_temp_data_rate_set(&(pObj->Ctx), new_odr) != STTS751_OK)
{
return STTS751_ERROR;
}
return STTS751_OK;
}
/**
* @brief Get the STTS751 temperature value
* @param pObj the device pObj
* @param Value pointer where the temperature value is written
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_TEMP_GetTemperature(STTS751_Object_t *pObj, float *Value)
{
int16_t raw_value;
/* Get the temperature */
if (stts751_temperature_raw_get(&(pObj->Ctx), &raw_value) != STTS751_OK)
{
return STTS751_ERROR;
}
*Value = stts751_from_lsb_to_celsius(raw_value);
return STTS751_OK;
}
/**
* @brief Get the STTS751 temperature data ready bit value
* @param pObj the device pObj
* @param Status the status of data ready bit
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_TEMP_Get_DRDY_Status(STTS751_Object_t *pObj, uint8_t *Status)
{
uint8_t val;
if (stts751_flag_busy_get(&(pObj->Ctx), &val) != STTS751_OK)
{
return STTS751_ERROR;
}
if(val)
{
*Status = 0;
} else
{
*Status = 1;
}
return STTS751_OK;
}
/**
* @brief Set the STTS751 high temperature threshold value
* @param pObj the device pObj
* @param Value the high temperature threshold to be set
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_TEMP_SetHighTemperatureThreshold(STTS751_Object_t *pObj, float Value)
{
int16_t raw_value;
raw_value = stts751_from_celsius_to_lsb(Value);
/* Set the temperature threshold */
if (stts751_high_temperature_threshold_set(&(pObj->Ctx), raw_value) != STTS751_OK)
{
return STTS751_ERROR;
}
return STTS751_OK;
}
/**
* @brief Set the STTS751 low temperature threshold value
* @param pObj the device pObj
* @param Value the high temperature threshold to be set
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_TEMP_SetLowTemperatureThreshold(STTS751_Object_t *pObj, float Value)
{
int16_t raw_value;
raw_value = stts751_from_celsius_to_lsb(Value);
/* Set the temperature threshold */
if (stts751_low_temperature_threshold_set(&(pObj->Ctx), raw_value) != STTS751_OK)
{
return STTS751_ERROR;
}
return STTS751_OK;
}
/**
* @brief Get the STTS751 temperature limits status
* @param pObj the device pObj
* @param HighLimit indicates that high temperature limit has been exceeded
* @param LowhLimit indicates that low temperature limit has been exceeded
* @param ThermLimit indicates that therm temperature limit has been exceeded
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_TEMP_GetTemperatureLimitStatus(STTS751_Object_t *pObj, uint8_t *HighLimit, uint8_t *LowLimit, uint8_t *ThermLimit)
{
stts751_status_t status;
/* Read status register */
if (stts751_status_reg_get(&(pObj->Ctx), &status) != STTS751_OK)
{
return STTS751_ERROR;
}
if(HighLimit != NULL)
{
*HighLimit = status.t_high;
}
if(LowLimit != NULL)
{
*LowLimit = status.t_low;
}
if(ThermLimit != NULL)
{
*ThermLimit = status.thrm;
}
return STTS751_OK;
}
/**
* @brief Enable or disable interrupt on EVENT pin
* @param pObj the device pObj
* @param Enable 0 disable the EVENT pin, 1 enable EVENT pin
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_TEMP_SetEventPin(STTS751_Object_t *pObj, uint8_t Enable)
{
uint8_t state;
/* The MASK1 bit in configuration register has inverted logic */
if (Enable == 0) state = PROPERTY_ENABLE; else state = PROPERTY_DISABLE;
if (stts751_pin_event_route_set(&(pObj->Ctx), state) != STTS751_OK)
{
return STTS751_ERROR;
}
return STTS751_OK;
}
/**
* @brief Get the STTS751 register value
* @param pObj the device pObj
* @param Reg address to be read
* @param Data pointer where the value is written
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_Read_Reg(STTS751_Object_t *pObj, uint8_t Reg, uint8_t *Data)
{
if (stts751_read_reg(&(pObj->Ctx), Reg, Data, 1) != STTS751_OK)
{
return STTS751_ERROR;
}
return STTS751_OK;
}
/**
* @brief Set the STTS751 register value
* @param pObj the device pObj
* @param Reg address to be written
* @param Data value to be written
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_Write_Reg(STTS751_Object_t *pObj, uint8_t Reg, uint8_t Data)
{
if (stts751_write_reg(&(pObj->Ctx), Reg, &Data, 1) != STTS751_OK)
{
return STTS751_ERROR;
}
return STTS751_OK;
}
/**
* @brief Set the STTS751 One Shot Mode
* @param pObj the device pObj
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_Set_One_Shot(STTS751_Object_t *pObj)
{
/* Start One Shot Measurement */
if(stts751_temp_data_rate_set(&(pObj->Ctx), STTS751_TEMP_ODR_ONE_SHOT) != STTS751_OK)
{
return STTS751_ERROR;
}
return STTS751_OK;
}
/**
* @brief Get the STTS751 One Shot Status
* @param pObj the device pObj
* @param Status pointer to the one shot status (1 means measurements available, 0 means measurements not available yet)
* @retval 0 in case of success, an error code otherwise
*/
int32_t STTS751_Get_One_Shot_Status(STTS751_Object_t *pObj, uint8_t *Status)
{
uint8_t busy;
/* Get Busy flag */
if(stts751_flag_busy_get(&(pObj->Ctx), &busy) != STTS751_OK)
{
return STTS751_ERROR;
}
if(busy)
{
*Status = 0;
}
else
{
*Status = 1;
}
return STTS751_OK;
}
/**
* @}
*/
/** @defgroup STTS751_Private_Functions STTS751 Private Functions
* @{
*/
/**
* @brief Wrap Read register component function to Bus IO function
* @param Handle the device handler
* @param Reg the register address
* @param pData the stored data pointer
* @param Length the length
* @retval 0 in case of success, an error code otherwise
*/
static int32_t ReadRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length)
{
uint16_t i;
int32_t ret = STTS751_OK;
STTS751_Object_t *pObj = (STTS751_Object_t *)Handle;
if (pObj->IO.BusType == (uint32_t)STTS751_I2C_BUS) /* I2C */
{
for (i = 0; i < Length; i++)
{
ret = pObj->IO.ReadReg(pObj->IO.Address, (Reg + i), &pData[i], 1);
if (ret != STTS751_OK)
{
return STTS751_ERROR;
}
}
}
return ret;
}
/**
* @brief Wrap Write register component function to Bus IO function
* @param Handle the device handler
* @param Reg the register address
* @param pData the stored data pointer
* @param Length the length
* @retval 0 in case of success, an error code otherwise
*/
static int32_t WriteRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint16_t Length)
{
uint16_t i;
int32_t ret = STTS751_OK;
STTS751_Object_t *pObj = (STTS751_Object_t *)Handle;
if (pObj->IO.BusType == (uint32_t)STTS751_I2C_BUS) /* I2C */
{
for (i = 0; i < Length; i++)
{
ret = pObj->IO.WriteReg(pObj->IO.Address, (Reg + i), &pData[i], 1);
if (ret != STTS751_OK)
{
return STTS751_ERROR;
}
}
}
return ret;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/