From b199352e54b9e8665e4f1421b3f3aae873233404 Mon Sep 17 00:00:00 2001 From: Dario Ernst Date: Fri, 10 Dec 2010 23:16:56 +0100 Subject: [PATCH] spi protocol defined? --- documentation/SPI-Proto | 4 ++++ firmware/shared/spi.c | 17 +++++++++++++++-- firmware/shared/spi.h | 9 +++++++-- firmware/shared/spi_proto.c | 30 ++++++++++++++++++++++++++++++ firmware/shared/spi_proto.h | 18 ++++++++++++++++++ firmware/slavechip/Makefile | 2 +- firmware/slavechip/main.c | 7 +++++++ firmware/slavechip/main.h | 6 ++++++ firmware/slavechip/spi_proto.c | 1 + firmware/slavechip/spi_proto.h | 1 + 10 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 firmware/shared/spi_proto.c create mode 100644 firmware/shared/spi_proto.h create mode 120000 firmware/slavechip/spi_proto.c create mode 120000 firmware/slavechip/spi_proto.h diff --git a/documentation/SPI-Proto b/documentation/SPI-Proto index 980ee66..cd16977 100644 --- a/documentation/SPI-Proto +++ b/documentation/SPI-Proto @@ -24,3 +24,7 @@ Where following values are Valid: | 0000005 0x05 Write-Var16 Number of the Value of Var Value of Var zeroes/Rand Writes 16 | | var-DEFINE to write (HIGH) to write (LOW) Bit Var | |--------------------------------------------------------------------------------------------------------------------------| +| 0000006 0x07 Call-Func Number of the zeroes/Rand zeroes/Rand zeroes/Rand Calls a | +| func-DEFINE remote | + function | +|--------------------------------------------------------------------------------------------------------------------------| diff --git a/firmware/shared/spi.c b/firmware/shared/spi.c index 226692a..2d653d1 100644 --- a/firmware/shared/spi.c +++ b/firmware/shared/spi.c @@ -1,17 +1,28 @@ #include #include #include - +#include "main.h" #include +uint16_t *spi_proto_globals8[] = { + &foo + }; +uint8_t *spi_proto_globals16[] = { + &bar + }; +funptr_t spi_proto_funcs[] = { + &baz + }; + + + /* SPI framework. * * currently alpha, uses interrupts, single master/single slave operation */ - uint8_t * volatile spi_writeptr; uint8_t * volatile spi_readptr; uint8_t volatile spi_writelen; @@ -19,6 +30,8 @@ uint8_t volatile spi_readlen; uint8_t spi_readbuf[SPI_READBUF_LEN]; + + void spi_init(){ uint8_t spcr, spsr, d; diff --git a/firmware/shared/spi.h b/firmware/shared/spi.h index 411a1e3..9ad1eba 100644 --- a/firmware/shared/spi.h +++ b/firmware/shared/spi.h @@ -1,11 +1,14 @@ +#ifndef __SPI_H +#define __SPI_H + +#include "spi_proto.h" + #define SPI_BAUDRATE 1000000 #define SPI_MASTER 1 #define SPI_READBUF_LEN 32 #define SPI_SS_PORT D #define SPI_SS_PIN 7 - - // copied/adapted from usbdrv.h #define SPI_CONCAT(a, b) a ## b @@ -19,3 +22,5 @@ uint8_t spi_write(uint8_t *data, uint8_t len); void spi_init(); + +#endif diff --git a/firmware/shared/spi_proto.c b/firmware/shared/spi_proto.c new file mode 100644 index 0000000..bcf9928 --- /dev/null +++ b/firmware/shared/spi_proto.c @@ -0,0 +1,30 @@ +#include "spi_proto.h" + +uint8_t spi_proto_needswrite(uint8_t opcode) { + if(opcode == 4 || opcode == 5) return 1; + return 0; +} + +uint16_t spi_proto_handlewrite(uint8_t opcode, uint8_t addr) { + if(opcode == 4) { + return (uint16_t) (*spi_proto_globals8[addr]); + } else if(opcode == 5) { + return (*spi_proto_globals16[addr]); + } else { + return 0xFFFF; + } + +} + +void spi_proto_handleread(uint8_t opcode, uint8_t addr, uint16_t data) { + if(opcode == 2) { + *spi_proto_globals8[addr] = (uint8_t) data; + } else if(opcode == 3) { + *spi_proto_globals16[addr] = data; + } else if (opcode == 6) { + funptr_t func = spi_proto_funcs[addr]; + (*func)(); + } else { + // boom + } +} diff --git a/firmware/shared/spi_proto.h b/firmware/shared/spi_proto.h new file mode 100644 index 0000000..e57f691 --- /dev/null +++ b/firmware/shared/spi_proto.h @@ -0,0 +1,18 @@ +#ifndef __SPIPROTO_H +#define __SPIPROTO_H + +#include +#include "main.h" + +typedef void(*funptr_t)(); + + +extern uint16_t *spi_proto_globals8[]; +extern uint8_t *spi_proto_globals16[]; +extern funptr_t spi_proto_funcs[]; + +uint8_t spi_proto_needswrite(uint8_t opcode); +uint16_t spi_proto_handlewrite(uint8_t opcode, uint8_t addr); +void spi_proto_handleread(uint8_t opcode, uint8_t addr, uint16_t data); + +#endif diff --git a/firmware/slavechip/Makefile b/firmware/slavechip/Makefile index 7b0d7a6..1846ba1 100644 --- a/firmware/slavechip/Makefile +++ b/firmware/slavechip/Makefile @@ -2,7 +2,7 @@ include ../Makefile.inc COMPILE = avr-gcc $(CFLAGS) $(DEFINES) -OBJECTS = main.o mcp_adc.o i2c_simple.o softtimer.o +OBJECTS = main.o mcp_adc.o i2c_simple.o softtimer.o spi_proto.o spi.o # symbolic targets: all: firmware.hex diff --git a/firmware/slavechip/main.c b/firmware/slavechip/main.c index 9e81ca0..3840cbb 100644 --- a/firmware/slavechip/main.c +++ b/firmware/slavechip/main.c @@ -1,6 +1,13 @@ #include "main.h" #include "spi.h" +uint16_t foo = 16; +uint8_t bar = 8; + +void baz() { + +} + void hardinit() { /* initializes the hardware */ diff --git a/firmware/slavechip/main.h b/firmware/slavechip/main.h index 9719590..55566ea 100644 --- a/firmware/slavechip/main.h +++ b/firmware/slavechip/main.h @@ -1,5 +1,6 @@ #ifndef __MAIN_H #define __MAIN_H + #include #include #include @@ -12,4 +13,9 @@ #define SOFTTIMERNUMS 4 #include "softtimer.h" +extern uint16_t foo; +extern uint8_t bar; + +void baz(); + #endif //__MAIN_H diff --git a/firmware/slavechip/spi_proto.c b/firmware/slavechip/spi_proto.c new file mode 120000 index 0000000..a86d415 --- /dev/null +++ b/firmware/slavechip/spi_proto.c @@ -0,0 +1 @@ +../shared/spi_proto.c \ No newline at end of file diff --git a/firmware/slavechip/spi_proto.h b/firmware/slavechip/spi_proto.h new file mode 120000 index 0000000..8d40308 --- /dev/null +++ b/firmware/slavechip/spi_proto.h @@ -0,0 +1 @@ +../shared/spi_proto.h \ No newline at end of file