From a3e69e715daa4693c105764519fa06771237bc40 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 18 Feb 2019 10:21:28 +1100 Subject: [PATCH] HAL_ChibiOS: avoid doing one-way SPI transfers these transfers use dummy bytes in the hal driver, which may not be in DMA safe memory, so best to avoid them --- libraries/AP_HAL_ChibiOS/Device.cpp | 4 ++-- libraries/AP_HAL_ChibiOS/SPIDevice.cpp | 8 +------- libraries/AP_HAL_ChibiOS/hwdef/common/bouncebuffer.c | 12 +++++++++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/libraries/AP_HAL_ChibiOS/Device.cpp b/libraries/AP_HAL_ChibiOS/Device.cpp index bf9e56f465..1281d2512a 100644 --- a/libraries/AP_HAL_ChibiOS/Device.cpp +++ b/libraries/AP_HAL_ChibiOS/Device.cpp @@ -161,10 +161,10 @@ bool DeviceBus::adjust_timer(AP_HAL::Device::PeriodicHandle h, uint32_t period_u void DeviceBus::bouncebuffer_setup(const uint8_t *&buf_tx, uint16_t tx_len, uint8_t *&buf_rx, uint16_t rx_len) { - if (buf_rx) { + if (rx_len != 0) { bouncebuffer_setup_read(bounce_buffer_rx, &buf_rx, rx_len); } - if (buf_tx) { + if (tx_len != 0) { bouncebuffer_setup_write(bounce_buffer_tx, &buf_tx, tx_len); } } diff --git a/libraries/AP_HAL_ChibiOS/SPIDevice.cpp b/libraries/AP_HAL_ChibiOS/SPIDevice.cpp index 328cb8e383..89bb46eea1 100644 --- a/libraries/AP_HAL_ChibiOS/SPIDevice.cpp +++ b/libraries/AP_HAL_ChibiOS/SPIDevice.cpp @@ -176,13 +176,7 @@ void SPIDevice::do_transfer(const uint8_t *send, uint8_t *recv, uint32_t len) bus.bouncebuffer_setup(send, len, recv, len); - if (send == nullptr) { - spiReceive(spi_devices[device_desc.bus].driver, len, recv); - } else if (recv == nullptr) { - spiSend(spi_devices[device_desc.bus].driver, len, send); - } else { - spiExchange(spi_devices[device_desc.bus].driver, len, send, recv); - } + spiExchange(spi_devices[device_desc.bus].driver, len, send, recv); bus.bouncebuffer_finish(send, recv, len); diff --git a/libraries/AP_HAL_ChibiOS/hwdef/common/bouncebuffer.c b/libraries/AP_HAL_ChibiOS/hwdef/common/bouncebuffer.c index 9b5204a549..7ae9d79c2c 100644 --- a/libraries/AP_HAL_ChibiOS/hwdef/common/bouncebuffer.c +++ b/libraries/AP_HAL_ChibiOS/hwdef/common/bouncebuffer.c @@ -48,6 +48,8 @@ void bouncebuffer_init(struct bouncebuffer_t **bouncebuffer, uint32_t prealloc_b /* setup for reading from a device into memory, allocating a bouncebuffer if needed + Note that *buf can be NULL, in which case we allocate DMA capable memory, but don't + copy to it in bouncebuffer_finish_read(). This avoids DMA failures in dummyrx in the SPI LLD */ void bouncebuffer_setup_read(struct bouncebuffer_t *bouncebuffer, uint8_t **buf, uint32_t size) { @@ -79,8 +81,10 @@ void bouncebuffer_setup_read(struct bouncebuffer_t *bouncebuffer, uint8_t **buf, void bouncebuffer_finish_read(struct bouncebuffer_t *bouncebuffer, const uint8_t *buf, uint32_t size) { if (bouncebuffer && buf == bouncebuffer->dma_buf) { - osalDbgAssert((bouncebuffer->busy == true), "bouncebuffer finish_read"); - memcpy(bouncebuffer->orig_buf, buf, size); + osalDbgAssert((bouncebuffer->busy == true), "bouncebuffer finish_read"); + if (bouncebuffer->orig_buf) { + memcpy(bouncebuffer->orig_buf, buf, size); + } bouncebuffer->busy = false; } } @@ -104,7 +108,9 @@ void bouncebuffer_setup_write(struct bouncebuffer_t *bouncebuffer, const uint8_t osalDbgAssert((bouncebuffer->dma_buf != NULL), "bouncebuffer write allocate"); bouncebuffer->size = size; } - memcpy(bouncebuffer->dma_buf, *buf, size); + if (*buf) { + memcpy(bouncebuffer->dma_buf, *buf, size); + } *buf = bouncebuffer->dma_buf; #if defined(STM32H7) osalDbgAssert((((uint32_t)*buf)&31) == 0, "bouncebuffer write align");