echo now _almost_ works, it gets right-shifted by 8

master
Dario Ernst 15 years ago
parent 8c24da5c8c
commit 8ac2673ac9

@ -127,14 +127,16 @@ void spi_sla_handle_packet(){
// TODO: make slave not hangup in case of partial read // TODO: make slave not hangup in case of partial read
uint8_t opcode, addr, do_write; uint8_t opcode, addr, do_write;
uint16_t data, newdata; uint16_t data, newdata;
opcode = readbuf[1]; writebuf[0] = 0; // generate invalid replies, so no inconsistent packets
addr = readbuf[2]; // are sent.
data = (readbuf[3]<<8)&0xff | readbuf[4]; opcode = readbuf[0];
addr = readbuf[1];
data = (readbuf[2]<<8)&0xff | readbuf[3];
newdata = spi_proto_slaveaction(opcode, addr, data); newdata = spi_proto_slaveaction(opcode, addr, data);
writebuf[0]=opcode;
writebuf[1]=addr; writebuf[1]=addr;
writebuf[2]=(newdata>>8)&0xff; writebuf[2]=(newdata>>8)&0xff;
writebuf[3]=(newdata)&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){ ISR(PCINT0_vect){ // SS-line changed
if(SPI_SSIN & _BV(SPI_SS_PIN)){ if(SPI_SSIN & _BV(SPI_SS_PIN)){ // SS-line was actually released
pos=0; pos=0;
SPCR &= ~(_BV(SPIE)); SPCR &= ~(_BV(SPIE));
sei(); sei();

@ -16,10 +16,11 @@ uint16_t spi_master_transceive(uint8_t opcode, uint8_t addr, uint16_t value)
spi_mst_start_packet(); spi_mst_start_packet();
spi_mst_write_read(5,mst_buf); spi_mst_write_read(5,mst_buf);
spi_mst_end_packet(); 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 // 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 spi_proto_slaveaction(uint8_t opcode, uint8_t addr, uint16_t data) {
uint16_t retval = 0; uint16_t retval = 0;
switch(opcode){ switch(opcode){
case 0:
retval = data;
break;
case 1: case 1:
//TODO retval = data;
retval = 0;
break; break;
case 2: case 2:
retval = (*spi_proto_globals8[addr]); retval = (*spi_proto_globals8[addr]);
@ -79,6 +76,13 @@ uint16_t spi_proto_slaveaction(uint8_t opcode, uint8_t addr, uint16_t data) {
break; break;
case 6: case 6:
(*spi_proto_funcs[addr])(); (*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; break;
} }
return retval; return retval;

Loading…
Cancel
Save