|
|
|
|
#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);
|
|
|
|
|
}
|