From 25c0387c446978483540971d91b8283c8e1841d9 Mon Sep 17 00:00:00 2001 From: Alan Carvalho de Assis Date: Fri, 4 Jan 2019 12:15:05 -0600 Subject: [PATCH] apps/examples/battery: Add Battery Charger Monitor Example --- examples/battery/.gitignore | 12 ++ examples/battery/Kconfig | 33 ++++ examples/battery/Make.defs | 39 +++++ examples/battery/Makefile | 59 ++++++++ examples/battery/batt_main.c | 266 +++++++++++++++++++++++++++++++++ netutils/ntpclient/ntpclient.c | 2 +- 6 files changed, 410 insertions(+), 1 deletion(-) create mode 100644 examples/battery/.gitignore create mode 100644 examples/battery/Kconfig create mode 100644 examples/battery/Make.defs create mode 100644 examples/battery/Makefile create mode 100644 examples/battery/batt_main.c diff --git a/examples/battery/.gitignore b/examples/battery/.gitignore new file mode 100644 index 000000000..caa9bdef2 --- /dev/null +++ b/examples/battery/.gitignore @@ -0,0 +1,12 @@ +/hello +/Make.dep +/.depend +/.built +/*.asm +/*.obj +/*.rel +/*.lst +/*.sym +/*.adb +/*.lib +/*.src diff --git a/examples/battery/Kconfig b/examples/battery/Kconfig new file mode 100644 index 000000000..d0fe4962c --- /dev/null +++ b/examples/battery/Kconfig @@ -0,0 +1,33 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_BATTERY + tristate "Battery monitor example" + default n + ---help--- + Enable the battery monitor example + +if EXAMPLES_BATTERY + +config EXAMPLES_BATTERY_PROGNAME + string "Program name" + default "batt" + depends on BUILD_LOADABLE + ---help--- + This is the name of the program that will be use when the NSH ELF + program is installed. + +config EXAMPLES_BATTERY_DEVNAME + string "Battery charger device name" + default "/dev/batt0" + +config EXAMPLES_BATTERY_PRIORITY + int "Battery task priority" + default 100 + +config EXAMPLES_BATTERY_STACKSIZE + int "Battery stack size" + default 2048 +endif diff --git a/examples/battery/Make.defs b/examples/battery/Make.defs new file mode 100644 index 000000000..3e3f0f32a --- /dev/null +++ b/examples/battery/Make.defs @@ -0,0 +1,39 @@ +############################################################################ +# apps/examples/batt/Make.defs +# Adds selected applications to apps/ build +# +# 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. +# +############################################################################ + +ifneq ($(CONFIG_EXAMPLES_BATTERY),) +CONFIGURED_APPS += examples/battery +endif diff --git a/examples/battery/Makefile b/examples/battery/Makefile new file mode 100644 index 000000000..2ce184aad --- /dev/null +++ b/examples/battery/Makefile @@ -0,0 +1,59 @@ +############################################################################ +# apps/examples/batt/Makefile +# +# Copyright (C) 2008, 2010-2013 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 + +# Battery built-in application info + +CONFIG_EXAMPLES_BATTERY_PRIORITY ?= SCHED_PRIORITY_DEFAULT +CONFIG_EXAMPLES_BATTERY_STACKSIZE ?= 2048 + +APPNAME = batt + +PRIORITY = $(CONFIG_EXAMPLES_BATTERY_PRIORITY) +STACKSIZE = $(CONFIG_EXAMPLES_BATTERY_STACKSIZE) + +# Hello, World! Example + +ASRCS = +CSRCS = +MAINSRC = batt_main.c + +CONFIG_EXAMPLES_BATTERY_PROGNAME ?= batt$(EXEEXT) +PROGNAME = $(CONFIG_EXAMPLES_BATTERY_PROGNAME) + +MODULE = CONFIG_EXAMPLES_BATTERY + +include $(APPDIR)/Application.mk diff --git a/examples/battery/batt_main.c b/examples/battery/batt_main.c new file mode 100644 index 000000000..797b8a944 --- /dev/null +++ b/examples/battery/batt_main.c @@ -0,0 +1,266 @@ +/**************************************************************************** + * examples/batt/batt_main.c + * + * Copyright (C) 2019 Alan Carvalho de Assis. All rights reserved. + * Author: Alan Carvalho de Assis + * + * 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 +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ +/* Configuration ************************************************************/ + +#ifndef CONFIG_EXAMPLES_BATTERY_DEVNAME +# define CONFIG_EXAMPLES_BATTERT_DEVNAME "/dev/batt0" +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * status_report + ****************************************************************************/ + +void status_report(int status) +{ + switch (status) + { + case BATTERY_UNKNOWN: + { + printf("Battery state is not known!\n"); + } + break; + + case BATTERY_FAULT: + { + printf("Charger fault, look at the health info!\n"); + } + break; + + case BATTERY_IDLE: + { + printf("Battery is idle, not full, not charging, not discharging!\n"); + } + break; + + case BATTERY_FULL: + { + printf("Battery fully charged, not discharging!\n"); + } + break; + + case BATTERY_CHARGING: + { + printf("Battery is charging, not full yet!\n"); + } + break; + + case BATTERY_DISCHARGING: + { + printf("Battery discharging!\n"); + } + break; + + default: + printf("ERROR: the value %d is not a defined status!\n", status); + break; + } +} + +/**************************************************************************** + * health_report + ****************************************************************************/ + +void health_report(int health) +{ + switch (health) + { + case BATTERY_HEALTH_UNKNOWN: + { + printf("Battery health is not known!\n"); + } + break; + + case BATTERY_HEALTH_GOOD: + { + printf("Battery is in good condiction!\n"); + } + break; + + case BATTERY_HEALTH_DEAD: + { + printf("Battery is dead, nothing we can do!\n"); + } + break; + + case BATTERY_HEALTH_OVERHEAT: + { + printf("Battery is over recommended temperature!\n"); + } + break; + + case BATTERY_HEALTH_OVERVOLTAGE: + { + printf("Battery voltage is over recommended level!\n"); + } + break; + + case BATTERY_HEALTH_UNSPEC_FAIL: + { + printf("Battery charger reported an unspected failure!\n"); + } + break; + + case BATTERY_HEALTH_COLD: + { + printf("Battery is under recommended temperature!\n"); + } + break; + + case BATTERY_HEALTH_WD_TMR_EXP: + { + printf("Battery WatchDog Timer Expired!\n"); + } + break; + + case BATTERY_HEALTH_SAFE_TMR_EXP: + { + printf("Battery Safety Timer Expired!\n"); + } + break; + + case BATTERY_HEALTH_DISCONNECTED: + { + printf("Battery is not connected!\n"); + } + break; + + default: + printf("ERROR: the value %d is not a defined health!\n", health); + break; + } +} + +/**************************************************************************** + * batt_main + ****************************************************************************/ + +#if defined(BUILD_MODULE) +int main(int argc, FAR char *argv[]) +#else +int batt_main(int argc, char *argv[]) +#endif +{ + int i; + int fd; + int ret; + int status; + int health; + + /* Open the battery charger device */ + + fd = open(CONFIG_EXAMPLES_BATTERY_DEVNAME, O_RDONLY); + if (fd < 0) + { + fprintf(stderr, "ERROR: Failed to open %s: %d\n", + CONFIG_EXAMPLES_BATTERY_DEVNAME, errno); + return EXIT_FAILURE; + } + + printf("Going to read battery info, updated each two seconds.\n"); + printf("Try to remove the board power supply, etc, to change its status.\n"); + + /* Wait the user read the information message above */ + + sleep(5); + + for (i = 0; i < 10; i++) + { + printf("\n-----------------------------------------------------------\n"); + + /* Read battery status */ + + ret = ioctl(fd, BATIOC_STATE, (unsigned long)((uintptr_t) &status)); + if (ret < 0) + { + fprintf(stderr, "ERROR: ioctl(BATIOC_STATE) failed: %d\n", errno); + goto errout_with_fd; + } + + /* Show status */ + + printf("STATUS: "); + + status_report(status); + + /* Read battery health */ + + ret = ioctl(fd, BATIOC_HEALTH, (unsigned long)((uintptr_t) &health)); + if (ret < 0) + { + fprintf(stderr, "ERROR: ioctl(BATIOC_HEALTH) failed: %d\n", errno); + goto errout_with_fd; + } + + /* Show health */ + + printf("HEALTH: "); + + health_report(health); + + /* Wait one second before reading again */ + + sleep(2); + } + + ret = OK; + +errout_with_fd: + close(fd); + return ret; +} diff --git a/netutils/ntpclient/ntpclient.c b/netutils/ntpclient/ntpclient.c index a43847e6a..162163895 100644 --- a/netutils/ntpclient/ntpclient.c +++ b/netutils/ntpclient/ntpclient.c @@ -112,7 +112,7 @@ struct ntpc_daemon_s * Private Data ****************************************************************************/ -/* This type describes the state of the NTP client daemon. Only once +/* This type describes the state of the NTP client daemon. Only one * instance of the NTP daemon is permitted in this implementation. This * limitation is due only to this global data structure. */