Skip to content

Instantly share code, notes, and snippets.

@bmarcot
Created May 14, 2018 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmarcot/17c00240800d22312f6401a23c652570 to your computer and use it in GitHub Desktop.
Save bmarcot/17c00240800d22312f6401a23c652570 to your computer and use it in GitHub Desktop.
int spi_write_then_write(struct spi_device *spi,
const void *tx_buf_0, unsigned tx_len_0,
const void *tx_buf_1, unsigned tx_len_1)
{
struct spi_message message;
struct spi_transfer x[2];
spi_message_init(&message);
memset(x, 0, sizeof(x));
if (tx_len_0) {
x[0].tx_buf = tx_buf_0;
x[0].len = tx_len_0;
/* x[0].cs_change = 1; */
spi_message_add_tail(&x[0], &message);
}
if (tx_len_1) {
x[1].tx_buf = tx_buf_1;
x[1].len = tx_len_1;
/* x[1].cs_change = 1; */
spi_message_add_tail(&x[1], &message);
}
return spi_sync(spi, &message);
}
/* int spi_w8w8(struct spi_device *spi, u8 header, u8 data) */
/* { */
/* return spi_write_then_write(spi, &header, 1, &data, 1); */
/* } */
static inline void SPI(u8 cmd, const u8 *buf, size_t len)
{
const u8 headers = {0x70, 0x72};
spi_write_then_write(spi, &headers[0], 1, &cmd, 1);
spi_write_then_write(spi, &headers[1], 1, buf, len);
}
static inline void SPI_R(u8 cmd, u8 *buf, size_t len)
{
const u8 headers = {0x70, 0x73};
spi_write_then_write(spi, &headers[0], 1, &cmd, 1);
spi_write_then_read(spi, &headers[1], 1, buf, len); // use overread char, set to 0
}
static inline void SPI_RID(u8 cmd, u8 *id)
{
*id = spi_w8r8(spi, 0x71); // use overread char, set to 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment