diff --git a/documentation/SPI-Proto b/documentation/SPI-Proto index 5f36039..56c3441 100644 --- a/documentation/SPI-Proto +++ b/documentation/SPI-Proto @@ -8,9 +8,6 @@ Where following values are Valid: |--------------------------------------------------------------------------------------------------------------------------| | Decimal Hex Name Address ValueL ValueH Reply Description| |--------------------------------------------------------------------------------------------------------------------------| -| 0000001 0x01 Read-Temp Number of zeroes/Rand zeroes/Rand A 16Bit Value Reads the | -| Thermometer Representing Temperature| -| Starting with 0 the Temperature | |--------------------------------------------------------------------------------------------------------------------------| | 0000002 0x02 Read-Var8 Number of the zeroes/Rand zeroes/Rand A 8Bit Value Reads 8Bit | | var-DEFINE read from VAR Variable | @@ -21,10 +18,15 @@ Where following values are Valid: | 0000004 0x04 Write-Var8 Number of the Value of Var zeroes/Rand zeroes/Rand Writes 8Bit| | var-DEFINE to write Variable | |--------------------------------------------------------------------------------------------------------------------------| -| 0000005 0x05 Write-Var16 Number of the Value of Var Value of Var zeroes/Rand Writes 16 | +| 0000005 0x05 Write-Var16 Number of the Value of Var Value of Var zeroes/Rand Writes 16 | | var-DEFINE to write (HIGH) to write (LOW) Bit Var | |--------------------------------------------------------------------------------------------------------------------------| -| 0000006 0x06 Call-Func Number of the zeroes/Rand zeroes/Rand zeroes/Rand Calls a | -| func-DEFINE remote | - function | +| 0000006 0x06 Call-Func Number of the zeroes/Rand zeroes/Rand zeroes/Rand Calls a| +| func-DEFINE remote | +| function | +|--------------------------------------------------------------------------------------------------------------------------| + +| 0000001 0x01 Read-Temp Number of zeroes/Rand zeroes/Rand A 16Bit Value Reads the | +| Thermometer Representing Temperature| +| Starting with 0 the Temperature | |--------------------------------------------------------------------------------------------------------------------------| diff --git a/firmware/shared/spi_proto.c b/firmware/shared/spi_proto.c index 857f9e2..b78d16b 100644 --- a/firmware/shared/spi_proto.c +++ b/firmware/shared/spi_proto.c @@ -1,5 +1,6 @@ #include "spi_proto.h" #include "spi.h" +#include "main.h" /***** MASTER *****/ @@ -90,8 +91,9 @@ uint16_t spi_proto_slaveaction(uint8_t opcode, uint8_t addr, uint16_t data) { //TODO: prevent a function from being run several times because the message gets repeated. break; case 7: - //TODO - retval = 0; + if(addr < 4) { + retval = temperatures[addr]; + } else return 0; break; default: break; diff --git a/firmware/slavechip/main.c b/firmware/slavechip/main.c index ae317b3..6f636f0 100644 --- a/firmware/slavechip/main.c +++ b/firmware/slavechip/main.c @@ -6,18 +6,11 @@ #include "debug.h" -uint8_t foo; -uint16_t bar; uint16_t timertmp; uint16_t temperatures[4]; - -int32_t temp_avg_cumul; -int16_t temp_avg_count; - - -void baz() { - foo++; -} +uint8_t sensor_active[4]; +int32_t temp_avg_cumul[4]; +int16_t temp_avg_count[4]; /* pinout * - i2c: @@ -36,9 +29,8 @@ void baz() { #define MUXER_CHANNEL_2 1 #define MUXER_CHANNEL_3 3 +/* initializes the hardware */ void hardinit() { - /* initializes the hardware */ - // enable softtimer isr TCCR1A = _BV(WGM10); TCCR1B = _BV(WGM12) | _BV(CS11); // clk/8 prescaler, 4kHz PWM-freq. @@ -47,53 +39,45 @@ void hardinit() { spi_init(); muxer_init(); - i2c_init(); sei(); } void softinit() { - - //test values - foo = 0x87; - bar= 0xfafa; - mcpadc_init(ADC_GAIN_2|ADC_CONV_CONT|ADC_BITS_16); - } int __attribute__((noreturn)) main(void) { - temperatures[2]=1; hardinit(); - temperatures[2]=10; softinit(); muxer_set(MUXER_CHANNEL_0); - temperatures[2]=0; - dbgLog("Hallo, Welt!\n"); for(;;){ SOFTTIMER(1,10) { - if(mcpadc_has_new_data()) { - temperatures[0] = mcpadc_get_data(); - temperatures[2]++; - int16_t temp = temperatures[0]; - temp_avg_cumul += temp; - temp_avg_count += 1; - } - temperatures[1] = 22; + for(int i=0; i<4; i++) { + if(sensor_active[i] && mcpadc_has_new_data(i)) { + temperatures[i] = mcpadc_get_data(i); + temp_avg_cumul[i] += temperatures[i]; + temp_avg_count[i] += 1; + } + } } - SOFTTIMER(2,500){ - int32_t temp = temp_avg_cumul; - temp /= temp_avg_count; - temp_avg_count = 0; temp_avg_cumul = 0; - int32_t nV = (temp * 625); - int32_t mK = nV/39; - dbgLog("temp: %6li (%li µV, %li mK)\n",temp,nV/1000, mK); + SOFTTIMER(2,500){ + for(int i=0; i<4; i++) { + if(sensor_active[i]) { + int32_t temp = temp_avg_cumul[i]; + temp /= temp_avg_count[i]; + temp_avg_count[i] = 0; temp_avg_cumul[i] = 0; + int32_t nV = (temp * 625); + int32_t mK = nV/39; + dbgLog("temp-%i: %6li (%li µV, %li mK)\n",i,temp,nV/1000, mK); + } + } } } diff --git a/firmware/slavechip/main.h b/firmware/slavechip/main.h index 4544e03..3344c5c 100644 --- a/firmware/slavechip/main.h +++ b/firmware/slavechip/main.h @@ -14,11 +14,7 @@ #include "softtimer.h" #include "debug.h" -extern uint8_t foo; -extern uint16_t bar; extern uint16_t timertmp; extern uint16_t temperatures[]; -void baz(); - #endif //__MAIN_H diff --git a/firmware/slavechip/mcp_adc.c b/firmware/slavechip/mcp_adc.c index ed40489..1c6856a 100644 --- a/firmware/slavechip/mcp_adc.c +++ b/firmware/slavechip/mcp_adc.c @@ -42,8 +42,10 @@ void mcpadc_init(uint8_t mode) curmode = mode; } -uint8_t mcpadc_has_new_data(void) +uint8_t mcpadc_has_new_data(uint8_t channel) { + // TODO: implement channel + /* this assumes that the first RDY bit read after the sample data indicates the old/new state of the sample we just read */ if(unread_data_available){ // we already have new data, don't overwrite that by reading more @@ -69,8 +71,9 @@ uint8_t mcpadc_has_new_data(void) } #if ADC_ENABLE_18_BIT_MODE -int32_t mcpadc_get_data(void) +int32_t mcpadc_get_data(uint8_t channel) { + // TODO: implement channel if(!unread_data_available){ return 0; } @@ -86,7 +89,7 @@ int32_t mcpadc_get_data(void) return value; } #else -int16_t mcpadc_get_data(void) +int16_t mcpadc_get_data(uint8_t channel) { if(!unread_data_available){ return 0; diff --git a/firmware/slavechip/mcp_adc.h b/firmware/slavechip/mcp_adc.h index db90bcc..33cf987 100644 --- a/firmware/slavechip/mcp_adc.h +++ b/firmware/slavechip/mcp_adc.h @@ -26,15 +26,15 @@ void mcpadc_init(uint8_t mode); /* tests if the adc has a new sample * returns nonzero if new data is aviable, 0 otherwise */ -uint8_t mcpadc_has_new_data(void); +uint8_t mcpadc_has_new_data(uint8_t channel); /* returns the most recent sample result of the adc * !! return type depends on defines */ #if ADC_ENABLE_18_BIT_MODE -int32_t mcpadc_get_data(void); +int32_t mcpadc_get_data(uint8_t channel); #else -int16_t mcpadc_get_data(void); +int16_t mcpadc_get_data(uint8_t channel); #endif /* instructs the adc to take a new sample now diff --git a/firmware/slavechip/spi_pointers.c b/firmware/slavechip/spi_pointers.c index cb09299..3bf5ae0 100644 --- a/firmware/slavechip/spi_pointers.c +++ b/firmware/slavechip/spi_pointers.c @@ -7,18 +7,9 @@ uint8_t *spi_proto_globals8[] = { }; uint16_t *spi_proto_globals16[] = { - &timertmp, - &temperatures[0], - &temperatures[1], - &temperatures[2], - &temperatures[3], }; funptr_t spi_proto_funcs[] = { &dbgHasNew, &dbgAdvanceChar }; - - - -