|
| 1 | +/* mpr.c - Driver for Honeywell MPR pressure sensor series */ |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2020 Sven Herrmann |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: Apache-2.0 |
| 7 | + */ |
| 8 | + |
| 9 | +#define DT_DRV_COMPAT honeywell_mpr |
| 10 | + |
| 11 | +#include <errno.h> |
| 12 | +#include <kernel.h> |
| 13 | +#include <sys/__assert.h> |
| 14 | +#include <drivers/sensor.h> |
| 15 | +#include <drivers/i2c.h> |
| 16 | +#include <sys/byteorder.h> |
| 17 | +#include <init.h> |
| 18 | +#include <logging/log.h> |
| 19 | +#include "mpr.h" |
| 20 | +#include "mpr_configuration.h" |
| 21 | + |
| 22 | +LOG_MODULE_REGISTER(MPR, CONFIG_SENSOR_LOG_LEVEL); |
| 23 | + |
| 24 | +static int mpr_init(struct device *dev) |
| 25 | +{ |
| 26 | + struct mpr_data *data = dev->driver_data; |
| 27 | + const struct mpr_config *cfg = dev->config->config_info; |
| 28 | + |
| 29 | + data->i2c_master = device_get_binding(cfg->i2c_bus); |
| 30 | + if (!data->i2c_master) { |
| 31 | + LOG_ERR("mpr: i2c master not found: %s", cfg->i2c_bus); |
| 32 | + return -EINVAL; |
| 33 | + } |
| 34 | + return 0; |
| 35 | +} |
| 36 | + |
| 37 | +static int mpr_read_reg(struct device *dev) |
| 38 | +{ |
| 39 | + struct mpr_data *data = dev->driver_data; |
| 40 | + const struct mpr_config *cfg = dev->config->config_info; |
| 41 | + |
| 42 | + u8_t write_buf[] = { MPR_OUTPUT_MEASUREMENT_COMMAND, 0x00, 0x00 }; |
| 43 | + u8_t read_buf[4] = { 0x0 }; |
| 44 | + |
| 45 | + int rc = i2c_write(data->i2c_master, write_buf, sizeof(write_buf), |
| 46 | + cfg->i2c_addr); |
| 47 | + |
| 48 | + if (rc < 0) { |
| 49 | + return rc; |
| 50 | + } |
| 51 | + |
| 52 | + u8_t retries = MPR_REG_READ_MAX_RETRIES; |
| 53 | + |
| 54 | + for (; retries > 0; retries--) { |
| 55 | + k_sleep(K_MSEC(MPR_REG_READ_DATA_CONV_DELAY_MS)); |
| 56 | + |
| 57 | + rc = i2c_read(data->i2c_master, read_buf, sizeof(read_buf), |
| 58 | + cfg->i2c_addr); |
| 59 | + if (rc < 0) { |
| 60 | + return rc; |
| 61 | + } |
| 62 | + |
| 63 | + if (!(*read_buf & MPR_STATUS_MASK_POWER_ON) |
| 64 | + || (*read_buf & MPR_STATUS_MASK_INTEGRITY_TEST_FAILED) |
| 65 | + || (*read_buf & MPR_STATUS_MASK_MATH_SATURATION)) { |
| 66 | + return -EIO; |
| 67 | + } |
| 68 | + |
| 69 | + if (!(*read_buf & MPR_STATUS_MASK_BUSY)) { |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (retries == 0) { |
| 75 | + return -EIO; |
| 76 | + } |
| 77 | + |
| 78 | + data->reg_val = (read_buf[1] << 16) |
| 79 | + | (read_buf[2] << 8) |
| 80 | + | read_buf[3]; |
| 81 | + |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
| 85 | +/* (reg_value - out_min) * (p_max - p_min) |
| 86 | + * pressure = --------------------------------------- + p_min |
| 87 | + * out_max - out_min |
| 88 | + * |
| 89 | + * returns pressure [kPa] * 10^6 |
| 90 | + */ |
| 91 | +static inline void mpr_convert_reg(const u32_t *reg, s64_t *value) |
| 92 | +{ |
| 93 | + *value = (*reg - MPR_OUTPUT_MIN) * (MPR_P_MAX - MPR_P_MIN); |
| 94 | + *value *= MPR_CONVERSION_FACTOR; |
| 95 | + *value /= MPR_OUTPUT_RANGE; |
| 96 | + *value += MPR_P_MIN; |
| 97 | +} |
| 98 | + |
| 99 | +static int mpr_sample_fetch(struct device *dev, enum sensor_channel chan) |
| 100 | +{ |
| 101 | + __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_PRESS); |
| 102 | + |
| 103 | + return mpr_read_reg(dev); |
| 104 | +} |
| 105 | + |
| 106 | +static int mpr_channel_get(struct device *dev, |
| 107 | + enum sensor_channel chan, |
| 108 | + struct sensor_value *val) |
| 109 | +{ |
| 110 | + const struct mpr_data *data = dev->driver_data; |
| 111 | + |
| 112 | + __ASSERT_NO_MSG(chan == SENSOR_CHAN_PRESS); |
| 113 | + |
| 114 | + s64_t value; |
| 115 | + |
| 116 | + mpr_convert_reg(&data->reg_val, &value); |
| 117 | + |
| 118 | + val->val1 = value / 1000000; |
| 119 | + val->val2 = value % 1000000; |
| 120 | + |
| 121 | + return 0; |
| 122 | +} |
| 123 | + |
| 124 | +static const struct sensor_driver_api mpr_api_funcs = { |
| 125 | + .sample_fetch = mpr_sample_fetch, |
| 126 | + .channel_get = mpr_channel_get, |
| 127 | +}; |
| 128 | + |
| 129 | +static struct mpr_data mpr_data; |
| 130 | +static const struct mpr_config mpr_cfg = { |
| 131 | + .i2c_bus = DT_INST_BUS_LABEL(0), |
| 132 | + .i2c_addr = DT_INST_REG_ADDR(0), |
| 133 | +}; |
| 134 | + |
| 135 | +DEVICE_AND_API_INIT(mpr, DT_INST_LABEL(0), mpr_init, &mpr_data, |
| 136 | + &mpr_cfg, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, |
| 137 | + &mpr_api_funcs); |
0 commit comments