Add Vishay VEML6070 driver and support for STM32F103-Minimum board

This commit is contained in:
Alan Carvalho de Assis 2016-11-13 11:44:28 -06:00 committed by Gregory Nutt
parent c7b5f20b5d
commit a6b7730f8c
12 changed files with 2038 additions and 0 deletions

View File

@ -636,3 +636,9 @@ Where <subdir> is one of the following:
CONFIG_CDCACM_CONSOLE=y : The CDC/ACM serial device is NOT the console
CONFIG_PL2303=y : The Prolifics PL2303 emulation is enabled
CONFIG_PL2303_CONSOLE=y : The PL2303 serial device is the console
veml6070:
--------
This is a config example to use the Vishay VEML6070 UV-A sensor. To use this
sensor you need to connect PB6 (I2C1 CLK) to SCL; PB7 (I2C1 SDA) to SDA of
sensor module. I used a GY-VEML6070 module to test this driver.

View File

@ -73,4 +73,8 @@ ifeq ($(CONFIG_LCD_ST7567),y)
CSRCS += stm32_lcd.c
endif
ifeq ($(CONFIG_VEML6070),y)
CSRCS += stm32_veml6070.c
endif
include $(TOPDIR)/configs/Board.mk

View File

@ -163,5 +163,15 @@ int stm32_bringup(void)
}
#endif
#ifdef CONFIG_VEML6070
/* Register the UV-A light sensor */
ret = stm32_veml6070initialize("/dev/uvlight0");
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: stm32_veml6070initialize() failed: %d\n", ret);
}
#endif
return ret;
}

View File

@ -0,0 +1,105 @@
/************************************************************************************
* configs/stm32f103-minimum/src/stm32_veml6070.c
*
* Copyright (C) 2016 Alan Carvalho de Assis. All rights reserved.
* Author: Alan Carvalho de Assis <acassis@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 <nuttx/spi/spi.h>
#include <nuttx/sensors/veml6070.h>
#include "stm32.h"
#include "stm32_i2c.h"
#include "stm32f103_minimum.h"
#if defined(CONFIG_I2C) && defined(CONFIG_VEML6070)
/************************************************************************************
* Pre-processor Definitions
************************************************************************************/
#define VEML6070_I2C_PORTNO 1 /* On I2C1 */
/************************************************************************************
* Public Functions
************************************************************************************/
/************************************************************************************
* Name: stm32_veml6070initialize
*
* Description:
* Initialize and register the VEML6070 UV-A Light sensor.
*
* Input parameters:
* devpath - The full path to the driver to register. E.g., "/dev/uvlight0"
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
************************************************************************************/
int stm32_veml6070initialize(FAR const char *devpath)
{
FAR struct i2c_master_s *i2c;
int ret;
sninfo("Initializing VEML6070!\n");
/* Initialize I2C */
i2c = stm32_i2cbus_initialize(VEML6070_I2C_PORTNO);
if (!i2c)
{
return -ENODEV;
}
/* Then register the light sensor */
ret = veml6070_register(devpath, i2c, VEML6070_I2C_DATA_LSB_CMD_ADDR);
if (ret < 0)
{
snerr("ERROR: Error registering BM180\n");
}
return ret;
}
#endif /* CONFIG_I2C && CONFIG_VEML6070 && CONFIG_STM32_I2C1 */

View File

@ -192,5 +192,18 @@ int stm32_mfrc522initialize(FAR const char *devpath);
int stm32_tone_setup(void);
#endif
/***********************************************************************************
* Name: stm32_veml6070initialize
*
* Description:
* Called to configure an I2C and to register VEML6070 for the stm32f103-minimum
* board.
*
***********************************************************************************/
#ifdef CONFIG_VEML6070
int stm32_veml6070initialize(FAR const char *devpath);
#endif
#endif /* __ASSEMBLY__ */
#endif /* __CONFIGS_STM32F103_MINIMUM_SRC_STM32F103_MINIMUM_H */

View File

