42#define CHIP_NAME "TE MS5837"
43#define MANUFACTURER_NAME "TE"
44#define SUPPLY_VOLTAGE_MIN 1.5f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 1.25f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
54#define MS5837_ADDRESS 0xEC
59#define MS5837_CMD_RESET 0x1E
60#define MS5837_CMD_D1 0x40
61#define MS5837_CMD_D2 0x50
62#define MS5837_CMD_ADC_READ 0x00
63#define MS5837_CMD_PROM_READ 0xA0
76static uint8_t a_ms5837_iic_read(
ms5837_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
99static uint8_t a_ms5837_iic_write(
ms5837_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
117static uint8_t a_ms5837_crc4(uint16_t n_prom[8])
123 n_prom[0] = ((n_prom[0]) & 0x0FFF);
125 for (cnt = 0; cnt < 16; cnt++)
129 n_rem ^= (uint16_t)((n_prom[cnt >> 1]) & 0x00FF);
133 n_rem ^= (uint16_t)(n_prom[cnt >> 1] >> 8);
135 for (n_bit = 8; n_bit > 0; n_bit--)
137 if ((n_rem & (0x8000U)) != 0)
139 n_rem = (n_rem << 1) ^ 0x3000;
143 n_rem = (n_rem << 1);
147 n_rem= ((n_rem >> 12) & 0x000F);
149 return (n_rem ^ 0x00);
161static void a_ms5837_calculate_temperature_pressure(
ms5837_handle_t *handle, uint32_t d2_temp,
float *temperature_c,
162 uint32_t d1_press,
float *pressure_mbar)
175 dt = d2_temp - (uint32_t)(handle->
c[4]) * 256;
178 sens = (int64_t)(handle->
c[0]) * 65536 + ((int64_t)(handle->
c[2]) * dt) / 128;
179 off = (int64_t)(handle->
c[1]) * 131072 + ((int64_t)(handle->
c[3]) * dt) / 64;
180 p = (int32_t)((d1_press * sens / 2097152 - off) / 32768);
184 sens = (int64_t)(handle->
c[0]) * 32768 + ((int64_t)(handle->
c[2]) * dt) / 256;
185 off = (int64_t)(handle->
c[1]) * 65536 + ((int64_t)(handle->
c[3]) * dt) / 128;
186 p = (int32_t)((d1_press * sens / 2097152 - off) / 8192);
188 temp = 2000 + (int64_t)(dt) * handle->
c[5] / 8388608;
191 if ((temp / 100) < 20)
193 ti = (int32_t)((11 * (int64_t)(dt) * (int64_t)(dt)) / 34359738368U);
194 offi = (31 * (temp - 2000) * (temp - 2000)) / 8;
195 sensi = (63 * (temp - 2000) * (temp - 2000)) / 32;
200 if ((temp / 100) < 20)
202 ti = (int32_t)((3 * (int64_t)(dt) * (int64_t)(dt)) / 8589934592U);
203 offi = (3 * (temp - 2000) * (temp - 2000)) / 2;
204 sensi = (5 * (temp - 2000) * (temp - 2000)) / 8;
205 if ((temp / 100) < -15)
207 offi = offi + 7 * (temp + 1500) * (temp + 1500);
208 sensi = sensi + 4 * (temp + 1500) * (temp + 1500);
213 ti = (int32_t)(2 * (dt * dt) / 137438953472U);
214 offi = (1 * (temp - 2000) * (temp - 2000)) / 16;
219 sens2 = sens - sensi;
221 *temperature_c = (float)(temp) / 100.0f;
224 p = (int32_t)((((d1_press * sens2) / 2097152 - off2) / 32768));
225 *pressure_mbar = (float)(p) / 100.0f;
229 p = (int32_t)((((d1_press * sens2) / 2097152 - off2) / 8192));
230 *pressure_mbar = (float)(p) / 10.0f;
241static void a_ms5837_calculate_temperature(
ms5837_handle_t *handle, uint32_t d2_temp,
float *temperature_c)
247 dt = d2_temp - (uint32_t)(handle->
c[4]) * 256;
248 temp = 2000 + (int64_t)(dt) * handle->
c[5] / 8388608;
251 if ((temp / 100) < 20)
253 ti = (int32_t)((11 * (int64_t)(dt) * (int64_t)(dt)) / 34359738368U);
258 if ((temp / 100) < 20)
260 ti = (int32_t)((3 * (int64_t)(dt) * (int64_t)(dt)) / 8589934592U);
264 ti = (int32_t)((2 * (dt * dt)) / 137438953472U);
268 *temperature_c = (float)(temp) / 100.0f;
303 handle->
debug_print(
"ms5837: iic_init is null.\n");
309 handle->
debug_print(
"ms5837: iic_deinit is null.\n");
315 handle->
debug_print(
"ms5837: iic_read is null.\n");
321 handle->
debug_print(
"ms5837: iic_write is null.\n");
327 handle->
debug_print(
"ms5837: delay_ms is null.\n");
346 memset(c, 0,
sizeof(uint16_t) * 8);
347 for (i = 0; i < 7; i++)
350 handle->
prom + i * 2, 2) != 0)
352 handle->
debug_print(
"ms5837: read prom failed.\n");
356 c[i] = ((uint16_t)(handle->
prom[i * 2]) << 8) |
357 (uint16_t)(handle->
prom[i * 2 + 1]);
360 crc_check = (c[0] >> 12) & 0xF;
361 type = (c[0] >> 5) & 0x7F;
362 crc = a_ms5837_crc4(c);
363 if (crc != crc_check)
379 else if (type == 0x15)
383 else if (type == 0x1A)
432 handle->
debug_print(
"ms5837: iic deinit failed.\n");
488 handle->
type = (uint8_t)(type);
544 handle->
debug_print(
"ms5837: 30ba26 can't support osr 8192.\n");
605 handle->
debug_print(
"ms5837: 30ba26 can't support osr 8192.\n");
630 uint32_t *pressure_raw,
float *pressure_mbar)
679 *temperature_raw = (((uint32_t)buf[0]) << 16) | (((uint32_t)buf[1]) << 8) | buf[2];
717 *pressure_raw = (((uint32_t)buf[0]) << 16) | (((uint32_t)buf[1]) << 8) | buf[2];
718 a_ms5837_calculate_temperature_pressure(handle, *temperature_raw, temperature_c,
719 *pressure_raw, pressure_mbar);
739 uint32_t temperature_raw;
787 temperature_raw = (((uint32_t)buf[0]) << 16) | (((uint32_t)buf[1]) << 8) | buf[2];
825 *pressure_raw = (((uint32_t)buf[0]) << 16) | (((uint32_t)buf[1]) << 8) | buf[2];
826 a_ms5837_calculate_temperature_pressure(handle, temperature_raw, &temperature_c,
827 *pressure_raw, pressure_mbar);
893 *temperature_raw = (((uint32_t)buf[0]) << 16) | (((uint32_t)buf[1]) << 8) | buf[2];
894 a_ms5837_calculate_temperature(handle, *temperature_raw, temperature_c);
957 res = a_ms5837_iic_write(handle, reg, buf, len);
960 handle->
debug_print(
"ms5837: write register failed.\n");
994 res = a_ms5837_iic_read(handle, reg, buf, len);
997 handle->
debug_print(
"ms5837: read register failed.\n");
#define MS5837_CMD_PROM_READ
#define MS5837_CMD_ADC_READ
#define MS5837_ADDRESS
chip address definition
#define SUPPLY_VOLTAGE_MAX
#define MANUFACTURER_NAME
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define MS5837_CMD_RESET
chip command definition
driver ms5837 header file
ms5837_type_t
ms5837 type enumeration definition
uint8_t ms5837_read_temperature_pressure(ms5837_handle_t *handle, uint32_t *temperature_raw, float *temperature_c, uint32_t *pressure_raw, float *pressure_mbar)
read the temperature and pressure
uint8_t ms5837_deinit(ms5837_handle_t *handle)
close the chip
uint8_t ms5837_get_type(ms5837_handle_t *handle, ms5837_type_t *type)
get the device type
uint8_t ms5837_reset(ms5837_handle_t *handle)
reset the device
uint8_t ms5837_set_pressure_osr(ms5837_handle_t *handle, ms5837_osr_t osr)
set the adc pressure osr
struct ms5837_handle_s ms5837_handle_t
ms5837 handle structure definition
uint8_t ms5837_get_temperature_osr(ms5837_handle_t *handle, ms5837_osr_t *osr)
get the adc temperature osr
uint8_t ms5837_info(ms5837_info_t *info)
get chip's information
uint8_t ms5837_set_type(ms5837_handle_t *handle, ms5837_type_t type)
set the device type
uint8_t ms5837_read_temperature(ms5837_handle_t *handle, uint32_t *temperature_raw, float *temperature_c)
read the temperature
ms5837_osr_t
ms5837 osr enumeration definition
uint8_t ms5837_get_pressure_osr(ms5837_handle_t *handle, ms5837_osr_t *osr)
get the adc pressure osr
uint8_t ms5837_read_pressure(ms5837_handle_t *handle, uint32_t *pressure_raw, float *pressure_mbar)
read the pressure
struct ms5837_info_s ms5837_info_t
ms5837 information structure definition
uint8_t ms5837_init(ms5837_handle_t *handle)
initialize the chip
uint8_t ms5837_set_temperature_osr(ms5837_handle_t *handle, ms5837_osr_t osr)
set the adc temperature osr
uint8_t ms5837_get_reg(ms5837_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
uint8_t ms5837_set_reg(ms5837_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_read)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
float supply_voltage_max_v
char manufacturer_name[32]
float supply_voltage_min_v