Blame view

RIOT/drivers/include/at30tse75x.h 8.23 KB
a752c7ab   elopes   add first test an...
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
  /*
   * Copyright (C) Daniel Krebs
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @defgroup    drivers_at30tse75x AT30TSE75x temperature sensor with EEPROM
   * @ingroup     drivers_sensors
   *
   * The connection between the MCU and the AT30TSE75x is based on the
   * I2C-interface. There are 3 versions of this IC, with either 2/4/8 Kb of
   * EEPROM.
   *
   * @{
   *
   * @file
   * @brief       Driver for the AT30TSE75x temperature sensor with serial EEPROM
   *
   * @author      Daniel Krebs <github@daniel-krebs.net>
   */
  
  #ifndef AT30TSE75X_H
  #define AT30TSE75X_H
  
  #include <stdint.h>
  #include "periph/i2c.h"
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  /**
   * @name    AT30TSE75x I2C addresses
   * @{
   */
  #define AT30TSE75X_TEMP_ADDR            (0x48)
  #define AT30TSE75X_EEPROM_ADDR          (0x50)
  /** @} */
  
  /**
   * @name    AT30TSE75x register addresses
   * @{
   */
  #define AT30TSE75X_REG__TEMPERATURE     (0x00)
  #define AT30TSE75X_REG__CONFIG          (0x01)
  #define AT30TSE75X_REG__LIMIT_LOW       (0x02)
  #define AT30TSE75X_REG__LIMIT_HIGH      (0x03)
  #define AT30TSE75X_REG__NV_CONFIG       (0x11)
  #define AT30TSE75X_REG__NV_LIMIT_LOW    (0x12)
  #define AT30TSE75X_REG__NV_LIMIR_HIGH   (0x13)
  /** @} */
  
  /**
   * @name    AT30TSE75x number formatting
   * @{
   */
  #define AT30TSE75X_INTEGER_MASK         (0x00ff)
  #define AT30TSE75X_INTEGER_SHIFT        (0)
  #define AT30TSE75X_FRACTIONAL_MASK      (0xf000)
  #define AT30TSE75X_FRACTIONAL_SHIFT     (12)
  #define AT30TSE75X_FRACTIONAL_BASE      (0.0625f)
  /** @} */
  
  
  /**
   * @name    AT30TSE75x configuration register
   *
   * Only upper byte can be read/written, so treat as 8-bit register.
   * @{
   */
  #define AT30TSE75X_CONFIG__OS_BIT               (1 << 7)
  #define AT30TSE75X_CONFIG__RESOLUTION_MASK      (0x60)
  #define AT30TSE75X_CONFIG__RESOLUTION_SHIFT     (5)
  #define AT30TSE75X_CONFIG__FTQ_MASK             (0x18)
  #define AT30TSE75X_CONFIG__FTQ_SHIFT            (3)
  #define AT30TSE75X_CONFIG__ALERT_POL_BIT        (1 << 2)
  #define AT30TSE75X_CONFIG__ALARM_MODE_BIT       (1 << 1)
  #define AT30TSE75X_CONFIG__SHUTDOWN_BIT         (1 << 0)
  /** @} */
  
  /**
   * @name    AT30TSE75x commands
   *
   * @{
   */
  #define AT30TSE75X_CMD__SAVE_TO_NVRAM           (0x48)
  #define AT30TSE75X_CMD__RESTORE_FROM_NVRAM      (0xb8)
  #define AT30TSE75X_CMD__GENERAL_CALL_RESET      (0x06)
  #define AT30TSE75X_CMD__GENERAL_CALL_RELATCH    (0x04)
  /** @} */
  
  /**
   * @name    AT30TSE75x constants
   *
   * @{
   */
  #define AT30TSE75X_BUS_FREE_TIME_US             (1U)
  /** @} */
  
  /**
    * @name AT30TSE75x configuration types
    * @{
    */
  
  /**
    * @brief   Temperature resolution
    */
  typedef enum {
      AT30TSE75X_RESOLUTION_9BIT = 0,
      AT30TSE75X_RESOLUTION_10BIT = 1,
      AT30TSE75X_RESOLUTION_11BIT = 2,
      AT30TSE75X_RESOLUTION_12BIT = 3
  } at30tse75x_resolution_t;
  
  /**
    * @brief   Operation mode
    *
    * The device can run in continous or one-shot mode. While in one-shot mode it
    * is effectively shutdown and only wakes up to perform a single measurement.
    * When in comparator or interrupt mode, the device samples contiously the
    * temperature and sets the ALERT pin according to the chosen mode.
    */
  typedef enum {
      AT30TSE75X_MODE_COMPARATOR,
      AT30TSE75X_MODE_INTERRUPT,
      AT30TSE75X_MODE_ONE_SHOT
  } at30tse75x_mode_t;
  
  /**
    * @brief   After how many limit exceeding measurements the ALERT pin is set
    */
  typedef enum {
      AT30TSE75X_ALARM_AFTER_1 = 0,
      AT30TSE75X_ALARM_AFTER_2 = 1,
      AT30TSE75X_ALARM_AFTER_4 = 2,
      AT30TSE75X_ALARM_AFTER_6 = 3
  } at30tse75x_fault_tolerance_t;
  
  /**
    * @brief   Polarity of the ALERT pin
    */
  typedef enum {
      AT30TSE75X_ALARM_ACTIVE_LOW,
      AT30TSE75X_ALARM_ACTIVE_HIGH
  } at30tse75x_alarm_polatity_t;
  
  /** @} */ /* AT30TSE75x configuration types */
  
  /**
    * @brief   Device descriptor for a AT30TSE75x device
    */
  typedef struct {
      i2c_t i2c;          /**< I2C device that sensor is connected to */
      uint8_t addr;       /**< I2C address of this particular sensor */
  } at30tse75x_t;
  
  /**
   * @brief   Initialize a AT30TSE75x device
   *
   * @param[out] dev          device descriptor
   * @param[in] i2c           I2C bus the device is connected to
   * @param[in] speed         I2C speed to use
   * @param[in] addr          I2C address of the device
   *
   * The valid address range is 0x48 - 0x4f depending on the configuration of the
   * address pins A0-A2.
   *
   * @return                   0 on success
   * @return                  -1 on error
   * @return                  -2 on invalid address
   */
  int at30tse75x_init(at30tse75x_t* dev, i2c_t i2c, i2c_speed_t speed, uint8_t addr);
  
  /**
   * @brief   Save configuration register to non-volatile backup register
   *
   * @param[in] dev           device descriptor
   *
   * @return                   0 on success
   * @return                  -1 on error
   */
  int at30tse75x_save_config(const at30tse75x_t* dev);
  
  /**
   * @brief   Restore configuration register from non-volatile backup register
   *
   * @param[in] dev           device descriptor
   *
   * @return                   0 on success
   * @return                  -1 on error
   */
  int at30tse75x_restore_config(const at30tse75x_t* dev);
  
  /**
   * @brief   Get content of configuration register
   *
   * @param[in] dev           device descriptor
   * @param[out] data         buffer where config register will be written to
   *
   * @return                   0 on success
   * @return                  -1 on error
   */
  int at30tse75x_get_config(const at30tse75x_t* dev, uint8_t* data);
  
  /**
   * @brief   Set content of configuration register
   *
   * @param[in] dev           device descriptor
   * @param[in] data          new value for configuration register
   *
   * @return                   0 on success
   * @return                  -1 on error
   */
  int at30tse75x_set_config(const at30tse75x_t* dev, uint8_t data);
  
  /**
   * @brief   Set temperature resolution
   *
   * @param[in] dev           device descriptor
   * @param[in] resolution    temperature resolution
   *
   * @return                   0 on success
   * @return                  -1 on error
   * @return                  -2 on bad user input
   */
  int at30tse75x_set_resolution(const at30tse75x_t* dev, at30tse75x_resolution_t resolution);
  
  /**
   * @brief   Set operation mode
   *
   * @param[in] dev           device descriptor
   * @param[in] mode          operation mode
   *
   * @return                   0 on success
   * @return                  -1 on device error
   * @return                  -2 on bad user input
   */
  int at30tse75x_set_mode(const at30tse75x_t* dev, at30tse75x_mode_t mode);
  
  /**
   * @brief   Set polarity of ALERT pin
   *
   * @param[in] dev           device descriptor
   * @param[in] polarity      polarity of ALERT pin
   *
   * @return                   0 on success
   * @return                  -1 on device error
   * @return                  -2 on bad user input
   */
  int at30tse75x_set_alarm_polarity(const at30tse75x_t* dev, at30tse75x_alarm_polatity_t polarity);
  
  /**
   * @brief   Set tolerance to outlying measurements
   *
   * @param[in] dev           device descriptor
   * @param[in] tolerance     tolerance
   *
   * @return                   0 on success
   * @return                  -1 on device error
   * @return                  -2 on bad user input
   */
  int at30tse75x_set_fault_tolerance(const at30tse75x_t* dev, at30tse75x_fault_tolerance_t tolerance);
  
  /**
   * @brief   Set T_Low limit
   *
   * @param[in] dev           device descriptor
   * @param[in] t_low         lower temperature limit
   *
   * @return                   0 on success
   * @return                  -1 on device error
   * @return                  -2 on bad user input
   */
  int at30tse75x_set_limit_low(const at30tse75x_t* dev, int8_t t_low);
  
  /**
   * @brief   Set T_High limit
   *
   * @param[in] dev           device descriptor
   * @param[in] t_high        upper temperature limit
   *
   * @return                   0 on success
   * @return                  -1 on error
   */
  int at30tse75x_set_limit_high(const at30tse75x_t* dev, int8_t t_high);
  
  /**
   * @brief   Get measured temperature
   *
   * @param[in] dev           device descriptor
   * @param[out] temperature  float buffer where temperature will be written to
   *
   * @return                   0 on success
   * @return                  -1 on error
   */
  int at30tse75x_get_temperature(const at30tse75x_t* dev, float* temperature);
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif /* AT30TSE75X_H */
  /** @} */