@ -0,0 +1,113 @@
############################################################################
# configs/stm32f103-minimum/vem6070/Make.defs
#
# Copyright (C) 2016 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# 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.
#
############################################################################
include ${TOPDIR}/.config
include ${TOPDIR}/tools/Config.mk
include ${TOPDIR}/arch/arm/src/armv7-m/Toolchain.defs
LDSCRIPT = ld.script
ifeq ($(WINTOOL),y)
# Windows-native toolchains
DIRLINK = $(TOPDIR)/tools/copydir.sh
DIRUNLINK = $(TOPDIR)/tools/unlink.sh
MKDEP = $(TOPDIR)/tools/mkwindeps.sh
ARCHINCLUDES = -I. -isystem "${shell cygpath -w $(TOPDIR)/include}"
ARCHXXINCLUDES = -I. -isystem "${shell cygpath -w $(TOPDIR)/include}" -isystem "${shell cygpath -w $(TOPDIR)/include/cxx}"
ARCHSCRIPT = -T "${shell cygpath -w $(TOPDIR)/configs/$(CONFIG_ARCH_BOARD)/scripts/$(LDSCRIPT)}"
else
# Linux/Cygwin-native toolchain
MKDEP = $(TOPDIR)/tools/mkdeps$(HOSTEXEEXT)
ARCHINCLUDES = -I. -isystem $(TOPDIR)/include
ARCHXXINCLUDES = -I. -isystem $(TOPDIR)/include -isystem $(TOPDIR)/include/cxx
ARCHSCRIPT = -T$(TOPDIR)/configs/$(CONFIG_ARCH_BOARD)/scripts/$(LDSCRIPT)
endif
CC = $(CROSSDEV)gcc
CXX = $(CROSSDEV)g++
CPP = $(CROSSDEV)gcc -E
LD = $(CROSSDEV)ld
AR = $(CROSSDEV)ar rcs
NM = $(CROSSDEV)nm
OBJCOPY = $(CROSSDEV)objcopy
OBJDUMP = $(CROSSDEV)objdump
ARCHCCVERSION = ${shell $(CC) -v 2>&1 | sed -n '/^gcc version/p' | sed -e 's/^gcc version \([0-9\.]\)/\1/g' -e 's/[-\ ].*//g' -e '1q'}
ARCHCCMAJOR = ${shell echo $(ARCHCCVERSION) | cut -d'.' -f1}
ifeq ($(CONFIG_DEBUG_SYMBOLS),y)
ARCHOPTIMIZATION = -g
endif
ifneq ($(CONFIG_DEBUG_NOOPT),y)
ARCHOPTIMIZATION += $(MAXOPTIMIZATION) -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer
endif
ARCHCFLAGS = -fno-builtin
ARCHCXXFLAGS = -fno-builtin -fno-exceptions -fcheck-new
ARCHWARNINGS = -Wall -Wstrict-prototypes -Wshadow -Wundef
ARCHWARNINGSXX = -Wall -Wshadow -Wundef
ARCHDEFINES =
ARCHPICFLAGS = -fpic -msingle-pic-base -mpic-register=r10
CFLAGS = $(ARCHCFLAGS) $(ARCHWARNINGS) $(ARCHOPTIMIZATION) $(ARCHCPUFLAGS) $(ARCHINCLUDES) $(ARCHDEFINES) $(EXTRADEFINES) -pipe
CPICFLAGS = $(ARCHPICFLAGS) $(CFLAGS)
CXXFLAGS = $(ARCHCXXFLAGS) $(ARCHWARNINGSXX) $(ARCHOPTIMIZATION) $(ARCHCPUFLAGS) $(ARCHXXINCLUDES) $(ARCHDEFINES) $(EXTRADEFINES) -pipe
CXXPICFLAGS = $(ARCHPICFLAGS) $(CXXFLAGS)
CPPFLAGS = $(ARCHINCLUDES) $(ARCHDEFINES) $(EXTRADEFINES)
AFLAGS = $(CFLAGS) -D__ASSEMBLY__
NXFLATLDFLAGS1 = -r -d -warn-common
NXFLATLDFLAGS2 = $(NXFLATLDFLAGS1) -T$(TOPDIR)/binfmt/libnxflat/gnu-nxflat-pcrel.ld -no-check-sections
LDNXFLATFLAGS = -e main -s 2048
ASMEXT = .S
OBJEXT = .o
LIBEXT = .a
EXEEXT =
ifneq ($(CROSSDEV),arm-nuttx-elf-)
LDFLAGS += -nostartfiles -nodefaultlibs
endif
ifeq ($(CONFIG_DEBUG_SYMBOLS),y)
LDFLAGS += -g
endif
HOSTCC = gcc
HOSTINCLUDES = -I.
HOSTCFLAGS = -Wall -Wstrict-prototypes -Wshadow -Wundef -g -pipe
HOSTLDFLAGS =

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,100 @@
#!/bin/bash
# configs//stm32f103-minimum/veml6070/setenv.sh
#
# Copyright (C) 2016 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# 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.
#
# 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.
#
if [ "$_" = "$0" ] ; then
echo "You must source this script, not run it!" 1>&2
exit 1
fi
WD=`pwd`
if [ ! -x "setenv.sh" ]; then
echo "This script must be executed from the top-level NuttX build directory"
exit 1
fi
if [ -z "${PATH_ORIG}" ]; then
export PATH_ORIG="${PATH}"
fi
# This is the Cygwin path to the location where I installed the CodeSourcery
# toolchain under windows. You will also have to edit this if you install
# the CodeSourcery toolchain in any other location
#export TOOLCHAIN_BIN="/cygdrive/c/Program Files (x86)/CodeSourcery/Sourcery G++ Lite/bin"
#export TOOLCHAIN_BIN="/cygdrive/c/Program Files (x86)/CodeSourcery/Sourcery_CodeBench_Lite_for_ARM_EABI/bin"
# export TOOLCHAIN_BIN="/cygdrive/c/Users/MyName/MentorGraphics/Sourcery_CodeBench_Lite_for_ARM_EABI/bin"
# This is the location where I installed the ARM "GNU Tools for ARM Embedded Processors"
# You can this free toolchain here https://launchpad.net/gcc-arm-embedded
export TOOLCHAIN_BIN="/cygdrive/c/Program Files (x86)/GNU Tools ARM Embedded/4.9 2015q2/bin"
# This is the path to the location where I installed the devkitARM toolchain
# You can get this free toolchain from http://devkitpro.org/ or http://sourceforge.net/projects/devkitpro/
#export TOOLCHAIN_BIN="/cygdrive/c/Program Files (x86)/devkitARM/bin"
# This is the Cygwin path to the location where I build the buildroot
# toolchain.
# export TOOLCHAIN_BIN="${WD}/../buildroot/build_arm_nofpu/staging_dir/bin"
# Add the path to the toolchain to the PATH varialble
export PATH="${TOOLCHAIN_BIN}:/sbin:/usr/sbin:${PATH_ORIG}"
echo "PATH : ${PATH}"

