sensors/nau7802: Add support for the NAU7802
Build Documentation / build-html (push) Has been cancelled

This patch adds support for the NAU7802 24 bit ADC from Nuvoton.
Please read the documentation for more details.

Signed-off-by: Daniel Byshkin <online@bskdany.com>
This commit is contained in:
Daniel Byshkin
2025-03-27 14:51:06 -04:00
committed by Xiang Xiao
parent 7482170f3f
commit 504f838577
8 changed files with 1288 additions and 0 deletions
@@ -29,6 +29,7 @@ general interafce.
sensors/lsm330.rst
sensors/mcp9600.rst
sensors/mpl115a.rst
sensors/nau7802.rst
sensors/sht4x.rst
sensors/lsm6dso32.rst
sensors/lis2mdl.rst
@@ -0,0 +1,211 @@
=======
NAU7802
=======
Contributed by Daniel Byshkin.
The Adafruit NAU7802 is a high-resolution 24-bit ADC with an integrated load cell amplifier. It is designed for use with load cells and other sensors that require high precision and low noise measurements. The NAU7802 features a built-in programmable gain amplifier (PGA) that allows for easy calibration and adjustment of the sensor's output.
The driver uses the :doc:`uorb
</components/drivers/special/sensors/sensors_uorb>` interface.
Application Programming Interface
=================================
.. code-block:: c
#include <nuttx/sensors/nau7802.h>
The NAU7802 registration function allows the driver to be registered as a UORB
driver. Registering this driver will cause the ``/dev/uorb/sensor_force<n>`` topic
to appear, where ``n`` is the value of ``devno``.
Registering the device in polling mode will create a kernel thread to poll the sensor
.. code-block:: c
int err = nau7802_register(i2c_master, 0, 0x2a);
if(err < 0){
printf("Failed to register NAU7802: %d\n", err);
}
The following are available commands for the NAU7802 driver:
``SNIOC_RESET``
----------------
Performs a reset of all registers on the NAU7802.
.. code-block:: c
orb_ioctl(sensor, SNIOC_RESET);
``SNIOC_SET_GAIN``
------------------------
This command sets the gain of the NAU7802. The possible values are dictated by the
``nau7802_gain_e`` enum. The default value is 128x.
.. code-block:: c
orb_ioctl(sensor, SNIOC_SET_GAIN, NAU7802_GAIN_128);
``SNIOC_SET_INTERVAL``
------------------------
This commands sets the polling interval of the NAU7802. The possible values are dictated by the
``nau7802_odr_e`` enum. The default value is 10HZ.
.. code-block:: c
orb_ioctl(sensor, SNIOC_SET_INTERVAL, NAU7802_ODR_10HZ);
``SNIOC_SET_LDO``
------------------------
This command sets the LDO voltage of the NAU7802. The possible values are dictated by the
``nau7802_ldo_e`` enum. The default value is 3.0V.
.. code-block:: c
orb_ioctl(sensor, SNIOC_SET_LDO, NAU7802_LDO_3V0);
``SNIOC_CALIBRATE``
------------------------
This commands performs one of the calibration procedures of the NAU7802. The possible calibration modes are:
- NAU7802_CALMOD_INTERNAL: Removes internal PGA gain and offset errors.
- NAU7802_CALMOD_OFFSET: Calibrates the zero point of the sensor.
- NAU7802_CALMOD_GAIN: Calibrates the max value of the sensor.
.. code-block:: c
orb_ioctl(sensor, SNIOC_CALIBRATE, NAU7802_CALMOD_INTERNAL);
For the gain calibration mode the user must place a known weight on the sensor. Unfortunately the NAU7802 records it as the maximum value, thus if your loadcell supports up to 100kg you shall put a 100kg weight on.
A workaround would be to do a manual calibration by placing a smaller known weight and polling the sensor to get an average point, then using such point to offset the recorded values. An example is provided below.
.. code-block:: c
#include "stdio.h"
#include <errno.h>
#include <fcntl.h>
#include <nuttx/sensors/nau7802.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <uORB/uORB.h>
#include <unistd.h>
int get_data(const struct orb_metadata *imu_meta, int imu, struct sensor_force *data) {
int err = 0;
bool update = false;
err = orb_check(imu, &update);
if (err < 0) {
return err;
}
err = orb_copy(imu_meta, imu, data);
if (err < 0) {
return err;
}
return err;
}
int main(int argc, char **argv) {
int err;
int imu;
char *name = "sensor_force0";
const struct orb_metadata *imu_meta = orb_get_meta(name);
if (imu_meta == NULL) {
fprintf(stderr, "Failed to get metadata for %s\n", name);
return EXIT_FAILURE;
}
imu = orb_subscribe(imu_meta);
if (imu < 0) {
fprintf(stderr, "Could not subsribe to %s: %d\n", name, errno);
return EXIT_FAILURE;
}
struct sensor_force data;
// flush 10 readings
for (int i = 0; i < 10; i++) {
err = get_data(imu_meta, imu, &data);
if (err < 0) {
printf("Error reading data\n");
}
usleep(100000);
}
long zero_point = 0;
for (int i = 0; i < 10; i++) {
err = get_data(imu_meta, imu, &data);
if (err < 0) {
printf("Error reading data\n");
} else {
zero_point += data.force / 10;
}
usleep(100000);
}
printf("Zero point: %ld\n", zero_point);
printf("Place weigth on the sensor... you have 5 seconds from when you see this message\n");
usleep(5000000);
printf("Starting gain calibration\n");
long weight_point = 0;
for (int i = 0; i < 10; i++) {
err = get_data(imu_meta, imu, &data);
if (err < 0) {
printf("Error reading data\n");
} else {
weight_point += data.force / 10;
}
usleep(100000);
}
printf("Weight value: %ld\n", weight_point);
float known_weight_val = 15000; // 1.5kg
while (true) {
err = get_data(imu_meta, imu, &data);
if (err < 0) {
printf("Error reading data\n");
} else {
printf("Force: %.3f\n", known_weight_val * (data.force - zero_point) / (weight_point - zero_point));
}
usleep(50000);
}
orb_unsubscribe(imu);
return EXIT_SUCCESS;
}
``SNIOC_GET_CALIBVALUE:``
----------------------------
This commands gets the gain calibration value when set by the ``SNIOC_CALIBRATE`` command.
.. code-block:: c
uint32_t cal_value;
orb_ioctl(sensor, SNIOC_GET_CALIBVALUE, (unsigned long)&cal_value);
``SNIOC_SET_CALIBVALUE:``
---------------------------
This commands sets the gain calibration value, useful when you calibrated the sensor with the gain calibration mode once and want to reuse it later on.
.. code-block:: c
uint32_t cal_value;
orb_ioctl(sensor, SNIOC_SET_CALIBVALUE, cal_value);
@@ -489,6 +489,7 @@ Implemented Drivers
- ltr308
- mpu9250
- ms56xx
- :doc:`nau7802`
- :doc:`sht4x`
- :doc:`lsm6dso32`
- wtgahrs2
+7
View File
@@ -194,6 +194,13 @@ config SENSORS_BH1750FVI
---help---
Enable driver support for the Rohm BH1750FVI light sensor.
config SENSORS_NAU7802
bool "Adafruit NAU7802 ADC sensor support"
default n
select I2C
---help---
Enable driver support for the Adafruit NAU7802 sensor.
config BH1750FVI_I2C_FREQUENCY
int "BH1750FVI I2C frequency"
default 400000
+5
View File
@@ -34,6 +34,11 @@ ifeq ($(CONFIG_SENSORS_RPMSG),y)
CSRCS += sensor_rpmsg.c
endif
ifeq ($(CONFIG_SENSORS_NAU7802),y)
CSRCS += nau7802.c
endif
ifeq ($(CONFIG_SENSORS_GNSS),y)
CSRCS += gnss_uorb.c
endif
File diff suppressed because it is too large Load Diff
+18
View File
@@ -480,6 +480,24 @@
#define SNIOC_LPF _SNIOC(0x00A0)
/* Command: SNIOC_SET_GAIN
* Description: Sets the gain of the sensor.
*/
#define SNIOC_SET_GAIN _SNIOC(0x00A1)
/* Command: SNIOC_SET_LDO
* Description: Sets the LDO voltage of the sensor.
*/
#define SNIOC_SET_LDO _SNIOC(0x00A2)
/* Command: SNIOC_GET_CALIBVALUE
* Description: Get the gain calibration value of the sensor.
*/
#define SNIOC_GET_CALIBVALUE _SNIOC(0x00A3)
/****************************************************************************
* Public types
****************************************************************************/
+108
View File
@@ -0,0 +1,108 @@
/****************************************************************************
* include/nuttx/sensors/nau7802.h
*
* SPDX-License-Identifier: Apache-2.0
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/irq.h>
#include <nuttx/sensors/ioctl.h>
/****************************************************************************
* Public Types
****************************************************************************/
struct i2c_master_s; /* Forward reference */
typedef int (*nau7802_attach)(xcpt_t, FAR void *arg);
/* Valid calibration modes */
typedef enum
{
NAU7802_CALMOD_INTERNAL = 0,
NAU7802_CALMOD_OFFSET = 2,
NAU7802_CALMOD_GAIN = 3
} nau7802_calibmode_e;
/* Valid LDO voltage settings */
typedef enum
{
NAU7802_LDO_V_4V5,
NAU7802_LDO_V_4V2,
NAU7802_LDO_V_3V9,
NAU7802_LDO_V_3V6,
NAU7802_LDO_V_3V3,
NAU7802_LDO_V_3V0,
NAU7802_LDO_V_2V7,
NAU7802_LDO_V_2V4,
NAU7802_LDO_V_EXTERNAL
} nau7802_ldo_e;
/* Valid gain settings */
typedef enum
{
NAU7802_GAIN_1,
NAU7802_GAIN_2,
NAU7802_GAIN_4,
NAU7802_GAIN_8,
NAU7802_GAIN_16,
NAU7802_GAIN_32,
NAU7802_GAIN_64,
NAU7802_GAIN_128
} nau7802_gain_e;
/* Valid odr values */
typedef enum
{
NAU7802_ODR_10HZ = 0,
NAU7802_ODR_20HZ = 1,
NAU7802_ODR_40HZ = 2,
NAU7802_ODR_80HZ = 3,
NAU7802_ODR_320HZ = 7
} nau7802_odr_e;
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: nau7802_register
*
* Description:
* Register the nau7802 device as a UORB sensor.
*
* Input Parameters:
* i2c - An instance of the I2C interface to use to communicate with
* the nau7802.
* addr - The I2C address of the nau7802. Should always be 0x2a.
* devno - The device number to use for the topic (i.e. /dev/mag0)
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
int nau7802_register(FAR struct i2c_master_s *i2c, int devno, uint8_t addr);