diff --git a/firmware/masterchip/main.h b/firmware/masterchip/main.h index fc9bc6a..0595353 100644 --- a/firmware/masterchip/main.h +++ b/firmware/masterchip/main.h @@ -26,10 +26,12 @@ #include "spi.h" #include "usb.h" +#define SOFTTIMERNUMS 4 +#include "softtimer.h" + extern uint8_t newThermoData; extern uint16_t thermoData[]; -#define SOFTTIMERNUMS 4 #define LED1PORT PORTC #define LED1APIN 0 diff --git a/firmware/masterchip/softtimer.c b/firmware/masterchip/softtimer.c new file mode 120000 index 0000000..28d64fd --- /dev/null +++ b/firmware/masterchip/softtimer.c @@ -0,0 +1 @@ +../shared/softtimer.c \ No newline at end of file diff --git a/firmware/masterchip/softtimer.h b/firmware/masterchip/softtimer.h new file mode 120000 index 0000000..2d6405e --- /dev/null +++ b/firmware/masterchip/softtimer.h @@ -0,0 +1 @@ +../shared/softtimer.h \ No newline at end of file diff --git a/firmware/shared/softtimer.c b/firmware/shared/softtimer.c new file mode 100644 index 0000000..2b25429 --- /dev/null +++ b/firmware/shared/softtimer.c @@ -0,0 +1,20 @@ +#include "softtimer.h" + +volatile uint16_t timer1_acc; +uint16_t softtimer_last[SOFTTIMERNUMS]; + +void softtimer_reset(uint8_t timernum){ + softtimer_last[timernum] = timer1_acc; +} + +uint8_t softtimer(uint8_t timernum, uint16_t interval){ + uint16_t timer1_acc_tmp = timer1_acc; // because of volatile + if((uint16_t)(timer1_acc_tmp - (uint16_t)(softtimer_last[timernum]) >= interval )){ + softtimer_last[timernum] = timer1_acc_tmp; + return(1); + } + return(0); +} + +// SOFTTIMER( , ); +// #define SOFTTIMER(n,a) if(softtimer((n),(a*8))) diff --git a/firmware/shared/softtimer.h b/firmware/shared/softtimer.h new file mode 100644 index 0000000..417e61b --- /dev/null +++ b/firmware/shared/softtimer.h @@ -0,0 +1,18 @@ +// NOTES: +// Please #define SOFTTIMERNUMS aNum before including this! + +#ifndef __SOFTTIMER_H +#define __SOFTTIMER_H + +extern volatile uint16_t timer1_acc; +extern uint16_t softtimer_last[SOFTTIMERNUMS]; + + +void softtimer_reset(uint8_t timernum); +uint8_t softtimer(uint8_t timernum, uint16_t interval); + +// SOFTTIMER( , ); +#define SOFTTIMER(n,a) if(softtimer((n),(a*8))) + + +#endif diff --git a/firmware/slavechip/i2c_simple.c b/firmware/slavechip/i2c_simple.c index f946064..1965d49 100644 --- a/firmware/slavechip/i2c_simple.c +++ b/firmware/slavechip/i2c_simple.c @@ -1,23 +1,97 @@ - #include #include "i2c_simple.h" - - -void i2c_init(){ - //TODO: implement +void i2c_init() +{ + TWBR = 0;//bit rate + TWSR = 0;//Prescaler + + TWAR = 0x80;//our address 1000 000, don't listen to general call + TWAMR = 0; + + TWCR = TWEA | TWEN;/* TWINT clear -> we must do something; unset TWSTA manually!, TWSTO */ + //TWDR - data } -uint8_t i2c_read(uint8_t addr, uint8_t len, uint8_t *data){ - return 0; //TODO: implement +uint8_t i2c_read(uint8_t addr, uint8_t len, uint8_t *data) +{ + TWCR &= ~TWSTO; + TWCR |= TWINT | TWSTA; + while(~TWCR & TWINT) {;}/* get bus access */ + /* no error possible */ + + TWDR = addr | 1; + TWCR &= ~(TWSTA | TWSTO); + TWCR |= TWINT; + while(~TWCR & TWINT) {;}/* transmit address */ + /* possible results: ACK, NAK, abitration lost */ + if(TW_STATUS == TW_MR_SLA_NACK) + { + TWCR |= TWINT | TWSTO; + return 0; + } + else if(TW_STATUS == TW_MR_ARB_LOST) {return 0;} + + uint8_t done = 0; + while(done + 1 < len) + { + TWCR |= TWEA | TWINT;/* send ack after byte */ + while(~TWCR & TWINT) {;}/* read data */ + data[done] = TWDR; + done++; + TWCR |= TWINT; + } + + TWCR &= ~TWEA; + TWCR |= TWINT;/* send nak after byte */ + while(~TWCR & TWINT) {;}/* read data */ + data[done] = TWDR; + done++; + TWCR |= TWINT | TWSTO; + return done; } -uint8_t i2c_write(uint8_t addr, uint8_t len, uint8_t *data){ - return 0; //TODO: implement +uint8_t i2c_write(uint8_t addr, uint8_t len, uint8_t *data) +{ + TWCR &= ~TWSTO; + TWCR |= TWINT | TWSTA; + while(~TWCR & TWINT) {;}/* get bus access */ + /* no error possible */ + + TWDR = addr; + TWCR &= ~(TWSTA | TWSTO); + TWCR |= TWINT; + while(~TWCR & TWINT) {;}/* transmit address */ + /* possible results: ACK, NAK, abitration lost */ + if(TW_STATUS == TW_MT_SLA_NACK) + { + TWCR |= TWINT | TWSTO; + return 0; + } + else if(TW_STATUS == TW_MT_ARB_LOST) {return 0;} + + uint8_t done = 0; + while(done < len) + { + TWDR = data[done]; + TWCR |= TWINT; + while(~TWCR & TWINT) {;}/* write data */ + /* possible results: ACK, NAK, abitration lost */ + if(TW_STATUS == TW_MT_DATA_NACK) + { + TWCR |= TWINT | TWSTO; + return done; + } + else if(TW_STATUS == TW_MT_ARB_LOST) {return done;} + done++; + } + + TWCR |= TWINT | TWSTO; + return done; } -uint8_t i2c_write_read(uint8_t addr, uint8_t writelen, uint8_t* writedata, uint8_t readlen, - uint8_t* readdata){ - return 0; //TODO: implement +uint8_t i2c_write_read(uint8_t addr, uint8_t writelen, uint8_t *writedata, uint8_t readlen, uint8_t *readdata) +{ + return 0; //TODO: implement } diff --git a/firmware/slavechip/i2c_simple.h b/firmware/slavechip/i2c_simple.h index c7aee5f..2e76444 100644 --- a/firmware/slavechip/i2c_simple.h +++ b/firmware/slavechip/i2c_simple.h @@ -1,7 +1,5 @@ - - - - +#ifndef I2C_SIMPLE_H +#define I2C_SIMPLE_H /* initializes i2c interface (master, 400kHz) */ void i2c_init(); @@ -13,5 +11,6 @@ uint8_t i2c_read(uint8_t addr, uint8_t len, uint8_t *data); uint8_t i2c_write(uint8_t addr, uint8_t len, uint8_t *data); /* writes, followed by a repeated start and a read */ -uint8_t i2c_write_read(uint8_t addr, uint8_t writelen, uint8_t* writedata, uint8_t readlen, - uint8_t* readdata); +uint8_t i2c_write_read(uint8_t addr, uint8_t writelen, uint8_t *writedata, uint8_t readlen, uint8_t *readdata); + +#endif