Skip to main content
Trupal_jasani12
Associate
June 6, 2023
Question

How to receive data by using SPI communication ?

  • June 6, 2023
  • 2 replies
  • 2634 views

I use the STM32Lo538 disco board, and this board is connected to one sensor. Moreover, the protocol of this sensor is SPI. I faced difficulties receiving the data from the sensor. When I watched the waveform of this sensor, I noticed that the CS pin was driven from high to low, but data was not received by the master. Furthermore, I checked every possible condition, but still, data is not received by the master. However, I also tried the NSS pin, but this time I am only able to read one register and cannot read another register. What should I do?

CODE :-

void max30001_cs_pin_reset(){

HAL_GPIO_WritePin(BSP_MAX30001_NSS_PIN_GPIO_Port, BSP_MAX30001_NSS_PIN_Pin,

GPIO_PIN_RESET); // Set chip select low

// HAL_GPIO_WritePin(BSP_MAX30001_CS_PIN_GPIO_Port, BSP_MAX30001_CS_PIN_Pin,

// GPIO_PIN_RESET); // Set chip select low

}

void max30001_cs_pin_set() {

HAL_GPIO_WritePin(BSP_MAX30001_NSS_PIN_GPIO_Port, BSP_MAX30001_NSS_PIN_Pin,

GPIO_PIN_SET); // Set chip select high

// HAL_GPIO_WritePin(BSP_MAX30001_CS_PIN_GPIO_Port, BSP_MAX30001_CS_PIN_Pin,

// GPIO_PIN_SET); // Set chip select high

}

int32_t BSP_SPI1_Read(uint8_t *tx_buff, uint16_t tx_size, uint8_t *rx_buff,

uint16_t rx_size) {

HAL_StatusTypeDef ret = HAL_ERROR;

max30001_cs_pin_reset();

HAL_Delay(100);

ret = HAL_SPI_TransmitReceive(&hspi1, tx_buff, rx_buff, rx_size, 500);

if (ret == HAL_OK) {

max30001_cs_pin_set();

HAL_Delay(100);

return ret;

}

return ret;

}

int32_t BSP_SPI1_write(uint8_t *tx_buff, uint16_t tx_size, uint8_t *rx_buff,

uint16_t rx_size) {

HAL_StatusTypeDef ret = HAL_ERROR;

max30001_cs_pin_reset();

HAL_Delay(100);

ret = HAL_SPI_Transmit(&hspi1, tx_buff, tx_size, 100);

if (ret == HAL_OK) {

max30001_cs_pin_set();

HAL_Delay(100);

return ret;

}

return ret;

}

This topic has been closed for replies.

2 replies

S.Ma
Principal
June 7, 2023

Do you see SCK toggling when oscilloscope probing?

Does the number of clock pulse match the number of data bits transferred? Is the slave working in SPI 4 or 3 wires SPI mode ?

Trupal_jasani12
Associate
June 7, 2023

Yes, the number of clock pulses matches the number of data bits transferred , but sometimes not all the times .

slave is working in SPI 4 wire. My slave is the MAX30001 sensor, and this sensor is connected to the STM32 with the level shifter and buck converter because this sensor requires 1.8 volts.

figure , which is mentioned below has some change D3 line is basically MOSI and D1 line is CLK
_legacyfs_online_stmicro_images_0693W00000dDHDxQAO.png

S.Ma
Principal
June 7, 2023

As you use SPI 4 wires, it is better to consider that using SPI_TransmitReceive() function.

Extra dummy TX data to get all the needed clock bits (for reading), and RX buffer should be as big as TX buffer. If the transaction is 2 bytes to send and 8 bytes to read, make 2 buffers of 10+ bytes and use both buffer in the TransmitReceive function.

NSS is a pure GPIO not related to SPI, controlled by SW, in master mode (at least the way I use it).

Trupal_jasani12
Associate
June 8, 2023

so I use CS pin now instead of NSS pin ?

I tried to control NSS pin by using SW but It is not controlled

can you please give me idea how to do this thing ?