cancel
Showing results for 
Search instead for 
Did you mean: 

SPI problem in STM32F446 NUCLEO

mrkayaalp
Associate III

Hello everyone im currently working on a adc AD7708. I want to read chip from the device but i cant get any valid value. Here is the register sturucture of AD7708:

mrkayaalp_0-1707326670954.png

I wrote a basic code to read the chip id from id register. Here is the code:

 

 

 

 

 

  while (1)
  {
	    HAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, 0);
	    status = HAL_SPI_Transmit(&hspi1, txdata, 1, 500);
	    while (hspi1.State != HAL_SPI_STATE_READY)1;

	    status = HAL_SPI_Receive(&hspi1, rxdata, 1, 500);
	    while (hspi1.State != HAL_SPI_STATE_READY);
	    HAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, 1);

	    HAL_Delay(10);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }

 

 

 

 

 

I examined it with the scope and i think there is no problem in MOSI line. But i cant see any signal in MISO line.  Here is my spi settings in stm32cubeide.

mrkayaalp_1-1707327149557.png

I couldn't find the problem. What should i do?

19 REPLIES 19
AScha.3
Principal III

Hi,

With full duplex you should use :   HAL_SPI_TransmitReceive( ..) ;

If dont need the received byte, just dont use it, but the spi here always transmit + receive same time.

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

If MOSI is working correctly there is no issue on the STM32 side.

Recheck connections to the chip, ensure it's powered and operational and not in any low power or sleep state.

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

I already tried it and its not working

I checked my power supply and its totally fine. Im pretty sure that adc is not  on the low power or sleep state. I think there is no problem with the software side. I will check everything again..

So at first set the SPI signals , as this adc needs :

AScha3_0-1707382264134.png

in Cube set : cpol hi + cpha 2 edge .

+ use HAL_SPI_TransmitReceive( ..) ;

+ how to set the command -- i hope you read the manual...

AScha3_1-1707382549482.png

 

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

So i changed my spi settings:

mrkayaalp_0-1707384728025.png

and then like you sad i used TransmitRecieve func. but im not sure that use it correctly:

  /* USER CODE BEGIN 2 */
  	uint8_t txdata[2];
    uint8_t rxdata[2];
    HAL_StatusTypeDef status;
    //first byte setting up the comm register.
    //second byte writing to the mode register
    txdata[0] = 1;
    txdata[1] = 18;

    HAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, 0);
    status = HAL_SPI_TransmitReceive(&hspi1, txdata, rxdata, 2, 250);
    while (hspi1.State != HAL_SPI_STATE_READY)1;
    HAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, 1);

    txdata[0] = 79; //read operation from id register (im setting the comm register)
    txdata[1] = 0;  //dummy data for response

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	    HAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, 0);
	    status = HAL_SPI_TransmitReceive(&hspi1, txdata, rxdata, 1, 250); //rxdata[1] will be my chip id register value
	    while (hspi1.State != HAL_SPI_STATE_READY);
	    HAL_GPIO_WritePin(SPI1_CS_GPIO_Port, SPI1_CS_Pin, 1);

	    HAL_Delay(10);

But i couldn't get any value in rxdata[2] im i doing something wrong?

Did you read the manual ??

All communications to the part must start with a write operation to the Communications Register.

The data written to the Communications Register determines whether the next operation is a read or write operation, the type of read operation, and on which register this operation takes place. For read or write operations, once the subsequent read or write operation to the selected register is complete, the interface returns to where it expects a write operation to the Communications Register.

<

I see no init/setup sequence ... maybe at first just set up simple sequence to read status, to see connection works - or not.

(You just write 1 , 18 .  in decimal...what should it do then ? )

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

First of all im writing 1 to the comm register '0b00000001'  last 4 bit indicates '0001' i want to communicate with mode register and then 7th bit indicates that its gonna be a write operation ('0' for write). Then 18 decimal value will be writed in mode register ( '0b00010010' now adc is in single conversion mode ). After that in while loop first im sending 79 value ('01001111'accesing id_reg in read mode) and then next 8 bit value will be the readded value from mode register. I think there is no problem with that. But like i sad im not sure that RecieveTransmit func. is used correctly. Is there anything else you think is wrong?

My actual code is more complex. So im just trying to communicate with the device first. These codes are more simplified version for me to see the problems clearly.