1 /* 2 * Copyright (c) 2024 Nordic Semiconductor ASA 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #ifndef SENSOR_H 8 #define SENSOR_H 9 10 #include <zephyr/kernel.h> 11 12 #define SLEEP_TIME_MS 250 13 #define MEASUREMENT_CYCLES 3 14 #define MAX_BURST_READ_SIZE 10 15 16 /* General */ 17 #define CHIP_ID_REGISTER_ADDRESS 0xD0 18 #define VARIANT_ID_REGISTER_ADDRESS 0xF0 19 #define CONF_REGISTER_ADDRESS 0x75 20 #define CTRL_MEAS_REGISTER_ADDRESS 0x74 21 #define CTRL_HUM_REGISTER_ADDRESS 0x72 22 #define RESET_REGISTER_ADDRESS 0xE0 23 #define SLEEP_MODE 0x0 24 #define FORCED_MODE 0x1 25 #define CTRL_MEAS_MODE_BIT_MSK 1 << 1 | 1 26 #define CTRL_MEAS_MODE_BIT_SHIFT 0 27 #define RESET_DEVICE 0xB6 28 #define SENSOR_MEMORY_SIZE_IN_BYTES 255 29 30 /* Calibration coeffcients */ 31 #define TEMP_PAR_T1_REGISTER_ADDRESS_LSB 0xE9 32 #define TEMP_PAR_T1_REGISTER_ADDRESS_MSB 0xEA 33 #define TEMP_PAR_T2_REGISTER_ADDRESS_LSB 0x8A 34 #define TEMP_PAR_T2_REGISTER_ADDRESS_MSB 0x8B 35 #define TEMP_PAR_T3_REGISTER_ADDRESS 0x8C 36 37 #define HUMI_PAR_REGISTERS_START_ADDRESS 0xE1 38 #define HUMI_PAR_REGISTERS_COUNT 8 39 #define HUMI_PAR_H1_LSB_BUF_POSITION 1 40 #define HUMI_PAR_H1_MSB_BUF_POSITION 2 41 #define HUMI_PAR_H2_LSB_BUF_POSITION 1 42 #define HUMI_PAR_H2_MSB_BUF_POSITION 0 43 #define HUMI_PAR_H3_BUF_POSITION 3 44 #define HUMI_PAR_H4_BUF_POSITION 4 45 #define HUMI_PAR_H5_BUF_POSITION 5 46 #define HUMI_PAR_H6_BUF_POSITION 6 47 #define HUMI_PAR_H7_BUF_POSITION 7 48 49 #define HUMI_PAR_H1_REGISTER_ADDRESS_LSB 0xE2 50 #define HUMI_PAR_H1_LSB_BIT_MASK 0xFF 51 #define HUMI_PAR_H1_REGISTER_ADDRESS_MSB 0xE3 52 #define HUMI_PAR_H2_REGISTER_ADDRESS_LSB 0xE2 53 #define HUMI_PAR_H2_REGISTER_ADDRESS_MSB 0xE1 54 #define HUMI_PAR_H3_REGISTER_ADDRESS 0xE4 55 #define HUMI_PAR_H4_REGISTER_ADDRESS 0xE5 56 #define HUMI_PAR_H5_REGISTER_ADDRESS 0xE6 57 #define HUMI_PAR_H6_REGISTER_ADDRESS 0xE7 58 #define HUMI_PAR_H7_REGISTER_ADDRESS 0xE8 59 60 #define PRES_PAR_P1_REGISTER_ADDRESS_LSB 0x8E 61 #define PRES_PAR_P1_REGISTER_ADDRESS_MSB 0x8F 62 #define PRES_PAR_P2_REGISTER_ADDRESS_LSB 0x90 63 #define PRES_PAR_P2_REGISTER_ADDRESS_MSB 0x91 64 #define PRES_PAR_P3_REGISTER_ADDRESS 0x92 65 #define PRES_PAR_P4_REGISTER_ADDRESS_LSB 0x94 66 #define PRES_PAR_P4_REGISTER_ADDRESS_MSB 0x95 67 #define PRES_PAR_P5_REGISTER_ADDRESS_LSB 0x96 68 #define PRES_PAR_P5_REGISTER_ADDRESS_MSB 0x97 69 #define PRES_PAR_P6_REGISTER_ADDRESS 0x99 70 #define PRES_PAR_P7_REGISTER_ADDRESS 0x98 71 #define PRES_PAR_P8_REGISTER_ADDRESS_LSB 0x9C 72 #define PRES_PAR_P8_REGISTER_ADDRESS_MSB 0x9D 73 #define PRES_PAR_P9_REGISTER_ADDRESS_LSB 0x9E 74 #define PRES_PAR_P9_REGISTER_ADDRESS_MSB 0x9F 75 #define PRES_PAR_P10_REGISTER_ADDRESS 0xA0 76 77 /* IIR filter */ 78 #define IIR_FILER_ORDER_BIT_MASK 1 << 4 | 1 << 3 | 1 << 2 79 #define IIR_FILER_ORDER_BIT_SHIFT 2 80 #define IIR_FILER_COEFF_3 0x2 81 82 /* Temperature measurement */ 83 #define TEMPERATURE_OVERSAMPLING_2X 0x2 84 #define TEMP_OVERSAMPLING_BIT_MSK 1 << 7 | 1 << 6 | 1 << 5 85 #define TEMP_OVERSAMPLING_BIT_SHIFT 5 86 87 #define TEMP_ADC_DATA_MSB_0 0x22 88 #define TEMP_ADC_DATA_LSB_0 0x23 89 #define TEMP_ADC_DATA_XLSB_0 0x24 90 91 /* Pressure measurement */ 92 #define PRESSURE_OVERSAMPLING_16X 0x5 93 #define PRES_OVERSAMPLING_BIT_MSK 1 << 4 | 1 << 3 | 1 << 2 94 #define PRES_OVERSAMPLING_BIT_SHIFT 2 95 96 #define PRES_ADC_DATA_MSB_0 0x1F 97 #define PRES_ADC_DATA_LSB_0 0x20 98 #define PRES_ADC_DATA_XLSB_0 0x21 99 100 /* Humidity measurement */ 101 #define HUMIDITY_OVERSAMPLING_1X 0x1 102 #define HUMIDITY_OVERSAMPLING_BIT_MSK 1 << 2 | 1 << 1 | 1 103 #define HUMIDITY_OVERSAMPLING_BIT_SHIFT 0 104 105 #define HUM_ADC_DATA_MSB_0 0x25 106 #define HUM_ADC_DATA_LSB_0 0x26 107 108 /* Measurement status */ 109 #define MEAS_STATUS_0_REG_ADDRESS 0x1D 110 #define MEAS_STATUS_1_REG_ADDRESS 0x2E 111 #define MEAS_STATUS_2_REG_ADDRESS 0x3F 112 #define MEASUREMENT_IN_PROGRESS_BIT_MASK 1 << 5 113 #define MEASUREMENT_NEW_DATA_BIT_MASK 1 << 7 114 115 struct calibration_coeffs { 116 /* Temperature */ 117 uint16_t par_t1; 118 uint16_t par_t2; 119 uint8_t par_t3; 120 121 /* Humidity */ 122 uint16_t par_h1; 123 uint16_t par_h2; 124 uint8_t par_h3; 125 uint8_t par_h4; 126 uint8_t par_h5; 127 uint8_t par_h6; 128 uint8_t par_h7; 129 130 /* Pressure */ 131 uint16_t par_p1; 132 int16_t par_p2; 133 int8_t par_p3; 134 int16_t par_p4; 135 int16_t par_p5; 136 int8_t par_p6; 137 int8_t par_p7; 138 int16_t par_p8; 139 int16_t par_p9; 140 uint8_t par_p10; 141 }; 142 143 /* Calculate the compensated temperature 144 * Note: 't_fine' is required for other measurements 145 */ 146 int32_t calculate_temperature(uint32_t adc_temp, int32_t *t_fine, 147 struct calibration_coeffs *cal_coeffs); 148 149 /* Calculate the compensated pressure 150 * Note: temperature must be calculated first 151 * to obtain the 't_fine' 152 */ 153 uint32_t calculate_pressure(uint32_t pres_adc, int32_t t_fine, 154 struct calibration_coeffs *cal_coeffs); 155 156 /* Calculate the relative humidity 157 * Note: temperature must be calculated first 158 * to obtain the 't_fine' 159 */ 160 uint32_t calculate_humidity(uint16_t hum_adc, int32_t t_fine, 161 struct calibration_coeffs *cal_coeffs); 162 163 #endif 164