1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/drivers/mag3110/mag3110.c

216 lines
5.2 KiB
C
Raw Normal View History

2014-11-27 18:38:45 +01:00
/*
* Copyright (C) 2014 PHYTEC Messtechnik GmbH
2017-04-10 22:16:45 +02:00
* 2017 HAW Hamburg
2014-11-27 18:38:45 +01:00
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*
*/
/**
* @ingroup drivers_mag3110
* @{
*
* @file
* @brief Driver for the Freescale MAG3110 magnetometer.
*
* @author Johann Fischer <j.fischer@phytec.de>
* @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
2017-04-10 22:16:45 +02:00
* @author Sebastian Meiling <s@mlng.net>
2014-11-27 18:38:45 +01:00
*
* @}
*/
#include <assert.h>
2014-11-27 18:38:45 +01:00
#include <stdint.h>
#include <stdbool.h>
2017-04-10 22:16:45 +02:00
#include <string.h>
#include "log.h"
2014-11-27 18:38:45 +01:00
#include "periph/i2c.h"
2017-04-10 22:16:45 +02:00
2014-11-27 18:38:45 +01:00
#include "mag3110.h"
#include "mag3110_reg.h"
2020-10-22 11:34:31 +02:00
#define ENABLE_DEBUG 0
2014-11-27 18:38:45 +01:00
#include "debug.h"
2017-04-10 22:16:45 +02:00
#define BUS (dev->params.i2c)
#define ADDR (dev->params.addr)
2014-11-27 18:38:45 +01:00
2017-04-10 22:16:45 +02:00
int mag3110_init(mag3110_t *dev, const mag3110_params_t *params)
2014-11-27 18:38:45 +01:00
{
uint8_t reg;
2014-11-27 18:38:45 +01:00
2017-04-10 22:16:45 +02:00
assert(dev);
assert(params);
2014-11-27 18:38:45 +01:00
2017-04-10 22:16:45 +02:00
/* write device descriptor */
dev->params = *params;
2014-11-27 18:38:45 +01:00
2017-04-10 22:16:45 +02:00
i2c_acquire(BUS);
/* test device */
2018-06-07 23:44:41 +02:00
i2c_read_regs(BUS, ADDR, MAG3110_WHO_AM_I, &reg, 1, 0);
2017-04-10 22:16:45 +02:00
if (reg != dev->params.type) {
i2c_release(BUS);
LOG_ERROR("mag3110_init: invalid WHO_AM_I value (0x%02x)!\n", (int)reg);
return -MAG3110_ERROR_DEV;
2014-11-27 18:38:45 +01:00
}
/* enable automatic magnetic sensor reset */
reg = MAG3110_CTRL_REG2_AUTO_MRST_EN;
2018-06-07 23:44:41 +02:00
if (i2c_write_regs(BUS, ADDR, MAG3110_CTRL_REG2, &reg, 1, 0) < 0) {
2017-04-10 22:16:45 +02:00
i2c_release(BUS);
LOG_ERROR("mag3110_init: failed to enable auto reset!\n");
return -MAG3110_ERROR_CNF;
}
/* set sample rate */
reg = MAG3110_CTRL_REG1_DROS(dev->params.dros);
2018-06-07 23:44:41 +02:00
if (i2c_write_regs(BUS, ADDR, MAG3110_CTRL_REG1, &reg, 1, 0) < 0) {
2017-04-10 22:16:45 +02:00
i2c_release(BUS);
LOG_ERROR("mag3110_init: failed to set sample rate!\n");
return -MAG3110_ERROR_CNF;
}
/* set device active */
2018-06-07 23:44:41 +02:00
if (i2c_read_regs(BUS, ADDR, MAG3110_CTRL_REG1, &reg, 1, 0) < 0) {
2017-04-10 22:16:45 +02:00
i2c_release(BUS);
LOG_ERROR("mag3110_init: failed to read device state!\n");
return -MAG3110_ERROR_I2C;
2014-11-27 18:38:45 +01:00
}
2017-04-10 22:16:45 +02:00
reg |= MAG3110_CTRL_REG1_AC;
2018-06-07 23:44:41 +02:00
if (i2c_write_regs(BUS, ADDR, MAG3110_CTRL_REG1, &reg, 1, 0) < 0) {
2017-04-10 22:16:45 +02:00
i2c_release(BUS);
LOG_ERROR("mag3110_init: failed to set device active!\n");
return -MAG3110_ERROR_CNF;
}
i2c_release(BUS);
/* write user offsets */
return mag3110_set_user_offset(dev, dev->params.offset[0],
dev->params.offset[1], dev->params.offset[2]);
2014-11-27 18:38:45 +01:00
}
2017-06-20 17:32:45 +02:00
int mag3110_set_user_offset(const mag3110_t *dev, int16_t x, int16_t y, int16_t z)
2014-11-27 18:38:45 +01:00
{
uint8_t buf[6];
2014-11-27 18:38:45 +01:00
2017-04-10 22:16:45 +02:00
assert(dev);
buf[0] = (x >> 8);
buf[1] = x;
buf[2] = (y >> 8);
buf[3] = y;
buf[4] = (z >> 8);
buf[5] = z;
2014-11-27 18:38:45 +01:00
2017-04-10 22:16:45 +02:00
DEBUG("[mag3110] setting user offset to X: %3i, Y: %3i, Z: %3i\n",
(int)x, (int)y, (int)z);
i2c_acquire(BUS);
2018-06-07 23:44:41 +02:00
if (i2c_write_regs(BUS, ADDR, MAG3110_OFF_X_MSB, buf, 6, 0) < 0) {
2017-04-10 22:16:45 +02:00
i2c_release(BUS);
LOG_ERROR("mag3110_set_user_offset: failed to set offsets!\n");
return -MAG3110_ERROR_I2C;
2014-11-27 18:38:45 +01:00
}
2017-04-10 22:16:45 +02:00
i2c_release(BUS);
2014-11-27 18:38:45 +01:00
2017-04-10 22:16:45 +02:00
return MAG3110_OK;
2014-11-27 18:38:45 +01:00
}
2017-06-20 17:32:45 +02:00
int mag3110_set_active(const mag3110_t *dev)
2014-11-27 18:38:45 +01:00
{
uint8_t reg;
2014-11-27 18:38:45 +01:00
2017-04-10 22:16:45 +02:00
assert(dev);
2014-11-27 18:38:45 +01:00
2017-04-10 22:16:45 +02:00
i2c_acquire(BUS);
2018-06-07 23:44:41 +02:00
if (i2c_read_regs(BUS, ADDR, MAG3110_CTRL_REG1, &reg, 1, 0) < 0) {
2017-04-10 22:16:45 +02:00
i2c_release(BUS);
return -MAG3110_ERROR_I2C;
2014-11-27 18:38:45 +01:00
}
reg |= MAG3110_CTRL_REG1_AC;
2018-06-07 23:44:41 +02:00
if (i2c_write_regs(BUS, ADDR, MAG3110_CTRL_REG1, &reg, 1, 0) < 0) {
2017-04-10 22:16:45 +02:00
i2c_release(BUS);
return -MAG3110_ERROR_I2C;
2014-11-27 18:38:45 +01:00
}
2017-04-10 22:16:45 +02:00
i2c_release(BUS);
2014-11-27 18:38:45 +01:00
2017-04-10 22:16:45 +02:00
return MAG3110_OK;
2014-11-27 18:38:45 +01:00
}
2017-06-20 17:32:45 +02:00
int mag3110_set_standby(const mag3110_t *dev)
2014-11-27 18:38:45 +01:00
{
uint8_t reg;
2014-11-27 18:38:45 +01:00
2017-04-10 22:16:45 +02:00
assert(dev);
i2c_acquire(BUS);
2018-06-07 23:44:41 +02:00
if (i2c_read_regs(BUS, ADDR, MAG3110_CTRL_REG1, &reg, 1, 0) < 0) {
2017-04-10 22:16:45 +02:00
i2c_release(BUS);
return -MAG3110_ERROR_I2C;
2014-11-27 18:38:45 +01:00
}
reg &= ~MAG3110_CTRL_REG1_AC;
2018-06-07 23:44:41 +02:00
if (i2c_write_regs(BUS, ADDR, MAG3110_CTRL_REG1, &reg, 1, 0) < 0) {
2017-04-10 22:16:45 +02:00
i2c_release(BUS);
return -MAG3110_ERROR_I2C;
2014-11-27 18:38:45 +01:00
}
2017-04-10 22:16:45 +02:00
i2c_release(BUS);
2014-11-27 18:38:45 +01:00
2017-04-10 22:16:45 +02:00
return MAG3110_OK;
2014-11-27 18:38:45 +01:00
}
2017-06-20 17:32:45 +02:00
int mag3110_is_ready(const mag3110_t *dev)
2014-11-27 18:38:45 +01:00
{
uint8_t reg;
2014-11-27 18:38:45 +01:00
2017-04-10 22:16:45 +02:00
assert(dev);
2014-11-27 18:38:45 +01:00
2017-04-10 22:16:45 +02:00
i2c_acquire(BUS);
2018-06-07 23:44:41 +02:00
if (i2c_read_regs(BUS, ADDR, MAG3110_DR_STATUS, &reg, 1, 0) < 0) {
2017-04-10 22:16:45 +02:00
i2c_release(BUS);
return -MAG3110_ERROR_I2C;
2014-11-27 18:38:45 +01:00
}
2017-04-10 22:16:45 +02:00
i2c_release(BUS);
2014-11-27 18:38:45 +01:00
return (int)(reg & MAG3110_DR_STATUS_ZYXDR);
}
2017-06-20 17:32:45 +02:00
int mag3110_read(const mag3110_t *dev, mag3110_data_t *data)
2014-11-27 18:38:45 +01:00
{
uint8_t buf[7];
2014-11-27 18:38:45 +01:00
2017-04-10 22:16:45 +02:00
assert(dev);
2014-11-27 18:38:45 +01:00
2017-04-10 22:16:45 +02:00
i2c_acquire(BUS);
2018-06-07 23:44:41 +02:00
if (i2c_read_regs(BUS, ADDR, MAG3110_DR_STATUS, buf, 7, 0) < 0) {
2017-04-10 22:16:45 +02:00
i2c_release(BUS);
return -MAG3110_ERROR_I2C;
2014-11-27 18:38:45 +01:00
}
2017-04-10 22:16:45 +02:00
i2c_release(BUS);
/* TODO: implement state handling, if needed?
uint8_t status = buf[0];
*/
data->x = ((int16_t)buf[1] << 8) | buf[2];
data->y = ((int16_t)buf[3] << 8) | buf[4];
data->z = ((int16_t)buf[5] << 8) | buf[6];
2014-11-27 18:38:45 +01:00
2017-04-10 22:16:45 +02:00
return MAG3110_OK;
2014-11-27 18:38:45 +01:00
}
2017-06-20 17:32:45 +02:00
int mag3110_read_dtemp(const mag3110_t *dev, int8_t *dtemp)
2014-11-27 18:38:45 +01:00
{
2017-04-10 22:16:45 +02:00
assert(dev);
2014-11-27 18:38:45 +01:00
2017-04-10 22:16:45 +02:00
i2c_acquire(BUS);
2018-06-07 23:44:41 +02:00
if (i2c_read_regs(BUS, ADDR, MAG3110_DIE_TEMP, dtemp, 1, 0) < 0) {
2017-04-10 22:16:45 +02:00
i2c_release(BUS);
return -MAG3110_ERROR_I2C;
2014-11-27 18:38:45 +01:00
}
2017-04-10 22:16:45 +02:00
i2c_release(BUS);
2014-11-27 18:38:45 +01:00
2017-04-10 22:16:45 +02:00
return MAG3110_OK;
2014-11-27 18:38:45 +01:00
}