MLX90632 Driver Library  a258525c
Universal MCU library for the 90632
mlx90632.h
Go to the documentation of this file.
1 
37 #ifndef _MLX90632_LIB_
38 #define _MLX90632_LIB_
39 
40 /* Including CRC calculation functions */
41 #include <errno.h>
42 #include "mlx90632_extended_meas.h"
43 
44 /* Solve errno not defined values */
45 #ifndef ETIMEDOUT
46 #define ETIMEDOUT 110
47 #endif
48 #ifndef EINVAL
49 #define EINVAL 22
50 #endif
51 #ifndef EPROTONOSUPPORT
52 #define EPROTONOSUPPORT 93
53 #endif
54 #ifndef ERANGE
55 #define ERANGE 34
56 #endif
57 #ifndef ENOKEY
58 #define ENOKEY 126
59 #endif
60 
61 
62 /* BIT, GENMASK and ARRAY_SIZE macros are imported from kernel */
63 #ifndef BIT
64 #define BIT(x)(1UL << (x))
65 #endif
66 #ifndef GENMASK
67 #ifndef BITS_PER_LONG
68 #warning "Using default BITS_PER_LONG value"
69 #define BITS_PER_LONG 64
70 #endif
71 #define GENMASK(h, l) \
72  (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
73 #endif
74 #ifndef ARRAY_SIZE
75 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
76 #endif
77 
78 /* Memory sections addresses */
79 #define MLX90632_ADDR_RAM 0x4000
80 #define MLX90632_ADDR_EEPROM 0x2480
82 /* EEPROM addresses - used at startup */
83 #define MLX90632_EE_CTRL 0x24d4
84 #define MLX90632_EE_CONTROL MLX90632_EE_CTRL
86 #define MLX90632_EE_I2C_ADDRESS 0x24d5
87 #define MLX90632_EE_VERSION 0x240b
89 #define MLX90632_EE_P_R 0x240c
90 #define MLX90632_EE_P_G 0x240e
91 #define MLX90632_EE_P_T 0x2410
92 #define MLX90632_EE_P_O 0x2412
93 #define MLX90632_EE_Aa 0x2414
94 #define MLX90632_EE_Ab 0x2416
95 #define MLX90632_EE_Ba 0x2418
96 #define MLX90632_EE_Bb 0x241a
97 #define MLX90632_EE_Ca 0x241c
98 #define MLX90632_EE_Cb 0x241e
99 #define MLX90632_EE_Da 0x2420
100 #define MLX90632_EE_Db 0x2422
101 #define MLX90632_EE_Ea 0x2424
102 #define MLX90632_EE_Eb 0x2426
103 #define MLX90632_EE_Fa 0x2428
104 #define MLX90632_EE_Fb 0x242a
105 #define MLX90632_EE_Ga 0x242c
106 #define MLX90632_EE_Gb 0x242e
107 #define MLX90632_EE_Ka 0x242f
108 #define MLX90632_EE_Ha 0x2481
109 #define MLX90632_EE_Hb 0x2482
111 #define MLX90632_EE_MEDICAL_MEAS1 0x24E1
112 #define MLX90632_EE_MEDICAL_MEAS2 0x24E2
113 #define MLX90632_EE_EXTENDED_MEAS1 0x24F1
114 #define MLX90632_EE_EXTENDED_MEAS2 0x24F2
115 #define MLX90632_EE_EXTENDED_MEAS3 0x24F3
117 /* Refresh Rate */
118 typedef enum mlx90632_meas_e {
129 #define MLX90632_EE_REFRESH_RATE_START 10
130 #define MLX90632_EE_REFRESH_RATE_SHIFT 8
131 #define MLX90632_EE_REFRESH_RATE_MASK GENMASK(MLX90632_EE_REFRESH_RATE_START, MLX90632_EE_REFRESH_RATE_SHIFT)
132 #define MLX90632_EE_REFRESH_RATE(ee_val) (ee_val & MLX90632_EE_REFRESH_RATE_MASK)
133 #define MLX90632_REFRESH_RATE(ee_val) (MLX90632_EE_REFRESH_RATE(ee_val) >> MLX90632_EE_REFRESH_RATE_SHIFT)
134 #define MLX90632_REFRESH_RATE_STATUS(mlx90632_meas) (mlx90632_meas << MLX90632_EE_REFRESH_RATE_SHIFT)
136 /* Register addresses - volatile */
137 #define MLX90632_REG_I2C_ADDR 0x3000
139 /* Control register address - volatile */
140 #define MLX90632_REG_CTRL 0x3001
141 #define MLX90632_CFG_SOC_SHIFT 3
142 #define MLX90632_CFG_SOC_MASK BIT(MLX90632_CFG_SOC_SHIFT)
143 #define MLX90632_CFG_PWR_MASK GENMASK(2, 1)
144 #define MLX90632_CFG_PWR(ctrl_val) (ctrl_val & MLX90632_CFG_PWR_MASK)
145 #define MLX90632_CFG_MTYP_SHIFT 4
146 #define MLX90632_CFG_MTYP_MASK GENMASK(8, MLX90632_CFG_MTYP_SHIFT)
147 #define MLX90632_CFG_MTYP(ctrl_val) (ctrl_val & MLX90632_CFG_MTYP_MASK)
148 #define MLX90632_CFG_SOB_SHIFT 11
149 #define MLX90632_CFG_SOB_MASK BIT(MLX90632_CFG_SOB_SHIFT)
150 #define MLX90632_CFG_SOB(ctrl_val) (ctrl_val << MLX90632_CFG_SOB_SHIFT)
151 
152 /* PowerModes statuses */
153 #define MLX90632_PWR_STATUS(ctrl_val) (ctrl_val << 1)
154 #define MLX90632_PWR_STATUS_HALT MLX90632_PWR_STATUS(0)
155 #define MLX90632_PWR_STATUS_SLEEP_STEP MLX90632_PWR_STATUS(1)
156 #define MLX90632_PWR_STATUS_STEP MLX90632_PWR_STATUS(2)
157 #define MLX90632_PWR_STATUS_CONTINUOUS MLX90632_PWR_STATUS(3)
158 /* Measurement type select*/
159 #define MLX90632_MTYP_STATUS(ctrl_val) (ctrl_val << MLX90632_CFG_MTYP_SHIFT)
160 #define MLX90632_MTYP_STATUS_MEDICAL MLX90632_MTYP_STATUS(0)
161 #define MLX90632_MTYP_STATUS_EXTENDED MLX90632_MTYP_STATUS(17)
162 #define MLX90632_MTYP(reg_val) (MLX90632_CFG_MTYP(reg_val) >> MLX90632_CFG_MTYP_SHIFT)
163 /* Start of burst measurement options */
164 #define MLX90632_START_BURST_MEAS MLX90632_CFG_SOB(1)
165 #define MLX90632_BURST_MEAS_NOT_PENDING MLX90632_CFG_SOB(0)
166 
167 /* Device status register - volatile */
168 #define MLX90632_REG_STATUS 0x3fff
169 #define MLX90632_STAT_BUSY BIT(10)
170 #define MLX90632_STAT_EE_BUSY BIT(9)
171 #define MLX90632_STAT_BRST BIT(8)
172 #define MLX90632_STAT_CYCLE_POS GENMASK(6, 2)
173 #define MLX90632_STAT_DATA_RDY BIT(0)
175 /* RAM_MEAS address-es for each channel */
176 #define MLX90632_RAM_1(meas_num) (MLX90632_ADDR_RAM + 3 * meas_num)
177 #define MLX90632_RAM_2(meas_num) (MLX90632_ADDR_RAM + 3 * meas_num + 1)
178 #define MLX90632_RAM_3(meas_num) (MLX90632_ADDR_RAM + 3 * meas_num + 2)
179 
180 /* Timings (ms) */
181 #define MLX90632_TIMING_EEPROM 100
183 /* Magic constants */
184 #define MLX90632_DSPv5 0x05 /* EEPROM DSP version */
185 #define MLX90632_EEPROM_VERSION MLX90632_ID_MEDICAL
186 #define MLX90632_EEPROM_WRITE_KEY 0x554C
187 #define MLX90632_RESET_CMD 0x0006
188 #define MLX90632_MAX_MEAS_NUM 31
189 #define MLX90632_EE_SEED 0x3f6d
190 #define MLX90632_REF_12 12.0
191 #define MLX90632_REF_3 12.0
192 #define MLX90632_XTD_RNG_KEY 0x0500
194 /* Measurement types - the MSBit is for software purposes only and has no hardware bit related to it. It indicates continuous '0' or sleeping step burst - '1' measurement mode*/
195 #define MLX90632_MTYP_MEDICAL 0x00
196 #define MLX90632_MTYP_EXTENDED 0x11
197 #define MLX90632_MTYP_MEDICAL_BURST 0x80
198 #define MLX90632_MTYP_EXTENDED_BURST 0x91
199 #define MLX90632_BURST_MEASUREMENT_TYPE(meas_type) (meas_type + 0x80)
201 #define MLX90632_MEASUREMENT_TYPE_STATUS(mtyp_type) (mtyp_type & 0x7F)
202 #define MLX90632_MEASUREMENT_BURST_STATUS(mtyp_type) (mtyp_type & 0x80)
204 #define MLX90632_MEAS_MAX_TIME 2000
205 #define MLX90632_MAX_NUMBER_MESUREMENT_READ_TRIES 100
207 /* Gets a new register value based on the old register value - only writing the value based on the desired bits
208  * Masks the old register and shifts the new value in
209  */
210 #define MLX90632_NEW_REG_VALUE(old_reg, new_value, h, l) \
211  ((old_reg & (0xFFFF ^ GENMASK(h, l))) | (new_value << MLX90632_EE_REFRESH_RATE_SHIFT))
212 
229 int32_t mlx90632_read_temp_raw(int16_t *ambient_new_raw, int16_t *ambient_old_raw,
230  int16_t *object_new_raw, int16_t *object_old_raw);
231 
248 int32_t mlx90632_read_temp_raw_burst(int16_t *ambient_new_raw, int16_t *ambient_old_raw,
249  int16_t *object_new_raw, int16_t *object_old_raw);
250 
263 double mlx90632_preprocess_temp_ambient(int16_t ambient_new_raw, int16_t ambient_old_raw, int16_t Gb);
264 
281 double mlx90632_preprocess_temp_object(int16_t object_new_raw, int16_t object_old_raw,
282  int16_t ambient_new_raw, int16_t ambient_old_raw,
283  int16_t Ka);
284 
301 double mlx90632_calc_temp_ambient(int16_t ambient_new_raw, int16_t ambient_old_raw, int32_t P_T,
302  int32_t P_R, int32_t P_G, int32_t P_O, int16_t Gb);
303 
324 double mlx90632_calc_temp_object(int32_t object, int32_t ambient,
325  int32_t Ea, int32_t Eb, int32_t Ga, int32_t Fa, int32_t Fb,
326  int16_t Ha, int16_t Hb);
327 
352 double mlx90632_calc_temp_object_reflected(int32_t object, int32_t ambient, double reflected,
353  int32_t Ea, int32_t Eb, int32_t Ga, int32_t Fa, int32_t Fb,
354  int16_t Ha, int16_t Hb);
355 
369 int32_t mlx90632_init(void);
370 
381 
388 void mlx90632_set_emissivity(double value);
389 
392 double mlx90632_get_emissivity(void);
393 
409 
417 int32_t mlx90632_get_measurement_time(uint16_t meas);
418 
428 
439 int32_t mlx90632_addressed_reset(void);
440 
448 
455 
457 
458 #ifdef TEST
459 int32_t mlx90632_read_temp_ambient_raw(int16_t *ambient_new_raw, int16_t *ambient_old_raw);
460 int32_t mlx90632_read_temp_object_raw(int32_t start_measurement_ret,
461  int16_t *object_new_raw, int16_t *object_old_raw);
462 
463 #endif
464 
465 #endif
int32_t mlx90632_set_refresh_rate(mlx90632_meas_t measRate)
Sets the refresh rate of the sensor using the MLX90632_EE_MEAS_1 and MLX90632_EE_MEAS_2 registers...
Definition: mlx90632.c:650
mlx90632_meas_t mlx90632_get_refresh_rate(void)
Gets the value in MLX90632_EE_MEAS_1 and converts it to the appropriate MLX90632_MEAS enum...
Definition: mlx90632.c:681
double mlx90632_preprocess_temp_ambient(int16_t ambient_new_raw, int16_t ambient_old_raw, int16_t Gb)
Calculation of raw ambient output.
Definition: mlx90632.c:242
int mlx90632_start_measurement(void)
Trigger start measurement for mlx90632.
Definition: mlx90632.c:47
int32_t mlx90632_calculate_dataset_ready_time(void)
Reads the refresh rate and calculates the time needed for a whole measurment table from the EEPROM se...
Definition: mlx90632.c:503
double mlx90632_calc_temp_ambient(int16_t ambient_new_raw, int16_t ambient_old_raw, int32_t P_T, int32_t P_R, int32_t P_G, int32_t P_O, int16_t Gb)
Calculation of ambient temperature.
Definition: mlx90632.c:264
int32_t mlx90632_addressed_reset(void)
Trigger system reset for mlx90632.
Definition: mlx90632.c:475
MLX90632 extended range measurement support functions.
double mlx90632_calc_temp_object(int32_t object, int32_t ambient, int32_t Ea, int32_t Eb, int32_t Ga, int32_t Fa, int32_t Fb, int16_t Ha, int16_t Hb)
Calculation of object temperature.
Definition: mlx90632.c:389
int32_t mlx90632_init(void)
Initialize MLX90632 driver and confirm EEPROM version.
Definition: mlx90632.c:441
int32_t mlx90632_get_measurement_time(uint16_t meas)
Reads the refresh rate and calculates the time needed for a single measurment from the EEPROM setting...
Definition: mlx90632.c:488
enum mlx90632_meas_e mlx90632_meas_t
int32_t mlx90632_read_temp_raw(int16_t *ambient_new_raw, int16_t *ambient_old_raw, int16_t *object_new_raw, int16_t *object_old_raw)
Read raw ambient and object temperature.
Definition: mlx90632.c:198
mlx90632_meas_e
Definition: mlx90632.h:118
int32_t mlx90632_start_measurement_burst(void)
Trigger start of burst measurement for mlx90632.
Definition: mlx90632.c:553
double mlx90632_get_emissivity(void)
Read value of emissivity.
Definition: mlx90632.c:377
STATIC int32_t mlx90632_read_temp_ambient_raw(int16_t *ambient_new_raw, int16_t *ambient_old_raw)
Read ambient raw old and new values based on mlx90632_start_measurement return value.
Definition: mlx90632.c:130
double mlx90632_calc_temp_object_reflected(int32_t object, int32_t ambient, double reflected, int32_t Ea, int32_t Eb, int32_t Ga, int32_t Fa, int32_t Fb, int16_t Ha, int16_t Hb)
Calculation of object temperature when the environment temperature differs from the sensor temperatur...
Definition: mlx90632.c:410
double mlx90632_preprocess_temp_object(int16_t object_new_raw, int16_t object_old_raw, int16_t ambient_new_raw, int16_t ambient_old_raw, int16_t Ka)
Calculation of raw object output.
Definition: mlx90632.c:252
void mlx90632_set_emissivity(double value)
Set emissivity which is retained in single variable.
Definition: mlx90632.c:372
int32_t mlx90632_read_temp_raw_burst(int16_t *ambient_new_raw, int16_t *ambient_old_raw, int16_t *object_new_raw, int16_t *object_old_raw)
Read raw ambient and object temperature in sleeping step mode.
Definition: mlx90632.c:219
STATIC int32_t mlx90632_read_temp_object_raw(int32_t start_measurement_ret, int16_t *object_new_raw, int16_t *object_old_raw)
Read object raw old and new values based on mlx90632_start_measurement return value.
Definition: mlx90632.c:162