View File

@ -234,6 +234,13 @@ config QENCODER
bool "Qencoder"
default n
config VEML6070
bool "Vishay VEML6070 UV-A Light Sensor support"
default n
select I2C
---help---
Enable driver support for the Vishay VEML6070 UV-A light sensor.
config XEN1210
bool "Sensixs XEN1210 Magnetometer"
default n

View File

@ -138,6 +138,12 @@ ifeq ($(CONFIG_QENCODER),y)
CSRCS += qencoder.c
endif
# Vishay VEML6070
ifeq ($(CONFIG_VEML6070),y)
CSRCS += veml6070.c
endif
# Sensixs XEN1210
ifeq ($(CONFIG_XEN1210),y)

352
drivers/sensors/veml6070.c Normal file
View File

@ -0,0 +1,352 @@
/****************************************************************************
* drivers/sensors/veml6070.c
* Character driver for the Vishay UV-A Light Sensor VEML6070
*
* Copyright (C) 2016 Alan Carvalho de Assis. All rights reserved.
* Author: Alan Carvalho de Assis <acassis@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/i2c_master.h>
#include <nuttx/sensors/veml6070.h>
#if defined(CONFIG_I2C) && defined(CONFIG_VEML6070)
/****************************************************************************
* 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,
FAR uint8_t *regval);
static int veml6070_write8(FAR struct veml6070_dev_s *priv,
uint8_t regval);
/* Character driver methods */
static int veml6070_open(FAR struct file *filep);
static int veml6070_close(FAR struct file *filep);
static ssize_t veml6070_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
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 =
{
veml6070_open, /* open */
veml6070_close, /* close */
veml6070_read, /* read */
veml6070_write, /* write */
NULL, /* seek */
NULL /* ioctl */
#ifndef CONFIG_DISABLE_POLL
, NULL /* poll */
#endif
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
, NULL /* unlink */
#endif
};
/****************************************************************************
* 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, &regval, 1);
if (ret < 0)
{
snerr("ERROR: i2c_write failed: %d\n", ret);
}
return ret;
}
/****************************************************************************
* Name: veml6070_open
*
* Description:
* This function is called whenever the VEML6070 device is opened.
*
****************************************************************************/
static int veml6070_open(FAR struct file *filep)
{
return OK;
}
/****************************************************************************
* Name: veml6070_close
*
* Description:
* This routine is called when the VEML6070 device is closed.
*
****************************************************************************/
static int veml6070_close(FAR struct file *filep)
{
return OK;
}
/****************************************************************************
* 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;
DEBUGASSERT(filep);
inode = filep->f_inode;
DEBUGASSERT(inode && inode->i_private);
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 */
usleep(125000);
/* Read the MSB first */
ret = veml6070_read8(priv, msb, (FAR uint8_t *) &regdata);
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 *) &regdata);
if (ret < 0)
{
snerr("ERROR: Error reading light sensor!\n");
return ret;
}
buffer[0] = regdata;
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"
* i2c - An instance of the I2C interface to use to communicate with VEML6070
* 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;
}
#endif /* CONFIG_I2C && CONFIG_VEML6070 */

