ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
ruuvi_interface_tmp117.c
Go to the documentation of this file.
2 
3 #if (RI_TMP117_ENABLED || DOXYGEN)
4 
5 #include "ruuvi_driver_error.h"
6 #include "ruuvi_driver_sensor.h"
10 
11 
28 #define TMP117_CC_RETRIES_MAX (5U)
29 #define TMP117_CC_RETRY_DELAY_MS (10U)
30 
31 static uint8_t m_address;
32 static uint16_t ms_per_sample;
33 static uint16_t ms_per_cc;
34 static float m_temperature;
35 static uint64_t m_timestamp;
36 static const char m_sensor_name[] = "TMP117";
37 static bool m_continuous = false;
38 
39 static 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 
47 static 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 
57 static rd_status_t tmp117_validate_id (void)
58 {
59  uint16_t id;
60  rd_status_t err_code = RD_SUCCESS;
61  err_code |= ri_i2c_tmp117_read (m_address, 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 
72 static 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 
86 static 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 
100 static 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 
114 static 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 
128 static 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  {
138  case TMP117_VALUE_OS_1:
139  reg_val |= os_1_set (&reg_val);
140  break;
141 
142  case TMP117_VALUE_OS_8:
143  reg_val |= os_8_set (&reg_val);
144  break;
145 
146  case TMP117_VALUE_OS_32:
147  reg_val |= os_32_set (&reg_val);
148  break;
149 
150  case TMP117_VALUE_OS_64:
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 
163 static rd_status_t
164 tmp117_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 
181 static 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 
232 static 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 
245 static 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 
259 static 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 
272 static 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  if (reg_val > 0x7FFFU)
280  {
281  dec_temperature = (int32_t) reg_val - 0xFFFF;
282  }
283  else
284  {
285  dec_temperature = reg_val;
286  }
287 
288  *temperature = (0.0078125F * dec_temperature);
289 
290  if ( (TMP117_VALUE_TEMP_NA == reg_val) || (RD_SUCCESS != err_code))
291  {
292  *temperature = NAN;
293  }
294 
295  return err_code;
296 }
297 
299 ri_tmp117_init (rd_sensor_t * environmental_sensor, rd_bus_t bus, uint8_t handle)
300 {
301  rd_status_t err_code = RD_SUCCESS;
302 
303  if (NULL == environmental_sensor)
304  {
305  err_code |= RD_ERROR_NULL;
306  }
307  else if (rd_sensor_is_init (environmental_sensor))
308  {
309  err_code |= RD_ERROR_INVALID_STATE;
310  }
311  else
312  {
313  rd_sensor_initialize (environmental_sensor);
314  environmental_sensor->name = m_sensor_name;
315  err_code = RD_SUCCESS;
316  m_address = handle;
317 
318  if (RD_BUS_I2C == bus)
319  {
320  err_code |= tmp117_validate_id();
321  }
322  else
323  {
324  err_code |= RD_ERROR_INVALID_PARAM;
325  }
326 
327  if (RD_SUCCESS == err_code)
328  {
329  err_code |= tmp117_soft_reset();
330  environmental_sensor->init = ri_tmp117_init;
331  environmental_sensor->uninit = ri_tmp117_uninit;
332  environmental_sensor->samplerate_set = ri_tmp117_samplerate_set;
333  environmental_sensor->samplerate_get = ri_tmp117_samplerate_get;
334  environmental_sensor->resolution_set = ri_tmp117_resolution_set;
335  environmental_sensor->resolution_get = ri_tmp117_resolution_get;
336  environmental_sensor->scale_set = ri_tmp117_scale_set;
337  environmental_sensor->scale_get = ri_tmp117_scale_get;
338  environmental_sensor->dsp_set = ri_tmp117_dsp_set;
339  environmental_sensor->dsp_get = ri_tmp117_dsp_get;
340  environmental_sensor->mode_set = ri_tmp117_mode_set;
341  environmental_sensor->mode_get = ri_tmp117_mode_get;
342  environmental_sensor->data_get = ri_tmp117_data_get;
343  environmental_sensor->configuration_set = rd_sensor_configuration_set;
344  environmental_sensor->configuration_get = rd_sensor_configuration_get;
345  environmental_sensor->provides.datas.temperature_c = 1;
346  m_timestamp = RD_UINT64_INVALID;
347  m_temperature = NAN;
348  ms_per_cc = 1000;
349  ms_per_sample = TMP117_OS_8_TSAMPLE_MS;
350  m_continuous = false;
351  }
352  }
353 
354  return err_code;
355 }
356 
357 rd_status_t ri_tmp117_uninit (rd_sensor_t * sensor, rd_bus_t bus, uint8_t handle)
358 {
359  UNUSED_VARIABLE (bus);
360  UNUSED_VARIABLE (handle);
361  rd_status_t err_code = RD_SUCCESS;
362 
363  if (NULL == sensor)
364  {
365  err_code |= RD_ERROR_NULL;
366  }
367  else
368  {
369  tmp117_sleep();
370  rd_sensor_uninitialize (sensor);
371  m_timestamp = RD_UINT64_INVALID;
372  m_temperature = NAN;
373  m_address = 0;
374  m_continuous = false;
375  }
376 
377  return err_code;
378 }
379 
380 
381 rd_status_t ri_tmp117_samplerate_set (uint8_t * samplerate)
382 {
383  rd_status_t err_code = RD_SUCCESS;
384 
385  if (NULL == samplerate)
386  {
387  err_code |= RD_ERROR_NULL;
388  }
389  else if (m_continuous)
390  {
391  err_code |= RD_ERROR_INVALID_STATE;
392  }
393  else if (RD_SENSOR_CFG_NO_CHANGE == *samplerate)
394  {
395  err_code |= ri_tmp117_samplerate_get (samplerate);
396  }
397  else if ( (RD_SENSOR_CFG_DEFAULT == *samplerate)
398  || (1 >= *samplerate))
399  {
400  *samplerate = 1;
401  err_code |= tmp117_samplerate_set (TMP117_VALUE_CC_1000_MS);
402  }
403  else if (2 >= *samplerate)
404  {
405  *samplerate = 2;
406  err_code |= tmp117_samplerate_set (TMP117_VALUE_CC_500_MS);
407  }
408  else if (4 >= *samplerate)
409  {
410  *samplerate = 4;
411  err_code |= tmp117_samplerate_set (TMP117_VALUE_CC_250_MS);
412  }
413  else if (8 >= *samplerate)
414  {
415  *samplerate = 8;
416  err_code |= tmp117_samplerate_set (TMP117_VALUE_CC_125_MS);
417  }
418  else if (64 >= *samplerate ||
419  RD_SENSOR_CFG_MAX == *samplerate)
420  {
421  *samplerate = 64;
422  err_code |= tmp117_samplerate_set (TMP117_VALUE_CC_16_MS);
423  }
424  else if (RD_SENSOR_CFG_CUSTOM_1 == *samplerate)
425  {
426  err_code |= tmp117_samplerate_set (TMP117_VALUE_CC_4000_MS);
427  }
428  else if (RD_SENSOR_CFG_CUSTOM_2 == *samplerate)
429  {
430  err_code |= tmp117_samplerate_set (TMP117_VALUE_CC_8000_MS);
431  }
432  else if (RD_SENSOR_CFG_CUSTOM_3 == *samplerate ||
433  RD_SENSOR_CFG_MIN == *samplerate)
434  {
435  *samplerate = RD_SENSOR_CFG_CUSTOM_3;
436  err_code |= tmp117_samplerate_set (TMP117_VALUE_CC_16000_MS);
437  }
438  else
439  {
440  err_code |= RD_ERROR_NOT_SUPPORTED;
441  }
442 
443  return err_code;
444 }
445 
446 rd_status_t ri_tmp117_samplerate_get (uint8_t * samplerate)
447 {
448  rd_status_t err_code = RD_SUCCESS;
449  uint16_t reg_val = 0;
450 
451  if (NULL == samplerate)
452  {
453  err_code |= RD_ERROR_NULL;
454  }
455  else
456  {
457  err_code |= ri_i2c_tmp117_read (m_address, TMP117_REG_CONFIGURATION,
458  &reg_val);
459  reg_val &= TMP117_MASK_CC;
460 
461  switch (reg_val)
462  {
464  *samplerate = 64;
465  break;
466 
468  *samplerate = 8;
469  break;
470 
472  *samplerate = 4;
473  break;
474 
476  *samplerate = 2;
477  break;
478 
480  *samplerate = 1;
481  break;
482 
484  *samplerate = RD_SENSOR_CFG_CUSTOM_1;
485  break;
486 
488  *samplerate = RD_SENSOR_CFG_CUSTOM_2;
489  break;
490 
492  *samplerate = RD_SENSOR_CFG_CUSTOM_3;
493  break;
494 
495  default:
496  err_code |= RD_ERROR_INTERNAL;
497  }
498  }
499 
500  return err_code;
501 }
502 
503 rd_status_t ri_tmp117_resolution_set (uint8_t * resolution)
504 {
505  rd_status_t err_code = RD_SUCCESS;
506 
507  if (NULL == resolution)
508  {
509  err_code |= RD_ERROR_NULL;
510  }
511  else if (m_continuous)
512  {
513  err_code |= RD_ERROR_INVALID_STATE;
514  }
515  else if (! param_is_valid (*resolution))
516  {
517  err_code |= RD_ERROR_NOT_SUPPORTED;
518  }
519  else
520  {
521  *resolution = RD_SENSOR_CFG_DEFAULT;
522  }
523 
524  return err_code;
525 }
526 
527 rd_status_t ri_tmp117_resolution_get (uint8_t * resolution)
528 {
529  rd_status_t err_code = RD_SUCCESS;
530 
531  if (NULL == resolution)
532  {
533  err_code |= RD_ERROR_NULL;
534  }
535  else
536  {
537  *resolution = RD_SENSOR_CFG_DEFAULT;
538  }
539 
540  return err_code;
541 }
542 
544 {
545  rd_status_t err_code = RD_SUCCESS;
546 
547  if (NULL == scale)
548  {
549  err_code |= RD_ERROR_NULL;
550  }
551  else if (m_continuous)
552  {
553  err_code |= RD_ERROR_INVALID_STATE;
554  }
555  else if (!param_is_valid (*scale))
556  {
557  err_code |= RD_ERROR_NOT_SUPPORTED;
558  }
559  else
560  {
561  *scale = RD_SENSOR_CFG_DEFAULT;
562  }
563 
564  return err_code;
565 }
566 
568 {
569  rd_status_t err_code = RD_SUCCESS;
570 
571  if (NULL == scale)
572  {
573  err_code |= RD_ERROR_NULL;
574  }
575  else
576  {
577  *scale = RD_SENSOR_CFG_DEFAULT;
578  }
579 
580  return err_code;
581 }
582 
583 rd_status_t ri_tmp117_dsp_set (uint8_t * dsp, uint8_t * parameter)
584 {
585  rd_status_t err_code = RD_SUCCESS;
586 
587  if ( (NULL == dsp) || (NULL == parameter))
588  {
589  err_code |= RD_ERROR_NULL;
590  }
591  else if (m_continuous)
592  {
593  err_code |= RD_ERROR_INVALID_STATE;
594  }
595  else
596  {
597  if (RD_SENSOR_CFG_NO_CHANGE == * dsp)
598  {
599  err_code |= ri_tmp117_dsp_get (dsp, parameter);
600  }
601  else if ( (RD_SENSOR_DSP_LAST == *dsp)
602  || (RD_SENSOR_CFG_DEFAULT == *dsp))
603  {
604  err_code |= tmp117_oversampling_set (TMP117_VALUE_OS_1);
605  *parameter = 1;
606  }
607  else if (RD_SENSOR_DSP_OS == *dsp)
608  {
609  if (1 >= *parameter)
610  {
611  *parameter = 1;
612  err_code |= tmp117_oversampling_set (TMP117_VALUE_OS_1);
613  }
614  else if (8 >= *parameter)
615  {
616  *parameter = 8;
617  err_code |= tmp117_oversampling_set (TMP117_VALUE_OS_8);
618  }
619  else if (32 >= *parameter)
620  {
621  *parameter = 32;
622  err_code |= tmp117_oversampling_set (TMP117_VALUE_OS_32);
623  }
624  else if (64 >= *parameter)
625  {
626  *parameter = 64;
627  err_code |= tmp117_oversampling_set (TMP117_VALUE_OS_64);
628  }
629  else if (RD_SENSOR_CFG_MIN == *parameter)
630  {
631  *parameter = 8;
632  err_code |= tmp117_oversampling_set (TMP117_VALUE_OS_8);
633  }
634  else if (RD_SENSOR_CFG_MAX == *parameter)
635  {
636  *parameter = 64;
637  err_code |= tmp117_oversampling_set (TMP117_VALUE_OS_64);
638  }
639  else
640  {
641  err_code |= RD_ERROR_NOT_SUPPORTED;
642  }
643  }
644  else
645  {
646  err_code |= RD_ERROR_NOT_SUPPORTED;
647  }
648  }
649 
650  return err_code;
651 }
652 
653 rd_status_t ri_tmp117_dsp_get (uint8_t * dsp, uint8_t * parameter)
654 {
655  rd_status_t err_code = RD_SUCCESS;
656 
657  if ( (NULL == dsp) || (NULL == parameter))
658  {
659  err_code |= RD_ERROR_NULL;
660  }
661  else
662  {
663  uint16_t reg_val = 0;
664  err_code |= ri_i2c_tmp117_read (m_address, TMP117_REG_CONFIGURATION,
665  &reg_val);
666  reg_val &= TMP117_MASK_OS;
667 
668  switch (reg_val)
669  {
670  case TMP117_VALUE_OS_1:
671  *dsp = RD_SENSOR_DSP_LAST;
672  *parameter = 1;
673  ms_per_sample = TMP117_OS_1_TSAMPLE_MS;
674  break;
675 
676  case TMP117_VALUE_OS_8:
677  *dsp = RD_SENSOR_DSP_OS;
678  *parameter = 8;
679  ms_per_sample = TMP117_OS_8_TSAMPLE_MS;
680  break;
681 
682  case TMP117_VALUE_OS_32:
683  *dsp = RD_SENSOR_DSP_OS;
684  *parameter = 32;
685  ms_per_sample = TMP117_OS_32_TSAMPLE_MS;
686  break;
687 
688  case TMP117_VALUE_OS_64:
689  *dsp = RD_SENSOR_DSP_OS;
690  *parameter = 64;
691  ms_per_sample = TMP117_OS_64_TSAMPLE_MS;
692  break;
693 
694  default:
695  err_code |= RD_ERROR_INTERNAL;
696  }
697  }
698 
699  return err_code;
700 }
701 
702 static rd_status_t tmp117_poll_drdy (bool * const drdy)
703 {
704  rd_status_t err_code = RD_SUCCESS;
705  uint16_t cfg = 0;
706  err_code |= ri_i2c_tmp117_read (m_address, TMP117_REG_CONFIGURATION, &cfg);
707  cfg &= TMP117_MASK_DRDY;
708  *drdy = (cfg != 0);
709  return err_code;
710 }
711 
712 static rd_status_t tmp117_wait_for_sample (const uint16_t initial_delay_ms)
713 {
714  rd_status_t err_code = RD_SUCCESS;
715  bool drdy = false;
716  uint8_t retries = 0;
717  ri_delay_ms (initial_delay_ms);
718 
719  while ( (RD_SUCCESS == err_code) && (!drdy) && (retries <= TMP117_CC_RETRIES_MAX))
720  {
721  err_code |= tmp117_poll_drdy (&drdy);
722 
723  if (!drdy)
724  {
726  retries++;
727  }
728  }
729 
730  if (retries > TMP117_CC_RETRIES_MAX)
731  {
732  err_code |= RD_ERROR_TIMEOUT;
733  }
734 
735  return err_code;
736 }
737 
738 static rd_status_t __attribute__ ( (nonnull))
739 tmp117_take_single_sample (uint8_t * const mode)
740 {
741  rd_status_t err_code = RD_SUCCESS;
742 
743  if (m_continuous)
744  {
745  err_code |= RD_ERROR_INVALID_STATE;
746  *mode = RD_SENSOR_CFG_CONTINUOUS;
747  }
748  else
749  {
750  err_code |= tmp117_sample ();
751 
752  if (RD_SUCCESS == err_code)
753  {
754  err_code |= tmp117_wait_for_sample (ms_per_sample);
755  }
756 
757  if (RD_SUCCESS == err_code)
758  {
759  err_code |= tmp117_read (&m_temperature);
760  }
761 
762  *mode = RD_SENSOR_CFG_SLEEP;
763  }
764 
765  return err_code;
766 }
767 
769 {
770  rd_status_t err_code = RD_SUCCESS;
771 
772  if (NULL == mode)
773  {
774  err_code |= RD_ERROR_NULL;
775  }
776  else
777  {
778  switch (*mode)
779  {
781  err_code |= tmp117_continuous();
782  m_continuous = true;
783  break;
784 
786  err_code |= tmp117_take_single_sample (mode);
787  break;
788 
789  case RD_SENSOR_CFG_SLEEP:
790  err_code |= tmp117_sleep();
791  m_continuous = false;
792  break;
793 
794  default:
795  err_code |= RD_ERROR_INVALID_PARAM;
796  break;
797  }
798  }
799 
800  return err_code;
801 }
802 
804 {
805  rd_status_t err_code = RD_SUCCESS;
806 
807  if (NULL == mode)
808  {
809  err_code |= RD_ERROR_NULL;
810  }
811  else if (m_continuous)
812  {
813  *mode = RD_SENSOR_CFG_CONTINUOUS;
814  }
815  else
816  {
817  *mode = RD_SENSOR_CFG_SLEEP;
818  }
819 
820  return err_code;
821 }
822 
824 {
825  rd_status_t err_code = RD_SUCCESS;
826 
827  if (NULL == data)
828  {
829  err_code |= RD_ERROR_NULL;
830  }
831 
832  if (m_continuous)
833  {
834  err_code |= tmp117_read (&m_temperature);
835  m_timestamp = rd_sensor_timestamp_get();
836  }
837 
838  if ( (RD_SUCCESS == err_code) && (RD_UINT64_INVALID != m_timestamp)
839  && !isnan (m_temperature))
840  {
841  rd_sensor_data_fields_t env_fields = {.bitfield = 0};
842  env_fields.datas.temperature_c = 1;
843  rd_sensor_data_set (data,
844  env_fields,
845  m_temperature);
846  data->timestamp_ms = m_timestamp;
847  }
848 
849  return err_code;
850 }
851 
853 #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_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
#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_MASK_MODE
#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.