cancel
Showing results for 
Search instead for 
Did you mean: 

Problems sending SPI Data from STM32 to arduino

HPont.2
Associate

I'm trying to learn how to communicate via SPI with STM32 but I've run into some problems.

The first step i took was to implement SPI communications using two arduino unos: the master writes a byte and the slave responds with another byte according to the input (when the master sends anything, triggering the exchange). Everything worked as it should to the best of my knowledge.

The second step is the one I'm stuck at which is to replace the master arduino with a NUCLEO-F767ZI board. I've set up SPI1 using the auto generated code for now, and I've set it up with what I think are the default options. In the main, I send a single byte to the arduino, and this is where the problem starts:

By using the serial port in the arduino I can see that it does receive data, but usually bit shifted. So if I send a 16, i usually get a 32 or other power of two in the arduino (see attached image)

0693W00000WINJ5QAP.pngI'm using clock polarity 0 and clock phase 0 on both microcontrollers (so clock idle on low and sampling on the rising edge). Just to be sure I've tried all possibilities but to no avail, it still doesn't work properly, and I don't really know why. Another thing I considered was that perhaps the clock is running too fast (the peripheral clock is set to 16 MHz), but if I change the prescaler to anything other than SPI_BAUDRATEPRESCALER_2, i stop getting any data on the arduino, only 0s, which I find odd, I would expect it to work just the same if only the master controls the clock.

Just for completeness, here are the spi settings I'm using:

   hspi1.Instance = SPI1;
   hspi1.Init.Mode = SPI_MODE_MASTER;
   hspi1.Init.Direction = SPI_DIRECTION_2LINES;
   hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
   hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
   hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
   hspi1.Init.NSS = SPI_NSS_HARD_OUTPUT;
   hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
   hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
   hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
   hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
   hspi1.Init.CRCPolynomial = 7;
   hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
   hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
 

The arduino slave code (I've removed the reply part for now):

byte data = 0;
byte c;
 
void setup (void)
{
 
  // have to send on master in, *slave out*
  pinMode(MISO, OUTPUT);
  Serial.begin(115200);
 
  // turn on SPI in slave mode
  SPCR |= _BV(SPE);
 
  // turn on interrupts
  SPCR |= _BV(SPIE);
 
 Serial.println("SPCR");
 Serial.println(SPCR,BIN);
 
}  // end of setup
 
// SPI interrupt routine
ISR (SPI_STC_vect)
{
  c = SPDR;
  data = 1;
}  // end of interrupt service routine (ISR) SPI_STC_vect
 
void loop (void)
{
  if (data){
    Serial.println(c);
    data = 0;}
}  // end of loop

STM32 master code (ommiting irrelevant parts):

  

MX_SPI1_Init();
uint8_t data = 16;
while (1)
   {
  	 HAL_SPI_Transmit(&hspi1, &data, 1, 100);
  	 HAL_Delay(500);
   }

Does anyone have a clue as to why this could be happening? I've found nothing on the prescaler issues and all bitshifting issue posts say that the problem is with clock phase or polarity discrepancies, which I've verified is not.

Thanks in advance

0 REPLIES 0