From 45b51dea1208fec87077ce48808481b1d7a02406 Mon Sep 17 00:00:00 2001 From: Victor Lamoine Date: Fri, 20 Apr 2018 14:38:57 +0200 Subject: [PATCH] Add simple SPI slave example --- .../SPI/examples/spi_slave/spi_slave.ino | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 STM32F1/libraries/SPI/examples/spi_slave/spi_slave.ino diff --git a/STM32F1/libraries/SPI/examples/spi_slave/spi_slave.ino b/STM32F1/libraries/SPI/examples/spi_slave/spi_slave.ino new file mode 100644 index 0000000..f0122d6 --- /dev/null +++ b/STM32F1/libraries/SPI/examples/spi_slave/spi_slave.ino @@ -0,0 +1,46 @@ +// SPI slave example +// STM32 acts as a SPI slave an reads 8 bit data frames over SPI +// The data sent to the master is a simple count (0, 1, 2, 3) that is incremented +// each time a data frame is received. +// Serial output is here for debug + +#include +#include + +void setupSPI(void) +{ + pinMode(PA7, INPUT); // MOSI1 + pinMode(PA6, INPUT); // MISO1 + pinMode(PA5, INPUT); // SCK + pinMode(BOARD_SPI_DEFAULT_SS, INPUT); // SS + + // Select SPI1 + SPI.setModule(1); + // The clock value is not used + SPI.beginTransactionSlave(SPISettings(18000000, MSBFIRST, SPI_MODE0, DATA_SIZE_8BIT)); +} + +void setup() +{ + Serial.begin(115200); + delay(100); + + // Data that master will receive when transferring a data frame over SPI + SPI.dev()->regs->DR = 0; + setupSPI(); +} + +uint8_t count(0); +void loop() +{ + // Blocking call to read SPI message + uint8_t msg(SPI.read()); + Serial.print("Received = 0b"); + Serial.print(msg, BIN); + Serial.print(", 0x"); + Serial.print(msg, HEX); + Serial.print(", "); + Serial.println(msg); + // Next data frame that will be received by master + SPI.dev()->regs->DR = ++count; +}