cancel
Showing results for 
Search instead for 
Did you mean: 

How to receive 32-bit data in SPI slave polling mode?

jg_spitfire
Senior

Hi, I want to receive 32 bit data from a SPI master but in the cubemx options there are only two options available, 8 bits and 16 bits, so what if I want to receive 32 bits frames, what should I do?

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Yep, that's pretty much it.
On the STM32, the most common way is to control the CS pins manually (i.e. using HAL_GPIO_WritePin). If you have two CS lines, you need to make sure only one is selected (low) at a time. If that's not happening, could be any number of things wrong.
If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

7 REPLIES 7
TDK
Guru

You read 4 bytes, and convert those into the 32 bit value of your choice.

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

But how could I read 4 bytes if the cubemx only gives two options for the data size in the slave?, 8 bits and 16 bits

0693W000000US1WQAW.jpg

TDK
Guru

You set up the peripheral for 8 bits and send multiple 8-bit transactions. On the bus, these look the same as a 32-bit operation. Are you familiar with the SPI protocol?

For example, if you are trying to send 32 bits:

uint8_t data[4] = {1, 2, 3, 4};
HAL_SPI_Transmit(hspi, data, 4, SPI_TIMEOUT_MAX)

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

I know a little of SPI, so you say that I should do something like this:

1.- CS low

2.- send the data of my 4 bytes buffer

3.-​ CS high

Is this correct? ​

And another question, I hope you could help me or maybe I need a new post, I was testing a máster with two slaves, and when I selected the second slave (with the first deselected) the first slave still was able to receive data in its MOSI line, and both CS have pull up resistors, do you know what could de happening?

Thanks ​

Yep, that's pretty much it.
On the STM32, the most common way is to control the CS pins manually (i.e. using HAL_GPIO_WritePin). If you have two CS lines, you need to make sure only one is selected (low) at a time. If that's not happening, could be any number of things wrong.
If you feel a post has answered your question, please click "Accept as Solution".

Thanks! ​

Hi, I have understand that in the master (a ST board) I must select low or high the CS to select the slave (another ST board), but what about the slave?, do I need to implement something like this to receive the data?

//first way
while(1)
{
 
if (HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1) == 0) //CS pin
		{
 
                    HAL_SPI_TransmitReceive(&hspi5,txbuffer,rxbuffer, 12, 5000);
 
		}
}

or the function transmitreceive detects internally if the CS is low to start the communication?, I mean, receive the data just doing this:

//second way
while(1)
{
      HAL_SPI_TransmitReceive(&hspi5,txbuffer,rxbuffer, 12, 5000);
}

I have this query because I am doing tests (second way) in the slave but always receive data, when I connect the CS pin to GND or 3.3V