From e4339355e4614905e0850f87668b56e92773ca9e Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Wed, 11 Oct 2017 10:49:48 -0600 Subject: [PATCH] examples/ina219: A simple infinite loop that polls the INA219 sensor and displays the measurements. --- examples/README.txt | 6 +++ examples/ina219/.gitignore | 11 +++++ examples/ina219/Kconfig | 30 ++++++++++++++ examples/ina219/Make.defs | 39 ++++++++++++++++++ examples/ina219/Makefile | 58 ++++++++++++++++++++++++++ examples/ina219/ina219_main.c | 78 +++++++++++++++++++++++++++++++++++ 6 files changed, 222 insertions(+) create mode 100644 examples/ina219/.gitignore create mode 100644 examples/ina219/Kconfig create mode 100644 examples/ina219/Make.defs create mode 100644 examples/ina219/Makefile create mode 100644 examples/ina219/ina219_main.c diff --git a/examples/README.txt b/examples/README.txt index e77dee047..005612af2 100644 --- a/examples/README.txt +++ b/examples/README.txt @@ -701,6 +701,12 @@ examples/i2cchar each time that this test executes. Not available in the kernel build mode. +examples/ina219 +^^^^^^^^^^^^^^^ + + This is a simple infinite loop that polls the INA219 sensor and displays + the measurements. + examples/ipforward ^^^^^^^^^^^^^^^^^^ diff --git a/examples/ina219/.gitignore b/examples/ina219/.gitignore new file mode 100644 index 000000000..fa1ec7579 --- /dev/null +++ b/examples/ina219/.gitignore @@ -0,0 +1,11 @@ +/Make.dep +/.depend +/.built +/*.asm +/*.obj +/*.rel +/*.lst +/*.sym +/*.adb +/*.lib +/*.src diff --git a/examples/ina219/Kconfig b/examples/ina219/Kconfig new file mode 100644 index 000000000..c882fe588 --- /dev/null +++ b/examples/ina219/Kconfig @@ -0,0 +1,30 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_INA219 + bool "INA219 example" + default n + ---help--- + Enable the INA219 example + +if EXAMPLES_INA219 + +config EXAMPLES_INA219_PROGNAME + string "Program name" + default "ina219" + depends on BUILD_KERNEL + ---help--- + This is the name of the program that will be use when the NSH ELF + program is installed. + +config EXAMPLES_INA219_PRIORITY + int "INA219 task priority" + default 100 + +config EXAMPLES_INA219_STACKSIZE + int "INA219 stack size" + default 2048 + +endif diff --git a/examples/ina219/Make.defs b/examples/ina219/Make.defs new file mode 100644 index 000000000..b48e9af79 --- /dev/null +++ b/examples/ina219/Make.defs @@ -0,0 +1,39 @@ +############################################################################ +# apps/examples/ina219/Make.defs +# Adds selected applications to apps/ build +# +# Copyright (C) 2017 Sebastien Lorquet. All rights reserved. +# Author: Sebastien Lorquet +# +# 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. +# +############################################################################ + +ifeq ($(CONFIG_EXAMPLES_INA219),y) +CONFIGURED_APPS += examples/ina219 +endif diff --git a/examples/ina219/Makefile b/examples/ina219/Makefile new file mode 100644 index 000000000..37c151a26 --- /dev/null +++ b/examples/ina219/Makefile @@ -0,0 +1,58 @@ +############################################################################ +# apps/examples/ina219/Makefile +# +# Copyright (C) 2017 Sebastien Lorquet. All rights reserved. +# Author: Sebastien Lorquet +# Copyright (C) 2008, 2010-2013, 2017 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# 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)/Make.defs + +# Hello, World! built-in application info + +CONFIG_EXAMPLES_INA219_PRIORITY ?= SCHED_PRIORITY_DEFAULT +CONFIG_EXAMPLES_INA219_STACKSIZE ?= 2048 + +APPNAME = ina219 +PRIORITY = $(CONFIG_EXAMPLES_INA219_PRIORITY) +STACKSIZE = $(CONFIG_EXAMPLES_INA219_STACKSIZE) + +# Hello, World! Example + +ASRCS = +CSRCS = +MAINSRC = ina219_main.c + +CONFIG_EXAMPLES_INA219_PROGNAME ?= ina219$(EXEEXT) +PROGNAME = $(CONFIG_EXAMPLES_INA219_PROGNAME) + +include $(APPDIR)/Application.mk diff --git a/examples/ina219/ina219_main.c b/examples/ina219/ina219_main.c new file mode 100644 index 000000000..0b81bcaa2 --- /dev/null +++ b/examples/ina219/ina219_main.c @@ -0,0 +1,78 @@ +/**************************************************************************** + * examples/ina219/ina219_main.c + * + * Copyright (C) 2017 Sebastien Lorquet. All rights reserved. + * Author: Sebastien Lorquet + * + * 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 +#include +#include +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * ina219_main + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int main(int argc, FAR char *argv[]) +#else +int ina219_main(int argc, char *argv[]) +#endif +{ + struct ina219_s sample; + int fd; + int ret; + + fd = open("/dev/ina219", O_RDWR); + while (1) + { + ret = read(fd, &sample, sizeof(sample)); + if (ret != sizeof(sample)) + { + break; + } + + printf("U=%12u uV I=%12d uA\n", sample.voltage, sample.current); + } + + close(fd); + return 0; +}