apps/examples/battery: Add Battery Charger Monitor Example

This commit is contained in:
Alan Carvalho de Assis 2019-01-04 12:15:05 -06:00 committed by Gregory Nutt
parent 5182dbc4b1
commit 25c0387c44
6 changed files with 410 additions and 1 deletions

12
examples/battery/.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
/hello
/Make.dep
/.depend
/.built
/*.asm
/*.obj
/*.rel
/*.lst
/*.sym
/*.adb
/*.lib
/*.src

33
examples/battery/Kconfig Normal file
View File

@ -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

View File

@ -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 <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.
#
############################################################################
ifneq ($(CONFIG_EXAMPLES_BATTERY),)
CONFIGURED_APPS += examples/battery
endif

59
examples/battery/Makefile Normal file
View File

@ -0,0 +1,59 @@
############################################################################
# apps/examples/batt/Makefile
#
# Copyright (C) 2008, 2010-2013 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)/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

View File

@ -0,0 +1,266 @@
/****************************************************************************
* examples/batt/batt_main.c
*
* Copyright (C) 2019 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 <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/power/battery_ioctl.h>
#include <nuttx/power/battery_charger.h>
/****************************************************************************
* 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;
}

View File

@ -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.
*/