cancel
Showing results for 
Search instead for 
Did you mean: 

SPI data transfer is not working when using registers to initialize the SPI unit

MSale.4
Associate II

I used this function to initialize and setup STM32H743IIT6 uC to work with SPI in master mode:

void InitSPI(void)
{
  RCC->APB2ENR=0x00102000; //Enabling SPI45 clock (default: rcc_pclk2=25MHz)
  SPI4->CFG1=0x30070007; //Baud rate = SPI clock / 16 = 1.5625 MHz, No CRC, 8 data bits
  SPI4->CFG2=0x00C00000; //CPOL=0, CPHA=0, LSB first, Master SPI, Full-duplex
  SPI4->CR2=0x00000000; //Size of transfer is unknown and should be 0 (EOT will never happen)
  SPI4->IFCR=0x00000200; //Clear possible mode faults
  SPI4->CR1=0x00000001; //Enabling SPI
  SPI4->CR1=0x00000201; //Enabling data transmission
}

after initialization, I use this function to transceive data:

unsigned int	TransceiveSPI(unsigned int	TransmitSPIData)
{
  while((SPI4->SR & SPI_TXD_REGISTER_EMPTY_MASK)==0); //Waiting for last transmission to complete
  SPI4->TXDR=TransmitSPIData; //Set the new data to transmit
    
  while (((SPI4->SR & SPI_RECEPTION_COMPLETE_MASK)==0));//Waiting for reception of new data
  return SPI4->RXDR; //return the received data
}

but no data is transmitted (even if no CLOCK signal is available on the uC pin)! What is the problem?

5 REPLIES 5

Read out and check the SPI and relevant GPIO registers content.

Observe the delay needed between enabling peripheral clock, and writing to given peripheral's registers:

0693W00000NphKJQAZ.png 

JW

MSale.4
Associate II

There is about a 1-second delay between the initialization function and the first time that I want to send data through SPI.

There is no delay between these two lines:

> RCC->APB2ENR=0x00102000; //Enabling SPI45 clock (default: rcc_pclk2=25MHz)

> SPI4->CFG1=0x30070007; //Baud rate = SPI clock / 16 = 1.5625 MHz, No CRC, 8 data bits

so SPI4->CFG1 might get written while clock is not effectively enabled, yet.

Read out and check the SPI and relevant GPIO registers content.

JW

MSale.4
Associate II

A 1-second delay was added but there is no positive effect :(

I am not familiar with the 'H7 SPI, and it's an overcomplicated beast; but generally, you should read out the SPI and GPIO registers content and check against expected values.

If everything matches, the next step is to play manually in debugger while observing pins using logic analyzer/oscilloscope, until you achieve the expected activity.

JW