From 8ac2673ac97964c6385d1d0cbb9c6954868f69cd Mon Sep 17 00:00:00 2001 From: Dario Ernst Date: Wed, 26 Jan 2011 20:05:42 +0100 Subject: [PATCH] echo now _almost_ works, it gets right-shifted by 8 --- firmware/shared/spi.c | 14 ++++++++------ firmware/shared/spi_proto.c | 18 +++++++++++------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/firmware/shared/spi.c b/firmware/shared/spi.c index 45a4204..83902fc 100644 --- a/firmware/shared/spi.c +++ b/firmware/shared/spi.c @@ -127,14 +127,16 @@ void spi_sla_handle_packet(){ // TODO: make slave not hangup in case of partial read uint8_t opcode, addr, do_write; uint16_t data, newdata; - opcode = readbuf[1]; - addr = readbuf[2]; - data = (readbuf[3]<<8)&0xff | readbuf[4]; + writebuf[0] = 0; // generate invalid replies, so no inconsistent packets + // are sent. + opcode = readbuf[0]; + addr = readbuf[1]; + data = (readbuf[2]<<8)&0xff | readbuf[3]; newdata = spi_proto_slaveaction(opcode, addr, data); - writebuf[0]=opcode; writebuf[1]=addr; writebuf[2]=(newdata>>8)&0xff; writebuf[3]=(newdata)&0xff; + writebuf[0]=opcode; // set opcode last to validate packet (it's a kind of locking) } @@ -151,8 +153,8 @@ ISR(SPI_STC_vect){ -ISR(PCINT0_vect){ - if(SPI_SSIN & _BV(SPI_SS_PIN)){ +ISR(PCINT0_vect){ // SS-line changed + if(SPI_SSIN & _BV(SPI_SS_PIN)){ // SS-line was actually released pos=0; SPCR &= ~(_BV(SPIE)); sei(); diff --git a/firmware/shared/spi_proto.c b/firmware/shared/spi_proto.c index 3496540..6259a6b 100644 --- a/firmware/shared/spi_proto.c +++ b/firmware/shared/spi_proto.c @@ -16,10 +16,11 @@ uint16_t spi_master_transceive(uint8_t opcode, uint8_t addr, uint16_t value) spi_mst_start_packet(); spi_mst_write_read(5,mst_buf); spi_mst_end_packet(); - } while(mst_buf[1] == 0); // wait for echoed opcode + } while(mst_buf[1] =! opcode || mst_buf[2] != addr); // wait for echoed opcode + //TODO: validate opcode and addr // bytes from slave are received shifted right by one - return (mst_buf[3] << 8) | mst_buf[4]; + return ((mst_buf[3] << 8) | mst_buf[4]); } @@ -58,12 +59,8 @@ void call_func(uint8_t number) uint16_t spi_proto_slaveaction(uint8_t opcode, uint8_t addr, uint16_t data) { uint16_t retval = 0; switch(opcode){ - case 0: - retval = data; - break; case 1: - //TODO - retval = 0; + retval = data; break; case 2: retval = (*spi_proto_globals8[addr]); @@ -79,6 +76,13 @@ uint16_t spi_proto_slaveaction(uint8_t opcode, uint8_t addr, uint16_t data) { break; case 6: (*spi_proto_funcs[addr])(); + //TODO: prevent a function from being run several times because the message gets repeated. + break; + case 7: + //TODO + retval = 0; + break; + default: break; } return retval;