diff --git a/cpu/stm32f1/include/periph_cpu.h b/cpu/stm32f1/include/periph_cpu.h index 0a22e6bc96..66b4c5c806 100644 --- a/cpu/stm32f1/include/periph_cpu.h +++ b/cpu/stm32f1/include/periph_cpu.h @@ -40,6 +40,13 @@ extern "C" { */ #define TIMER_MAXVAL (0xffff) +/** + * @brief declare needed generic SPI functions + * @{ + */ +#undef PERIPH_SPI_NEEDS_TRANSFER_BYTES +#define PERIPH_SPI_NEEDS_TRANSFER_BYTE + /** * @brief Generate GPIO mode bitfields * diff --git a/cpu/stm32f1/periph/spi.c b/cpu/stm32f1/periph/spi.c index 8075b9c91a..79c52776ee 100644 --- a/cpu/stm32f1/periph/spi.c +++ b/cpu/stm32f1/periph/spi.c @@ -183,8 +183,9 @@ int spi_release(spi_t dev) return 0; } -int spi_transfer_byte(spi_t dev, char out, char *in) +int spi_transfer_bytes(spi_t dev, char *out, char *in, unsigned int length) { + SPI_TypeDef *spi; int transferred = 0; @@ -210,27 +211,34 @@ int spi_transfer_byte(spi_t dev, char out, char *in) return -1; } - while (!(spi->SR & SPI_SR_TXE)) {} - spi->DR = out; - transferred++; - - while (!(spi->SR & SPI_SR_RXNE)) {} - if (in != NULL) { - *in = spi->DR; - transferred++; - } - else { + if(!in){ + for (unsigned i = 0; i < length; i++) { + while (!(spi->SR & SPI_SR_TXE)) {} + spi->DR = (uint8_t)out[i]; + } + /* SPI busy */ + while ((spi->SR & SPI_SR_BSY)) {} spi->DR; } - - /* SPI busy */ - while ((spi->SR & 0x80)) {} -#if ENABLE_DEBUG - if (in != NULL) { - DEBUG("\nout: %x in: %x transferred: %x\n", out, *in, transferred); + else if(!out) { + for (unsigned i = 0; i < length; i++) { + spi->DR = 0; + while (!(spi->SR & SPI_SR_RXNE)) {} + in[i] = (char)spi->DR; + } } else { - DEBUG("\nout: %x in: was nullPointer transferred: %x\n", out, transferred); + for (unsigned i = 0; i < length; i++) { + while (!(spi->SR & SPI_SR_TXE)) {} + spi->DR = out[i]; + while (!(spi->SR & SPI_SR_RXNE)) {} + in[i] = (char)spi->DR; + } + } + +#if ENABLE_DEBUG + if (in != NULL) { + DEBUG("\nSPI: transferred %i Bytes\n", length); } #endif /*ENABLE_DEBUG */ @@ -275,6 +283,7 @@ void spi_poweroff(spi_t dev) switch (dev) { #if SPI_0_EN case SPI_0: + while ((SPI_0_DEV->SR & SPI_SR_BSY)) {} SPI_0_DEV->CR1 &= ~(SPI_CR1_SPE); /* turn SPI peripheral off */ SPI_0_CLKDIS(); break; @@ -282,6 +291,7 @@ void spi_poweroff(spi_t dev) #if SPI_1_EN case SPI_1: + while ((SPI_1_DEV->SR & SPI_SR_BSY)) {} SPI_1_DEV->CR1 &= ~(SPI_CR1_SPE); /* turn SPI peripheral off */ SPI_1_CLKDIS(); break; @@ -289,6 +299,7 @@ void spi_poweroff(spi_t dev) #if SPI_2_EN case SPI_2: + while ((SPI_2_DEV->SR & SPI_SR_BSY)) {} SPI_2_DEV->CR1 &= ~(SPI_CR1_SPE); /* turn SPI peripheral off */ SPI_2_CLKDIS(); break;