ruuvi.drivers.c ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
Loading...
Searching...
No Matches
ruuvi_interface_tmp117.c
Go to the documentation of this file.
2
3#if (RI_TMP117_ENABLED || DOXYGEN)
4
10
11
28#define TMP117_CC_RETRIES_MAX (5U)
29#define TMP117_CC_RETRY_DELAY_MS (10U)
30
31static uint8_t m_address;
32static uint16_t ms_per_sample;
33static uint16_t ms_per_cc;
34static float m_temperature;
35static uint64_t m_timestamp;
36static const char m_sensor_name[] = "TMP117";
37static bool m_continuous = false;
38
39static inline bool param_is_valid (const uint8_t param)
40{
41 return ( (RD_SENSOR_CFG_DEFAULT == param)
42 || (RD_SENSOR_CFG_MIN == param)
43 || (RD_SENSOR_CFG_MAX == param)
44 || (RD_SENSOR_CFG_NO_CHANGE == param));
45}
46
47static rd_status_t tmp117_soft_reset (void)
48{
49 uint16_t reset = TMP117_MASK_RESET & 0xFFFF;
50 rd_status_t err_code = ri_i2c_tmp117_write (m_address,
52 reset);
54 return err_code;
55}
56
57static rd_status_t tmp117_validate_id (const uint8_t handle)
58{
59 uint16_t id;
60 rd_status_t err_code = RD_SUCCESS;
61 err_code |= ri_i2c_tmp117_read (handle, TMP117_REG_DEVICE_ID, &id);
62 id &= TMP117_MASK_ID;
63
64 if (TMP117_VALUE_ID != id)
65 {
66 err_code |= RD_ERROR_NOT_FOUND;
67 }
68
69 return err_code;
70}
71
72static inline rd_status_t os_1_set (uint16_t * const reg_val)
73{
74 rd_status_t err_code = RD_SUCCESS;
75 *reg_val |= TMP117_VALUE_OS_1;
76
77 if (TMP117_OS_1_TSAMPLE_MS > ms_per_cc)
78 {
79 err_code |= RD_ERROR_INVALID_STATE;
80 }
81
82 ms_per_sample = TMP117_OS_1_TSAMPLE_MS;
83 return err_code;
84}
85
86static inline rd_status_t os_8_set (uint16_t * const reg_val)
87{
88 rd_status_t err_code = RD_SUCCESS;
89 *reg_val |= TMP117_VALUE_OS_8;
90
91 if (TMP117_OS_8_TSAMPLE_MS > ms_per_cc)
92 {
93 err_code |= RD_ERROR_INVALID_STATE;
94 }
95
96 ms_per_sample = TMP117_OS_8_TSAMPLE_MS;
97 return err_code;
98}
99
100static inline rd_status_t os_32_set (uint16_t * const reg_val)
101{
102 rd_status_t err_code = RD_SUCCESS;
103 *reg_val |= TMP117_VALUE_OS_32;
104
105 if (TMP117_OS_32_TSAMPLE_MS > ms_per_cc)
106 {
107 err_code |= RD_ERROR_INVALID_STATE;
108 }
109
110 ms_per_sample = TMP117_OS_32_TSAMPLE_MS;
111 return err_code;
112}
113
114static inline rd_status_t os_64_set (uint16_t * const reg_val)
115{
116 rd_status_t err_code = RD_SUCCESS;
117 *reg_val |= TMP117_VALUE_OS_64;
118
119 if (TMP117_OS_64_TSAMPLE_MS > ms_per_cc)
120 {
121 err_code |= RD_ERROR_INVALID_STATE;
122 }
123
124 ms_per_sample = TMP117_OS_64_TSAMPLE_MS;
125 return err_code;
126}
127
128static rd_status_t tmp117_oversampling_set (const uint8_t num_os)
129{
130 uint16_t reg_val;
131 rd_status_t err_code = RD_SUCCESS;
132 err_code |= ri_i2c_tmp117_read (m_address, TMP117_REG_CONFIGURATION,
133 &reg_val);
134 reg_val &= ~TMP117_MASK_OS;
135
136 switch (num_os)
137 {
139 reg_val |= os_1_set (&reg_val);
140 break;
141
143 reg_val |= os_8_set (&reg_val);
144 break;
145
147 reg_val |= os_32_set (&reg_val);
148 break;
149
151 reg_val |= os_64_set (&reg_val);
152 break;
153
154 default:
155 err_code |= RD_ERROR_INVALID_PARAM;
156 }
157
158 err_code |= ri_i2c_tmp117_write (m_address, TMP117_REG_CONFIGURATION,
159 reg_val);
160 return err_code;
161}
162
163static rd_status_t
164tmp117_cc_check (uint16_t * const reg, const uint16_t ts, const uint16_t reg_val)
165{
166 rd_status_t err_code = RD_SUCCESS;
167
168 if (ts < ms_per_sample)
169 {
170 err_code |= RD_ERROR_INVALID_STATE;
171 }
172 else
173 {
174 *reg |= reg_val;
175 ms_per_cc = ts;
176 }
177
178 return err_code;
179}
180
181static rd_status_t tmp117_samplerate_set (const uint16_t num_os)
182{
183 uint16_t reg_val;
184 rd_status_t err_code = RD_SUCCESS;
185 err_code |= ri_i2c_tmp117_read (m_address, TMP117_REG_CONFIGURATION,
186 &reg_val);
187 reg_val &= ~TMP117_MASK_CC;
188
189 switch (num_os)
190 {
192 err_code |= tmp117_cc_check (&reg_val, ms_per_sample, TMP117_VALUE_CC_16_MS);
193 break;
194
196 err_code |= tmp117_cc_check (&reg_val, ms_per_sample, TMP117_VALUE_CC_125_MS);
197 break;
198
200 err_code |= tmp117_cc_check (&reg_val, ms_per_sample, TMP117_VALUE_CC_250_MS);
201 break;
202
204 err_code |= tmp117_cc_check (&reg_val, ms_per_sample, TMP117_VALUE_CC_500_MS);
205 break;
206
208 err_code |= tmp117_cc_check (&reg_val, ms_per_sample, TMP117_VALUE_CC_1000_MS);
209 break;
210
212 err_code |= tmp117_cc_check (&reg_val, ms_per_sample, TMP117_VALUE_CC_4000_MS);
213 break;
214
216 err_code |= tmp117_cc_check (&reg_val, ms_per_sample, TMP117_VALUE_CC_8000_MS);
217 break;
218
220 err_code |= tmp117_cc_check (&reg_val, ms_per_sample, TMP117_VALUE_CC_16000_MS);
221 break;
222
223 default:
224 err_code |= RD_ERROR_INVALID_PARAM;
225 break;
226 }
227
228 err_code |= ri_i2c_tmp117_write (m_address, TMP117_REG_CONFIGURATION, reg_val);
229 return err_code;
230}
231
232static rd_status_t tmp117_sleep (void)
233{
234 uint16_t reg_val;
235 rd_status_t err_code;
236 err_code = ri_i2c_tmp117_read (m_address, TMP117_REG_CONFIGURATION,
237 &reg_val);
238 reg_val &= ~TMP117_MASK_MODE;
239 reg_val |= TMP117_VALUE_MODE_SLEEP;
240 err_code |= ri_i2c_tmp117_write (m_address, TMP117_REG_CONFIGURATION,
241 reg_val);
242 return err_code;
243}
244
245static rd_status_t tmp117_sample (void)
246{
247 uint16_t reg_val = 0;
248 rd_status_t err_code = RD_SUCCESS;
249 err_code |= ri_i2c_tmp117_read (m_address, TMP117_REG_CONFIGURATION,
250 &reg_val);
251 reg_val &= ~TMP117_MASK_MODE;
252 reg_val |= TMP117_VALUE_MODE_SINGLE;
253 err_code |= ri_i2c_tmp117_write (m_address, TMP117_REG_CONFIGURATION,
254 reg_val);
255 m_timestamp = rd_sensor_timestamp_get();
256 return err_code;
257}
258
259static rd_status_t tmp117_continuous (void)
260{
261 uint16_t reg_val;
262 rd_status_t err_code;
263 err_code = ri_i2c_tmp117_read (m_address, TMP117_REG_CONFIGURATION,
264 &reg_val);
265 reg_val &= ~TMP117_MASK_MODE;
266 reg_val |= TMP117_VALUE_MODE_CONT;
267 err_code |= ri_i2c_tmp117_write (m_address, TMP117_REG_CONFIGURATION,
268 reg_val);
269 return err_code;
270}
271
272static rd_status_t tmp117_read (float * const temperature)
273{
274 uint16_t reg_val;
275 rd_status_t err_code;
276 err_code = ri_i2c_tmp117_read (m_address, TMP117_REG_TEMP_RESULT, &reg_val);
277 int32_t dec_temperature;
278
279 // 0x8000 is a specific value for "Not available" and should not be returned to app.
280 if (reg_val > 0x8000U)
281 {
282 dec_temperature = (int32_t) reg_val - 0xFFFF;
283 }
284 else
285 {
286 dec_temperature = reg_val;
287 }
288
289 if ( (TMP117_VALUE_TEMP_NA == reg_val) || (RD_SUCCESS != err_code))
290 {
291 *temperature = NAN;
292 err_code |= RD_ERROR_INVALID_STATE;
293 }
294 else
295 {
296 *temperature = (0.0078125F * dec_temperature);
297 }
298
299 return err_code;
300}
301
303ri_tmp117_init (rd_sensor_t * environmental_sensor, rd_bus_t bus, uint8_t handle)
304{
305 rd_status_t err_code = RD_SUCCESS;
306
307 if (NULL == environmental_sensor)
308 {
309 err_code |= RD_ERROR_NULL;
310 }
311 else if (rd_sensor_is_init (environmental_sensor))
312 {
313 err_code |= RD_ERROR_INVALID_STATE;
314 }
315 else
316 {
317 rd_sensor_initialize (environmental_sensor);
318 environmental_sensor->name = m_sensor_name;
319 err_code = RD_SUCCESS;
320
321 if (RD_BUS_I2C == bus)
322 {
323 err_code |= tmp117_validate_id (handle);
324 }
325 else
326 {
327 err_code |= RD_ERROR_INVALID_PARAM;
328 }
329
330 if (RD_SUCCESS == err_code)
331 {
332 m_address = handle;
333 err_code |= tmp117_soft_reset();
334 environmental_sensor->init = ri_tmp117_init;
335 environmental_sensor->uninit = ri_tmp117_uninit;
336 environmental_sensor->samplerate_set = ri_tmp117_samplerate_set;
337 environmental_sensor->samplerate_get = ri_tmp117_samplerate_get;
338 environmental_sensor->resolution_set = ri_tmp117_resolution_set;
339 environmental_sensor->resolution_get = ri_tmp117_resolution_get;
340 environmental_sensor->scale_set = ri_tmp117_scale_set;
341 environmental_sensor->scale_get = ri_tmp117_scale_get;
342 environmental_sensor->dsp_set = ri_tmp117_dsp_set;
343 environmental_sensor->dsp_get = ri_tmp117_dsp_get;
344 environmental_sensor->mode_set = ri_tmp117_mode_set;
345 environmental_sensor->mode_get = ri_tmp117_mode_get;
346 environmental_sensor->data_get = ri_tmp117_data_get;
347 environmental_sensor->configuration_set = rd_sensor_configuration_set;
348 environmental_sensor->configuration_get = rd_sensor_configuration_get;
349 environmental_sensor->provides.datas.temperature_c = 1;
350 m_timestamp = RD_UINT64_INVALID;
351 m_temperature = NAN;
352 ms_per_cc = 1000;
353 ms_per_sample = TMP117_OS_8_TSAMPLE_MS;
354 m_continuous = false;
355 }
356 }
357
358 return err_code;
359}
360
361rd_status_t ri_tmp117_uninit (rd_sensor_t * sensor, rd_bus_t bus, uint8_t handle)
362{
363 UNUSED_VARIABLE (bus);
364 UNUSED_VARIABLE (handle);
365 rd_status_t err_code = RD_SUCCESS;
366
367 if (NULL == sensor)
368 {
369 err_code |= RD_ERROR_NULL;
370 }
371 else
372 {
373 tmp117_sleep();
374 rd_sensor_uninitialize (sensor);
375 m_timestamp = RD_UINT64_INVALID;
376 m_temperature = NAN;
377 m_address = 0;
378 m_continuous = false;
379 }
380
381 return err_code;
382}
383
384
386{
387 rd_status_t err_code = RD_SUCCESS;
388
389 if (NULL == samplerate)
390 {
391 err_code |= RD_ERROR_NULL;
392 }
393 else if (m_continuous)
394 {
395 err_code |= RD_ERROR_INVALID_STATE;
396 }
397 else if (RD_SENSOR_CFG_NO_CHANGE == *samplerate)
398 {
399 err_code |= ri_tmp117_samplerate_get (samplerate);
400 }
401 else if ( (RD_SENSOR_CFG_DEFAULT == *samplerate)
402 || (1 >= *samplerate))
403 {
404 *samplerate = 1;
405 err_code |= tmp117_samplerate_set (TMP117_VALUE_CC_1000_MS);
406 }
407 else if (2 >= *samplerate)
408 {
409 *samplerate = 2;
410 err_code |= tmp117_samplerate_set (TMP117_VALUE_CC_500_MS);
411 }
412 else if (4 >= *samplerate)
413 {
414 *samplerate = 4;
415 err_code |= tmp117_samplerate_set (TMP117_VALUE_CC_250_MS);
416 }
417 else if (8 >= *samplerate)
418 {
419 *samplerate = 8;
420 err_code |= tmp117_samplerate_set (TMP117_VALUE_CC_125_MS);
421 }
422 else if (64 >= *samplerate ||
423 RD_SENSOR_CFG_MAX == *samplerate)
424 {
425 *samplerate = 64;
426 err_code |= tmp117_samplerate_set (TMP117_VALUE_CC_16_MS);
427 }
428 else if (RD_SENSOR_CFG_CUSTOM_1 == *samplerate)
429 {
430 err_code |= tmp117_samplerate_set (TMP117_VALUE_CC_4000_MS);
431 }
432 else if (RD_SENSOR_CFG_CUSTOM_2 == *samplerate)
433 {
434 err_code |= tmp117_samplerate_set (TMP117_VALUE_CC_8000_MS);
435 }
436 else if (RD_SENSOR_CFG_CUSTOM_3 == *samplerate ||
437 RD_SENSOR_CFG_MIN == *samplerate)
438 {
439 *samplerate = RD_SENSOR_CFG_CUSTOM_3;
440 err_code |= tmp117_samplerate_set (TMP117_VALUE_CC_16000_MS);
441 }
442 else
443 {
444 err_code |= RD_ERROR_NOT_SUPPORTED;
445 }
446
447 return err_code;
448}
449
451{
452 rd_status_t err_code = RD_SUCCESS;
453 uint16_t reg_val = 0;
454
455 if (NULL == samplerate)
456 {
457 err_code |= RD_ERROR_NULL;
458 }
459 else
460 {
461 err_code |= ri_i2c_tmp117_read (m_address, TMP117_REG_CONFIGURATION,
462 &reg_val);
463 reg_val &= TMP117_MASK_CC;
464
465 switch (reg_val)
466 {
468 *samplerate = 64;
469 break;
470
472 *samplerate = 8;
473 break;
474
476 *samplerate = 4;
477 break;
478
480 *samplerate = 2;
481 break;
482
484 *samplerate = 1;
485 break;
486
488 *samplerate = RD_SENSOR_CFG_CUSTOM_1;
489 break;
490
492 *samplerate = RD_SENSOR_CFG_CUSTOM_2;
493 break;
494
496 *samplerate = RD_SENSOR_CFG_CUSTOM_3;
497 break;
498
499 default:
500 err_code |= RD_ERROR_INTERNAL;
501 }
502 }
503
504 return err_code;
505}
506
508{
509 rd_status_t err_code = RD_SUCCESS;
510
511 if (NULL == resolution)
512 {
513 err_code |= RD_ERROR_NULL;
514 }
515 else if (m_continuous)
516 {
517 err_code |= RD_ERROR_INVALID_STATE;
518 }
519 else if (! param_is_valid (*resolution))
520 {
521 err_code |= RD_ERROR_NOT_SUPPORTED;
522 }
523 else
524 {
525 *resolution = RD_SENSOR_CFG_DEFAULT;
526 }
527
528 return err_code;
529}
530
532{
533 rd_status_t err_code = RD_SUCCESS;
534
535 if (NULL == resolution)
536 {
537 err_code |= RD_ERROR_NULL;
538 }
539 else
540 {
541 *resolution = RD_SENSOR_CFG_DEFAULT;
542 }
543
544 return err_code;
545}
546
548{
549 rd_status_t err_code = RD_SUCCESS;
550
551 if (NULL == scale)
552 {
553 err_code |= RD_ERROR_NULL;
554 }
555 else if (m_continuous)
556 {
557 err_code |= RD_ERROR_INVALID_STATE;
558 }
559 else if (!param_is_valid (*scale))
560 {
561 err_code |= RD_ERROR_NOT_SUPPORTED;
562 }
563 else
564 {
565 *scale = RD_SENSOR_CFG_DEFAULT;
566 }
567
568 return err_code;
569}
570
572{
573 rd_status_t err_code = RD_SUCCESS;
574
575 if (NULL == scale)
576 {
577 err_code |= RD_ERROR_NULL;
578 }
579 else
580 {
581 *scale = RD_SENSOR_CFG_DEFAULT;
582 }
583
584 return err_code;
585}
586
587rd_status_t ri_tmp117_dsp_set (uint8_t * dsp, uint8_t * parameter)
588{
589 rd_status_t err_code = RD_SUCCESS;
590
591 if ( (NULL == dsp) || (NULL == parameter))
592 {
593 err_code |= RD_ERROR_NULL;
594 }
595 else if (m_continuous)
596 {
597 err_code |= RD_ERROR_INVALID_STATE;
598 }
599 else
600 {
601 if (RD_SENSOR_CFG_NO_CHANGE == * dsp)
602 {
603 err_code |= ri_tmp117_dsp_get (dsp, parameter);
604 }
605 else if ( (RD_SENSOR_DSP_LAST == *dsp)
606 || (RD_SENSOR_CFG_DEFAULT == *dsp))
607 {
608 err_code |= tmp117_oversampling_set (TMP117_VALUE_OS_1);
609 *parameter = 1;
610 }
611 else if (RD_SENSOR_DSP_OS == *dsp)
612 {
613 if (1 >= *parameter)
614 {
615 *parameter = 1;
616 err_code |= tmp117_oversampling_set (TMP117_VALUE_OS_1);
617 }
618 else if (8 >= *parameter)
619 {
620 *parameter = 8;
621 err_code |= tmp117_oversampling_set (TMP117_VALUE_OS_8);
622 }
623 else if (32 >= *parameter)
624 {
625 *parameter = 32;
626 err_code |= tmp117_oversampling_set (TMP117_VALUE_OS_32);
627 }
628 else if (64 >= *parameter)
629 {
630 *parameter = 64;
631 err_code |= tmp117_oversampling_set (TMP117_VALUE_OS_64);
632 }
633 else if (RD_SENSOR_CFG_MIN == *parameter)
634 {
635 *parameter = 8;
636 err_code |= tmp117_oversampling_set (TMP117_VALUE_OS_8);
637 }
638 else if (RD_SENSOR_CFG_MAX == *parameter)
639 {
640 *parameter = 64;
641 err_code |= tmp117_oversampling_set (TMP117_VALUE_OS_64);
642 }
643 else
644 {
645 err_code |= RD_ERROR_NOT_SUPPORTED;
646 }
647 }
648 else
649 {
650 err_code |= RD_ERROR_NOT_SUPPORTED;
651 }
652 }
653
654 return err_code;
655}
656
657rd_status_t ri_tmp117_dsp_get (uint8_t * dsp, uint8_t * parameter)
658{
659 rd_status_t err_code = RD_SUCCESS;
660
661 if ( (NULL == dsp) || (NULL == parameter))
662 {
663 err_code |= RD_ERROR_NULL;
664 }
665 else
666 {
667 uint16_t reg_val = 0;
668 err_code |= ri_i2c_tmp117_read (m_address, TMP117_REG_CONFIGURATION,
669 &reg_val);
670 reg_val &= TMP117_MASK_OS;
671
672 switch (reg_val)
673 {
675 *dsp = RD_SENSOR_DSP_LAST;
676 *parameter = 1;
677 ms_per_sample = TMP117_OS_1_TSAMPLE_MS;
678 break;
679
681 *dsp = RD_SENSOR_DSP_OS;
682 *parameter = 8;
683 ms_per_sample = TMP117_OS_8_TSAMPLE_MS;
684 break;
685
687 *dsp = RD_SENSOR_DSP_OS;
688 *parameter = 32;
689 ms_per_sample = TMP117_OS_32_TSAMPLE_MS;
690 break;
691
693 *dsp = RD_SENSOR_DSP_OS;
694 *parameter = 64;
695 ms_per_sample = TMP117_OS_64_TSAMPLE_MS;
696 break;
697
698 default:
699 err_code |= RD_ERROR_INTERNAL;
700 }
701 }
702
703 return err_code;
704}
705
706static rd_status_t tmp117_poll_drdy (bool * const drdy)
707{
708 rd_status_t err_code = RD_SUCCESS;
709 uint16_t cfg = 0;
710 err_code |= ri_i2c_tmp117_read (m_address, TMP117_REG_CONFIGURATION, &cfg);
711 cfg &= TMP117_MASK_DRDY;
712 *drdy = (cfg != 0);
713 return err_code;
714}
715
716static rd_status_t tmp117_wait_for_sample (const uint16_t initial_delay_ms)
717{
718 rd_status_t err_code = RD_SUCCESS;
719 bool drdy = false;
720 uint8_t retries = 0;
721 ri_delay_ms (initial_delay_ms);
722
723 while ( (RD_SUCCESS == err_code) && (!drdy) && (retries <= TMP117_CC_RETRIES_MAX))
724 {
725 err_code |= tmp117_poll_drdy (&drdy);
726
727 if (!drdy)
728 {
730 retries++;
731 }
732 }
733
734 if (retries > TMP117_CC_RETRIES_MAX)
735 {
736 err_code |= RD_ERROR_TIMEOUT;
737 }
738
739 return err_code;
740}
741
742static rd_status_t __attribute__ ( (nonnull))
743tmp117_take_single_sample (uint8_t * const mode)
744{
745 rd_status_t err_code = RD_SUCCESS;
746
747 if (m_continuous)
748 {
749 err_code |= RD_ERROR_INVALID_STATE;
751 }
752 else
753 {
754 err_code |= tmp117_sample ();
755
756 if (RD_SUCCESS == err_code)
757 {
758 err_code |= tmp117_wait_for_sample (ms_per_sample);
759 }
760
761 if (RD_SUCCESS == err_code)
762 {
763 err_code |= tmp117_read (&m_temperature);
764 }
765
766 *mode = RD_SENSOR_CFG_SLEEP;
767 }
768
769 return err_code;
770}
771
773{
774 rd_status_t err_code = RD_SUCCESS;
775
776 if (NULL == mode)
777 {
778 err_code |= RD_ERROR_NULL;
779 }
780 else
781 {
782 switch (*mode)
783 {
785 err_code |= tmp117_continuous();
786 m_continuous = true;
787 break;
788
790 err_code |= tmp117_take_single_sample (mode);
791 break;
792
794 err_code |= tmp117_sleep();
795 m_continuous = false;
796 break;
797
798 default:
799 err_code |= RD_ERROR_INVALID_PARAM;
800 break;
801 }
802 }
803
804 return err_code;
805}
806
808{
809 rd_status_t err_code = RD_SUCCESS;
810
811 if (NULL == mode)
812 {
813 err_code |= RD_ERROR_NULL;
814 }
815 else if (m_continuous)
816 {
818 }
819 else
820 {
821 *mode = RD_SENSOR_CFG_SLEEP;
822 }
823
824 return err_code;
825}
826
828{
829 rd_status_t err_code = RD_SUCCESS;
830 rd_sensor_data_fields_t env_fields = {.bitfield = 0};
831 env_fields.datas.temperature_c = 1;
832
833 if (NULL == data)
834 {
835 err_code |= RD_ERROR_NULL;
836 return err_code;
837 }
838
839 if (m_continuous)
840 {
841 err_code |= tmp117_read (&m_temperature);
842 m_timestamp = rd_sensor_timestamp_get();
843 }
844
845 if ( (RD_SUCCESS == err_code) && (RD_UINT64_INVALID != m_timestamp)
846 && !isnan (m_temperature))
847 {
848 rd_sensor_data_set (data,
849 env_fields,
850 m_temperature);
851 data->timestamp_ms = m_timestamp;
852 }
853 else if ( (RD_ERROR_INVALID_STATE & err_code) != 0)
854 {
855 // Handle case where e.g. external sensor has become disconnected after initialization
856 // By returning NOT_VALID as a valid value, signaling application that correct data is not available.
857 rd_sensor_data_set (data,
858 env_fields,
860 data->timestamp_ms = m_timestamp;
861 }
862 else if (RD_SUCCESS == err_code)
863 {
864 err_code |= RD_ERROR_INTERNAL;
865 }
866 else
867 {
868 // No action needed, pass original error code upwards.
869 }
870
871 return err_code;
872}
873
875#endif
#define RD_ERROR_INVALID_PARAM
Invalid Parameter.
#define RD_ERROR_NULL
Null Pointer.
uint32_t rd_status_t
bitfield for representing errors
#define RD_FLOAT_INVALID
Signal that value should not be used.
#define RD_ERROR_NOT_SUPPORTED
Not supported.
#define RD_SUCCESS
Internal Error.
#define RD_ERROR_TIMEOUT
Operation timed out.
#define RD_ERROR_NOT_FOUND
Not found.
#define RD_UINT64_INVALID
Signal that value should not be used.
#define RD_ERROR_INVALID_STATE
Invalid state, operation disallowed in this state.
#define RD_ERROR_INTERNAL
Internal Error.
rd_status_t ri_i2c_tmp117_write(const uint8_t dev_id, const uint8_t reg_addr, const uint16_t reg_val)
I2C write function for TMP117.
rd_status_t ri_i2c_tmp117_read(const uint8_t dev_id, const uint8_t reg_addr, uint16_t *const reg_val)
I2C Read function for TMP117.
#define RD_SENSOR_DSP_LAST
Return last value from sensor. Parameter: No effect. Use default.
bool rd_sensor_is_init(const rd_sensor_t *const sensor)
Check if given sensor structure is already initialized.
#define RD_SENSOR_CFG_SLEEP
Sensor should go to sleep immediately.
#define RD_SENSOR_CFG_MAX
Configure largest supported and implemented value.
#define RD_SENSOR_CFG_DEFAULT
Default value, always valid for the sensor.
void rd_sensor_uninitialize(rd_sensor_t *const p_sensor)
Mark sensor as uninitialized by calling the generic initialization. Will not clear the name of the se...
#define RD_SENSOR_DSP_OS
Oversample sensor values. Parameter: Number of samples.
rd_status_t rd_sensor_configuration_get(const rd_sensor_t *sensor, rd_sensor_configuration_t *config)
Implementation of ref rd_configuration_fp.
void rd_sensor_initialize(rd_sensor_t *const p_sensor)
Initialize sensor struct with non-null pointers which return RD_ERROR_NOT_INITIALIZED.
#define RD_SENSOR_CFG_CUSTOM_3
Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values.
#define RD_SENSOR_CFG_CUSTOM_1
Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values.
rd_status_t rd_sensor_configuration_set(const rd_sensor_t *sensor, rd_sensor_configuration_t *config)
Implementation of ref rd_configuration_fp.
#define RD_SENSOR_CFG_MIN
Configure smallest supported and implemented value.
#define RD_SENSOR_CFG_CONTINUOUS
Sensor will keep sampling at defined sample rate.
#define RD_SENSOR_CFG_NO_CHANGE
Do not change configured value.
rd_bus_t
Type of bus sensor uses.
void rd_sensor_data_set(rd_sensor_data_t *const target, const rd_sensor_data_fields_t field, const float value)
Set a desired value to target data.
#define UNUSED_VARIABLE(X)
#define RD_SENSOR_CFG_CUSTOM_2
Configuration range is 0...200, i.e. 0 ... 0xC8. Use C9 ... CF as sensor-specific values.
uint64_t rd_sensor_timestamp_get(void)
Calls the timestamp function and returns its value.
#define RD_SENSOR_CFG_SINGLE
Sensor should go to sleep after single measurement.
@ RD_BUS_I2C
I2C bus.
#define TMP117_VALUE_ID
#define TMP117_VALUE_CC_500_MS
rd_status_t ri_tmp117_resolution_set(uint8_t *resolution)
rd_sensor_setup_fp
#define TMP117_MASK_CC
#define TMP117_MASK_DRDY
rd_status_t ri_tmp117_dsp_get(uint8_t *dsp, uint8_t *parameter)
rd_sensor_dsp_fp
#define TMP117_OS_8_TSAMPLE_MS
#define TMP117_MASK_ID
rd_status_t ri_tmp117_data_get(rd_sensor_data_t *const data)
rd_sensor_data_fp NOTE: This driver will return NAN as a valid value, blocking automatic passthrough ...
#define TMP117_VALUE_CC_8000_MS
rd_status_t ri_tmp117_resolution_get(uint8_t *resolution)
rd_sensor_setup_fp
#define TMP117_OS_64_TSAMPLE_MS
rd_status_t ri_tmp117_uninit(rd_sensor_t *sensor, rd_bus_t bus, uint8_t handle)
rd_sensor_init_fp
rd_status_t ri_tmp117_scale_get(uint8_t *scale)
rd_sensor_setup_fp
#define TMP117_VALUE_CC_16000_MS
#define TMP117_CC_RESET_DELAY_MS
#define TMP117_VALUE_OS_8
#define TMP117_VALUE_CC_125_MS
rd_status_t ri_tmp117_samplerate_get(uint8_t *samplerate)
rd_sensor_setup_fp
#define TMP117_VALUE_OS_32
rd_status_t ri_tmp117_scale_set(uint8_t *scale)
rd_sensor_setup_fp
rd_status_t ri_tmp117_mode_set(uint8_t *mode)
rd_sensor_setup_fp
#define TMP117_VALUE_CC_16_MS
#define TMP117_MASK_OS
#define TMP117_REG_TEMP_RESULT
#define TMP117_VALUE_CC_250_MS
#define TMP117_OS_1_TSAMPLE_MS
rd_status_t ri_tmp117_dsp_set(uint8_t *dsp, uint8_t *parameter)
rd_sensor_dsp_fp
rd_status_t ri_tmp117_mode_get(uint8_t *mode)
rd_sensor_setup_fp
#define TMP117_REG_DEVICE_ID
#define TMP117_VALUE_OS_1
#define TMP117_VALUE_OS_64
#define TMP117_VALUE_TEMP_NA
#define TMP117_MASK_RESET
#define TMP117_REG_CONFIGURATION
#define TMP117_VALUE_MODE_SINGLE
rd_status_t ri_tmp117_samplerate_set(uint8_t *samplerate)
rd_sensor_setup_fp
#define TMP117_OS_32_TSAMPLE_MS
#define TMP117_VALUE_MODE_CONT
#define TMP117_CC_RETRIES_MAX
#define TMP117_VALUE_MODE_SLEEP
rd_status_t ri_tmp117_init(rd_sensor_t *environmental_sensor, rd_bus_t bus, uint8_t handle)
rd_sensor_init_fp
#define TMP117_VALUE_CC_4000_MS
#define TMP117_CC_RETRY_DELAY_MS
#define TMP117_VALUE_CC_1000_MS
Header to enable and disable module compilation.
Ruuvi error codes and error check function.
Ruuvi sensor interface Lifecycle: Beta
I2C read/write functions for TI TMP117.
rd_status_t ri_delay_ms(uint32_t time)
Delay a given number of milliseconds.
unsigned int temperature_c
Temperature, celcius.
Generic sensor data struct.
uint64_t timestamp_ms
Timestamp of the event, rd_sensor_timestamp_get.
Interface to sensor. Some sensors can implement additional functions. The additional functions are de...
rd_sensor_data_fp data_get
rd_sensor_data_fp
rd_configuration_fp configuration_set
rd_configuration_fp
rd_sensor_setup_fp samplerate_get
rd_sensor_setup_fp
rd_sensor_setup_fp resolution_get
rd_sensor_setup_fp
rd_sensor_init_fp uninit
rd_sensor_init_fp
rd_configuration_fp configuration_get
rd_configuration_fp
rd_sensor_dsp_fp dsp_get
rd_sensor_dsp_fp
rd_sensor_data_fields_t provides
Description of data fields the sensor is able to provide.
rd_sensor_dsp_fp dsp_set
rd_sensor_dsp_fp
rd_sensor_setup_fp mode_set
rd_sensor_setup_fp
const char * name
Sensor human-readable name. Should be at most 8 bytes long.
rd_sensor_setup_fp mode_get
rd_sensor_setup_fp
rd_sensor_setup_fp samplerate_set
rd_sensor_setup_fp
rd_sensor_init_fp init
rd_sensor_init_fp
rd_sensor_setup_fp scale_set
rd_sensor_setup_fp
rd_sensor_setup_fp resolution_set
rd_sensor_setup_fp
rd_sensor_setup_fp scale_get
rd_sensor_setup_fp
Union to access sensor data.
uint32_t bitfield
Bitfield used to access sensor data.
rd_sensor_data_bitfield_t datas
Structured data field.