industry/abnt_codi, include/industry/abnt_code.h, examples.abntcodi: Adds support for ABNT CODI library. This is an energy meter protocol used in Brazil.

This commit is contained in:
Alan Carvalho de Assis 2019-06-15 08:38:04 -06:00 committed by Gregory Nutt
parent bd14bf7165
commit 656157399d
13 changed files with 801 additions and 0 deletions

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

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

32
examples/abntcodi/Kconfig Normal file
View File

@ -0,0 +1,32 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
config EXAMPLES_ABNTCODI
tristate "ABNT CODI example"
default n
select INDUSTRY_ABNT_CODI_LIB
---help---
Enable the ABNT CODI test example
if EXAMPLES_ABNTCODI
config EXAMPLES_ABNTCODI_PROGNAME
string "Program name"
default "abntcodi"
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_ABNTCODI_PRIORITY
int "ABNTCODI task priority"
default 100
config EXAMPLES_ABNTCODI_STACKSIZE
int "ABNTCODI stack size"
default 2048
endif

View File

@ -0,0 +1,40 @@
############################################################################
# apps/examples/abntcodi/Make.defs
# Adds selected applications to apps/ build
#
# Copyright (C) 2019 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_ABNTCODI),)
CONFIGURED_APPS += examples/abntcodi
endif

View File

@ -0,0 +1,59 @@
############################################################################
# apps/examples/abntcodi/Makefile
#
# Copyright (C) 2019 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
# ABNTCODI built-in application info
CONFIG_EXAMPLES_ABNTCODI_PRIORITY ?= SCHED_PRIORITY_DEFAULT
CONFIG_EXAMPLES_ABNTCODI_STACKSIZE ?= 2048
APPNAME = abntcodi
PRIORITY = $(CONFIG_EXAMPLES_ABNTCODI_PRIORITY)
STACKSIZE = $(CONFIG_EXAMPLES_ABNTCODI_STACKSIZE)
# ABNTCODI Example
ASRCS =
CSRCS =
MAINSRC = abntcodi_main.c
CONFIG_EXAMPLES_ABNTCODI_PROGNAME ?= abntcodi$(EXEEXT)
PROGNAME = $(CONFIG_EXAMPLES_ABNTCODI_PROGNAME)
MODULE = CONFIG_EXAMPLES_ABNTCODI
include $(APPDIR)/Application.mk

View File

@ -0,0 +1,148 @@
/****************************************************************************
* examples/hello/abntcodi_main.c
*
* Copyright (C) 2019 Gregory Nutt. 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 <stdio.h>
#include <fcntl.h>
#include <syslog.h>
#include <stdlib.h>
#include "industry/abnt_codi.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
void print_abnt_codi(FAR struct abnt_codi_proto_s *proto)
{
printf("Seconds missing to end of the active demand: %d\n",
proto->end_act_dem);
printf("Current Bill Indicator: %d\n", proto->bill_indicator);
printf("Reactive Interval Indicator: %d\n", proto->react_interval);
printf("Capacitive Reactive Pulses are used to calculate consumption: %s\n",
boolstr(proto->react_cap_pulse));
printf("Inductive Reactive Pulses are used to calculate consumption: %s\n",
boolstr(proto->react_ind_pulse));
printf("Segment type: %s\n",
proto->segment_type == SEGMENT_PEEK ? "PEEK" :
proto->segment_type == SEGMENT_OUT_PEEK ? "OUT OF PEEK" :
proto->segment_type == SEGMENT_RESERVED ? "RESERVED" : "UNKNOWN");
printf("Charges type: %s\n",
proto->charge_type == CHARGES_BLUE ? "BLUE" :
proto->charge_type == CHARGES_GREEN ? "GREEN" :
proto->charge_type == CHARGES_IRRIGATORS ? "IRRIGATORS" : "OTHERS");
printf("Number of Active pulses since the beginning of current demand: %d\n",
proto->pulses_act_dem);
printf("Number of Reactive pulses since beginning of current demand: %d\n",
proto->pulses_react_dem);
}
/****************************************************************************
* abntcodi_main
****************************************************************************/
#ifdef BUILD_MODULE
int main(int argc, FAR char *argv[])
#else
int abntcodi_main(int argc, char *argv[])
#endif
{
FAR struct abnt_codi_proto_s *proto;
int fd;
int cnt;
int ret;
uint8_t byte;
uint8_t data[8];
/* Allocate memory to protocol struct */
proto = malloc(sizeof(struct abnt_codi_proto_s));
if (!proto)
{
printf("Failed to allocate memory to abnt_codi_proto_s!\n");
return -ENOMEM;
}
/* Open the serial port used to read ABNT CODI (Baudrate: 110bps) */
fd = open("/dev/ttyS1", O_RDONLY);
if (fd < 0)
{
printf("Unable to open file /dev/ttyS1\n");
return -ENODEV;
}
/* Run forever */
for (; ; )
{
/* Read until we complete a sequence of 8 bytes */
cnt = 0;
do
{
ret = read(fd, &byte, 1);
if (ret != 1)
{
continue;
}
data[cnt++] = byte;
}
while (cnt < 8);
/* Parse the received data */
if (abnt_codi_parse(data, proto))
{
print_abnt_codi(proto);
}
/* Avoid busy wait using 100% CPU cycles */
usleep(5000000);
}
return 0;
}

