moved spi_proto into a single file again

master
Nidan 15 years ago
parent d1b82c2432
commit d7d4e5836d

@ -1,76 +0,0 @@
#include "spi_proto.h"
#include "spi.h"
uint8_t read[2];
uint8_t write[4];
void talk_to_slave(uint8_t opcode, uint8_t addr, uint8_t flags)
{
write[0] = opcode;
write[1] = addr;
spi_mst_start_packet();
spi_mst_write(wlen, flags & SPI_WRITE_DATA? 4 : 2);
spi_mst_read(rlen, flags & SPI_READ_DATA? 2 : 0);
spi_mst_end_packet();
}
uint16_t speak_raw(uint8_t opcode, uint8_t number, uint8_t flags, uint16_t value)
{
write[2] = value >> 8;
write[3] = value & 0xff;
talk_to_slave(opcode, number, flags);
return (read[0] << 8) | read[1];
}
uint8_t spi_proto_needs(uint8_t opcode)
{
uint8_t r = SPI_NONE;
switch(opcode)
{
case 1: case 2: case 3:
r = SPI_READ_DATA;
break;
case 4: case 5:
r = SPI_WRITE_DATA;
break;
}
return r;
}
uint16_t read_temperature(uint8_t number)
{
talk_to_slave(1, number, SPI_READ_DATA);
return (read[0] << 8) | read[1];
}
uint8_t read_var8(uint8_t number)
{
talk_to_slave(2, number, SPI_READ_DATA);
return read[1];
}
uint16_t read_var16(uint8_t number)
{
talk_to_slave(3, number, SPI_READ_DATA);
return (read[0] << 8) | read[1];
}
void write_var8(uint8_t number, uint8_t value)
{
write[2] = 0;
write[3] = number;
talk_to_slave(4, number, SPI_WRITE_DATA);
}
void write_var16(uint8_t number, uint16_t value)
{
write[2] = value >> 8;
write[3] = value & 0xff;
talk_to_slave(5, number, SPI_WRITE_DATA);
}
void call_func(uint8_t number)
{
talk_to_slave(6, number, SPI_NONE);
}

@ -0,0 +1 @@
../shared/spi_proto.c

@ -1,19 +0,0 @@
#ifndef SPI_PROTO_H
#define SPI_PROTO_H
#define SPI_WRITE_DATA 0x01
#define SPI_READ_DATA 0x10
#define SPI_NONE 0
uint16_t speak_raw(uint8_t opcode, uint8_t number, uint8_t flags, uint16_t data);
uint8_t spi_proto_needs(uint8_t opcode);
uint16_t read_temperature(uint8_t number);
uint8_t read_var8(uint8_t number);
uint16_t read_var16(uint8_t number);
void write_var8(uint8_t number, uint8_t value);
void write_var16(uint8_t number, uint16_t value);
void call_func(uint8_t number);
#endif

@ -0,0 +1 @@
../shared/spi_proto.h

@ -0,0 +1,114 @@
#include "spi_proto.h"
#include "spi.h"
/***** MASTER *****/
uint8_t read_data[2];
uint8_t write_data[4];
void talk_to_slave(uint8_t opcode, uint8_t addr, uint8_t flags)
{
write_data[0] = opcode;
write_data[1] = addr;
spi_mst_start_packet();
spi_mst_write(flags & SPI_WRITE_DATA? 4 : 2, write_data);
spi_mst_read(flags & SPI_READ_DATA? 2 : 0, read_data);
spi_mst_end_packet();
}
uint16_t speak_raw(uint8_t opcode, uint8_t number, uint8_t flags, uint16_t value)
{
write_data[2] = value >> 8;
write_data[3] = value & 0xff;
talk_to_slave(opcode, number, flags);
return (read_data[0] << 8) | read_data[1];
}
uint8_t spi_proto_needs(uint8_t opcode)
{
uint8_t r = SPI_NONE;
switch(opcode)
{
case 1: case 2: case 3:
r = SPI_READ_DATA;
break;
case 4: case 5:
r = SPI_WRITE_DATA;
break;
}
return r;
}
uint16_t read_temperature(uint8_t number)
{
talk_to_slave(1, number, SPI_READ_DATA);
return (read_data[0] << 8) | read_data[1];
}
uint8_t read_var8(uint8_t number)
{
talk_to_slave(2, number, SPI_READ_DATA);
return read_data[1];
}
uint16_t read_var16(uint8_t number)
{
talk_to_slave(3, number, SPI_READ_DATA);
return (read_data[0] << 8) | read_data[1];
}
void write_var8(uint8_t number, uint8_t value)
{
write_data[2] = 0;
write_data[3] = number;
talk_to_slave(4, number, SPI_WRITE_DATA);
}
void write_var16(uint8_t number, uint16_t value)
{
write_data[2] = value >> 8;
write_data[3] = value & 0xff;
talk_to_slave(5, number, SPI_WRITE_DATA);
}
void call_func(uint8_t number)
{
talk_to_slave(6, number, SPI_NONE);
}
/***** MASTER END *****/
/***** SLAVE *****/
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
}
}
/***** SLAVE END *****/

@ -0,0 +1,38 @@
#ifndef SPI_PROTO_H
#define SPI_PROTO_H
#include <stdint.h>
/***** MASTER *****/
#define SPI_WRITE_DATA 0x01
#define SPI_READ_DATA 0x10
#define SPI_NONE 0
uint16_t speak_raw(uint8_t opcode, uint8_t number, uint8_t flags, uint16_t data);
uint8_t spi_proto_needs(uint8_t opcode);
uint16_t read_temperature(uint8_t number);
uint8_t read_var8(uint8_t number);
uint16_t read_var16(uint8_t number);
void write_var8(uint8_t number, uint8_t value);
void write_var16(uint8_t number, uint16_t value);
void call_func(uint8_t number);
/***** MASTER END *****/
/***** SLAVE *****/
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);
/***** SLAVE END *****/
#endif

@ -1,30 +0,0 @@
#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
}
}

@ -0,0 +1 @@
../shared/spi_proto.c

@ -1,18 +0,0 @@
#ifndef __SPIPROTO_H
#define __SPIPROTO_H
#include <stdint.h>
#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

@ -0,0 +1 @@
../shared/spi_proto.h
Loading…
Cancel
Save