cancel
Showing results for 
Search instead for 
Did you mean: 

SPI slave issue

MichaelR
Associate III

Hello

I am using 2 boards with SPI communication between them: STM32F412G-DISCO as the master, NUCLEO-G071RB as the slave. The MOSI direction works well, but the MISO direction doesn't works. Monitoring the MISO line via. oscilloscope shows that there is no communication in this line, also it is expected to be.

The boards configuration:

MichaelR_0-1717914363668.pngMichaelR_1-1717914403022.png

The master code:

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2, GPIO_PIN_RESET);

HAL_SPI_Transmit(&hspi1, (uint8_t *)&spi_tx, 2,100);

HAL_SPI_Receive(&hspi1, (uint8_t *)&spi_rx, 2,100);

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2, GPIO_PIN_SET);

 

The slave code:

HAL_SPI_Receive_IT(&hspi1,(uint8_t *)&spi_rx,2);

HAL_SPI_Transmit_IT(&hspi1, (uint8_t *)&spi_tx,2);

 

void HAL_SPI_RxCpltCallback (SPI_HandleTypeDef * hspi) {

counter++;

if (counter==10) {

counter=0;

}

sprintf(spi_tx,"c%d",counter);

for(int i=0;i<4;i++) {

spi_buf[counter*2+i]=spi_rx[i];

}

HAL_SPI_Receive_IT(&hspi1,(uint8_t *)&spi_rx,2);

}

 

void HAL_SPI_TxCpltCallback (SPI_HandleTypeDef * hspi) {

HAL_SPI_Transmit_IT(&hspi1, (uint8_t *)&spi_tx,2);

}

Does anyone have a suggestion what is the problem?

 

 

4 REPLIES 4
AScha.3
Chief III

Hi,

1. for transmit + receive (at same time) use :  HAL_SPI_TransmitReceive_IT(..);

2. just for transmitting 2 bytes : why not set the spi to 16bit and do it in one spi transfer?

3. using NSS hardware on receiver, but nss-software on transmitter...might create problem, if you dont do in software, what other chip spi-hardware expects. Try setting both to hardware nss. And use TransmitReceive on both sides.

If you feel a post has answered your question, please click "Accept as Solution".

Hi,

I tried your suggestion, now the slave indicates that the Busy flag is set, and the HAL_SPI_TxRxCpltCallback doesn't called back.

general question: is the HAL_SPI_TransmitReceive_IT(..) works also when the number of bytes differ for the transmit and receive directions?

No. SPI is more or less just a serial shift register, so what you shift IN (bits number), will come out.

So one master-slave TransmitReceive is like : exchanging the spi values master <--> slave.

Number of bytes always same for the transmit and receive directions.

If you want receive 3 bytes, but have only 1 (command or..) to send, you have to send 3 , even 2 are 00 or whatever.

Because you want 3x spi transfer action.

And think about timing : when master shifts spi, slave has to shift out data, without any delay  - if "you" (=slave) is ready - or not.

So slave needs to be "faster" than master, always having the needed data ready for transfer, if a transfer "might" come.

If you feel a post has answered your question, please click "Accept as Solution".

I understand what you are saying for the general question. So if I want to transfer different number of bytes, I should use HAL_SPI_Transmit (); HAL_SPI_Receive (); in the master, and 2 interrupts in the slave.

Again, my problem is that the MISO transmit doesn't works, and when I am trying to force it (manually or using the HAL_SPI_TransmitReceive_IT (); function) the bus stays busy (Busy flag kept set).