Separate send/receive buffers

This commit is contained in:
UncleRus 2016-03-08 04:00:11 +05:00
parent b5c2120efc
commit 9dc565ff7c
2 changed files with 19 additions and 12 deletions

View file

@ -11,8 +11,6 @@
#include "esp/gpio.h" #include "esp/gpio.h"
#include <string.h> #include <string.h>
//#include <stdio.h>
#define _SPI0_SCK_GPIO 6 #define _SPI0_SCK_GPIO 6
#define _SPI0_MISO_GPIO 7 #define _SPI0_MISO_GPIO 7
#define _SPI0_MOSI_GPIO 8 #define _SPI0_MOSI_GPIO 8
@ -161,14 +159,15 @@ static uint32_t _spi_single_transfer (uint8_t bus, uint32_t data, uint8_t len)
} }
// works properly only with little endian byte order // works properly only with little endian byte order
static void _spi_buf_transfer (uint8_t bus, uint8_t *data, size_t len) static void _spi_buf_transfer (uint8_t bus, const uint8_t *out_data, uint8_t *in_data, size_t len)
{ {
_wait(bus); _wait(bus);
_set_size(bus, len); _set_size(bus, len);
memcpy((void *)&SPI(bus).W0, data, len); memcpy((void *)&SPI(bus).W0, out_data, len);
_start(bus); _start(bus);
_wait(bus); _wait(bus);
memcpy(data, (void *)&SPI(bus).W0, len); if (in_data)
memcpy(in_data, (void *)&SPI(bus).W0, len);
} }
uint8_t spi_transfer_8(uint8_t bus, uint8_t data) uint8_t spi_transfer_8(uint8_t bus, uint8_t data)
@ -186,21 +185,28 @@ uint32_t spi_transfer_32(uint8_t bus, uint32_t data)
return _spi_single_transfer(bus, data, sizeof(data)); return _spi_single_transfer(bus, data, sizeof(data));
} }
void spi_transfer(uint8_t bus, void *data, size_t len) void spi_transfer(uint8_t bus, const void *out_data, void *in_data, size_t len)
{ {
if (!data || !len) return; if (!out_data || !len) return;
_wait(bus); _wait(bus);
spi_endianness_t e = spi_get_endianness(bus); spi_endianness_t e = spi_get_endianness(bus);
spi_set_endianness(bus, SPI_LITTLE_ENDIAN); spi_set_endianness(bus, SPI_LITTLE_ENDIAN);
size_t blocks = len / _SPI_BUF_SIZE; size_t blocks = len / _SPI_BUF_SIZE;
for (uint8_t i = 0; i < blocks; i++) for (size_t i = 0; i < blocks; i++)
_spi_buf_transfer(bus, data + i * _SPI_BUF_SIZE, _SPI_BUF_SIZE); {
size_t offset = i * _SPI_BUF_SIZE;
_spi_buf_transfer(bus, (const uint8_t *)out_data + offset,
in_data ? (uint8_t *)in_data + offset : NULL, _SPI_BUF_SIZE);
}
uint8_t tail = len % _SPI_BUF_SIZE; uint8_t tail = len % _SPI_BUF_SIZE;
if (tail) if (tail)
_spi_buf_transfer(bus, data + blocks * _SPI_BUF_SIZE, tail); {
_spi_buf_transfer(bus, (const uint8_t *)out_data + blocks * _SPI_BUF_SIZE,
in_data ? (uint8_t *)in_data + blocks * _SPI_BUF_SIZE : NULL, tail);
}
spi_set_endianness(bus, e); spi_set_endianness(bus, e);
} }

View file

@ -183,10 +183,11 @@ uint32_t spi_transfer_32(uint8_t bus, uint32_t data);
/** /**
* \brief Transfer buffer over SPI * \brief Transfer buffer over SPI
* \param bus Bus ID: 0 - system, 1 - user * \param bus Bus ID: 0 - system, 1 - user
* \param data Data to send. Buffer contents will be replaced with received data * \param out_data Data to send.
* \param in_data Receive buffer. If NULL, received data will be lost.
* \param len Buffer size * \param len Buffer size
*/ */
void spi_transfer(uint8_t bus, void *data, size_t len); void spi_transfer(uint8_t bus, const void *out_data, void *in_data, size_t len);
#ifdef __cplusplus #ifdef __cplusplus
} }