You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
2.3 KiB
115 lines
2.3 KiB
|
15 years ago
|
#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 *****/
|