cancel
Showing results for 
Search instead for 
Did you mean: 

SPI sends extra 0x00 packet

Ronnie Coleman
Associate
Posted on July 05, 2018 at 20:00

I'm trying to initialize SPI on STM32F746ZG-nucleo and send some data.

When i check with oscilloscope it shows an extra 0x00 packet at the end of each transmission.

I can't figure out why, please help.

#include ''stm32f7xx.h''

void SPI2_tx(uint8_t data){

  SPI2->DR = data;

  while(!(SPI2->SR & 2));}

uint8_t SPI2_rx(void){

  while(!(SPI2->SR & 1));

  return(SPI2->DR);}

int main(void){

  uint8_t asd;

 

  RCC->AHB1ENR |= (1<<2)|(1<<3);                //c,d

  RCC->APB1ENR |= (1<<14);                      //spi2

  GPIOC->MODER |= (2<<4)|(2<<6)|1;              //  0,2,3

  GPIOC->OSPEEDR |= 3|(3<<4)|(3<<6);

  GPIOC->AFR[0] |= (5<<8)|(5<<12);

 

  GPIOD->MODER |= (2<<6);                       //  3

  GPIOD->OSPEEDR |= (3<<6);

  GPIOD->AFR[0] |= (5<<12);

 

  GPIOC->ODR |= 1;                              //spics

 

  SPI2->CR2 = 0x1704;

  SPI2->CR1 = 0x0354;

  SPI2_tx(0x0D);   

  SPI2_tx(0xFF);

  asd=SPI2->DR;

while(1);}

0690X00000604caQAA.jpg
2 REPLIES 2
Ronnie Coleman
Associate
Posted on July 05, 2018 at 20:44

How long does it usually take for post to get approved?

Posted on July 06, 2018 at 08:20

The newer STM32's SPI transmits two bytes if you write (unknowingly) a halfword into the data register. Typecast the DR register to 8-bit

*(volatile * uint8_t)&SPI2->DR = data;

See 'Data packing' in the SPI chapter of RM (and/or look it up here, this is a common gotcha).

How long does it usually take for post to get approved?

Until around morning GMT. ST is Europe based.

JW