From ec5752b69ce25b0abb1bf4b8493b3e6078e41c1a Mon Sep 17 00:00:00 2001 From: Paul Goeser Date: Sat, 5 Feb 2011 00:50:37 +0100 Subject: [PATCH 1/2] i2c fixed --- firmware/slavechip/i2c_simple.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/firmware/slavechip/i2c_simple.c b/firmware/slavechip/i2c_simple.c index 4523863..571ea60 100644 --- a/firmware/slavechip/i2c_simple.c +++ b/firmware/slavechip/i2c_simple.c @@ -2,14 +2,30 @@ #include #include #include +#include #include "i2c_simple.h" +#include "debug.h" + +//#define I2C_WAIT() while(!(TWCR & _BV(TWINT))) +#define I2C_WAIT() i2c_wait(); + + +void inline i2c_wait(){ + uint16_t cntr=0; + while(!(TWCR & _BV(TWINT))){ + cntr++; + if(cntr >= 1000){ + dbgLog("i2c waittime exceeded!\n"); + } + } +} + -#define I2C_WAIT() while(!(TWCR & _BV(TWINT))) void i2c_init() { - TWBR = 32;//bit rate + TWBR = 32;//bit rate: 100kbit TWSR = 0;//Prescaler // TWAR = 0x80;//our address 1000 000, don't listen to general call @@ -53,6 +69,7 @@ uint8_t i2c_read(uint8_t addr, uint8_t len, uint8_t *data) data[done] = TWDR; done++; TWCR |= _BV(TWINT) | _BV(TWSTO); + _delay_us(20); // wait about 2 bit periods return done; } @@ -91,6 +108,7 @@ uint8_t i2c_write_i(uint8_t addr, uint8_t len, uint8_t *data, uint8_t stop) } if(stop) {TWCR |= _BV(TWINT) | _BV(TWSTO);} + _delay_us(20); // wait about 2 bit periods return done; } From 2ef03fc8f282857401ed2a369a1c0f43f97380ba Mon Sep 17 00:00:00 2001 From: Paul Goeser Date: Sat, 5 Feb 2011 00:50:51 +0100 Subject: [PATCH 2/2] successful temperature readout --- cmdline/read-temp.c | 2 +- firmware/masterchip/spi_config.h | 2 +- firmware/slavechip/main.c | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cmdline/read-temp.c b/cmdline/read-temp.c index fffcd8b..59f1702 100644 --- a/cmdline/read-temp.c +++ b/cmdline/read-temp.c @@ -166,7 +166,7 @@ int cnt, vid, pid; fprintf(stderr, "%c", rxIndex); } } - usleep(1000); + usleep(100); } } else { usage(); diff --git a/firmware/masterchip/spi_config.h b/firmware/masterchip/spi_config.h index 6cb5a59..adc910d 100644 --- a/firmware/masterchip/spi_config.h +++ b/firmware/masterchip/spi_config.h @@ -1,5 +1,5 @@ -#define SPI_BAUDRATE 10000 +#define SPI_BAUDRATE 30000 #define SPI_MASTER #define SPI_SS_PORT D #define SPI_SS_PIN 7 diff --git a/firmware/slavechip/main.c b/firmware/slavechip/main.c index f0e48d8..ae317b3 100644 --- a/firmware/slavechip/main.c +++ b/firmware/slavechip/main.c @@ -11,6 +11,9 @@ uint16_t bar; uint16_t timertmp; uint16_t temperatures[4]; +int32_t temp_avg_cumul; +int16_t temp_avg_count; + void baz() { foo++; @@ -77,9 +80,21 @@ int __attribute__((noreturn)) main(void) { 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; } + 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); + } } }