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(); } 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 75cd804..1a2fa88 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(); @@ -47,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(); } @@ -65,6 +62,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++) { @@ -81,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 f0669db..4f6e5f2 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< + +extern int16_t offset_measure[4]; +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); + +#endif diff --git a/firmware/slavechip/spi_pointers.c b/firmware/slavechip/spi_pointers.c index 005610b..b5d49a1 100644 --- a/firmware/slavechip/spi_pointers.c +++ b/firmware/slavechip/spi_pointers.c @@ -1,6 +1,7 @@ #include "spi_pointers.h" #include "main.h" #include "debug.h" +#include "offset.h" uint8_t *spi_proto_globals8[] = { }; @@ -9,5 +10,7 @@ uint16_t *spi_proto_globals16[] = { }; funptr_t spi_proto_funcs[] = { - &dbg_getchar + &dbg_getchar, + &offset_measure_start, + &offset_measure_stop };