View File

@ -0,0 +1,125 @@
/****************************************************************************
* apps/include/industry/abnt_codi.h
*
* Copyright © 2019 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.
*
****************************************************************************/
#ifndef __APPS_INCLUDE_INDUSTRY_ABNTCODI_H
#define __APPS_INCLUDE_INDUSTRY_ABNTCODI_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define boolstr(s) ((s) ? "true" : "false")
#define END_ACT_DEM_MASK 0x0f
#define BILL_INDICADOR (1 << 4) /* This bit is inverted at each
* replenishment of demand */
#define REACT_INTERVAL (1 << 5) /* Reactive Interval Indicator, inverted
* at end of reactive interval
* consumption */
#define REACT_CAP_PULSE (1 << 6) /* If equal 1 means that reactive
* capacitive pulses are computed to
* calculate UFER and DMCR */
#define REACT_IND_PULSE (1 << 7) /* If equal 1 means that reactive
* inductive pulses are computed to
* calculate UFER and DMCR */
#define SEGMENT_MASK 0x0f
#define SEGMENT_PEEK 1 /* Current segment is in the peek */
#define SEGMENT_OUT_PEEK 2 /* Current segment is out of the peek */
#define SEGMENT_RESERVED 8 /* Reserved */
#define CHARGES_MASK (3 << 4)
#define CHARGES_BLUE (0 << 4) /* The charges is blue */
#define CHARGES_GREEN (1 << 4) /* The charges is green */
#define CHARGES_IRRIGATORS (2 << 4) /* The charges is irrigators */
#define CHARGES_OTHER (3 << 4) /* The is other */
/* bit 6 not used */
#define CHARGES_REACT_EN (1 << 7) /* Reactive charging is enabled */
#define NOT_USED_BIT (1 << 7) /* Bit 7 of 5th and 7th octet are not used */
/****************************************************************************
* Public Types
****************************************************************************/
struct abnt_codi_proto_s
{
uint16_t end_act_dem; /* Seconds missing to end of the active demand */
bool bill_indicator; /* Bill indicator alternating bit */
bool react_interval; /* Reactive indicator alternating bit */
bool react_cap_pulse; /* Indicates if capacitive pulses are used to
* calculate consumption */
bool react_ind_pulse; /* Indicates if inductive pulses are used to
* calculate consumption */
uint8_t segment_type; /* Segment type */
uint8_t charge_type; /* Charging type */
bool react_charge_en; /* Reactive charging enable */
uint16_t pulses_act_dem; /* Number of active pulses since the beginning
* of current demand */
uint16_t pulses_react_dem; /* Number of reactive pulses since the beginning
* of current demand */
uint8_t checksum; /* Checksum: xor of previous 7 bytes and
* complement (bits inversion) the result */
};
#ifdef __cplusplus
extern "C"
{
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/* Calculate raw sentence checksum. Does not check sentence integrity. */
uint8_t abnt_codi_checksum(FAR const uint8_t *data);
/* Parse a specific ABNT CODI sequence. */
bool abnt_codi_parse(FAR const uint8_t *data, FAR struct abnt_codi_proto_s *proto);
#ifdef __cplusplus
}
#endif
#endif /* __APPS_INCLUDE_INDUSTRY_ABNTCODI_H */

37
industry/Make.defs Normal file
View File

@ -0,0 +1,37 @@
############################################################################
# apps/industry/Make.defs
# Adds selected applications to apps/ build
#
# Copyright (C) 2019 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 $(wildcard industry/*/Make.defs)

38
industry/Makefile Normal file
View File

@ -0,0 +1,38 @@
############################################################################
# apps/industry/Makefile
#
# Copyright (C) 2019 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.
#
############################################################################
MENUDESC = "Industrial Applications"
include $(APPDIR)/Directory.mk

View File

@ -0,0 +1,11 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
config INDUSTRY_ABNT_CODI_LIB
tristate "ABNT CODI Library"
default n
---help---
Enable or disable the ABNT CODI Library

View File

@ -0,0 +1,39 @@
############################################################################
# apps/industry/abnt_codi/Make.defs
# Adds selected applications to apps/ build
#
# Copyright (C) 2019 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.
#
############################################################################
ifeq ($(CONFIG_INDUSTRY_ABNT_CODI_LIB),y)
CONFIGURED_APPS += industry/abnt_codi
endif

View File

@ -0,0 +1,43 @@
############################################################################
# apps/industry/abnt_codi/Makefile
#
# Copyright (C) 2019 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
# NSH Library
CSRCS = abnt_codi.c
CFLAGS += -std=c99
include $(APPDIR)/Application.mk

View File

