2009-06-05 19:59:55 +00:00
|
|
|
//
|
2009-11-16 00:56:00 +00:00
|
|
|
// main.h : AVR uC header file for flukso sensor board
|
2009-06-05 19:59:55 +00:00
|
|
|
// Copyright (c) 2008-2009 jokamajo.org
|
2010-01-05 13:51:09 +00:00
|
|
|
// Copyright (c) 2010 flukso.net
|
2009-06-05 19:59:55 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 2
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
//
|
2009-06-22 12:54:51 +00:00
|
|
|
// $Id$
|
2009-06-05 19:59:55 +00:00
|
|
|
//
|
|
|
|
|
2010-02-26 21:39:46 +00:00
|
|
|
# define PULSE 0
|
|
|
|
# define POWER 1
|
|
|
|
|
2010-02-28 20:11:37 +00:00
|
|
|
# define WATT 1000000000
|
2010-03-25 14:16:04 +00:00
|
|
|
# define SECOND 624 // 625Hz - 1
|
2010-02-28 20:11:37 +00:00
|
|
|
|
2010-01-05 20:05:10 +00:00
|
|
|
#ifndef SENSOR0
|
2010-04-09 17:23:41 +00:00
|
|
|
#error "SENSOR0 not defined"
|
2010-01-05 20:05:10 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SENSOR1
|
2010-04-09 17:23:41 +00:00
|
|
|
#error "SENSOR1 not defined"
|
2010-01-05 20:05:10 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SENSOR2
|
2010-04-09 17:23:41 +00:00
|
|
|
#error "SENSOR2 not defined"
|
2010-01-05 20:05:10 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SENSOR3
|
2010-04-09 17:23:41 +00:00
|
|
|
#error "SENSOR3 not defined"
|
2010-01-05 20:05:10 +00:00
|
|
|
#endif
|
|
|
|
|
2010-04-09 21:20:22 +00:00
|
|
|
#ifndef PHASE
|
|
|
|
#error "PHASE not defined"
|
|
|
|
#endif
|
|
|
|
|
2010-04-09 17:23:41 +00:00
|
|
|
#ifndef METERCONST
|
|
|
|
#error "METERCONST not defined"
|
2010-01-05 20:05:10 +00:00
|
|
|
#endif
|
2009-10-28 14:26:47 +00:00
|
|
|
|
2010-10-10 18:06:11 +00:00
|
|
|
#ifndef PULSE_CONST_2
|
|
|
|
#error "PULSE_CONST_2 not defined"
|
2010-09-10 08:16:35 +00:00
|
|
|
#endif
|
|
|
|
|
2010-10-10 18:06:11 +00:00
|
|
|
#ifndef PULSE_HALF_2
|
|
|
|
#error "PULSE_HALF_2 not defined"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef PULSE_CONST_3
|
|
|
|
#error "PULSE_CONST_3 not defined"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef PULSE_HALF_3
|
|
|
|
#error "PULSE_HALF_3 not defined"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef PULSE_CONST_4
|
|
|
|
#error "PULSE_CONST_4 not defined"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef PULSE_HALF_4
|
|
|
|
#error "PULSE_HALF_4 not defined"
|
2010-09-10 08:16:35 +00:00
|
|
|
#endif
|
|
|
|
|
2009-06-05 19:59:55 +00:00
|
|
|
#define START 0
|
|
|
|
#define END3 0xffffffff
|
|
|
|
#define END2 0xeeeeeeee
|
|
|
|
#define END1 0xdddddddd
|
|
|
|
#define END0 0xcccccccc
|
|
|
|
|
2009-11-16 00:56:00 +00:00
|
|
|
|
|
|
|
// This macro performs a 16x16 -> 32 unsigned MAC in 37 cycles with operands and results in memory
|
|
|
|
// based on http://www2.ife.ee.ethz.ch/~roggend/publications/wear/DSPMic_v1.1.pdf par 3.4 and table 31.
|
|
|
|
#define MacU16X16to32(uint_32Acc, uint_16In1, uint_16In2) \
|
|
|
|
asm volatile ( \
|
|
|
|
"clr r2 \n\t" \
|
|
|
|
"mul %B2, %B1 \n\t" \
|
|
|
|
"movw r4, r0 \n\t" \
|
|
|
|
"mul %A2, %A1 \n\t" \
|
|
|
|
"add %A0, r0 \n\t" \
|
|
|
|
"adc %B0, r1 \n\t" \
|
|
|
|
"adc %C0, r4 \n\t" \
|
|
|
|
"adc %D0, r5 \n\t" \
|
|
|
|
"mul %B2, %A1 \n\t" \
|
|
|
|
"add %B0, r0 \n\t" \
|
|
|
|
"adc %C0, r1 \n\t" \
|
|
|
|
"adc %D0, r2 \n\t" \
|
|
|
|
"mul %A2, %B1 \n\t" \
|
|
|
|
"add %B0, r0 \n\t" \
|
|
|
|
"adc %C0, r1 \n\t" \
|
|
|
|
"adc %D0, r2 \n\t" \
|
|
|
|
"clr r1 \n\t" \
|
|
|
|
: \
|
|
|
|
"+r" (uint_32Acc) \
|
|
|
|
: \
|
|
|
|
"a" (uint_16In1), \
|
|
|
|
"a" (uint_16In2) \
|
|
|
|
: \
|
|
|
|
"r2", "r4", "r5" \
|
|
|
|
)
|
|
|
|
|
2009-06-05 19:59:55 +00:00
|
|
|
// datastructures
|
2010-09-04 17:44:54 +00:00
|
|
|
struct time_struct {
|
|
|
|
boolean skip;
|
|
|
|
uint32_t ms;
|
|
|
|
};
|
|
|
|
|
2009-06-05 19:59:55 +00:00
|
|
|
struct state {
|
2010-09-04 17:44:54 +00:00
|
|
|
boolean pulse;
|
|
|
|
boolean toggle;
|
2010-09-10 08:16:35 +00:00
|
|
|
boolean half;
|
2009-06-05 19:59:55 +00:00
|
|
|
uint32_t nano;
|
2010-02-26 21:39:46 +00:00
|
|
|
uint16_t adc;
|
2010-02-28 20:11:37 +00:00
|
|
|
|
|
|
|
boolean power;
|
|
|
|
uint32_t nano_start;
|
|
|
|
uint32_t nano_end;
|
|
|
|
uint8_t pulse_count;
|
|
|
|
uint8_t pulse_count_final;
|
2010-09-04 17:44:54 +00:00
|
|
|
|
|
|
|
uint32_t time;
|
2009-06-05 19:59:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct sensor {
|
|
|
|
char id[33];
|
|
|
|
uint32_t value;
|
|
|
|
};
|
|
|
|
|
|
|
|
// prototypes
|
|
|
|
void WDT_off(void);
|
|
|
|
void WDT_on(void);
|
2010-10-10 18:06:11 +00:00
|
|
|
void pulse_add(volatile struct sensor *measurement, volatile struct state *aux, uint8_t pulse_const, uint8_t pulse_half);
|
2010-02-26 21:39:46 +00:00
|
|
|
void send(uint8_t msg_type, const struct sensor *measurement, const struct state *aux);
|