From 669104d841e29eb95338a4b70e6e5cce75e6486f Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Fri, 1 Oct 2021 12:41:03 +0200 Subject: [PATCH] cpu/sam0_common: SPI: don't perform DMA transfer for small buffers Setting up a DMA transfer can take longer than sending out a buffer byte by byte if the buffer is small. DMA only shows advantages for large buffers, using it for every transfer will cause a net slowdown. Since we did not come up with a good way to determine the treshold based on the SPI frequency, just use a fixed buffer for now so that DMA can be used without slowing things down overall. --- cpu/sam0_common/periph/Kconfig.spi | 14 ++++++++++++++ cpu/sam0_common/periph/spi.c | 10 +++++++++- cpu/samd21/periph/Kconfig.spi | 1 + cpu/samd5x/periph/Kconfig.spi | 1 + cpu/saml1x/periph/Kconfig.spi | 1 + cpu/saml21/periph/Kconfig.spi | 1 + drivers/periph_common/Kconfig.spi | 3 +++ 7 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 cpu/sam0_common/periph/Kconfig.spi create mode 100644 cpu/samd21/periph/Kconfig.spi create mode 100644 cpu/samd5x/periph/Kconfig.spi create mode 100644 cpu/saml1x/periph/Kconfig.spi create mode 100644 cpu/saml21/periph/Kconfig.spi diff --git a/cpu/sam0_common/periph/Kconfig.spi b/cpu/sam0_common/periph/Kconfig.spi new file mode 100644 index 0000000000..1ad4dfa815 --- /dev/null +++ b/cpu/sam0_common/periph/Kconfig.spi @@ -0,0 +1,14 @@ +# Copyright (c) 2020 HAW Hamburg +# +# This file is subject to the terms and conditions of the GNU Lesser +# General Public License v2.1. See the file LICENSE in the top level +# directory for more details. + +config SPI_DMA_THRESHOLD_BYTES + int "SPI DMA threshold (bytes)" + depends on MODULE_PERIPH_DMA + depends on MODULE_PERIPH_SPI + default 16 + help + Threshold in bytes under which no SPI DMA transfer will be performed. + Polling will be used instead. diff --git a/cpu/sam0_common/periph/spi.c b/cpu/sam0_common/periph/spi.c index d7cd5803e9..27bef0ee03 100644 --- a/cpu/sam0_common/periph/spi.c +++ b/cpu/sam0_common/periph/spi.c @@ -36,6 +36,14 @@ #define ENABLE_DEBUG 0 #include "debug.h" +/** + * @brief Threshold under which polling transfers are used instead of DMA + * TODO: determine at run-time based on SPI clock + */ +#ifndef CONFIG_SPI_DMA_THRESHOLD_BYTES +#define CONFIG_SPI_DMA_THRESHOLD_BYTES 16 +#endif + /** * @brief Array holding one pre-initialized mutex for each SPI device */ @@ -508,7 +516,7 @@ void spi_transfer_bytes(spi_t bus, spi_cs_t cs, bool cont, gpio_clear((gpio_t)cs); } - if (_use_dma(bus)) { + if (_use_dma(bus) && len > CONFIG_SPI_DMA_THRESHOLD_BYTES) { #ifdef MODULE_PERIPH_DMA /* The DMA promises not to modify the const out data */ _dma_transfer(bus, out, in, len); diff --git a/cpu/samd21/periph/Kconfig.spi b/cpu/samd21/periph/Kconfig.spi new file mode 100644 index 0000000000..ca58d56672 --- /dev/null +++ b/cpu/samd21/periph/Kconfig.spi @@ -0,0 +1 @@ +source "$(RIOTCPU)/sam0_common/periph/Kconfig.spi" diff --git a/cpu/samd5x/periph/Kconfig.spi b/cpu/samd5x/periph/Kconfig.spi new file mode 100644 index 0000000000..ca58d56672 --- /dev/null +++ b/cpu/samd5x/periph/Kconfig.spi @@ -0,0 +1 @@ +source "$(RIOTCPU)/sam0_common/periph/Kconfig.spi" diff --git a/cpu/saml1x/periph/Kconfig.spi b/cpu/saml1x/periph/Kconfig.spi new file mode 100644 index 0000000000..ca58d56672 --- /dev/null +++ b/cpu/saml1x/periph/Kconfig.spi @@ -0,0 +1 @@ +source "$(RIOTCPU)/sam0_common/periph/Kconfig.spi" diff --git a/cpu/saml21/periph/Kconfig.spi b/cpu/saml21/periph/Kconfig.spi new file mode 100644 index 0000000000..ca58d56672 --- /dev/null +++ b/cpu/saml21/periph/Kconfig.spi @@ -0,0 +1 @@ +source "$(RIOTCPU)/sam0_common/periph/Kconfig.spi" diff --git a/drivers/periph_common/Kconfig.spi b/drivers/periph_common/Kconfig.spi index eca91e5dca..9ad5562add 100644 --- a/drivers/periph_common/Kconfig.spi +++ b/drivers/periph_common/Kconfig.spi @@ -41,4 +41,7 @@ config MODULE_PERIPH_INIT_SPI_GPIO_MODE default y if MODULE_PERIPH_INIT depends on MODULE_PERIPH_SPI_GPIO_MODE +# Include CPU specific configurations +osource "$(RIOTCPU)/$(CPU)/periph/Kconfig.spi" + endif # MODULE_PERIPH_SPI