refactor: adjust SPI transaction queue size and improve read_stream validation

This commit is contained in:
2025-08-07 11:06:03 +08:00
parent ffcaba10fd
commit 249c125447
2 changed files with 69 additions and 32 deletions

View File

@ -87,7 +87,7 @@ void init(spi_config_t config) {
.clock_speed_hz = config.clock_speed_hz,
.spics_io_num = CS_PIN,
.flags = SPI_DEVICE_NO_DUMMY,
.queue_size = 2,
.queue_size = 1,
};
ESP_ERROR_CHECK(spi_bus_initialize(config.host_id, &bus_config, SPI_DMA_CH_AUTO));
@ -158,7 +158,7 @@ read_stream(uint8_t cmd, std::span<uint8_t> data, const size_t timeout_ms) {
std::array<uint8_t, STACK_BUFFER_SIZE> _rx_buf;
std::array<uint8_t, STACK_BUFFER_SIZE> _tx_buf;
if (data.size() > STACK_BUFFER_SIZE - 1) {
if (data.empty() or data.size() > STACK_BUFFER_SIZE - 1) {
return ue_t{error::INVALID_SIZE};
}
@ -170,12 +170,18 @@ read_stream(uint8_t cmd, std::span<uint8_t> data, const size_t timeout_ms) {
auto tx_buffer = std::span{_tx_buf}.subspan(0, rx_buffer.size());
std::ranges::fill(tx_buffer, PADDING);
spi_transaction_ext_t transaction;
spi_transaction_ext_t transaction{};
// during the Command and Address phases, the members spi_transaction_t::cmd
// and spi_transaction_t::addr are sent to the bus, nothing is read at this
// time.
transaction = spi_transaction_ext_t{
.base = {
.flags = SPI_TRANS_VARIABLE_CMD,
.cmd = cmd,
.length = static_cast<size_t>(rx_buffer.size() * 8),
.flags = SPI_TRANS_VARIABLE_CMD,
.cmd = cmd,
// total data length, in bits
.length = static_cast<size_t>(rx_buffer.size() * 8),
// total data length received, should be not greater than length in
// full-duplex mode (0 defaults this to the value of length).
.rxlength = static_cast<size_t>(rx_buffer.size() * 8),
.tx_buffer = tx_buffer.data(),
.rx_buffer = rx_buffer.data(),
@ -215,7 +221,7 @@ write_stream(uint8_t cmd, std::span<const uint8_t> data, const size_t timeout_ms
}
const auto rx_buffer = std::span{_rx_buf}.subspan(0, data.size());
spi_transaction_ext_t transaction;
spi_transaction_ext_t transaction{};
transaction = spi_transaction_ext_t{
.base = {
.flags = SPI_TRANS_VARIABLE_CMD,