2016-11-13 18:44:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/sensors/veml6070.c
|
|
|
|
*
|
2021-03-13 10:55:07 +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
|
2016-11-13 18:44:28 +01:00
|
|
|
*
|
2021-03-13 10:55:07 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-11-13 18:44:28 +01:00
|
|
|
*
|
2021-03-13 10:55:07 +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.
|
2016-11-13 18:44:28 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-13 10:55:07 +01:00
|
|
|
/* Character driver for the Vishay UV-A Light Sensor VEML6070 */
|
|
|
|
|
2016-11-13 18:44:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2016-11-13 18:44:28 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
|
|
|
#include <nuttx/random.h>
|
2016-11-13 18:44:28 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/i2c/i2c_master.h>
|
|
|
|
#include <nuttx/sensors/veml6070.h>
|
|
|
|
|
2017-08-24 18:26:53 +02:00
|
|
|
#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_VEML6070)
|
2016-11-13 18:44:28 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-process Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_VEML6070_I2C_FREQUENCY
|
|
|
|
# define CONFIG_VEML6070_I2C_FREQUENCY 100000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct veml6070_dev_s
|
|
|
|
{
|
|
|
|
FAR struct i2c_master_s *i2c; /* I2C interface */
|
|
|
|
uint8_t addr; /* I2C address */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* I2C Helpers */
|
|
|
|
|
|
|
|
static int veml6070_read8(FAR struct veml6070_dev_s *priv, int offset,
|
2022-04-12 12:16:13 +02:00
|
|
|
FAR uint8_t *regval);
|
2016-11-13 18:44:28 +01:00
|
|
|
static int veml6070_write8(FAR struct veml6070_dev_s *priv,
|
2022-04-12 12:16:13 +02:00
|
|
|
uint8_t regval);
|
2016-11-13 18:44:28 +01:00
|
|
|
|
|
|
|
/* Character driver methods */
|
|
|
|
|
|
|
|
static ssize_t veml6070_read(FAR struct file *filep, FAR char *buffer,
|
2022-04-12 12:16:13 +02:00
|
|
|
size_t buflen);
|
2016-11-13 18:44:28 +01:00
|
|
|
static ssize_t veml6070_write(FAR struct file *filep,
|
|
|
|
FAR const char *buffer, size_t buflen);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_veml6070_fops =
|
|
|
|
{
|
2022-04-12 12:16:13 +02:00
|
|
|
NULL, /* open */
|
|
|
|
NULL, /* close */
|
2016-11-13 18:44:28 +01:00
|
|
|
veml6070_read, /* read */
|
|
|
|
veml6070_write, /* write */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: veml6070_read8
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read 8-bit register
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int veml6070_read8(FAR struct veml6070_dev_s *priv, int offset,
|
|
|
|
FAR uint8_t *regval)
|
|
|
|
{
|
|
|
|
struct i2c_config_s config;
|
|
|
|
uint8_t data[1];
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
/* Set up the I2C configuration */
|
|
|
|
|
|
|
|
config.frequency = CONFIG_VEML6070_I2C_FREQUENCY;
|
|
|
|
config.address = priv->addr + offset;
|
|
|
|
config.addrlen = 7;
|
|
|
|
|
|
|
|
/* Read 8-bits from the device */
|
|
|
|
|
|
|
|
ret = i2c_read(priv->i2c, &config, data, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr ("i2c_read failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the content of the buffer to the location of the uint8_t pointer */
|
|
|
|
|
|
|
|
*regval = data[0];
|
|
|
|
|
|
|
|
sninfo("value: %08x ret: %d\n", *regval, ret);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: veml6070_write8
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write from an 8-bit register
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int veml6070_write8(FAR struct veml6070_dev_s *priv, uint8_t regval)
|
|
|
|
{
|
|
|
|
struct i2c_config_s config;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
sninfo("value: %02x\n", regval);
|
|
|
|
|
|
|
|
/* Set up the I2C configuration */
|
|
|
|
|
|
|
|
config.frequency = CONFIG_VEML6070_I2C_FREQUENCY;
|
|
|
|
config.address = priv->addr;
|
|
|
|
config.addrlen = 7;
|
|
|
|
|
|
|
|
/* Write 8 bits to device */
|
|
|
|
|
|
|
|
ret = i2c_write(priv->i2c, &config, ®val, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("ERROR: i2c_write failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: veml6070_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t veml6070_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct veml6070_dev_s *priv;
|
|
|
|
int msb = 1;
|
|
|
|
uint16_t regdata;
|
|
|
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2016-11-13 18:44:28 +01:00
|
|
|
priv = (FAR struct veml6070_dev_s *)inode->i_private;
|
|
|
|
|
|
|
|
/* Check if the user is reading the right size */
|
|
|
|
|
|
|
|
if (buflen != 2)
|
|
|
|
{
|
|
|
|
snerr("ERROR: You need to read 2 bytes from this sensor!\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable the sensor */
|
|
|
|
|
|
|
|
ret = veml6070_write8(priv, VEML6070_CMD_RSV & ~VEML6070_CMD_SD);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("ERROR: Failed to enable the VEML6070!\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 1T for Rset 270Kohms is 125ms */
|
|
|
|
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(125000);
|
2016-11-13 18:44:28 +01:00
|
|
|
|
|
|
|
/* Read the MSB first */
|
|
|
|
|
|
|
|
ret = veml6070_read8(priv, msb, (FAR uint8_t *) ®data);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("ERROR: Error reading light sensor!\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[1] = regdata;
|
|
|
|
|
|
|
|
/* Read the LSB */
|
|
|
|
|
|
|
|
msb = 0;
|
|
|
|
ret = veml6070_read8(priv, msb, (FAR uint8_t *) ®data);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("ERROR: Error reading light sensor!\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[0] = regdata;
|
|
|
|
|
2017-03-30 15:38:37 +02:00
|
|
|
/* Feed sensor data to entropy pool */
|
|
|
|
|
|
|
|
add_sensor_randomness((buffer[1] << 16) ^ buffer[0]);
|
|
|
|
|
2016-11-13 18:44:28 +01:00
|
|
|
return buflen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: veml6070_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t veml6070_write(FAR struct file *filep,
|
|
|
|
FAR const char *buffer, size_t buflen)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: veml6070_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register the VEML6070 character device as 'devpath'
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* devpath - The full path to the driver to register. E.g., "/dev/uvlight0"
|
2021-03-13 11:55:37 +01:00
|
|
|
* i2c - An instance of the I2C interface to use to communicate with
|
|
|
|
* VEML6070
|
2016-11-13 18:44:28 +01:00
|
|
|
* addr - The I2C address of the VEML6070.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int veml6070_register(FAR const char *devpath, FAR struct i2c_master_s *i2c,
|
|
|
|
uint8_t addr)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
DEBUGASSERT(i2c != NULL);
|
|
|
|
|
|
|
|
/* Initialize the VEML6070 device structure */
|
|
|
|
|
|
|
|
FAR struct veml6070_dev_s *priv =
|
|
|
|
(FAR struct veml6070_dev_s *)kmm_malloc(sizeof(struct veml6070_dev_s));
|
|
|
|
|
|
|
|
if (priv == NULL)
|
|
|
|
{
|
|
|
|
snerr("ERROR: Failed to allocate instance\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->i2c = i2c;
|
|
|
|
priv->addr = addr;
|
|
|
|
|
|
|
|
/* Initialize the device (shut it down) */
|
|
|
|
|
|
|
|
ret = veml6070_write8(priv, VEML6070_CMD_RSV | VEML6070_CMD_SD);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("ERROR: Failed to initialize the VEML6070!\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register the character driver */
|
|
|
|
|
|
|
|
ret = register_driver(devpath, &g_veml6070_fops, 0666, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
snerr("ERROR: Failed to register driver: %d\n", ret);
|
|
|
|
kmm_free(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-24 18:26:53 +02:00
|
|
|
#endif /* CONFIG_I2C && CONFIG_SENSORS_VEML6070 */
|