From f840b0925a76cb1efd7001befa5484da6eaa0cce Mon Sep 17 00:00:00 2001 From: Bart Van Der Meerssche Date: Mon, 16 Nov 2009 00:56:00 +0000 Subject: [PATCH] uc: implement 16x16 -> 32 unsigned MAC macro --- uc/main.c | 7 +++++-- uc/main.h | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/uc/main.c b/uc/main.c index 6fa1ac9..c03ec45 100644 --- a/uc/main.c +++ b/uc/main.c @@ -1,5 +1,5 @@ // -// main.1mhz.c : AVR uC code for flukso sensor board +// main.c : AVR uC code for flukso sensor board // Copyright (c) 2008-2009 jokamajo.org // // This program is free software; you can redistribute it and/or @@ -70,7 +70,8 @@ ISR(PCINT2_vect) { ISR(TIMER2_OVF_vect) { // read ADC result // add to nano(Wh) counter - aux[0].nano += (uint32_t)METERCONST * ADC; + MacU16X16to32(aux[0].nano, METERCONST, ADC); + if (aux[0].nano > 1000000000) { measurements[0].value++; aux[0].pulse = true; @@ -269,6 +270,8 @@ int main(void) for (i=0; i<4; i++) _delay_ms(5000); serialFlush(); + printString("\n"); + WDT_on(); for (;;) loop(); diff --git a/uc/main.h b/uc/main.h index 06b6858..6287167 100644 --- a/uc/main.h +++ b/uc/main.h @@ -1,5 +1,5 @@ // -// main.1mhz.h : AVR uC header file for flukso sensor board +// main.h : AVR uC header file for flukso sensor board // Copyright (c) 2008-2009 jokamajo.org // // This program is free software; you can redistribute it and/or @@ -49,6 +49,37 @@ #define END1 0xdddddddd #define END0 0xcccccccc + +// 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" \ +) + // datastructures struct state { boolean pulse;