cancel
Showing results for 
Search instead for 
Did you mean: 

what's the problem?

idrissmc
Associate III

hello everyone,

can someone help me please, I can't understand why I can't receive 7, it's weird...

I sent 7 , i want to receive 7, but I receive 255, 0, 15.. all the values except 7, that's really insane.

0690X000009ZeWtQAK.png

20 REPLIES 20

this code for the F413H:

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)

{

 GPIO_InitTypeDef GPIO_InitStruct = {0};

 if(hspi->Instance==SPI3)

 {

 /* USER CODE BEGIN SPI2_MspInit 0 */

 /* USER CODE END SPI2_MspInit 0 */

  /* Peripheral clock enable */

/*Enable SPI clock*/

  __HAL_RCC_SPI3_CLK_ENABLE();

  /*Enable GPIO SPI clock*/

  __HAL_RCC_GPIOB_CLK_ENABLE();

  /**SPI2 GPIO Configuration

  PB4   ------> SPI3_MISO

  PB5   ------> SPI3_MOSI

  PB12   ------> SPI3_SCK

  */

  GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_12;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

  GPIO_InitStruct.Alternate = GPIO_AF5_SPI3;

  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /*GPIO_InitStruct.Pin = GPIO_PIN_12;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull = GPIO_PULLUP;

  GPIO_InitStruct.Speed = GPIO_SPEED_FAST;

  GPIO_InitStruct.Alternate = GPIO_AF5_SPI3;

  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);*/

 /* USER CODE BEGIN SPI2_MspInit 1 */

 /* USER CODE END SPI2_MspInit 1 */

 }

}

static void MX_GPIO_Init(void)

{

GPIO_InitTypeDef GPIO_InitStruct = {0};

__HAL_RCC_GPIOA_CLK_ENABLE();

  /*Enable GPIO SPI clock*/

  __HAL_RCC_GPIOB_CLK_ENABLE();

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET); // CS

GPIO_InitStruct.Pin = GPIO_PIN_15;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

this code for the f411E:

void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)

{

 GPIO_InitTypeDef GPIO_InitStruct;

  

 /* Enable GPIO TX/RX clock */

 SPIx_SCK_GPIO_CLK_ENABLE();

 SPIx_MISO_GPIO_CLK_ENABLE();

 SPIx_MOSI_GPIO_CLK_ENABLE();

 /* Enable SPI clock */

 SPIx_CLK_ENABLE(); 

  

 /* SPI SCK GPIO pin configuration */

 GPIO_InitStruct.Pin    = SPIx_SCK_PIN;

 GPIO_InitStruct.Mode   = GPIO_MODE_AF_PP;

 GPIO_InitStruct.Pull   = GPIO_NOPULL;

 GPIO_InitStruct.Speed   = GPIO_SPEED_FREQ_VERY_HIGH;

 GPIO_InitStruct.Alternate = SPIx_SCK_AF;

  

 HAL_GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStruct);

   

 GPIO_InitStruct.Pin = SPIx_MISO_PIN;

 GPIO_InitStruct.Alternate = SPIx_MISO_AF;

  

 HAL_GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStruct);

  

 GPIO_InitStruct.Pin = SPIx_MOSI_PIN;

 GPIO_InitStruct.Alternate = SPIx_MOSI_AF;

   

 HAL_GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStruct);   

}

static void MX_GPIO_init(void)

 {

GPIO_InitTypeDef GPIO_InitStruct = {0};

__HAL_RCC_GPIOA_CLK_ENABLE();

//__HAL_RCC_GPIOE_CLK_ENABLE();

__HAL_RCC_GPIOB_CLK_ENABLE();

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET);

GPIO_InitStruct.Pin = GPIO_PIN_1;

GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_9;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

what do you think about the configuration?

What with SPI configuration? Who master, who slave?

I don't see any problem in my eyes, I still suspect this is hardware problem because sending and receiving 1 byte shouldn't be that problematic. Please confirm if data is correctly put into the wire with a oscillator/multimeter/logic analyzer(one easy way is to send 170 instead of 7, in that case miso pin has a frequency=CLK/2 and duty cycle=50%), if no it is sending software problem, but if yes than it is wire/receiver software problem.

@oleksandr.karbivsky​  the F413H is the slave and the F411E is the master!

@JChau​  I'll see that, else I'll use the CAN bus and I'll try with UART too

Share SPI configuration for both devices.

Yes please, because you have three unknown parts in your situation, sender/wire/receiver, so it is better to isolate the no-problem part 1 by 1.

@JChau​  if it’s the receiver is there any solution?

@oleksandr.karbivsky​  I described it above

You confirmed signal bits arrived the receiver correctly? If that's the case the only solution is to check the receiver code, you can also add a breakpoint in stm32f4xx_hal_spi.c line 697 and check the DR register by yourself when receiving in F413H. btw line 697 should reads like this:

/* Check the RXNE flag */
      if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE))
      {
        /* read the received data */
        (* (uint8_t *)pData)= *(__IO uint8_t *)&hspi->Instance->DR; //<--THIS LINE
        pData += sizeof(uint8_t);
        hspi->RxXferCount--;
      }

if DR register is indeed 0, it means hardware do really receive 0 so you must check again sender/wire. BTW you can also check DR in transmit in line 552 (in this case evaluate (*pData) in expression panel instead):

while (hspi->TxXferCount > 0U)
    {
      /* Wait until TXE flag is set to send data */
      if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE))
      {
        *((__IO uint8_t*)&hspi->Instance->DR) = (*pData); //<--THIS LINE
        pData += sizeof(uint8_t);
        hspi->TxXferCount--;
      }

 EDIT: Sorry my bad, TX line is wrong, updated.