sensors/bh1749nuc: add support for the new sensor framework

bh1749nuc can be used with the new sensor framework
This commit is contained in:
raiden00pl
2024-03-02 09:28:16 +01:00
committed by Alan Carvalho de Assis
parent 7c4e3aff14
commit 41803a0c20
8 changed files with 870 additions and 200 deletions
+9
View File
@@ -60,6 +60,15 @@ if(CONFIG_SENSORS)
list(APPEND SRCS adxl362_uorb.c)
endif()
if(CONFIG_SENSORS_BH1749NUC)
list(APPEND SRCS bh1749nuc_base.c)
if(CONFIG_SENSORS_BH1749NUC_UORB)
list(APPEND SRCS bh1749nuc_uorb.c)
else()
list(APPEND SRCS bh1749nuc.c)
endif()
endif()
if(CONFIG_SENSORS_DHTXX)
list(APPEND SRCS dhtxx.c)
endif()
+33
View File
@@ -141,6 +141,39 @@ config SENSORS_BH1749NUC
---help---
Enable driver support for the Rohm BH1749NUC color sensor.
if SENSORS_BH1749NUC
config SENSORS_BH1749NUC_UORB
bool "BH1749NUC UORB Interface"
default n
if SENSORS_BH1749NUC_UORB
config SENSORS_BH1749NUC_POLL
bool "Enables polling sensor data"
default n
---help---
Enables polling of sensor.
config SENSORS_BH1749NUC_POLL_INTERVAL
int "Polling interval in microseconds, default 1 sec"
depends on SENSORS_BH1749NUC_POLL
default 1000000
range 0 4294967295
---help---
The interval until a new sensor measurement will be triggered.
config SENSORS_BH1749NUC_THREAD_STACKSIZE
int "Worker thread stack size"
depends on SENSORS_BH1749NUC_POLL
default 1024
---help---
The stack size for the worker thread.
endif #SENSORS_BH1749NUC_UORB
endif # SENSORS_BH1749NUC
config SENSORS_BH1750FVI
bool "Rohm BH1750FVI Ambient Light Sensor support"
default n
+5
View File
@@ -147,8 +147,13 @@ ifeq ($(CONFIG_ADXL345_I2C),y)
endif
ifeq ($(CONFIG_SENSORS_BH1749NUC),y)
CSRCS += bh1749nuc_base.c
ifeq ($(CONFIG_SENSORS_BH1749NUC_UORB),y)
CSRCS += bh1749nuc_uorb.c
else
CSRCS += bh1749nuc.c
endif
endif
ifeq ($(CONFIG_SENSORS_BH1750FVI),y)
CSRCS += bh1750fvi.c
+1 -200
View File
@@ -24,66 +24,7 @@
#include <nuttx/config.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/kmalloc.h>
#include <nuttx/i2c/i2c_master.h>
#include <nuttx/sensors/bh1749nuc.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define BH1749NUC_I2C_FREQ 400000
#define BH1749NUC_MANUFACTID 0xE0 /* Manufact ID */
#define BH1749NUC_PARTID 0x0D /* Part ID */
/* BH1749NUC Registers */
#define BH1749NUC_SYSTEM_CONTROL 0x40
#define BH1749NUC_MODE_CONTROL1 0x41
#define BH1749NUC_MODE_CONTROL2 0x42
#define BH1749NUC_RED_DATA_LSB 0x50
#define BH1749NUC_GREEN_DATA_LSB 0x52
#define BH1749NUC_BLUE_DATA_LSB 0x54
#define BH1749NUC_IR_DATA_LSB 0x58
#define BH1749NUC_GREEN2_DATA_LSB 0x5a
#define BH1749NUC_MANUFACTURER_ID 0x92
/* Register SYSTEM_CONTROL */
#define BH1749NUC_SYSTEM_CONTROL_SW_RESET (1 << 7)
#define BH1749NUC_SYSTEM_CONTROL_INT_RESET (1 << 6)
/* Register MODE_CONTROL1 */
#define BH1749NUC_MODE_CONTROL1_IR_GAIN_X1 (0x20)
#define BH1749NUC_MODE_CONTROL1_IR_GAIN_X32 (0x60)
#define BH1749NUC_MODE_CONTROL1_RGB_GAIN_X1 (0x08)
#define BH1749NUC_MODE_CONTROL1_RGB_GAIN_X32 (0x18)
#define BH1749NUC_MODE_CONTROL1_MEAS_TIME160MS (0x02)
/* Register MODE_CONTROL2 */
#define BH1749NUC_MODE_CONTROL2_RGBI_EN (1 << 4)
/****************************************************************************
* Private Type Definitions
****************************************************************************/
/* Structure for bh1749nuc device */
struct bh1749nuc_dev_s
{
FAR struct i2c_master_s *i2c; /* I2C interface */
int freq; /* Frequency */
uint8_t addr; /* I2C address */
};
#include "bh1749nuc_base.h"
/****************************************************************************
* Private Function Prototypes
@@ -120,146 +61,6 @@ static const struct file_operations g_bh1749nucfops =
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: bh1749nuc_getreg8
*
* Description:
* Read from an 8-bit BH1749NUC register
*
****************************************************************************/
static uint8_t bh1749nuc_getreg8(FAR struct bh1749nuc_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 = I2C_M_NOSTOP;
msg[0].buffer = &regaddr;
msg[0].length = 1;
msg[1].frequency = priv->freq;
msg[1].addr = priv->addr;
msg[1].flags = I2C_M_READ;
msg[1].buffer = &regval;
msg[1].length = 1;
ret = I2C_TRANSFER(priv->i2c, msg, 2);
if (ret < 0)
{
snerr("I2C_TRANSFER failed: %d\n", ret);
}
return regval;
}
/****************************************************************************
* Name: bh1749nuc_read16
*
* Description:
* Read 16-bit register
*
****************************************************************************/
static uint16_t bh1749nuc_read16(FAR struct bh1749nuc_dev_s *priv,
uint8_t regaddr)
{
struct i2c_msg_s msg[2];
uint8_t regval[2];
int ret;
msg[0].frequency = priv->freq;
msg[0].addr = priv->addr;
msg[0].flags = I2C_M_NOSTOP;
msg[0].buffer = &regaddr;
msg[0].length = 1;
msg[1].frequency = priv->freq;
msg[1].addr = priv->addr;
msg[1].flags = I2C_M_READ;
msg[1].buffer = (uint8_t *)&regval;
msg[1].length = 2;
ret = I2C_TRANSFER(priv->i2c, msg, 2);
if (ret < 0)
{
snerr("I2C_TRANSFER failed: %d\n", ret);
}
return regval[1] << 8 | regval[0] << 0;
}
/****************************************************************************
* Name: bh1749nuc_putreg8
*
* Description:
* Write to an 8-bit BH1749NUC register
*
****************************************************************************/
static void bh1749nuc_putreg8(FAR struct bh1749nuc_dev_s *priv,
uint8_t regaddr, uint8_t regval)
{
struct i2c_msg_s msg[2];
uint8_t txbuffer[2];
int ret;
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);
}
}
/****************************************************************************
* Name: bh1749nuc_checkid
*
* Description:
* Read and verify the BH1749NUC chip ID
*
****************************************************************************/
static int bh1749nuc_checkid(FAR struct bh1749nuc_dev_s *priv)
{
uint8_t id;
/* Read Manufact ID */
id = bh1749nuc_getreg8(priv, BH1749NUC_MANUFACTURER_ID);
if (id != BH1749NUC_MANUFACTID)
{
/* Manufact ID is not Correct */
snerr("Wrong Manufact ID! %02x\n", id);
return -ENODEV;
}
/* Read Part ID */
id = bh1749nuc_getreg8(priv, BH1749NUC_SYSTEM_CONTROL);
if ((id & 0x3f) != BH1749NUC_PARTID)
{
/* Part ID is not Correct */
snerr("Wrong Part ID! %02x\n", id);
return -ENODEV;
}
return OK;
}
/****************************************************************************
* Name: bh1749nuc_open
*
+173
View File
@@ -0,0 +1,173 @@
/****************************************************************************
* drivers/sensors/bh1749nuc_base.c
*
* 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/config.h>
#include "bh1749nuc_base.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: bh1749nuc_getreg8
*
* Description:
* Read from an 8-bit BH1749NUC register
*
****************************************************************************/
uint8_t bh1749nuc_getreg8(FAR struct bh1749nuc_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 = I2C_M_NOSTOP;
msg[0].buffer = &regaddr;
msg[0].length = 1;
msg[1].frequency = priv->freq;
msg[1].addr = priv->addr;
msg[1].flags = I2C_M_READ;
msg[1].buffer = &regval;
msg[1].length = 1;
ret = I2C_TRANSFER(priv->i2c, msg, 2);
if (ret < 0)
{
snerr("I2C_TRANSFER failed: %d\n", ret);
}
return regval;
}
/****************************************************************************
* Name: bh1749nuc_read16
*
* Description:
* Read 16-bit register
*
****************************************************************************/
uint16_t bh1749nuc_read16(FAR struct bh1749nuc_dev_s *priv, uint8_t regaddr)
{
struct i2c_msg_s msg[2];
uint8_t regval[2];
int ret;
msg[0].frequency = priv->freq;
msg[0].addr = priv->addr;
msg[0].flags = I2C_M_NOSTOP;
msg[0].buffer = &regaddr;
msg[0].length = 1;
msg[1].frequency = priv->freq;
msg[1].addr = priv->addr;
msg[1].flags = I2C_M_READ;
msg[1].buffer = (uint8_t *)&regval;
msg[1].length = 2;
ret = I2C_TRANSFER(priv->i2c, msg, 2);
if (ret < 0)
{
snerr("I2C_TRANSFER failed: %d\n", ret);
}
return regval[1] << 8 | regval[0] << 0;
}
/****************************************************************************
* Name: bh1749nuc_putreg8
*
* Description:
* Write to an 8-bit BH1749NUC register
*
****************************************************************************/
void bh1749nuc_putreg8(FAR struct bh1749nuc_dev_s *priv,
uint8_t regaddr, uint8_t regval)
{
struct i2c_msg_s msg[2];
uint8_t txbuffer[2];
int ret;
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);
}
}
/****************************************************************************
* Name: bh1749nuc_checkid
*
* Description:
* Read and verify the BH1749NUC chip ID
*
****************************************************************************/
int bh1749nuc_checkid(FAR struct bh1749nuc_dev_s *priv)
{
uint8_t id;
/* Read Manufact ID */
id = bh1749nuc_getreg8(priv, BH1749NUC_MANUFACTURER_ID);
if (id != BH1749NUC_MANUFACTID)
{
/* Manufact ID is not Correct */
snerr("Wrong Manufact ID! %02x\n", id);
return -ENODEV;
}
/* Read Part ID */
id = bh1749nuc_getreg8(priv, BH1749NUC_SYSTEM_CONTROL);
if ((id & 0x3f) != BH1749NUC_PARTID)
{
/* Part ID is not Correct */
snerr("Wrong Part ID! %02x\n", id);
return -ENODEV;
}
return OK;
}
+102
View File
@@ -0,0 +1,102 @@
/****************************************************************************
* drivers/sensors/bh1749nuc_base.h
*
* 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.
*
****************************************************************************/
#ifndef __INCLUDE_NUTTX_SENSORS_BH1749NUC_BASE_H
#define __INCLUDE_NUTTX_SENSORS_BH1749NUC_BASE_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/kmalloc.h>
#include <nuttx/i2c/i2c_master.h>
#include <nuttx/sensors/bh1749nuc.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define BH1749NUC_I2C_FREQ 400000
#define BH1749NUC_MANUFACTID 0xE0 /* Manufact ID */
#define BH1749NUC_PARTID 0x0D /* Part ID */
/* BH1749NUC Registers */
#define BH1749NUC_SYSTEM_CONTROL 0x40
#define BH1749NUC_MODE_CONTROL1 0x41
#define BH1749NUC_MODE_CONTROL2 0x42
#define BH1749NUC_RED_DATA_LSB 0x50
#define BH1749NUC_GREEN_DATA_LSB 0x52
#define BH1749NUC_BLUE_DATA_LSB 0x54
#define BH1749NUC_IR_DATA_LSB 0x58
#define BH1749NUC_GREEN2_DATA_LSB 0x5a
#define BH1749NUC_MANUFACTURER_ID 0x92
/* Register SYSTEM_CONTROL */
#define BH1749NUC_SYSTEM_CONTROL_SW_RESET (1 << 7)
#define BH1749NUC_SYSTEM_CONTROL_INT_RESET (1 << 6)
/* Register MODE_CONTROL1 */
#define BH1749NUC_MODE_CONTROL1_IR_GAIN_X1 (0x20)
#define BH1749NUC_MODE_CONTROL1_IR_GAIN_X32 (0x60)
#define BH1749NUC_MODE_CONTROL1_RGB_GAIN_X1 (0x08)
#define BH1749NUC_MODE_CONTROL1_RGB_GAIN_X32 (0x18)
#define BH1749NUC_MODE_CONTROL1_MEAS_TIME160MS (0x02)
/* Register MODE_CONTROL2 */
#define BH1749NUC_MODE_CONTROL2_RGBI_EN (1 << 4)
#define BH1749NUC_MODE_CONTROL2_VALID (1 << 7)
/****************************************************************************
* Public Types
****************************************************************************/
/* Structure for bh1749nuc device */
struct bh1749nuc_dev_s
{
FAR struct i2c_master_s *i2c; /* I2C interface */
int freq; /* Frequency */
uint8_t addr; /* I2C address */
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
uint8_t bh1749nuc_getreg8(FAR struct bh1749nuc_dev_s *priv, uint8_t regaddr);
uint16_t bh1749nuc_read16(FAR struct bh1749nuc_dev_s *priv, uint8_t regaddr);
void bh1749nuc_putreg8(FAR struct bh1749nuc_dev_s *priv,
uint8_t regaddr, uint8_t regval);
int bh1749nuc_checkid(FAR struct bh1749nuc_dev_s *priv);
#endif /* __INCLUDE_NUTTX_SENSORS_BH1749NUC_BASE_H */
File diff suppressed because it is too large Load Diff
+11
View File
@@ -46,6 +46,12 @@ struct bh1749nuc_data_s
uint16_t green2;
};
struct bh1749nuc_config_s
{
FAR struct i2c_master_s *i2c;
uint8_t addr;
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
@@ -75,8 +81,13 @@ extern "C"
*
****************************************************************************/
#ifndef CONFIG_SENSORS_BH1749NUC_UORB
int bh1749nuc_register(FAR const char *devpath, FAR struct i2c_master_s *i2c,
uint8_t addr);
#else
int bh1749nuc_register_uorb(int devno,
FAR struct bh1749nuc_config_s *config);
#endif
#undef EXTERN
#ifdef __cplusplus