2020-01-31 17:39:41 +08:00
|
|
|
/* ST Microelectronics IIS2MDC 3-axis magnetometer sensor
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 STMicroelectronics
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Datasheet:
|
|
|
|
* https://www.st.com/resource/en/datasheet/iis2mdc.pdf
|
|
|
|
*/
|
|
|
|
|
2020-03-27 05:47:47 +08:00
|
|
|
#define DT_DRV_COMPAT st_iis2mdc
|
|
|
|
|
2020-01-31 17:39:41 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <drivers/i2c.h>
|
|
|
|
#include <logging/log.h>
|
|
|
|
|
|
|
|
#include "iis2mdc.h"
|
|
|
|
|
2020-05-07 02:23:07 +08:00
|
|
|
#if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
|
2020-01-31 17:39:41 +08:00
|
|
|
|
|
|
|
#define LOG_LEVEL CONFIG_SENSOR_LOG_LEVEL
|
|
|
|
LOG_MODULE_DECLARE(IIS2MDC);
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
static int iis2mdc_i2c_read(struct device *dev, uint8_t reg_addr,
|
|
|
|
uint8_t *value, uint16_t len)
|
2020-01-31 17:39:41 +08:00
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct iis2mdc_data *data = dev->data;
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct iis2mdc_config *cfg = dev->config;
|
2020-01-31 17:39:41 +08:00
|
|
|
|
|
|
|
return i2c_burst_read(data->bus, cfg->i2c_slv_addr,
|
|
|
|
reg_addr, value, len);
|
|
|
|
}
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
static int iis2mdc_i2c_write(struct device *dev, uint8_t reg_addr,
|
|
|
|
uint8_t *value, uint16_t len)
|
2020-01-31 17:39:41 +08:00
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct iis2mdc_data *data = dev->data;
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct iis2mdc_config *cfg = dev->config;
|
2020-01-31 17:39:41 +08:00
|
|
|
|
|
|
|
return i2c_burst_write(data->bus, cfg->i2c_slv_addr,
|
|
|
|
reg_addr, value, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int iis2mdc_i2c_init(struct device *dev)
|
|
|
|
{
|
2020-05-29 03:23:02 +08:00
|
|
|
struct iis2mdc_data *data = dev->data;
|
2020-01-31 17:39:41 +08:00
|
|
|
|
|
|
|
data->ctx_i2c.read_reg = (stmdev_read_ptr) iis2mdc_i2c_read;
|
|
|
|
data->ctx_i2c.write_reg = (stmdev_write_ptr) iis2mdc_i2c_write;
|
|
|
|
|
|
|
|
data->ctx = &data->ctx_i2c;
|
|
|
|
data->ctx->handle = dev;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-07 02:23:07 +08:00
|
|
|
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
|