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-07-09 15:13:48 +08:00
|
|
|
static int iis2mdc_i2c_read(struct iis2mdc_data *data, uint8_t reg_addr,
|
|
|
|
uint8_t *value, uint16_t len)
|
|
|
|
{ const struct iis2mdc_config *cfg = data->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-07-09 15:13:48 +08:00
|
|
|
static int iis2mdc_i2c_write(struct iis2mdc_data *data, uint8_t reg_addr,
|
|
|
|
uint8_t *value, uint16_t len)
|
2020-01-31 17:39:41 +08:00
|
|
|
{
|
2020-07-09 15:13:48 +08:00
|
|
|
const struct iis2mdc_config *cfg = data->dev->config ;
|
2020-01-31 17:39:41 +08:00
|
|
|
|
|
|
|
return i2c_burst_write(data->bus, cfg->i2c_slv_addr,
|
|
|
|
reg_addr, value, len);
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
int iis2mdc_i2c_init(const struct device *dev)
|
2020-01-31 17:39:41 +08:00
|
|
|
{
|
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;
|
2020-07-09 15:13:48 +08:00
|
|
|
data->ctx->handle = data;
|
2020-01-31 17:39:41 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-07 02:23:07 +08:00
|
|
|
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
|