View File

@ -0,0 +1,121 @@
/****************************************************************************
* include/nuttx/input/veml6070.h
*
* Copyright (C) 2015-2016 Gregory Nutt. All rights reserved.
* Copyright (C) 2016 Alan Carvalho de Assis. All rights reserved.
* Author: Alan Carvalho de Assis <acassis@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.
*
****************************************************************************/
#ifndef __INCLUDE_NUTTX_SENSORS_VEML6070_H
#define __INCLUDE_NUTTX_SENSORS_VEML6070_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/irq.h>
#include <nuttx/sensors/ioctl.h>
#if defined(CONFIG_VEML6070)
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Device I2C Address */
#define VEML6070_I2C_DATA_LSB_CMD_ADDR 0x38
#define VEML6070_I2C_DATA_MSB_ADDR 0x39
/* Command Register Format
* Bits:
* 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
* RSV | RSV | ACK | ACK_THD | IT1 | IT0 | RSV | SD |
*
* NOTE: The RSV Bit 1 needs to be always 1
*/
#define VEML6070_CMD_SD 0x01 /* Shutdown command */
#define VEML6070_CMD_RSV 0x02
#define VEML6070_CMD_IT_0_5T 0x00 /* IT1=0 : IT0=0 */
#define VEML6070_CMD_IT_1T 0x04 /* IT1=0 : IT0=1 */
#define VEML6070_CMD_IT_2T 0x08 /* IT1=1 : IT0=0 */
#define VEML6070_CMD_IT_4T 0x0c /* IT1=1 : IT0=1 */
#define VEML6070_CMD_ACK_THD 0x10 /* Acknowledge thresold:
0 = 102 steps
1 = 145 steps */
#define VEML6070_CMD_ACK 0x20 /* Acknowledge activity */
/****************************************************************************
* Public Types
****************************************************************************/
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* 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"
* i2c - An instance of the I2C interface to use to communicate with
* VEML6070
* 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);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* CONFIG_VEML6070 */
#endif /* __INCLUDE_NUTTX_SENSORS_VEML6070_H */