From 40d2de6b83c0def09ce705ca4fd2cd7c8b0ec0ae Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 6 Jan 2015 12:31:33 -0600 Subject: [PATCH] Add a tiny system application to read the current temperature --- system/Kconfig | 4 ++ system/Make.defs | 4 ++ system/Makefile | 4 +- system/lm75/.gitignore | 11 +++ system/lm75/Kconfig | 41 +++++++++++ system/lm75/Makefile | 149 +++++++++++++++++++++++++++++++++++++++ system/lm75/lm75.c | 156 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 367 insertions(+), 2 deletions(-) create mode 100644 system/lm75/.gitignore create mode 100644 system/lm75/Kconfig create mode 100644 system/lm75/Makefile create mode 100644 system/lm75/lm75.c diff --git a/system/Kconfig b/system/Kconfig index e3b9689ac..978cdbaae 100644 --- a/system/Kconfig +++ b/system/Kconfig @@ -75,6 +75,10 @@ menu "Sysinfo" source "$APPSDIR/system/sysinfo/Kconfig" endmenu +menu "Temperature" +source "$APPSDIR/system/lm75/Kconfig" +endmenu + menu "VI Work-Alike Editor" source "$APPSDIR/system/vi/Kconfig" endmenu diff --git a/system/Make.defs b/system/Make.defs index 23a8d69c5..62c2958a2 100644 --- a/system/Make.defs +++ b/system/Make.defs @@ -126,6 +126,10 @@ ifeq ($(CONFIG_SYSTEM_CLE),y) CONFIGURED_APPS += system/cle endif +ifeq ($(CONFIG_SYSTEM_LM75),y) +CONFIGURED_APPS += system/lm75 +endif + ifeq ($(CONFIG_SYSTEM_VI),y) CONFIGURED_APPS += system/vi endif diff --git a/system/Makefile b/system/Makefile index c93137cfa..cf77709de 100644 --- a/system/Makefile +++ b/system/Makefile @@ -38,8 +38,8 @@ # Sub-directories containing system tasks/libraries SUBDIRS = cdcacm cle composite cu flash_eraseall free i2c hex2bin inifile -SUBDIRS += install mdio nxplayer poweroff ramtest ramtron readline sdcard -SUBDIRS += stackmonitor sudoku sysinfo usbmonitor usbmsc vi zmodem +SUBDIRS += install lm75 mdio nxplayer poweroff ramtest ramtron readline +SUBDIRS += sdcard stackmonitor sudoku sysinfo usbmonitor usbmsc vi zmodem # Create the list of installed runtime modules (INSTALLED_DIRS) diff --git a/system/lm75/.gitignore b/system/lm75/.gitignore new file mode 100644 index 000000000..83bd7b811 --- /dev/null +++ b/system/lm75/.gitignore @@ -0,0 +1,11 @@ +/Make.dep +/.depend +/.built +/*.asm +/*.rel +/*.lst +/*.sym +/*.adb +/*.lib +/*.src +/*.obj diff --git a/system/lm75/Kconfig b/system/lm75/Kconfig new file mode 100644 index 000000000..2a186dab4 --- /dev/null +++ b/system/lm75/Kconfig @@ -0,0 +1,41 @@ +# +# For a description of the syntax of this configuration file, +# see misc/tools/kconfig-language.txt. +# + +config SYSTEM_LM75 + bool "Temperature" + default n + depends on I2C_LM75 + ---help--- + Enable support for the NSH temp command. This command will read the + current temperature from an LM-75 (or compatible) temperature sensor + and show the temperature on stdout. + +if SYSTEM_LM75 + +config SYSTEM_LM75_DEVNAME + string "Temperature sensing device" + default "/dev/temp" + +choice + prompt "Temperature units" + default SYSTEM_LM75_FAHRENHEIT + +config SYSTEM_LM75_FAHRENHEIT + bool "Fahrenheit" + +config SYSTEM_LM75_CENTIGRADE + bool "Centigrade" + +endchoice + +config SYSTEM_LM75_STACKSIZE + int "Stack size (bytes)" + default 1024 + +config SYSTEM_LM75_PRIORITY + int "Command priority" + default 100 + +endif diff --git a/system/lm75/Makefile b/system/lm75/Makefile new file mode 100644 index 000000000..a80286254 --- /dev/null +++ b/system/lm75/Makefile @@ -0,0 +1,149 @@ +############################################################################ +# apps/system/lm75/Makefile +# +# Copyright (C) 2015 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. +# +############################################################################ + +# TODO, this makefile should run make under the app dirs, instead of +# sourcing the Make.defs! + +-include $(TOPDIR)/.config +-include $(TOPDIR)/Make.defs +include $(APPDIR)/Make.defs + +ifeq ($(WINTOOL),y) +INCDIROPT = -w +endif + +# LM-75 Temperature Sensor Application + +CONFIG_SYSTEM_LM75_PRIORITY ?= 100 +CONFIG_SYSTEM_LM75_STACKSIZE ?= 1024 + +APPNAME = temp +PRIORITY = $(CONFIG_SYSTEM_LM75_PRIORITY) +STACKSIZE = $(CONFIG_SYSTEM_LM75_STACKSIZE) + +ASRCS = +CSRCS = +MAINSRC = lm75.c + +AOBJS = $(ASRCS:.S=$(OBJEXT)) +COBJS = $(CSRCS:.c=$(OBJEXT)) +MAINOBJ = $(MAINSRC:.c=$(OBJEXT)) + +SRCS = $(ASRCS) $(CSRCS) $(MAINSRC) +OBJS = $(AOBJS) $(COBJS) + +ifneq ($(CONFIG_BUILD_KERNEL),y) + OBJS += $(MAINOBJ) +endif + +ifeq ($(CONFIG_WINDOWS_NATIVE),y) + BIN = ..\..\libapps$(LIBEXT) +else +ifeq ($(WINTOOL),y) + BIN = ..\\..\\libapps$(LIBEXT) +else + BIN = ../../libapps$(LIBEXT) +endif +endif + +ifeq ($(WINTOOL),y) + INSTALL_DIR = "${shell cygpath -w $(BIN_DIR)}" +else + INSTALL_DIR = $(BIN_DIR) +endif + +CONFIG_XYZ_PROGNAME ?= lm75$(EXEEXT) +PROGNAME = $(CONFIG_XYZ_PROGNAME) + +ROOTDEPPATH = --dep-path . + +# Common build + +VPATH = + +all: .built +.PHONY: context depend clean distclean + +$(AOBJS): %$(OBJEXT): %.S + $(call ASSEMBLE, $<, $@) + +$(COBJS) $(MAINOBJ): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +.built: $(OBJS) + $(call ARCHIVE, $(BIN), $(OBJS)) + $(Q) touch .built + +ifeq ($(CONFIG_BUILD_KERNEL),y) +$(BIN_DIR)$(DELIM)$(PROGNAME): $(OBJS) $(MAINOBJ) + @echo "LD: $(PROGNAME)" + $(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(PROGNAME) $(ARCHCRT0OBJ) $(MAINOBJ) $(LDLIBS) + $(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(PROGNAME) + +install: $(BIN_DIR)$(DELIM)$(PROGNAME) + +else +install: + +endif + +# Register application + +ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) +$(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat: $(DEPCONFIG) Makefile + $(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),lm75_main) + +context: $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat +else +context: +endif + +# Create dependencies + +.depend: Makefile $(SRCS) + $(Q) $(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep + $(Q) touch $@ + +depend: .depend + +clean: + $(call DELFILE, .built) + $(call CLEAN) + +distclean: clean + $(call DELFILE, Make.dep) + $(call DELFILE, .depend) + +-include Make.dep diff --git a/system/lm75/lm75.c b/system/lm75/lm75.c new file mode 100644 index 000000000..5af91f304 --- /dev/null +++ b/system/lm75/lm75.c @@ -0,0 +1,156 @@ +/**************************************************************************** + * apps/system/lm75/lm75.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Authors: 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef CONFIG_SYSTEM_LM75_DEVNAME +# warning CONFIG_SYSTEM_LM75_DEVNAME is not defined +# define CONFIG_SYSTEM_LM75_DEVNAME"/dev/temp" +#endif + +#if !defined(CONFIG_SYSTEM_LM75_FAHRENHEIT) && !defined(CONFIG_SYSTEM_LM75_CENTIGRADE) +# warning one of CONFIG_SYSTEM_LM75_FAHRENHEIT or CONFIG_SYSTEM_LM75_CENTIGRADE must be defined +# defeind CONFIG_SYSTEM_LM75_FAHRENHEIT 1 +#endif + +#if defined(CONFIG_SYSTEM_LM75_FAHRENHEIT) && defined(CONFIG_SYSTEM_LM75_CENTIGRADE) +# warning both of CONFIG_SYSTEM_LM75_FAHRENHEIT and CONFIG_SYSTEM_LM75_CENTIGRADE defined +# undef CONFIG_SYSTEM_LM75_CENTIGRADE +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int main(int argc, FAR char *argv[]) +#else +int lm75_main(int argc, char *argv[]) +#endif +{ +#ifdef CONFIG_LIBC_FLOATINGPOINT + double temp; +#endif + b16_t temp16; + ssize_t nbytes; + int fd; + int ret; + + /* Open the temperature sensor device */ + + fd = open(CONFIG_SYSTEM_LM75_DEVNAME, O_RDONLY); + if (fd < 0) + { + fprintf(stderr, "ERROR: Failed to open %s: %d\n", + CONFIG_SYSTEM_LM75_DEVNAME, errno); + return EXIT_FAILURE; + } + +#ifdef CONFIG_SYSTEM_LM75_FAHRENHEIT + /* Select Fahrenheit scaling */ + + ret = ioctl(fd, SNIOC_FAHRENHEIT, 0); + if (ret < 0) + { + fprintf(stderr, "ERROR: ioctl(SNIOC_FAHRENHEIT) failed: %d\n", + errno); + return EXIT_FAILURE; + } +#else + /* Select Fahrenheit scaling */ + + ret = ioctl(fd, SNIOC_CENTIGRADE, 0); + if (ret < 0) + { + fprintf(stderr, "ERROR: ioctl(SNIOC_CENTIGRADE) failed: %d\n", + errno); + return EXIT_FAILURE; + } +#endif + + /* Read the current temperature as a fixed precision number */ + + nbytes = read(fd, &temp16, sizeof(b16_t)); + close(fd); + + if (nbytes < 0) + { + fprintf(stderr, "ERROR: read(%d) failed: %d\n", + sizeof(b16_t), errno); + return EXIT_FAILURE; + } + + if (nbytes != sizeof(b16_t)) + { + fprintf(stderr, "ERROR: Unexpected read size: %Ld vs %d\n", + (long)nbytes, sizeof(b16_t)); + return EXIT_FAILURE; + } + +#ifdef CONFIG_LIBC_FLOATINGPOINT + temp = (double)temp16 / 65536.0 +#ifdef CONFIG_SYSTEM_LM75_FAHRENHEIT + printf("%3.2f degrees Fahrenheit\n", temp); +#else + printf("%3.2f degrees Centigrade\n", temp); +#endif +#else +#ifdef CONFIG_SYSTEM_LM75_FAHRENHEIT + printf("0x%04x.%04x degrees Fahrenheit\n", temp16 >> 16, temp16 & 0xffff); +#else + printf("0x%04x.%04x degrees Centigrade\n", temp16 >> 16, temp16 & 0xffff); +#endif +#endif + + return EXIT_SUCCESS; +}