2019-07-10 13:31:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/sensors/ak09912.c
|
|
|
|
*
|
2021-01-25 10:00:05 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2019-07-10 13:31:36 +02:00
|
|
|
*
|
2021-01-25 10:00:05 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-07-10 13:31:36 +02:00
|
|
|
*
|
2021-01-25 10:00:05 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2019-07-10 13:31:36 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fixedmath.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <arch/types.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/i2c/i2c_master.h>
|
2020-02-01 08:17:32 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2019-07-10 13:31:36 +02:00
|
|
|
#include <nuttx/sensors/ak09912.h>
|
|
|
|
#include <nuttx/wdog.h>
|
|
|
|
#include <nuttx/irq.h>
|
|
|
|
|
|
|
|
#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_AK09912)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define AK09911_DEVID 0x05
|
|
|
|
#define AK09912_DEVID 0x04
|
|
|
|
#define AK09912_ADDR 0x0C
|
|
|
|
#define AK09912_FREQ 400000
|
|
|
|
|
|
|
|
/* REGISTER: WIA
|
|
|
|
* Who I am.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define AK09912_WIA1 0x00
|
|
|
|
#define AK09912_WIA2 0x01
|
|
|
|
|
|
|
|
/* REGISTER: CNTL2
|
|
|
|
* Power mode
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define POWER_MODE_ADDR 0x31
|
|
|
|
|
|
|
|
/* REGISTER: ASAX
|
|
|
|
* Sensitivity values
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define AK09912_ASAX 0x60
|
|
|
|
|
|
|
|
/* REGISTER: CNTL1
|
2020-08-06 19:41:45 +02:00
|
|
|
* Enable or disable temparator measure or enable or disable Noise
|
|
|
|
* suppression filter.
|
2019-07-10 13:31:36 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define AK09912_CTRL1 0x30
|
|
|
|
|
|
|
|
/* REGISTER: HXL
|
|
|
|
* The start address of data registers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define AK09912_HXL 0x11
|
|
|
|
|
|
|
|
/* Polling timeout
|
|
|
|
* The unit is 10 millisecond.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define AK09912_POLLING_TIMEOUT (1) // 10 ms
|
|
|
|
|
|
|
|
/* The parameter for compensating. */
|
|
|
|
|
|
|
|
#define AK09912_SENSITIVITY (128)
|
|
|
|
#define AK09912_SENSITIVITY_DIV (256)
|
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* Noise Suppression Filter */
|
2019-07-10 13:31:36 +02:00
|
|
|
|
|
|
|
#define AK09912_NSF_NONE 0b00
|
|
|
|
#define AK09912_NSF_LOW 0b01
|
|
|
|
#define AK09912_NSF_MIDDLE 0b10
|
|
|
|
#define AK09912_NSF_HIGH 0b11
|
|
|
|
|
|
|
|
/* Power mode */
|
|
|
|
|
|
|
|
#define AKM_POWER_DOWN_MODE 0b0000
|
|
|
|
#define AKM_SINGL_MEAS_MODE 0b00001
|
|
|
|
#define AKM_CONT_MEAS_1 0b00010
|
|
|
|
#define AKM_CONT_MEAS_2 0b00100
|
|
|
|
#define AKM_CONT_MEAS_3 0b00110
|
|
|
|
#define AKM_CONT_MEAS_4 0b01000
|
|
|
|
#define AKM_EXT_TRIG_MODE 0b01010
|
|
|
|
#define AKM_SELF_TEST_MODE 0b10000
|
|
|
|
#define AKM_FUSE_ROM_MODE 0b11111
|
|
|
|
|
|
|
|
/* REGISTER: ST1
|
|
|
|
* DRDY: Data ready bit. 0: not ready, 1: ready
|
|
|
|
* DOR: Data overrun. 0: Not overrun, 1: overrun
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define AK09912_ST1 0x10
|
|
|
|
|
|
|
|
#define SET_BITSLICE(s, v, offset, mask) \
|
|
|
|
do { \
|
|
|
|
s &= mask; \
|
|
|
|
s |= (v << offset) & mask;} while(0)
|
|
|
|
|
|
|
|
#define MERGE_BYTE(low, high) \
|
|
|
|
((low & 0xff) | ((high << 8) & ~0xff))
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Type Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Structure for compensating data. */
|
|
|
|
|
|
|
|
struct sensi_data_s
|
|
|
|
{
|
|
|
|
uint8_t x;
|
|
|
|
uint8_t y;
|
|
|
|
uint8_t z;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Structure for ak09912 device */
|
|
|
|
|
|
|
|
struct ak09912_dev_s
|
|
|
|
{
|
|
|
|
FAR struct i2c_master_s *i2c; /* I2C interface */
|
|
|
|
uint8_t addr; /* I2C address */
|
|
|
|
int freq; /* Frequency <= 3.4MHz */
|
|
|
|
int compensated; /* 0: uncompensated, 1:compensated */
|
|
|
|
struct sensi_data_s asa_data; /* sensitivity data */
|
|
|
|
uint8_t mode; /* power mode */
|
2020-02-23 09:50:23 +01:00
|
|
|
uint8_t nsf; /* noise suppression filter setting */
|
2020-08-04 12:31:31 +02:00
|
|
|
struct wdog_s wd;
|
2019-07-10 13:31:36 +02:00
|
|
|
sem_t wait;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Character driver methods */
|
|
|
|
|
|
|
|
static int ak09912_open(FAR struct file *filep);
|
|
|
|
static int ak09912_close(FAR struct file *filep);
|
|
|
|
static ssize_t ak09912_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t ak09912_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static int ak09912_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_ak09912fops =
|
|
|
|
{
|
|
|
|
ak09912_open, /* open */
|
|
|
|
ak09912_close, /* close */
|
|
|
|
ak09912_read, /* read */
|
|
|
|
ak09912_write, /* write */
|
2022-01-10 15:51:17 +01:00
|
|
|
NULL, /* seek */
|
2019-07-10 13:31:36 +02:00
|
|
|
ak09912_ioctl, /* ioctl */
|
2022-01-10 15:51:17 +01:00
|
|
|
NULL /* poll */
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
, NULL /* unlink */
|
2019-07-10 13:31:36 +02:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ak09912_getreg8
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read from an 8-bit BMP280 register
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static uint8_t ak09912_getreg8(FAR struct ak09912_dev_s *priv,
|
|
|
|
uint8_t regaddr)
|
|
|
|
{
|
|
|
|
struct i2c_msg_s msg[2];
|
|
|
|
uint8_t regval = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
msg[0].frequency = priv->freq;
|
|
|
|
msg[0].addr = priv->addr;
|
|
|
|
msg[0].flags = 0;
|
|
|
|
msg[0].buffer = ®addr;
|
|
|
|
msg[0].length = 1;
|
|
|
|
|
|
|
|
msg[1].frequency = priv->freq;
|
|
|
|
msg[1].addr = priv->addr;
|
|
|
|
msg[1].flags = I2C_M_READ;
|
|
|
|
msg[1].buffer = ®val;
|
|
|
|
msg[1].length = 1;
|
|
|
|
|
|
|
|
ret = I2C_TRANSFER(priv->i2c, msg, 2);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("I2C_TRANSFER failed: %d\n", ret);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return regval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ak09912_putreg8
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write to an 8-bit BMP280 register
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ak09912_putreg8(FAR struct ak09912_dev_s *priv,
|
|
|
|
uint8_t regaddr, uint8_t regval)
|
|
|
|
{
|
|
|
|
struct i2c_msg_s msg[2];
|
|
|
|
int ret;
|
|
|
|
uint8_t txbuffer[2];
|
|
|
|
|
|
|
|
txbuffer[0] = regaddr;
|
|
|
|
txbuffer[1] = regval;
|
|
|
|
|
|
|
|
msg[0].frequency = priv->freq;
|
|
|
|
msg[0].addr = priv->addr;
|
|
|
|
msg[0].flags = 0;
|
|
|
|
msg[0].buffer = txbuffer;
|
|
|
|
msg[0].length = 2;
|
|
|
|
|
|
|
|
ret = I2C_TRANSFER(priv->i2c, msg, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("I2C_TRANSFER failed: %d\n", ret);
|
|
|
|
}
|
2019-12-07 02:07:34 +01:00
|
|
|
|
2019-07-10 13:31:36 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ak09912_getreg
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read cnt bytes from a ak09912 register
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int32_t ak09912_getreg(FAR struct ak09912_dev_s *priv,
|
2019-12-07 02:07:34 +01:00
|
|
|
uint8_t regaddr, FAR uint8_t *buffer,
|
|
|
|
uint32_t cnt)
|
2019-07-10 13:31:36 +02:00
|
|
|
{
|
|
|
|
struct i2c_msg_s msg[2];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
msg[0].frequency = priv->freq;
|
|
|
|
msg[0].addr = priv->addr;
|
|
|
|
msg[0].flags = 0;
|
|
|
|
msg[0].buffer = ®addr;
|
|
|
|
msg[0].length = 1;
|
|
|
|
|
|
|
|
msg[1].frequency = priv->freq;
|
|
|
|
msg[1].addr = priv->addr;
|
|
|
|
msg[1].flags = I2C_M_READ;
|
|
|
|
msg[1].buffer = buffer;
|
|
|
|
msg[1].length = cnt;
|
|
|
|
|
|
|
|
ret = I2C_TRANSFER(priv->i2c, msg, 2);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("I2C_TRANSFER failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ak09912_delay_msec
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* System level delay
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void ak09912_delay_msek(uint16_t msek)
|
|
|
|
{
|
|
|
|
/* This is used to short delay without schedule. */
|
|
|
|
|
|
|
|
up_udelay(msek * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ak09912_set_power_mode
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* set POWER MODE for ak09912
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-12-07 02:07:34 +01:00
|
|
|
static int ak09912_set_power_mode(FAR struct ak09912_dev_s *priv,
|
2019-07-10 13:31:36 +02:00
|
|
|
uint32_t mode)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
ret = ak09912_putreg8(priv, POWER_MODE_ADDR, (uint8_t)mode);
|
|
|
|
ak09912_delay_msek(1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ak09912_read_sensitivity_data
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* read sensitivity date in fuse mode.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-12-07 02:07:34 +01:00
|
|
|
static int ak09912_read_sensitivity_data(FAR struct ak09912_dev_s *priv,
|
|
|
|
FAR struct sensi_data_s *asa_data)
|
2019-07-10 13:31:36 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
uint8_t buffer[3];
|
|
|
|
|
|
|
|
ret = ak09912_getreg(priv, AK09912_ASAX, buffer, sizeof(buffer));
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
asa_data->x = buffer[0];
|
|
|
|
asa_data->y = buffer[1];
|
|
|
|
asa_data->z = buffer[2];
|
|
|
|
}
|
2019-12-07 02:07:34 +01:00
|
|
|
|
2019-07-10 13:31:36 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-02-23 09:50:23 +01:00
|
|
|
* Name: ak09912_set_noise_suppr_flt
|
2019-07-10 13:31:36 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2020-02-23 09:50:23 +01:00
|
|
|
* set noise suppression filter for ak09912
|
2019-07-10 13:31:36 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
static int ak09912_set_noise_suppr_flt(FAR struct ak09912_dev_s *priv,
|
2019-07-10 13:31:36 +02:00
|
|
|
uint32_t nsf)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
uint8_t ctrl1 = 0;
|
|
|
|
|
|
|
|
ctrl1 = ak09912_getreg8(priv, AK09912_CTRL1);
|
|
|
|
SET_BITSLICE(ctrl1, nsf, 5, 0x60);
|
|
|
|
ret = ak09912_putreg8(priv, AK09912_CTRL1, ctrl1);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ak09912_wd_timeout
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* a timer to check if data is ready.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-08-09 20:29:35 +02:00
|
|
|
static void ak09912_wd_timeout(wdparm_t arg)
|
2019-07-10 13:31:36 +02:00
|
|
|
{
|
2020-08-06 11:08:40 +02:00
|
|
|
struct ak09912_dev_s *priv = (struct ak09912_dev_s *)arg;
|
2019-07-10 13:31:36 +02:00
|
|
|
irqstate_t flags = enter_critical_section();
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_post(&priv->wait);
|
2019-07-10 13:31:36 +02:00
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ak09912_read_mag_uncomp_data
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* read mag uncompensating data.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-12-07 02:07:34 +01:00
|
|
|
static int ak09912_read_mag_uncomp_data(FAR struct ak09912_dev_s *priv,
|
|
|
|
FAR struct mag_data_s *mag_data)
|
2019-07-10 13:31:36 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
uint8_t state = 0;
|
|
|
|
uint8_t buffer[8]; /* TMPS and ST2 is read, but the value is omitted. */
|
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
wd_start(&priv->wd, AK09912_POLLING_TIMEOUT,
|
2020-08-09 20:29:35 +02:00
|
|
|
ak09912_wd_timeout, (wdparm_t)priv);
|
2019-07-10 13:31:36 +02:00
|
|
|
state = ak09912_getreg8(priv, AK09912_ST1);
|
|
|
|
while (! (state & 0x1))
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_wait(&priv->wait);
|
2019-07-10 13:31:36 +02:00
|
|
|
}
|
2019-12-07 02:07:34 +01:00
|
|
|
|
2020-08-04 12:31:31 +02:00
|
|
|
wd_cancel(&priv->wd);
|
2019-07-10 13:31:36 +02:00
|
|
|
ret = ak09912_getreg(priv, AK09912_HXL, buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
mag_data->x = MERGE_BYTE(buffer[0], buffer[1]);
|
|
|
|
mag_data->y = MERGE_BYTE(buffer[2], buffer[3]);
|
|
|
|
mag_data->z = MERGE_BYTE(buffer[4], buffer[5]);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ak09912_read_mag_data
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* read mag data with compensation
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-12-07 02:07:34 +01:00
|
|
|
static int ak09912_read_mag_data(FAR struct ak09912_dev_s *priv,
|
|
|
|
FAR struct mag_data_s *mag_data)
|
2019-07-10 13:31:36 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ret = ak09912_read_mag_uncomp_data(priv, mag_data);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("Failed to read mag data from device.\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
mag_data->x = (int16_t)(mag_data->x *
|
|
|
|
((int32_t)priv->asa_data.x + AK09912_SENSITIVITY) /
|
|
|
|
AK09912_SENSITIVITY_DIV);
|
|
|
|
mag_data->y = (int16_t)(mag_data->y *
|
|
|
|
((int32_t)priv->asa_data.y + AK09912_SENSITIVITY) /
|
|
|
|
AK09912_SENSITIVITY_DIV);
|
|
|
|
mag_data->z = (int16_t)(mag_data->z *
|
|
|
|
((int32_t)priv->asa_data.z + AK09912_SENSITIVITY) /
|
|
|
|
AK09912_SENSITIVITY_DIV);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ak09912_checkid
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read and verify the AK09912 chip ID
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ak09912_checkid(FAR struct ak09912_dev_s *priv)
|
|
|
|
{
|
|
|
|
uint16_t devid = 0;
|
|
|
|
|
|
|
|
/* Read device ID */
|
|
|
|
|
|
|
|
devid = ak09912_getreg8(priv, AK09912_WIA1);
|
|
|
|
devid += ak09912_getreg8(priv, AK09912_WIA2) << 8;
|
|
|
|
sninfo("devid: 0x%02x\n", devid);
|
|
|
|
|
|
|
|
if (devid != AK09911_DEVID && devid != AK09912_DEVID)
|
|
|
|
{
|
|
|
|
/* ID is not Correct */
|
|
|
|
|
|
|
|
snerr("Wrong Device ID! %02x\n", devid);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ak09912_updatecaldata
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Update Calibration Coefficient Data
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ak09912_initialize(FAR struct ak09912_dev_s *priv)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ret += ak09912_set_power_mode(priv, AKM_FUSE_ROM_MODE);
|
|
|
|
ret += ak09912_read_sensitivity_data(priv, &priv->asa_data);
|
|
|
|
ret += ak09912_set_power_mode(priv, AKM_POWER_DOWN_MODE);
|
2020-02-23 09:50:23 +01:00
|
|
|
ret += ak09912_set_noise_suppr_flt(priv, priv->nsf);
|
2019-07-10 13:31:36 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ak09912_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is called whenever the AK09912 device is opened.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ak09912_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
2022-02-01 17:55:50 +01:00
|
|
|
FAR struct ak09912_dev_s *priv = inode->i_private;
|
2019-07-10 13:31:36 +02:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ret = ak09912_set_power_mode(priv, priv->mode);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("Failed to set power mode to %d.\n", priv->mode);
|
|
|
|
return ret;
|
|
|
|
}
|
2019-12-07 02:07:34 +01:00
|
|
|
|
2019-07-10 13:31:36 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ak09912_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This routine is called when the AK09912 device is closed.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ak09912_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
2022-02-01 17:55:50 +01:00
|
|
|
FAR struct ak09912_dev_s *priv = inode->i_private;
|
2019-07-10 13:31:36 +02:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ret = ak09912_set_power_mode(priv, AKM_POWER_DOWN_MODE);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("Failed to set power mode to %d.\n", AKM_POWER_DOWN_MODE);
|
|
|
|
return ret;
|
|
|
|
}
|
2019-12-07 02:07:34 +01:00
|
|
|
|
2019-07-10 13:31:36 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ak09912_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t ak09912_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
2019-12-07 02:07:34 +01:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct ak09912_dev_s *priv = inode->i_private;
|
2019-07-10 13:31:36 +02:00
|
|
|
int32_t ret = 0;
|
2019-12-07 02:07:34 +01:00
|
|
|
FAR struct mag_data_s *mag_data = (FAR struct mag_data_s *)buffer;
|
2019-07-10 13:31:36 +02:00
|
|
|
|
|
|
|
if (! buffer)
|
|
|
|
{
|
|
|
|
snerr("Buffer is null\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buflen != sizeof(struct mag_data_s))
|
|
|
|
{
|
|
|
|
snerr("You can't read something other than 32 bits (4 bytes)\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->compensated)
|
|
|
|
{
|
|
|
|
irqstate_t flags = enter_critical_section();
|
|
|
|
ret = ak09912_read_mag_data(priv, mag_data);
|
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
irqstate_t flags = enter_critical_section();
|
|
|
|
ret = ak09912_read_mag_uncomp_data(priv, mag_data);
|
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("Failed to read data from ak09912.\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sizeof(struct mag_data_s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ak09912_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t ak09912_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ak09912_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ak09912_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
2022-02-01 17:55:50 +01:00
|
|
|
FAR struct ak09912_dev_s *priv = inode->i_private;
|
2019-07-10 13:31:36 +02:00
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case SNIOC_ENABLE_COMPENSATED:
|
|
|
|
priv->compensated = (int)arg;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
snerr("Unrecognized cmd: %d\n", cmd);
|
|
|
|
ret = - ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ak09912_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register the AK09912 character device as 'devpath'
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* devpath - The full path to the driver to register. E.g., "/dev/compass0"
|
|
|
|
* i2c - An instance of the I2C interface to use to communicate with
|
|
|
|
* AK09912
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ak09912_register(FAR const char *devpath, FAR struct i2c_master_s *i2c)
|
|
|
|
{
|
|
|
|
FAR struct ak09912_dev_s *priv;
|
|
|
|
char path[16];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Initialize the AK09912 device structure */
|
|
|
|
|
2020-08-11 21:02:34 +02:00
|
|
|
priv = kmm_zalloc(sizeof(struct ak09912_dev_s));
|
2019-07-10 13:31:36 +02:00
|
|
|
if (!priv)
|
|
|
|
{
|
|
|
|
snerr("Failed to allocate instance\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->i2c = i2c;
|
|
|
|
priv->addr = AK09912_ADDR;
|
|
|
|
priv->freq = AK09912_FREQ;
|
|
|
|
priv->compensated = ENABLE_COMPENSATED;
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_init(&priv->wait, 0, 0);
|
2019-07-10 13:31:36 +02:00
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* set default noise suppression filter. */
|
2019-07-10 13:31:36 +02:00
|
|
|
|
|
|
|
priv->nsf = AK09912_NSF_LOW;
|
|
|
|
|
|
|
|
/* set default power mode */
|
|
|
|
|
|
|
|
priv->mode = AKM_CONT_MEAS_4;
|
|
|
|
|
|
|
|
/* Check Device ID */
|
|
|
|
|
|
|
|
ret = ak09912_checkid(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("Failed to register driver: %d\n", ret);
|
|
|
|
kmm_free(priv);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ak09912_initialize(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("Failed to initialize physical device ak09912:%d\n", ret);
|
|
|
|
kmm_free(priv);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register the character driver */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
snprintf(path, sizeof(path), "%s%d", devpath, 0);
|
2019-07-10 13:31:36 +02:00
|
|
|
ret = register_driver(path, &g_ak09912fops, 0666, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("Failed to register driver: %d\n", ret);
|
|
|
|
kmm_free(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
sninfo("AK09912 driver loaded successfully!\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_I2C && CONFIG_SENSORS_AK09912 */
|