@ -0,0 +1,68 @@
The ABNT CODI is an old energy meter standard used in Brazil.
This code interprets the end user serial output existent in the energy meter.
That output externalizes its data blinking an LED as a serial protocol at the
baudrate of 110 BPS and uses 8 octects:
_____________________________________________________________________________
| | | |
| OCTECT | bits | Description |
|________|______|_____________________________________________________________|
| | | |
| 001 | 0-7 | Number of seconds to the end of current active demand (LSB) |
|________|______|_____________________________________________________________|
| | | |
| 002 | 0-3 | Number of seconds to the end of current active demand (MSB) |
|________|______|_____________________________________________________________|
| | | |
| | 4 | Bill indicator. It is inverted at each demand replenishment |
|________|______|_____________________________________________________________|
| | | |
| | 5 | Reactive Interval Indicator. Inverted at end react interval |
|________|______|_____________________________________________________________|
| | | |
| | 6 | If 1 means the reactive-capacitive is used to calculate UFER|
|________|______|_____________________________________________________________|
| | | |
| | 7 | If 1 means the reactive-inductive is used to calculate UFER |
|________|______|_____________________________________________________________|
| | | |
| 003 | 0-3 | Current seasonal segment: |
| | | 0001 - tip |
| | | 0010 - out of tip |
| | | 1000 - reserved |
|________|______|_____________________________________________________________|
| | | |
| | 4-5 | Type of charging indicator (flag): |
| | | 00 - Blue |
| | | 01 - Green |
| | | 10 - Irrigators |
| | | 11 - Other |
|________|______|_____________________________________________________________|
| | | |
| | 6 | Not used |
|________|______|_____________________________________________________________|
| | | |
| | 7 | If equal 1 means reactive rate is enabled |
|________|______|_____________________________________________________________|
| | | |
| 004 | 0-7 | Number of pulses for active energy of cur dem interv (LSB) |
|________|______|_____________________________________________________________|
| | | |
| 005 | 0-6 | Number of pulses for active energy of cur dem interv (MSB) |
|________|______|_____________________________________________________________|
| | | |
| | 7 | Not used |
|________|______|_____________________________________________________________|
| | | |
| 006 | 0-7 | Number of pulses for reactive energy of cur dem interv (LSB)|
|________|______|_____________________________________________________________|
| | | |
| 007 | 0-6 | Number of pulses for reactive energy of cur dem interv (MSB)|
|________|______|_____________________________________________________________|
| | | |
| | 7 | Not used |
|________|______|_____________________________________________________________|
| | | |
| 008 | 0-7 | Inverted bits of "xor" from previous octects |
|________|______|_____________________________________________________________|

View File

@ -0,0 +1,149 @@
/****************************************************************************
* apps/industry/abnt_codi/abnt_codi.c
*
* Copyright (C) 2019 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 <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <wchar.h>
#include <stdarg.h>
#include "industry/abnt_codi.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
uint8_t abnt_codi_checksum(FAR const uint8_t *data)
{
uint8_t checksum = 0x00;
int i;
for (i = 0; i < 7; i++)
{
checksum ^= *data++;
}
/* Return the complement of the resulting checksum */
checksum ^= 0xff;
return checksum;
}
bool abnt_codi_parse(FAR const uint8_t *data,
FAR struct abnt_codi_proto_s *proto)
{
uint8_t checksum;
/* Checksum is the last byte in the CODI byte sequence */
checksum = data[7];
/* Verify if the checksum is correct */
if (abnt_codi_checksum(data) == checksum)
{
printf("Checksum is correct, this is a valid CODI sequence!\n");
}
else
{
printf("Invalid checksum! Received: 0x%02x Expected: 0x%02x!\n",
checksum, abnt_codi_checksum(data));
return false;
}
/* Seconds missing to the end of current active demand */
proto->end_act_dem = ((data[1] & END_ACT_DEM_MASK) << 8) | data[0];
/* Bill indicator, it is inverted at end of active current demand */
proto->bill_indicator = (data[1] & BILL_INDICADOR) == BILL_INDICADOR ?
true : false;
/* Reactive indicator, it is invered at end of reactive interval */
proto->react_interval = (data[1] & REACT_INTERVAL) == REACT_INTERVAL ?
true : false;
/* Verify if capacitive pulses are used to calculate consumption */
proto->react_cap_pulse = (data[1] & REACT_CAP_PULSE) == REACT_CAP_PULSE ?
true : false;
/* Verify if inductive pulses are used to calculate consumption */
proto->react_ind_pulse = (data[1] & REACT_IND_PULSE) == REACT_IND_PULSE ?
true : false;
/* Seasonal segment type */
proto->segment_type = data[2] & CHARGES_MASK;
/* Tariff type */
proto->charge_type = data[2] & CHARGES_MASK;
/* Verify if Reactive Tariff is enable */
proto->react_charge_en = (data[2] & CHARGES_REACT_EN) == CHARGES_REACT_EN ?
true : false;
/* Number of active pulses since the beginning of current demand */
proto->pulses_act_dem = ((data[4] & ~NOT_USED_BIT)) << 8 | data[3];
/* Number of reactive pulses since the beginning of current demand */
proto->pulses_react_dem = ((data[6] & ~NOT_USED_BIT)) << 8 | data[5];
/* Save the checksum too */
proto->checksum = checksum;
return true;
}