2015-08-07 16:24:29 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/sensors/as5048b.c
|
|
|
|
* Character driver for the AMS AS5048B Magnetic Rotary Encoder
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Alexandru Duru. All rights reserved.
|
|
|
|
* Author: Alexandru Duru <alexandruduru@gmail.com>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/i2c.h>
|
|
|
|
#include <nuttx/sensors/as5048b.h>
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
#if defined(CONFIG_I2C) && defined(CONFIG_QENCODER) && defined(CONFIG_AS5048B)
|
2015-08-07 16:24:29 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct as5048b_dev_s
|
|
|
|
{
|
2015-08-10 16:14:44 +02:00
|
|
|
struct qe_lowerhalf_s lower; /* AS5048B quadrature encoder lower half */
|
|
|
|
FAR struct i2c_dev_s *i2c; /* I2C interface */
|
|
|
|
uint8_t addr; /* I2C address */
|
2015-08-07 16:24:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
/* 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);
|
2015-08-10 16:14:44 +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);
|
|
|
|
static int as5048b_readagc(FAR struct as5048b_dev_s *priv, FAR uint8_t *agc);
|
|
|
|
static int as5048b_readdiag(FAR struct as5048b_dev_s *priv,
|
|
|
|
FAR uint8_t *diag);
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
static int as5048b_position(FAR struct qe_lowerhalf_s *lower, int32_t *pos);
|
|
|
|
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
|
|
|
{
|
2015-08-10 16:14:44 +02:00
|
|
|
as5048b_setup,
|
|
|
|
as5048b_shutdown,
|
|
|
|
as5048b_position,
|
|
|
|
as5048b_reset,
|
2015-08-07 16:24:29 +02:00
|
|
|
as5048b_ioctl
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-08-10 16:14:44 +02:00
|
|
|
* Name: as5048b_readu8
|
2015-08-07 16:24:29 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read from an 8-bit register
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Write the register address */
|
|
|
|
|
|
|
|
I2C_SETADDRESS(priv->i2c, priv->addr, 7);
|
|
|
|
ret = I2C_WRITE(priv->i2c, ®addr, sizeof(regaddr));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
sndbg("I2C_WRITE failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restart and read 8 bits from the register */
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
ret = I2C_READ(priv->i2c, regval, sizeof(*regval));
|
2015-08-07 16:24:29 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
sndbg("I2C_READ failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
sndbg("addr: %02x value: %02x ret: %d\n", regaddr, *regval, ret);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
uint8_t hi, lo;
|
|
|
|
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)
|
|
|
|
{
|
2015-08-10 16:14:44 +02:00
|
|
|
sndbg("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)
|
|
|
|
{
|
2015-08-10 16:14:44 +02:00
|
|
|
sndbg("as5048b_readu8 failed: %d\n", ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
*regval = (uint16_t)hi << 6 | (uint16_t)lo;
|
|
|
|
sndbg("addrhi: %02x addrlo: %02x value: %04x ret: %d\n",
|
|
|
|
regaddrhi, regaddrlo, *regval, ret);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
uint8_t buffer[2];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
sndbg("addr: %02x value: %02x\n", regaddr, regval);
|
|
|
|
|
|
|
|
/* Set up a 2-byte message to send */
|
|
|
|
|
|
|
|
buffer[0] = regaddr;
|
|
|
|
buffer[1] = regval;
|
|
|
|
|
|
|
|
/* Write the register address followed by the data (no RESTART) */
|
|
|
|
|
|
|
|
I2C_SETADDRESS(priv->i2c, priv->addr, 7);
|
|
|
|
ret = I2C_WRITE(priv->i2c, buffer, sizeof(buffer));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
sndbg("I2C_WRITE failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-10 16:14:44 +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;
|
|
|
|
|
|
|
|
sndbg("addrhi: %02x addrlo: %02x value: %04x\n",
|
|
|
|
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)
|
|
|
|
{
|
2015-08-10 16:14:44 +02:00
|
|
|
sndbg("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)
|
|
|
|
{
|
2015-08-10 16:14:44 +02:00
|
|
|
sndbg("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)
|
|
|
|
{
|
2015-08-10 16:14:44 +02:00
|
|
|
sndbg("as5048b_readu16 failed: %d\n", ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
sndbg("zero: %04x ret: %d\n", *zero, ret);
|
|
|
|
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;
|
|
|
|
|
|
|
|
sndbg("zero: %04x\n", zero);
|
|
|
|
|
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)
|
|
|
|
{
|
2015-08-10 16:14:44 +02:00
|
|
|
sndbg("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)
|
|
|
|
{
|
2015-08-10 16:14:44 +02:00
|
|
|
sndbg("as5048b_readu8 failed: %d\n", ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
sndbg("agc: %02x ret: %d\n", *agc, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: as5048b_readdiag
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read from the diagnostics register
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int as5048b_readdiag(FAR struct as5048b_dev_s *priv, FAR uint8_t *diag)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2015-08-10 16:14:44 +02:00
|
|
|
sndbg("as5048b_readu8 failed: %d\n", ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
sndbg("diag: %02x ret: %d\n", *diag, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: as5048b_readmag
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read from the magnitude registers
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int as5048b_readmag(FAR struct as5048b_dev_s *priv, FAR uint16_t *mag)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2015-08-10 16:14:44 +02:00
|
|
|
sndbg("as5048b_readu16 failed: %d\n", ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
sndbg("mag: %04x ret: %d\n", *mag, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: as5048b_readang
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read from the angle registers
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int as5048b_readang(FAR struct as5048b_dev_s *priv, FAR uint16_t *ang)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2015-08-10 16:14:44 +02:00
|
|
|
sndbg("as5048b_readu16 failed: %d\n", ret);
|
2015-08-07 16:24:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
sndbg("ang: %04x ret: %d\n", *ang, ret);
|
|
|
|
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 16:14:44 +02:00
|
|
|
static int as5048b_position(FAR struct qe_lowerhalf_s *lower, 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
|
|
|
{
|
2015-08-10 16:14:44 +02:00
|
|
|
sndbg("as5048b_readang failed: %d\n", ret);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
sndbg("as5048b_writezero failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = as5048b_readang(priv, &ang);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
sndbg("as5048b_readang failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = as5048b_writezero(priv, ang);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
sndbg("as5048b_writezero failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2015-08-07 16:24:29 +02:00
|
|
|
sndbg("zero: %04x ret: %d\n", *ptr, ret);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Read from the automatic gain control register. Arg: uint8_t* pointer. */
|
|
|
|
|
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);
|
|
|
|
sndbg("agc: %02x ret: %d\n", *ptr, ret);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
sndbg("diag: %02x ret: %d\n", *ptr, ret);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2015-08-07 16:24:29 +02:00
|
|
|
sndbg("mag: %04x ret: %d\n", *ptr, ret);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-08-10 16:14:44 +02:00
|
|
|
sndbg("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:
|
2015-08-10 16:14:44 +02:00
|
|
|
* A new lower half quadrature encoder interface for the AS5048B on success;
|
|
|
|
* NULL on failure.
|
2015-08-07 16:24:29 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
FAR struct qe_lowerhalf_s *as5048b_initialize(FAR struct i2c_dev_s *i2c,
|
|
|
|
uint8_t addr)
|
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)
|
|
|
|
{
|
|
|
|
sndbg("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;
|
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
|
|
|
}
|
|
|
|
|
2015-08-10 16:14:44 +02:00
|
|
|
#endif /* CONFIG_I2C && CONFIG_QENCODER && CONFIG_AS5048B */
|