Blame view

RIOT/drivers/include/lm75a-temp-sensor.h 8 KB
fb11e647   vrobic   reseau statique a...
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
  /*
   * Copyright (C) 2013 Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
   *
   * 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_lm75a LM75A
   * @ingroup     drivers_sensors
   * @brief       Driver for the LM75A digital temperature sensor and thermal watchdog
   *
   * The connection between the MCU and the LM75A is based on the i2c-interface.
   *
   * @{
   *
   * @file
   * @internal
   * @brief       Definitions of the LM75A temperature sensor driver.
   *
   * The connection between the LM75A and the MCU is based on the I2C-interface.
   *
   * @author      Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
   */
  
  #ifndef LM75A_H_
  #define LM75A_H_
  
  #include <stdint.h>
  #include <math.h>
  #include "i2c.h"
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  /**
   * @name LM75A register addresses
   * @{
   */
  #define LM75A_ADDR                      0x48
  #define LM75A_TEMPERATURE_REG           0x0
  #define LM75A_CONFIG_REG                0x1
  #define LM75A_THYST_REG                 0x2
  #define LM75A_OVER_TEMP_REG             0x3
  /** @} */
  
  /**
   * @brief Define the used I2C Interface
   */
  //#define LM75A_I2C_INTERFACE I2C0                // P0.27 SDA0, P0.28 SCL0
  #define LM75A_I2C_INTERFACE             I2C1_0    // P0.0  SDA1, P0.1  SCL1
  //#define LM75A_I2C_INTERFACE I2C1_1              // P0.19 SDA1, P0.20 SCL1
  //#define LM75A_I2C_INTERFACE I2C2                // P0.10 SDA2, P0.11 SCL2
  
  /**
   * @brief LM75A operation modes
   */
  enum OPERATION_MODES {
      LM75A_NORMAL_OPERATION_MODE,
      LM75A_SHUTDOWN_MODE,
      LM75A_COMPARATOR_MODE,
      LM75A_INTERRUPT_MODE
  };
  
  /**
   * @name Common definitions for LMA75A
   * @{
   */
  #define LM75A_BIT0                      0x0
  #define LM75A_BIT1                      0x1
  #define LM75A_BIT2                      0x2
  #define LM75A_BIT3                      0x3
  #define LM75A_BIT4                      0x4
  #define LM75A_BIT5                      0x5
  #define LM75A_BIT6                      0x6
  #define LM75A_BIT7                      0x7
  #define LM75A_BIT8                      0x8
  #define LM75A_BIT9                      0x9
  #define LM75A_BIT10                     0xA
  #define LM75A_BIT15                     0xF
  #define LM75A_MOST_SIG_BYTE_MASK        0xFF00
  #define LM75A_LEAST_SIG_BYTE_MASK       0x00FF
  #define LM75A_DATA_BITS_MASK            0x07FF
  #define LM75A_SIGN_BIT_MASK             (1<<LM75A_BIT10)
  #define LM75A_LSB_MASK                  0x1
  #define LM75A_EXTINT_MODE               0x1
  /** @} */
  
  /**
   * @name LM75A configuration register
   * @{
   */
  #define LM75A_ACTIVE_LOW                0
  #define LM75A_ACTIVE_HIGH               1
  #define LM75A_DEFAULT_CONFIG_VALUE      0
  /** @} */
  
  /**
   * @brief LM75A default values
   */
  enum DEFAULT_VALUES {
      LM75A_DEFAULT_TOS = 80,
      LM75A_DEFAULT_THYST = 75,
      LM75A_DEFAULT_OPERATION = LM75A_NORMAL_OPERATION_MODE,
      LM75A_DEFAULT_MODE = LM75A_COMPARATOR_MODE,
      LM75A_DEFAULT_POLARITY = LM75A_ACTIVE_LOW,
      LM75A_DEFAULT_FAULT_NUM = 1
  };
  
  /**
   * @name define inter-threads messages
   * @{
   */
  #define LM75A_EXIT_MSG                  0
  #define LM75A_SAMPLING_MSG              1
  #define LM75A_SLEEP_MSG                 2
  #define LM75A_WEAKUP_MSG                3
  /** @} */
  
  /**
   * @brief       Set the over-temperature shutdown threshold (TOS).
   *
   * @param[in] tos       the TOS value.
   *
   */
  void lm75A_set_over_temperature(float_t tos);
  
  
  /**
   * @brief       Set the hysteresis temperature.
   *
   * @param[in]  thsyt    the hysteresis value.
   *
   */
  void lm75A_set_hysteresis_temperature(float_t thsyt);
  
  
  /**
   * @brief       Set various operation modes of the temperature sensor.
   *              The LM75A provide four modes: normal, comparator, interrupt,
   *              and the shutdown mode.
   *              All these modes are defined in the lm75a-temp-sensor.h
   *
   * @param[in]  op_mode  the operation mode value: the normal, shutdown,
   *             comparator, or interrupt mode.
   *
   */
  void lm75A_set_operation_mode(uint8_t op_mode);
  
  
  /**
   * @brief       Get the content of the configuration register.
   *
   * @return the configuration register value.
   *
   */
  uint8_t lm75A_get_config_reg(void);
  
  
  /**
   * @brief       Get the adjusted hysteresis temperature.
   *
   * @return the content of the hysteresis register.
   *
   */
  float_t lm75A_get_hysteresis_temperature(void);
  
  
  /**
   * @brief       Get the adjusted over-temperature shutdown threshold (TOS).
   *
   * @return the content of the TOS-register.
   *
   */
  float_t lm75A_get_over_temperature(void);
  
  
  /**
   * @brief       Get the ambient temperature which is measured from the
   *              LM75A sensor.
   *
   * @return the content of the temperature register.
   *
   */
  float_t lm75A_get_ambient_temperature(void);
  
  
  /**
   * @brief       Set the LM75A sensor in the initial state.
   *              The temperature sensor has the following values in this state:
   *              config_register = 0; hyst_register = 75; the tos_reg = 80.
   *
   */
  void lm75A_reset(void);
  
  
  /**
   * @brief       Start a continuous sampling of the temperature values.
   *              This function prints the values of all registers over
   *              the rs232 interface.
   *
   * @param[in] extern_interrupt_task  pointer to an external task handler that
   *                                   is executed, if an external interrupt
   *                                   occurrs left. This is an optional
   *                                   parameter therefore NULL is a legal value.
   */
  void lm75A_start_sensor_sampling(void (*extern_interrupt_task)(void));
  
  
  /**
   * @brief       Register an interrupt handler for the external interrupt.
   *              Only the port0 and port2 are supported.
   *
   * @param[in] port              port number.
   * @param[in] pin_bit_mask      pin number in form of a bit mask: Pin0 --> BIT0,
   *                              Pin1 --> BIT1, Pin2 --> BIT2 = 2^2 = 4
   * @param[in] flags             define if the interrupt is generated on rising
   *                              or falling edge (#GPIOINT_RISING_EDGE,
   *                              #GPIOINT_FALLING_EDGE).
   * @param[in] handler           pointer to an interrupt handler.
   *
   * @return true if the the external interrupt handler is successfully
   *         registered, otherwise false.
   */
  bool lm75A_ext_irq_handler_register(int32_t port, uint32_t pin_bit_mask,
                                      int32_t flags, void *handler);
  
  
  /**
   * @brief       Initialize the LM75A temperature sensor.
   *              The baud rate and the handler for the external interrupt can be
   *              initialized. The external interrupt handler is optional, if no
   *              handler is available, the NULL-value can be entered.
   *              The hysteresis and the over-temperature are displayed before and
   *              after a rest action is performed. After this the LM7A sensor is
   *              set in the interrupt or the comparator mode.
   *
   * @param[in] i2c_interface             the i2c interface, several interfaces
   *                                      can be selected: i2c0, i2c1 and i2c2.
   * @param[in] baud_rate                 the baud rate.
   * @param[in] external_interr_handler   pointer to a handler for the external
   *                                      interrupt.
   *
   * @return true if the I2C interface and the external interrupt handler are
   *         successfully initialized, otherwise false.
   */
  bool lm75A_init(uint8_t i2c_interface, uint32_t baud_rate,
                  void *external_interr_handler);
  
  
  /**
   * @brief       Register the external interrupt handler for the over-temperature
   *              shutdown output.
   *
   * @param[in] handler   pointer to a handler for the external interrupts.
   *
   * @return true if the the external interrupt handler is successfully
   *         registered, otherwise false.
   */
  bool lm75A_external_interrupt_register(void *handler);
  
  
  /**
   * @brief       Alarm the sensor sampling task about an external interrupt.
   *
   * @param[in] b is true if an external interrupt is occurred, otherwise false.
   *
   */
  void lm75A_set_in_alarm(bool b);
  
  #ifdef __cplusplus
  }
  #endif
  
  /** @} */
  #endif /* LM75A_H_ */