2015-08-07 16:24:29 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/sensors/as5048b.c
|
|
|
|
*
|
2021-09-28 11:40:59 +02: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
|
2015-08-07 16:24:29 +02:00
|
|
|
*
|
2021-09-28 11:40:59 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-02-01 15:57:22 +01:00
|
|
|
*
|
2021-09-28 11:40:59 +02: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.
|
2015-08-07 16:24:29 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2015-08-07 16:24:29 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
2016-01-30 15:00:16 +01:00
|
|
|
#include <nuttx/i2c/i2c_master.h>
|
2015-08-07 16:24:29 +02:00
|
|
|
#include <nuttx/sensors/as5048b.h>
|
|
|
|
|
2017-08-24 18:26:53 +02:00
|
|
|
#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_QENCODER) && defined(CONFIG_SENSORS_AS5048B)
|
2015-08-07 16:24:29 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct as5048b_dev_s
|
|
|
|
{
|
2016-01-30 15:36:47 +01:00
|
|
|
struct qe_lowerhalf_s lower; /* AS5048B quadrature encoder lower half */
|
|
|
|
FAR struct i2c_master_s *i2c; /* I2C interface */
|
|
|
|
uint8_t addr; /* I2C address */
|
2017-03-09 12:01:01 +01:00
|
|
|
uint32_t frequency; /* I2C frequency */
|
2015-08-07 16:24:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
2019-12-07 02:07:34 +01:00
|
|
|
|
2015-08-07 16:24:29 +02:00
|
|
|
/* I2C Helpers */
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
static int as5048b_readu8(FAR struct as5048b_dev_s *priv, uint8_t regaddr,
|
2015-08-07 16:24:29 +02:00
|
|
|
FAR uint8_t *regval);
|
2015-08-10 16:14:44 +02:00
|
|
|
static int as5048b_readu16(FAR struct as5048b_dev_s *priv, uint8_t regaddrhi,
|
2015-08-07 16:24:29 +02:00
|
|
|
uint8_t regaddrlo, FAR uint16_t *regval);
|
2015-08-10 16:14:44 +02:00
|
|
|
static int as5048b_writeu8(FAR struct as5048b_dev_s *priv, uint8_t regaddr,
|
2015-08-07 16:24:29 +02:00
|
|
|
uint8_t regval);
|
2021-04-04 11:58:11 +02:00
|
|
|
static int as5048b_writeu16(FAR struct as5048b_dev_s *priv,
|
|
|
|
uint8_t regaddrhi,
|
2015-08-07 16:24:29 +02:00
|
|
|
uint8_t regaddrlo, uint16_t regval);
|
|
|
|
static int as5048b_readzero(FAR struct as5048b_dev_s *priv,
|
|
|
|
FAR uint16_t *zero);
|
|
|
|
static int as5048b_writezero(FAR struct as5048b_dev_s *priv, uint16_t zero);
|
2021-04-04 11:58:11 +02:00
|
|
|
static int as5048b_readagc(FAR struct as5048b_dev_s *priv,
|
|
|
|
FAR uint8_t *agc);
|
2015-08-07 16:24:29 +02:00
|
|
|
static int as5048b_readdiag(FAR struct as5048b_dev_s *priv,
|
|
|
|
FAR uint8_t *diag);
|
2021-04-04 11:58:11 +02:00
|
|
|
static int as5048b_readmag(FAR struct as5048b_dev_s *priv,
|
|
|
|
FAR uint16_t *mag);
|
|
|
|
static int as5048b_readang(FAR struct as5048b_dev_s *priv,
|
|
|
|
FAR uint16_t *ang);
|
2015-08-07 16:24:29 +02:00
|
|
|
|
|
|
|
/* Character Driver Methods */
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
static int as5048b_setup(FAR struct qe_lowerhalf_s *lower);
|
|
|
|
static int as5048b_shutdown(FAR struct qe_lowerhalf_s *lower);
|
2015-08-10 20:40:36 +02:00
|
|
|
static int as5048b_position(FAR struct qe_lowerhalf_s *lower,
|
|
|
|
FAR int32_t *pos);
|
2015-08-10 16:14:44 +02:00
|
|
|
static int as5048b_reset(FAR struct qe_lowerhalf_s *lower);
|
|
|
|
static int as5048b_ioctl(FAR struct qe_lowerhalf_s *lower, int cmd,
|
|
|
|
unsigned long arg);
|
2015-08-07 16:24:29 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
static const struct qe_ops_s g_qeops =
|
2015-08-07 16:24:29 +02:00
|
|
|
{
|
2022-08-27 05:10:20 +02:00
|
|
|
as5048b_setup, /* setup */
|
|
|
|
as5048b_shutdown, /* shutdown */
|
|
|
|
as5048b_position, /* position */
|
|
|
|
NULL, /* setposmax */
|
|
|
|
as5048b_reset, /* reset */
|
|
|
|
NULL, /* setindex */
|
|
|
|
as5048b_ioctl /* ioctl */
|
2015-08-07 16:24:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-02-01 15:57:22 +01:00
|
|
|
/****************************************************************************
|
2016-02-01 19:14:31 +01:00
|
|
|
* Name: as5048b_readu8
|
2016-02-01 15:57:22 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2016-02-01 19:14:31 +01:00
|
|
|
* Read from an 8-bit register
|
2016-02-01 15:57:22 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-02-01 19:14:31 +01:00
|
|
|
static int as5048b_readu8(FAR struct as5048b_dev_s *priv, uint8_t regaddr,
|
|
|
|
FAR uint8_t *regval)
|
2016-02-01 15:57:22 +01:00
|
|
|
{
|
2016-02-01 19:14:31 +01:00
|
|
|
int ret;
|
2016-02-01 15:57:22 +01:00
|
|
|
struct i2c_config_s config;
|
|
|
|
|
2016-02-01 19:14:31 +01:00
|
|
|
/* Set up the I2C configuration */
|
2016-02-01 15:57:22 +01:00
|
|
|
|
|
|
|
config.frequency = priv->frequency;
|
|
|
|
config.address = priv->addr;
|
|
|
|
config.addrlen = 7;
|
|
|
|
|
2015-08-07 16:24:29 +02:00
|
|
|
/* Write the register address */
|
|
|
|
|
2016-02-01 19:14:31 +01:00
|
|
|
ret = i2c_write(priv->i2c, &config, ®addr, sizeof(regaddr));
|
2015-08-07 16:24:29 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: i2c_write failed: %d\n", ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restart and read 8 bits from the register */
|
|
|
|
|
2016-02-01 19:14:31 +01:00
|
|
|
ret = i2c_read(priv->i2c, &config, regval, sizeof(*regval));
|
2015-08-07 16:24:29 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: i2c_read failed: %d\n", ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("addr: %02x value: %02x ret: %d\n", regaddr, *regval, ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-08-10 16:14:44 +02:00
|
|
|
* Name: as5048b_readu16
|
2015-08-07 16:24:29 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read from two 8-bit registers
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
static int as5048b_readu16(FAR struct as5048b_dev_s *priv, uint8_t regaddrhi,
|
2015-08-07 16:24:29 +02:00
|
|
|
uint8_t regaddrlo, FAR uint16_t *regval)
|
|
|
|
{
|
2019-12-07 02:07:34 +01:00
|
|
|
uint8_t hi;
|
|
|
|
uint8_t lo;
|
2015-08-07 16:24:29 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Read the high 8 bits of the 13-bit value */
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
ret = as5048b_readu8(priv, regaddrhi, &hi);
|
2015-08-07 16:24:29 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: as5048b_readu8 failed: %d\n", ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the low 5 bits of the 13-bit value */
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
ret = as5048b_readu8(priv, regaddrlo, &lo);
|
2015-08-07 16:24:29 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: as5048b_readu8 failed: %d\n", ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
*regval = (uint16_t)hi << 6 | (uint16_t)lo;
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("addrhi: %02x addrlo: %02x value: %04x ret: %d\n",
|
|
|
|
regaddrhi, regaddrlo, *regval, ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-08-10 16:14:44 +02:00
|
|
|
* Name: as5048b_writeu8
|
2015-08-07 16:24:29 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write from an 8-bit register
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
static int as5048b_writeu8(FAR struct as5048b_dev_s *priv, uint8_t regaddr,
|
2015-08-07 16:24:29 +02:00
|
|
|
uint8_t regval)
|
|
|
|
{
|
2016-02-01 19:14:31 +01:00
|
|
|
struct i2c_config_s config;
|
2015-08-07 16:24:29 +02:00
|
|
|
uint8_t buffer[2];
|
2015-10-05 00:01:21 +02:00
|
|
|
int ret;
|
2015-08-07 16:24:29 +02:00
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("addr: %02x value: %02x\n", regaddr, regval);
|
2015-08-07 16:24:29 +02:00
|
|
|
|
2016-02-01 19:14:31 +01:00
|
|
|
/* Set up the I2C configuration */
|
|
|
|
|
|
|
|
config.frequency = priv->frequency;
|
|
|
|
config.address = priv->addr;
|
|
|
|
config.addrlen = 7;
|
|
|
|
|
2015-08-07 16:24:29 +02:00
|
|
|
/* Set up a 2-byte message to send */
|
|
|
|
|
|
|
|
buffer[0] = regaddr;
|
|
|
|
buffer[1] = regval;
|
|
|
|
|
|
|
|
/* Write the register address followed by the data (no RESTART) */
|
|
|
|
|
2016-02-01 19:14:31 +01:00
|
|
|
ret = i2c_write(priv->i2c, &config, buffer, sizeof(buffer));
|
2015-08-07 16:24:29 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: i2c_write failed: %d\n", ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-08-10 16:14:44 +02:00
|
|
|
* Name: as5048b_writeu16
|
2015-08-07 16:24:29 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write to two 8-bit registers
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-04 11:58:11 +02:00
|
|
|
static int as5048b_writeu16(FAR struct as5048b_dev_s *priv,
|
|
|
|
uint8_t regaddrhi,
|
2015-08-07 16:24:29 +02:00
|
|
|
uint8_t regaddrlo, uint16_t regval)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("addrhi: %02x addrlo: %02x value: %04x\n",
|
2015-08-07 16:24:29 +02:00
|
|
|
regaddrhi, regaddrlo, regval);
|
|
|
|
|
|
|
|
/* Write the high 8 bits of the 13-bit value */
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
ret = as5048b_writeu8(priv, regaddrhi, (uint8_t)(regval >> 6));
|
2015-08-07 16:24:29 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: as5048b_writeu8 failed: %d\n", ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the low 5 bits of the 13-bit value */
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
ret = as5048b_writeu8(priv, regaddrhi, (uint8_t)regval);
|
2015-08-07 16:24:29 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: as5048b_writeu8 failed: %d\n", ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: as5048b_readzero
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read from the zero position registers
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int as5048b_readzero(FAR struct as5048b_dev_s *priv,
|
|
|
|
FAR uint16_t *zero)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
ret = as5048b_readu16(priv, AS5048B_ZEROHI_REG, AS5048B_ZEROLO_REG, zero);
|
2015-08-07 16:24:29 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: as5048b_readu16 failed: %d\n", ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("zero: %04x ret: %d\n", *zero, ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: as5048b_writezero
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write to the zero position registers
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int as5048b_writezero(FAR struct as5048b_dev_s *priv, uint16_t zero)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("zero: %04x\n", zero);
|
2015-08-07 16:24:29 +02:00
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
ret = as5048b_writeu16(priv, AS5048B_ZEROHI_REG, AS5048B_ZEROLO_REG, zero);
|
2015-08-07 16:24:29 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: as5048b_writeu16 failed: %d\n", ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: as5048b_readagc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read from the automatic gain control register
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int as5048b_readagc(FAR struct as5048b_dev_s *priv, FAR uint8_t *agc)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
ret = as5048b_readu8(priv, AS5048B_AGC_REG, agc);
|
2015-08-07 16:24:29 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: as5048b_readu8 failed: %d\n", ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("agc: %02x ret: %d\n", *agc, ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: as5048b_readdiag
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read from the diagnostics register
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-04 11:58:11 +02:00
|
|
|
static int as5048b_readdiag(FAR struct as5048b_dev_s *priv,
|
|
|
|
FAR uint8_t *diag)
|
2015-08-07 16:24:29 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
ret = as5048b_readu8(priv, AS5048B_DIAG_REG, diag);
|
2015-08-07 16:24:29 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: as5048b_readu8 failed: %d\n", ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("diag: %02x ret: %d\n", *diag, ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: as5048b_readmag
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read from the magnitude registers
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-04 11:58:11 +02:00
|
|
|
static int as5048b_readmag(FAR struct as5048b_dev_s *priv,
|
|
|
|
FAR uint16_t *mag)
|
2015-08-07 16:24:29 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
ret = as5048b_readu16(priv, AS5048B_MAGHI_REG, AS5048B_MAGLO_REG, mag);
|
2015-08-07 16:24:29 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: as5048b_readu16 failed: %d\n", ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("mag: %04x ret: %d\n", *mag, ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: as5048b_readang
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read from the angle registers
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-04 11:58:11 +02:00
|
|
|
static int as5048b_readang(FAR struct as5048b_dev_s *priv,
|
|
|
|
FAR uint16_t *ang)
|
2015-08-07 16:24:29 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
ret = as5048b_readu16(priv, AS5048B_ANGHI_REG, AS5048B_ANGLO_REG, ang);
|
2015-08-07 16:24:29 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: as5048b_readu16 failed: %d\n", ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("ang: %04x ret: %d\n", *ang, ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-08-10 16:14:44 +02:00
|
|
|
* Name: as5048b_setup
|
2015-08-07 16:24:29 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2015-08-10 16:14:44 +02:00
|
|
|
* This method is called when the driver is opened
|
2015-08-07 16:24:29 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
static int as5048b_setup(FAR struct qe_lowerhalf_s *lower)
|
2015-08-07 16:24:29 +02:00
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-08-10 16:14:44 +02:00
|
|
|
* Name: as5048b_shutdown
|
2015-08-07 16:24:29 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2015-08-10 16:14:44 +02:00
|
|
|
* This method is called when the driver is closed
|
2015-08-07 16:24:29 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
static int as5048b_shutdown(FAR struct qe_lowerhalf_s *lower)
|
2015-08-07 16:24:29 +02:00
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-08-10 16:14:44 +02:00
|
|
|
* Name: as5048b_position
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the current position measurement
|
|
|
|
*
|
2015-08-07 16:24:29 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-10 20:40:36 +02:00
|
|
|
static int as5048b_position(FAR struct qe_lowerhalf_s *lower,
|
|
|
|
FAR int32_t *pos)
|
2015-08-07 16:24:29 +02:00
|
|
|
{
|
2015-08-10 16:14:44 +02:00
|
|
|
FAR struct as5048b_dev_s *priv = (FAR struct as5048b_dev_s *)lower;
|
|
|
|
uint16_t ang;
|
|
|
|
int ret;
|
2015-08-07 16:24:29 +02:00
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
ret = as5048b_readang(priv, &ang);
|
|
|
|
if (ret < 0)
|
2015-08-07 16:24:29 +02:00
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: as5048b_readang failed: %d\n", ret);
|
2015-08-10 16:14:44 +02:00
|
|
|
return ret;
|
2015-08-07 16:24:29 +02:00
|
|
|
}
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
*pos = (int32_t)ang;
|
|
|
|
return ret;
|
2015-08-07 16:24:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-08-10 16:14:44 +02:00
|
|
|
* Name: as5048b_reset
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Reset the position measurement to zero
|
|
|
|
*
|
2015-08-07 16:24:29 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
static int as5048b_reset(FAR struct qe_lowerhalf_s *lower)
|
2015-08-07 16:24:29 +02:00
|
|
|
{
|
2015-08-10 16:14:44 +02:00
|
|
|
FAR struct as5048b_dev_s *priv = (FAR struct as5048b_dev_s *)lower;
|
|
|
|
uint16_t ang;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = as5048b_writezero(priv, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: as5048b_writezero failed: %d\n", ret);
|
2015-08-10 16:14:44 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = as5048b_readang(priv, &ang);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: as5048b_readang failed: %d\n", ret);
|
2015-08-10 16:14:44 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = as5048b_writezero(priv, ang);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: as5048b_writezero failed: %d\n", ret);
|
2015-08-10 16:14:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2015-08-07 16:24:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: as5048b_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
static int as5048b_ioctl(FAR struct qe_lowerhalf_s *lower, int cmd,
|
|
|
|
unsigned long arg)
|
2015-08-07 16:24:29 +02:00
|
|
|
{
|
2015-08-10 16:14:44 +02:00
|
|
|
FAR struct as5048b_dev_s *priv = (FAR struct as5048b_dev_s *)lower;
|
|
|
|
int ret = OK;
|
2015-08-07 16:24:29 +02:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2015-08-10 16:14:44 +02:00
|
|
|
/* Read from the zero position registers. Arg: int32_t* pointer. */
|
2015-08-07 16:24:29 +02:00
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
case QEIOC_ZEROPOSITION:
|
2015-08-07 16:24:29 +02:00
|
|
|
{
|
2015-08-10 16:14:44 +02:00
|
|
|
FAR int32_t *ptr = (FAR int32_t *)((uintptr_t)arg);
|
|
|
|
uint16_t zero;
|
|
|
|
DEBUGASSERT(ptr != NULL);
|
|
|
|
ret = as5048b_readzero(priv, &zero);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
*ptr = (int32_t)zero;
|
|
|
|
}
|
2016-06-12 02:05:29 +02:00
|
|
|
|
|
|
|
sninfo("zero: %04x ret: %d\n", *ptr, ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-04-04 11:58:11 +02:00
|
|
|
/* Read from the automatic gain control register.
|
|
|
|
* Arg: uint8_t* pointer.
|
|
|
|
*/
|
2015-08-07 16:24:29 +02:00
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
case QEIOC_AUTOGAINCTL:
|
2015-08-07 16:24:29 +02:00
|
|
|
{
|
|
|
|
FAR uint8_t *ptr = (FAR uint8_t *)((uintptr_t)arg);
|
2015-08-10 16:14:44 +02:00
|
|
|
DEBUGASSERT(ptr != NULL);
|
2015-08-07 16:24:29 +02:00
|
|
|
ret = as5048b_readagc(priv, ptr);
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("agc: %02x ret: %d\n", *ptr, ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Read from the diagnostics register. Arg: uint8_t* pointer. */
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
case QEIOC_DIAGNOSTICS:
|
2015-08-07 16:24:29 +02:00
|
|
|
{
|
|
|
|
FAR uint8_t *ptr = (FAR uint8_t *)((uintptr_t)arg);
|
2015-08-10 16:14:44 +02:00
|
|
|
DEBUGASSERT(ptr != NULL);
|
2015-08-07 16:24:29 +02:00
|
|
|
ret = as5048b_readdiag(priv, ptr);
|
2016-06-12 02:05:29 +02:00
|
|
|
sninfo("diag: %02x ret: %d\n", *ptr, ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
/* Read from the magnitude registers. Arg: int32_t* pointer. */
|
2015-08-07 16:24:29 +02:00
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
case QEIOC_MAGNITUDE:
|
2015-08-07 16:24:29 +02:00
|
|
|
{
|
2015-08-10 16:14:44 +02:00
|
|
|
FAR int32_t *ptr = (FAR int32_t *)((uintptr_t)arg);
|
|
|
|
uint16_t mag;
|
|
|
|
DEBUGASSERT(ptr != NULL);
|
|
|
|
ret = as5048b_readmag(priv, &mag);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
*ptr = (int32_t)mag;
|
|
|
|
}
|
2016-06-12 02:05:29 +02:00
|
|
|
|
|
|
|
sninfo("mag: %04x ret: %d\n", *ptr, ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: Unrecognized cmd: %d arg: %ld\n", cmd, arg);
|
2015-08-07 16:24:29 +02:00
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-08-10 16:14:44 +02:00
|
|
|
* Name: as5048b_initialize
|
2015-08-07 16:24:29 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2015-08-10 16:14:44 +02:00
|
|
|
* Initialize the AS5048B device.
|
2015-08-07 16:24:29 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2015-08-10 16:14:44 +02:00
|
|
|
* i2c - An I2C driver instance.
|
|
|
|
* addr - The I2C address of the AS5048B.
|
2015-08-07 16:24:29 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2021-04-04 11:58:11 +02:00
|
|
|
* A new lower half quadrature encoder interface for the AS5048B on
|
|
|
|
* success;
|
2015-08-10 16:14:44 +02:00
|
|
|
* NULL on failure.
|
2015-08-07 16:24:29 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-01-30 15:36:47 +01:00
|
|
|
FAR struct qe_lowerhalf_s *as5048b_initialize(FAR struct i2c_master_s *i2c,
|
2021-04-04 11:58:11 +02:00
|
|
|
uint8_t addr,
|
|
|
|
uint32_t frequency)
|
2015-08-07 16:24:29 +02:00
|
|
|
{
|
|
|
|
FAR struct as5048b_dev_s *priv;
|
2015-08-10 16:14:44 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(i2c != NULL);
|
2015-08-07 16:24:29 +02:00
|
|
|
|
|
|
|
/* Initialize the device's structure */
|
|
|
|
|
|
|
|
priv = (FAR struct as5048b_dev_s *)kmm_malloc(sizeof(*priv));
|
|
|
|
if (priv == NULL)
|
|
|
|
{
|
2016-06-12 02:05:29 +02:00
|
|
|
snerr("ERROR: Failed to allocate instance\n");
|
2015-08-10 16:14:44 +02:00
|
|
|
return NULL;
|
2015-08-07 16:24:29 +02:00
|
|
|
}
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
priv->lower.ops = &g_qeops;
|
|
|
|
priv->i2c = i2c;
|
|
|
|
priv->addr = addr;
|
2017-03-09 12:01:01 +01:00
|
|
|
priv->frequency = frequency;
|
2015-08-07 16:24:29 +02:00
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
return &priv->lower;
|
2015-08-07 16:24:29 +02:00
|
|
|
}
|
|
|
|
|
2017-08-24 18:26:53 +02:00
|
|
|
#endif /* CONFIG_I2C && CONFIG_SENSORS_QENCODER && CONFIG_SENSORS_AS5048B */
|