From ed66f05f4c0eb3bbb30c91c3e5b00cda987c3fd4 Mon Sep 17 00:00:00 2001 From: Paul Goeser Date: Sat, 11 Dec 2010 22:18:00 +0100 Subject: [PATCH 1/2] still debugging, pure chaos --- firmware/masterchip/main.c | 12 ++++++------ firmware/shared/spi.c | 9 ++++----- firmware/shared/spi_proto.c | 2 +- firmware/shared/spi_proto.h | 2 +- firmware/slavechip/main.c | 2 ++ firmware/slavechip/spi_pointers.c | 14 ++++++++++++++ 6 files changed, 28 insertions(+), 13 deletions(-) diff --git a/firmware/masterchip/main.c b/firmware/masterchip/main.c index 06a9f49..e9a13af 100644 --- a/firmware/masterchip/main.c +++ b/firmware/masterchip/main.c @@ -84,10 +84,10 @@ int __attribute__((noreturn)) main(void) { } SOFTTIMER(1,250) { - //updateTemperature(); + updateTemperature(); } - SOFTTIMER(3,100) { +/* SOFTTIMER(3,100) { /* uint8_t foo[4]; foo[0]=0x55; foo[1]=0x88; @@ -96,20 +96,20 @@ int __attribute__((noreturn)) main(void) { spi_mst_start_packet(); spi_mst_write_read(2,foo+2); spi_mst_end_packet(); -*/ + uint16_t foo=0x3456; foo=spi_master_communicate(3,5,foo); display_gotoyx(0,0); -/* display_puthex(foo[0]); + display_puthex(foo[0]); display_puthex(foo[1]); display_puthex(foo[2]); - display_puthex(foo[3]);*/ + display_puthex(foo[3]); display_puthex((foo>>8)&0xff); display_puthex((foo)&0xff); display_puts("\nfoo"); display_update(); - } + }*/ } } diff --git a/firmware/shared/spi.c b/firmware/shared/spi.c index 7d428b4..8a74662 100644 --- a/firmware/shared/spi.c +++ b/firmware/shared/spi.c @@ -120,7 +120,6 @@ void __attribute__((always_inline)) spi_sla_handle_packet(){ // TODO: make slave not hangup in case of partial read uint8_t opcode, addr, do_write; uint16_t data; - SPI_WAIT; SPDR = 0x53; // random ack SPI_WAIT; opcode = SPDR; @@ -146,13 +145,13 @@ void __attribute__((always_inline)) spi_sla_handle_packet(){ ISR(SPI_STC_vect){ - uint8_t a; +/* uint8_t a; a = SPDR; - SPDR = ~a; -/* SPCR &= ~(_BV(SPIE)); //disable spi interrupts + SPDR = ~a; //*/ + SPCR &= ~(_BV(SPIE)); //disable spi interrupts spi_sla_handle_packet(); SPCR |= _BV(SPIE); - */ + //*/ } #endif //not SPI_MASTER diff --git a/firmware/shared/spi_proto.c b/firmware/shared/spi_proto.c index 13cf067..2f43bdd 100644 --- a/firmware/shared/spi_proto.c +++ b/firmware/shared/spi_proto.c @@ -18,7 +18,7 @@ void talk_to_slave(uint8_t opcode, uint8_t addr, uint8_t flags) uint16_t spi_master_communicate(uint8_t opcode, uint8_t address, uint16_t value) { - uint8_t flags = spi_proto_needs(); + uint8_t flags = spi_proto_needs(opcode); write_data[2] = value >> 8; write_data[3] = value & 0xff; talk_to_slave(opcode, address, flags); diff --git a/firmware/shared/spi_proto.h b/firmware/shared/spi_proto.h index 67afbf3..1d08792 100644 --- a/firmware/shared/spi_proto.h +++ b/firmware/shared/spi_proto.h @@ -9,7 +9,7 @@ #define SPI_READ_DATA 0x10 #define SPI_NONE 0 -uint16_t spi_master_communicate(uint8_t opcode, uint8_t address, uint16_t value) +uint16_t spi_master_communicate(uint8_t opcode, uint8_t address, uint16_t value); uint8_t spi_proto_needs(uint8_t opcode); diff --git a/firmware/slavechip/main.c b/firmware/slavechip/main.c index 836f802..debf805 100644 --- a/firmware/slavechip/main.c +++ b/firmware/slavechip/main.c @@ -30,6 +30,8 @@ void softinit() { int __attribute__((noreturn)) main(void) { hardinit(); softinit(); + foo= 0xfafa; + bar = 0x87; for(;;){ diff --git a/firmware/slavechip/spi_pointers.c b/firmware/slavechip/spi_pointers.c index f2835a2..b692a0b 100644 --- a/firmware/slavechip/spi_pointers.c +++ b/firmware/slavechip/spi_pointers.c @@ -3,9 +3,23 @@ #include "main.h" uint16_t *spi_proto_globals8[] = { + &foo, + &foo, + &foo, + &foo, + &foo, + &foo, &foo }; uint8_t *spi_proto_globals16[] = { + &bar, + &bar, + &bar, + &bar, + &bar, + &bar, + &bar, + &bar, &bar }; funptr_t spi_proto_funcs[] = { From 4eac148f8e579c12a265d0a72f259cfdf134c056 Mon Sep 17 00:00:00 2001 From: Paul Goeser Date: Sat, 11 Dec 2010 22:35:01 +0100 Subject: [PATCH 2/2] new protocol for spi started --- firmware/shared/spi.c | 48 ++++++++++++++++++--------------- firmware/shared/spi_proto.c | 11 +++++--- firmware/slavechip/spi_config.h | 4 +-- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/firmware/shared/spi.c b/firmware/shared/spi.c index 8a74662..29854a0 100644 --- a/firmware/shared/spi.c +++ b/firmware/shared/spi.c @@ -14,6 +14,9 @@ * currently alpha, uses interrupts, single master/single slave operation */ +uint8_t readbuf[4]; +uint8_t writebuf[4]; +uint8_t pos; @@ -70,6 +73,7 @@ void spi_init(){ #ifndef SPI_MASTER SPCR |= _BV(SPIE); //enable interrupts #endif + pos = 0; } @@ -116,28 +120,25 @@ void spi_mst_write_read(uint8_t len, uint8_t *data){ #ifndef SPI_MASTER -void __attribute__((always_inline)) spi_sla_handle_packet(){ +void spi_sla_handle_packet(){ // TODO: make slave not hangup in case of partial read uint8_t opcode, addr, do_write; uint16_t data; - SPDR = 0x53; // random ack - SPI_WAIT; - opcode = SPDR; + opcode = readbuf[0]; + addr = readbuf[1]; do_write = spi_proto_needswrite(opcode); - SPI_WAIT; - addr = SPDR; - SPDR = 0x00; // buys some pause if(do_write){ data = spi_proto_handlewrite(opcode, addr); - SPI_WAIT; - SPDR = data >> 8; - SPI_WAIT; - SPDR = data & 0xff; + writebuf[0]=opcode; + writebuf[1]=addr; + writebuf[2]=(data>>8)&0xff; + writebuf[3]=(data)&0xff; } else { - SPI_WAIT; - data = SPDR << 8; - SPI_WAIT; - data |= SPDR; + writebuf[0]=0; + writebuf[1]=0; + writebuf[2]=0; + writebuf[3]=0; + data = (readbuf[3]<<8)&0xff | readbuf[4]; spi_proto_handleread(opcode, addr, data); } } @@ -145,13 +146,16 @@ void __attribute__((always_inline)) spi_sla_handle_packet(){ ISR(SPI_STC_vect){ -/* uint8_t a; - a = SPDR; - SPDR = ~a; //*/ - SPCR &= ~(_BV(SPIE)); //disable spi interrupts - spi_sla_handle_packet(); - SPCR |= _BV(SPIE); - //*/ + SPDR = *(writebuf+pos); + *(readbuf+pos) = SPDR; + pos++; + if(SPI_SSIN & _BV(SPI_SS_PIN)){ + pos=0; + SPCR &= ~(_BV(SPIE)); + sei(); + spi_sla_handle_packet(); + SPCR |= _BV(SPIE); + } } #endif //not SPI_MASTER diff --git a/firmware/shared/spi_proto.c b/firmware/shared/spi_proto.c index 2f43bdd..0017088 100644 --- a/firmware/shared/spi_proto.c +++ b/firmware/shared/spi_proto.c @@ -16,13 +16,18 @@ void talk_to_slave(uint8_t opcode, uint8_t addr, uint8_t flags) spi_mst_end_packet(); } -uint16_t spi_master_communicate(uint8_t opcode, uint8_t address, uint16_t value) +uint16_t spi_master_communicate(uint8_t opcode, uint8_t addr, uint16_t value) { uint8_t flags = spi_proto_needs(opcode); + write_data[0] = opcode; + write_data[1] = addr; write_data[2] = value >> 8; write_data[3] = value & 0xff; - talk_to_slave(opcode, address, flags); - return (read_data[0] << 8) | read_data[1]; + spi_mst_write_read(4,write_data); + while(write_data[0] == 0){ + spi_mst_write_read(4,write_data); + } + return (write_data[2] << 8) | read_data[3]; } uint8_t spi_proto_needs(uint8_t opcode) diff --git a/firmware/slavechip/spi_config.h b/firmware/slavechip/spi_config.h index 30e155c..cb36b2f 100644 --- a/firmware/slavechip/spi_config.h +++ b/firmware/slavechip/spi_config.h @@ -3,5 +3,5 @@ #ifdef SPI_MASTER #undef SPI_MASTER #endif //SPI_MASTER -//#define SPI_SS_PORT D -//#define SPI_SS_PIN 7 +#define SPI_SS_PORT B +#define SPI_SS_PIN 2