From 10e2ab9277aa92da666934b9505b573a8cdbc3ce Mon Sep 17 00:00:00 2001 From: Dario Ernst Date: Mon, 25 Jul 2011 21:19:39 +0200 Subject: [PATCH 1/4] cleaned old offsetcode, made new --- firmware/slavechip/Makefile | 2 +- firmware/slavechip/main.c | 37 ++++++++++++++----------------- firmware/slavechip/main.h | 2 -- firmware/slavechip/muxer.c | 22 ------------------ firmware/slavechip/muxer.h | 5 ----- firmware/slavechip/spi_pointers.c | 5 ++++- 6 files changed, 22 insertions(+), 51 deletions(-) diff --git a/firmware/slavechip/Makefile b/firmware/slavechip/Makefile index 0307efb..7df9d46 100644 --- a/firmware/slavechip/Makefile +++ b/firmware/slavechip/Makefile @@ -8,7 +8,7 @@ CFLAGS += -ffunction-sections -Wl,--gc-sections -Wl,--relax COMPILE = avr-gcc $(CFLAGS) $(DEFINES) -OBJECTS = main.o mcp_adc.o i2c_simple.o softtimer.o spi_proto.o spi.o spi_pointers.o muxer.o debug.o filter.o ringbuf_small.o +OBJECTS = main.o mcp_adc.o i2c_simple.o softtimer.o spi_proto.o spi.o spi_pointers.o muxer.o debug.o filter.o ringbuf_small.o offset.o # symbolic targets: all: firmware.hex diff --git a/firmware/slavechip/main.c b/firmware/slavechip/main.c index 0fd4dfb..cd9a032 100644 --- a/firmware/slavechip/main.c +++ b/firmware/slavechip/main.c @@ -5,12 +5,12 @@ #include "mcp_adc.h" #include "debug.h" #include "filter.h" +#include "offset.h" #include #include #include - uint16_t timertmp; uint16_t temperatures[4]; @@ -21,9 +21,6 @@ uint16_t temperatures[4]; * - multiplexer: * C3: inhibit * C0-C2: muxer select - * - offset measurement - * D0-D3 offset compensation - * * * amp 0 is on muxer channel 2 */ @@ -39,7 +36,6 @@ void hardinit() { spi_init(); muxer_init(); - offset_measure_init(); i2c_init(); @@ -65,6 +61,8 @@ int __attribute__((noreturn)) main(void) { int16_t data; for(;;){ printf("====== loop iteration\n"); + + // measure temps 5*2 times for(uint8_t i=0; i<5; i++) { @@ -82,22 +80,21 @@ int __attribute__((noreturn)) main(void) { } } - printf("====== now measuring offsets\n"); // measure 2 offsets - for(uint8_t active_sensor=0; active_sensor<2; active_sensor++) { // only measuring two offsets atm - printf("=== active sensor: %i\n",active_sensor); - muxer_set(active_sensor); - offset_measure_start(active_sensor); - while(!mcpadc_has_new_data()) _delay_ms(10); - mcpadc_get_data(); // first data after switch to trash - while(!mcpadc_has_new_data()) _delay_ms(10); - - data = mcpadc_get_data(); - printf("retrieved offset data %i\n", data); - // TODO: what to do with the offset? - - offset_measure_stop(); - } + for(uint8_t active_sensor=0; active_sensor<2; active_sensor++) + if(offset_measure[active_sensor]) { + printf("=== measuring offset, sensor: %i\n",active_sensor); + muxer_set(active_sensor); + while(!mcpadc_has_new_data()) _delay_ms(10); + mcpadc_get_data(); // first data after switch to trash + while(!mcpadc_has_new_data()) _delay_ms(10); + + offset_val[active_sensor] = mcpadc_get_data(); + + while(offset_measure[active_sensor]) { + offset_val[active_sensor] += mcpadc_get_data(); + } + } // measure ambient printf("====== now measuring ambient\n"); diff --git a/firmware/slavechip/main.h b/firmware/slavechip/main.h index 3344c5c..3df4080 100644 --- a/firmware/slavechip/main.h +++ b/firmware/slavechip/main.h @@ -14,7 +14,5 @@ #include "softtimer.h" #include "debug.h" -extern uint16_t timertmp; -extern uint16_t temperatures[]; #endif //__MAIN_H diff --git a/firmware/slavechip/muxer.c b/firmware/slavechip/muxer.c index 8671753..6f6ee06 100644 --- a/firmware/slavechip/muxer.c +++ b/firmware/slavechip/muxer.c @@ -11,8 +11,6 @@ void muxer_init(){ DDRC |= (_BV(0)|_BV(1)|_BV(2)|_BV(3)); // conf as outputs PORTC |= _BV(3); // inhibit - // for good measure call the offset init, calling it twice doesn't hurt - offset_measure_init(); } void muxer_set(uint8_t channel){ @@ -32,23 +30,3 @@ uint8_t inline muxer_channel_to_selectbits(uint8_t channel){ } return(table[channel]); } - -void offset_measure_init(){ - DDRD |= 0x0f; // first 4 pins as output -// PORTD = 0x00; // turn everything off -} - -void offset_measure_start(uint8_t channel){ - if(channel > 3){ - return; - } - PORTD |= (1< Date: Mon, 25 Jul 2011 21:24:29 +0200 Subject: [PATCH 2/4] forgot files --- firmware/slavechip/offset.c | 20 ++++++++++++++++++++ firmware/slavechip/offset.h | 15 +++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 firmware/slavechip/offset.c create mode 100644 firmware/slavechip/offset.h diff --git a/firmware/slavechip/offset.c b/firmware/slavechip/offset.c new file mode 100644 index 0000000..9135214 --- /dev/null +++ b/firmware/slavechip/offset.c @@ -0,0 +1,20 @@ +#include "offset.h" + +int16_t offset_measure[4]; +int16_t offset_count[4]; +int16_t offset_val[4]; +int16_t offsets[4]; + + +int16_t offset_measure_start(int16_t channel) { + offset_measure[channel] = 1; + offset_count[channel] = 1; + offset_val[channel] = 0; + return 1; +} + +int16_t offset_measure_stop(int16_t channel) { + offset_measure[channel] = 0; + offsets[channel] = offset_val[channel] / offset_count[channel]; + return offsets[channel]; +} diff --git a/firmware/slavechip/offset.h b/firmware/slavechip/offset.h new file mode 100644 index 0000000..2c23609 --- /dev/null +++ b/firmware/slavechip/offset.h @@ -0,0 +1,15 @@ +#ifndef __OFFSET_H +#define __OFFSET_H + +#include "main.h" +#include + +extern int16_t offset_measure[4]; +extern int16_t offset_count[4]; +extern int16_t offset_val[4]; +extern int16_t offsets[4]; + +int16_t offset_measure_start(int16_t channel); +int16_t offset_measure_stop(int16_t channel); + +#endif From 33f61a8671bf1548c6b0bc0c2adeca740eef58d9 Mon Sep 17 00:00:00 2001 From: Dario Ernst Date: Mon, 25 Jul 2011 22:18:39 +0200 Subject: [PATCH 3/4] maybe have offset measurement --- cmdline/read-temp.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/cmdline/read-temp.c b/cmdline/read-temp.c index 47efe30..62ccc7a 100644 --- a/cmdline/read-temp.c +++ b/cmdline/read-temp.c @@ -29,7 +29,7 @@ respectively. #include "../firmware/masterchip/usbdrv/usbconfig.h" /* device's VID/PID and names */ void usage() { - printf("Usage: read-temp [spi|temp|dbg] [[opcode] [addr] [value]]\n"); + printf("Usage:\tread-temp [spi|temp|dbg] [[opcode] [addr] [value]]\n\tread-temp [offset] [channel]\n"); } int main(int argc, char **argv) @@ -128,7 +128,6 @@ int cnt, vid, pid; int dbg_getchar[] = {6, 0, 0}; int *rq; - fprintf(stdout, "answer: value=0x%04x (%i)\n", 1,1); while(1) { // check whether we have new rq = dbg_getchar; @@ -146,6 +145,39 @@ int cnt, vid, pid; fflush(stdout); } } + } else if(strcasecmp(argv[1], "offset") == 0) { + if(argc != 3) { + usage(); + return 0; + } + + int chan; + int *rq; + chan = atoi(argv[2]); + int offset_start[] = {6,1,chan}; + int offset_stop[] = {6,2,chan}; + + rq = offset_start; + + fprintf(stdout, "starting offset measuring for channel %i", chan); + cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, rq[0], rq[1], rq[2], buffer, sizeof(buffer), 5000); + if(cnt < 0){ + fprintf(stdout, "\nUSB error in iteration ?!?: %s\n", usb_strerror()); + } + + sleep(10); + + rq = offset_stop; + fprintf(stdout, "stopping and retrieving..."); + cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, rq[0], rq[1], rq[2], buffer, sizeof(buffer), 5000); + if(cnt < 0){ + fprintf(stdout, "\nUSB error in iteration ?!?: %s\n", usb_strerror()); + } + rxIndex = ((int)buffer[1] & 0xff) | (((int)buffer[0] & 0xff) << 8); + fprintf(stdout, "measured offset %i", rxIndex); + + + } else { usage(); } From 26f72f830e10da9b1f6ec74f2280251404b30c46 Mon Sep 17 00:00:00 2001 From: Dario Ernst Date: Mon, 25 Jul 2011 22:24:28 +0200 Subject: [PATCH 4/4] haz static offset definitions --- firmware/slavechip/main.c | 3 ++- firmware/slavechip/offset.c | 7 +++++++ firmware/slavechip/offset.h | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/firmware/slavechip/main.c b/firmware/slavechip/main.c index cd9a032..8f58f56 100644 --- a/firmware/slavechip/main.c +++ b/firmware/slavechip/main.c @@ -43,10 +43,11 @@ void hardinit() { } void softinit() { - mcpadc_init(ADC_GAIN_2|ADC_CONV_CONT|ADC_BITS_16); dbg_init(); stdout = &mystdout; printf("======= starting logging\n"); + mcpadc_init(ADC_GAIN_2|ADC_CONV_CONT|ADC_BITS_16); + offset_init(); } diff --git a/firmware/slavechip/offset.c b/firmware/slavechip/offset.c index 9135214..cb633c8 100644 --- a/firmware/slavechip/offset.c +++ b/firmware/slavechip/offset.c @@ -6,6 +6,13 @@ int16_t offset_val[4]; int16_t offsets[4]; +void offset_init() { + offsets[0] = 2342; + offsets[1] = 4223; + offsets[2] = 2423; + offsets[3] = 4232; +} + int16_t offset_measure_start(int16_t channel) { offset_measure[channel] = 1; offset_count[channel] = 1; diff --git a/firmware/slavechip/offset.h b/firmware/slavechip/offset.h index 2c23609..c051ba1 100644 --- a/firmware/slavechip/offset.h +++ b/firmware/slavechip/offset.h @@ -9,6 +9,7 @@ extern int16_t offset_count[4]; extern int16_t offset_val[4]; extern int16_t offsets[4]; +void offset_init(); int16_t offset_measure_start(int16_t channel); int16_t offset_measure_stop(int16_t channel);