Using an STM32L476RG Nucleo microcontroller board as the SPI master and an Arduino mega as a slave with pin 52 for SCLK, pin 53 for SS and pin 51 for MOSI on the Arduino. There is no issue with the STM32 transmission as the USB logic analyzer I'm using (parallel to the arduino) shows a correct output on the master side so this is an Arduino issue:
The problem is the Arduino isn't recieving properly. The arduino code:
#include <SPI.h>
#include<stdint.h>
#define SPI_SCK 52
#define SPI_MISO 50
#define SPI_MOSI 51
#define SPI_SS 53
char dataBuff[500];
//Initialize SPI slave.
void SPI_SlaveInit(void)
{
// Initialize SPI pins.
pinMode(SCK, INPUT);
pinMode(MOSI, INPUT);
pinMode(MISO, OUTPUT);
pinMode(SS, INPUT);
//make SPI as slave
// Enable SPI as slave.
SPCR = (1 << SPE);
}
//This function returns SPDR Contents
uint8_t SPI_SlaveReceive(void)
{
/* Wait for reception complete */
while(!(SPSR & (1<<SPIF)));
/* Return Data Register */
return SPDR;
}
//sends one byte of data
void SPI_SlaveTransmit(char data)
{
/* Start transmission */
SPDR = data;
/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)));
}
// The setup() function runs right after reset.
void setup()
{
// Initialize serial communication
Serial.begin(9600);
// Initialize SPI Slave.
SPI_SlaveInit();
Serial.println("Slave Initialized");
}
// The loop function runs continuously after setup().
void loop()
{
uint32_t i;
uint16_t dataLen = 0;
Serial.println("Slave waiting for ss to go low");
while(digitalRead(SS));
i = 0;
dataLen = SPI_SlaveReceive();
for(i = 0 ; i < dataLen ; i++ )
{
dataBuff[i] = SPI_SlaveReceive();
}
// Serial.println(String(i,HEX));
dataBuff[i] = '\0';
Serial.println("Rcvd:");
Serial.println(dataBuff);
Serial.print("Length:");
Serial.println(dataLen);
}
The message i'm transmitting is my full name and is 12 characters long. The master first sends the length of the message (12 in this case) so the arduino knows how many characters to recieve and then the message is transmitted. On the logic analyzer output everything looks fine and I can see the message. However I keep getting results like this in the Arduino serial monitor:
I should also note I'm using a sparkfun logic level converter between the master and slave to step the STM32 3.3v signal to 5v for the arduino input: (Imagine the arduino is not uno but master in the following image with the pins changed to the ones I've listed above)
I'm seeing no message next to 'Rcvd:' and a length of 0 most of the time, if not than a number much larger than 12. What seems to be the issue?