Skip to main content
yuri CH
Senior
November 24, 2021
Solved

SPI recieving problems

Hello!

I am working with the dual core stm32h745 nucleo board, currently on the M4 core.

here are the defenitions in cubeMX:

0693W00000GYg2LQAT.png 

the defenition is for a recieve only master which belongs to the m4 cortex.

while trying to use HAL_SPI_Receive_IT() or HAL_SPI_Receive() i am not able to get HAL_OK status. the board isnt connected to any other hardware and i am trying to see that i am ok on my side.

my actions are pretty straight fowards, could you please advise on what i might be doing wrong and what can create such a problem ?

thank you!

    This topic has been closed for replies.
    TDK
    TDKBest answer
    November 25, 2021

    > hspi1.Init.Direction = SPI_DIRECTION_2LINES_RXONLY;

    > Status = HAL_SPI_Transmit(&hspi1, Data, 1,1);

    The code you posted doesn't use HAL_SPI_Receive. Rather, it uses HAL_SPI_Transmit. You have the SPI set up in RX only mode. Hence, the configuration isn't compatible with what you're trying to do.

    I would recommend using 2-line mode and leaving one of the pins uninitialized if you don't need it. The RX-only mode of the hardware is awkward and can lead to unexpected/undesired extra clocks.

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

    5 replies

    TDK
    November 24, 2021

    > i am not able to get HAL_OK status

    So what do you get instead?

    Show your code, likely a software issue, perhaps the peripheral isn't being initialized. You can step through HAL_SPI_Receive to see what it objects to.

    "If you feel a post has answered your question, please click ""Accept as Solution""."
    yuri CH
    yuri CHAuthor
    Senior
    November 25, 2021

    Hi!

    the  MX_SPI1_Init() function returns a HAL_OK status and seems to go fine.

    when i use the HAL_SPI_Receive() the status returned is HAL_ERROR.

    when i use the HAL_SPI_Receive_IT() the status returned is HAL_OK for the first call and then its always HAL_BUSY.

    after expiriencing these issues i have created a very simple test project using CubeMX,

    the only code i have added was in th main loop:

    while (1)

     {

      if(HAL_GetTick() - Tick > 1000)

      {

       Tick = HAL_GetTick();

       Status = HAL_SPI_Transmit(&hspi1, Data, 1,1);

       if(Status == HAL_OK)

        HAL_GPIO_TogglePin(LD1_GPIO_Port, LD1_Pin);

      }

    i have noticed something a bit disturbing, the SPI readings were successful on the M7 core,

    when i tried the same on the M4 core they all failed as describe above.

    perhaps this can give a clue about whats going on?

    yuri CH
    yuri CHAuthor
    Senior
    November 25, 2021
    /* Includes ------------------------------------------------------------------*/
    #include "main.h"
    #include "string.h"
     
    /* Private includes ----------------------------------------------------------*/
    /* USER CODE BEGIN Includes */
     
    /* USER CODE END Includes */
     
    /* Private typedef -----------------------------------------------------------*/
    /* USER CODE BEGIN PTD */
     
    /* USER CODE END PTD */
     
    /* Private define ------------------------------------------------------------*/
    /* USER CODE BEGIN PD */
     
    #ifndef HSEM_ID_0
    #define HSEM_ID_0 (0U) /* HW semaphore 0*/
    #endif
    /* USER CODE END PD */
     
    /* Private macro -------------------------------------------------------------*/
    /* USER CODE BEGIN PM */
     
    /* USER CODE END PM */
     
    /* Private variables ---------------------------------------------------------*/
    #if defined ( __ICCARM__ ) /*!< IAR Compiler */
     
    #pragma location=0x30040000
    ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
    #pragma location=0x30040060
    ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
    #pragma location=0x30040200
    uint8_t Rx_Buff[ETH_RX_DESC_CNT][ETH_MAX_PACKET_SIZE]; /* Ethernet Receive Buffers */
     
    #elif defined ( __CC_ARM ) /* MDK ARM Compiler */
     
    __attribute__((at(0x30040000))) ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
    __attribute__((at(0x30040060))) ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
    __attribute__((at(0x30040200))) uint8_t Rx_Buff[ETH_RX_DESC_CNT][ETH_MAX_PACKET_SIZE]; /* Ethernet Receive Buffer */
     
    #elif defined ( __GNUC__ ) /* GNU Compiler */
     
    ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT] __attribute__((section(".RxDecripSection"))); /* Ethernet Rx DMA Descriptors */
    ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT] __attribute__((section(".TxDecripSection"))); /* Ethernet Tx DMA Descriptors */
    uint8_t Rx_Buff[ETH_RX_DESC_CNT][ETH_MAX_PACKET_SIZE] __attribute__((section(".RxArraySection"))); /* Ethernet Receive Buffers */
     
    #endif
     
    ETH_TxPacketConfig TxConfig;
     
    ETH_HandleTypeDef heth;
     
    SPI_HandleTypeDef hspi1;
     
    UART_HandleTypeDef huart3;
     
    /* USER CODE BEGIN PV */
     
    /* USER CODE END PV */
     
    /* Private function prototypes -----------------------------------------------*/
    static void MX_GPIO_Init(void);
    static void MX_SPI1_Init(void);
    /* USER CODE BEGIN PFP */
     
    /* USER CODE END PFP */
     
    /* Private user code ---------------------------------------------------------*/
    /* USER CODE BEGIN 0 */
    uint8_t Data[4];
    uint32_t Tick;
    HAL_StatusTypeDef Status;
    /* USER CODE END 0 */
     
    /**
     * @brief The application entry point.
     * @retval int
     */
    int main(void)
    {
     /* USER CODE BEGIN 1 */
     
     /* USER CODE END 1 */
     
    /* USER CODE BEGIN Boot_Mode_Sequence_1 */
     /*HW semaphore Clock enable*/
     __HAL_RCC_HSEM_CLK_ENABLE();
     /* Activate HSEM notification for Cortex-M4*/
     HAL_HSEM_ActivateNotification(__HAL_HSEM_SEMID_TO_MASK(HSEM_ID_0));
     /*
     Domain D2 goes to STOP mode (Cortex-M4 in deep-sleep) waiting for Cortex-M7 to
     perform system initialization (system clock config, external memory configuration.. )
     */
     HAL_PWREx_ClearPendingEvent();
     HAL_PWREx_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFE, PWR_D2_DOMAIN);
     /* Clear HSEM flag */
     __HAL_HSEM_CLEAR_FLAG(__HAL_HSEM_SEMID_TO_MASK(HSEM_ID_0));
     
    /* USER CODE END Boot_Mode_Sequence_1 */
     /* MCU Configuration--------------------------------------------------------*/
     
     /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
     HAL_Init();
     
     /* USER CODE BEGIN Init */
     
     /* USER CODE END Init */
     
     /* USER CODE BEGIN SysInit */
     
     /* USER CODE END SysInit */
     
     /* Initialize all configured peripherals */
     MX_GPIO_Init();
     MX_SPI1_Init();
     /* USER CODE BEGIN 2 */
     
     /* USER CODE END 2 */
     
     /* Infinite loop */
     /* USER CODE BEGIN WHILE */
     while (1)
     {
     if(HAL_GetTick() - Tick > 1000)
     {
     Tick = HAL_GetTick();
     Status = HAL_SPI_Transmit(&hspi1, Data, 1,1);
     if(Status == HAL_OK)
     HAL_GPIO_TogglePin(LD1_GPIO_Port, LD1_Pin);
     
     }
     /* USER CODE END WHILE */
     
     /* USER CODE BEGIN 3 */
     }
     /* USER CODE END 3 */
    }
     
    /**
     * @brief ETH Initialization Function
     * @param None
     * @retval None
     */
    void MX_ETH_Init(void)
    {
     
     /* USER CODE BEGIN ETH_Init 0 */
     
     /* USER CODE END ETH_Init 0 */
     
     static uint8_t MACAddr[6];
     
     /* USER CODE BEGIN ETH_Init 1 */
     
     /* USER CODE END ETH_Init 1 */
     heth.Instance = ETH;
     MACAddr[0] = 0x00;
     MACAddr[1] = 0x80;
     MACAddr[2] = 0xE1;
     MACAddr[3] = 0x00;
     MACAddr[4] = 0x00;
     MACAddr[5] = 0x00;
     heth.Init.MACAddr = &MACAddr[0];
     heth.Init.MediaInterface = HAL_ETH_RMII_MODE;
     heth.Init.TxDesc = DMATxDscrTab;
     heth.Init.RxDesc = DMARxDscrTab;
     heth.Init.RxBuffLen = 1524;
     
     /* USER CODE BEGIN MACADDRESS */
     
     /* USER CODE END MACADDRESS */
     
     if (HAL_ETH_Init(&heth) != HAL_OK)
     {
     Error_Handler();
     }
     
     memset(&TxConfig, 0 , sizeof(ETH_TxPacketConfig));
     TxConfig.Attributes = ETH_TX_PACKETS_FEATURES_CSUM | ETH_TX_PACKETS_FEATURES_CRCPAD;
     TxConfig.ChecksumCtrl = ETH_CHECKSUM_IPHDR_PAYLOAD_INSERT_PHDR_CALC;
     TxConfig.CRCPadCtrl = ETH_CRC_PAD_INSERT;
     /* USER CODE BEGIN ETH_Init 2 */
     
     /* USER CODE END ETH_Init 2 */
     
    }
     
    /**
     * @brief SPI1 Initialization Function
     * @param None
     * @retval None
     */
    static void MX_SPI1_Init(void)
    {
     
     /* USER CODE BEGIN SPI1_Init 0 */
     
     /* USER CODE END SPI1_Init 0 */
     
     /* USER CODE BEGIN SPI1_Init 1 */
     
     /* USER CODE END SPI1_Init 1 */
     /* SPI1 parameter configuration*/
     hspi1.Instance = SPI1;
     hspi1.Init.Mode = SPI_MODE_MASTER;
     hspi1.Init.Direction = SPI_DIRECTION_2LINES_RXONLY;
     hspi1.Init.DataSize = SPI_DATASIZE_19BIT;
     hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
     hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
     hspi1.Init.NSS = SPI_NSS_SOFT;
     hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
     hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
     hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
     hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
     hspi1.Init.CRCPolynomial = 0x0;
     hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
     hspi1.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
     hspi1.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
     hspi1.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
     hspi1.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
     hspi1.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
     hspi1.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
     hspi1.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
     hspi1.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
     hspi1.Init.IOSwap = SPI_IO_SWAP_DISABLE;
     if (HAL_SPI_Init(&hspi1) != HAL_OK)
     {
     Error_Handler();
     }
     /* USER CODE BEGIN SPI1_Init 2 */
     
     /* USER CODE END SPI1_Init 2 */
     
    }
     
    /**
     * @brief USART3 Initialization Function
     * @param None
     * @retval None
     */
    void MX_USART3_UART_Init(void)
    {
     
     /* USER CODE BEGIN USART3_Init 0 */
     
     /* USER CODE END USART3_Init 0 */
     
     /* USER CODE BEGIN USART3_Init 1 */
     
     /* USER CODE END USART3_Init 1 */
     huart3.Instance = USART3;
     huart3.Init.BaudRate = 115200;
     huart3.Init.WordLength = UART_WORDLENGTH_8B;
     huart3.Init.StopBits = UART_STOPBITS_1;
     huart3.Init.Parity = UART_PARITY_NONE;
     huart3.Init.Mode = UART_MODE_TX_RX;
     huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
     huart3.Init.OverSampling = UART_OVERSAMPLING_16;
     huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
     huart3.Init.ClockPrescaler = UART_PRESCALER_DIV1;
     huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
     if (HAL_UART_Init(&huart3) != HAL_OK)
     {
     Error_Handler();
     }
     if (HAL_UARTEx_SetTxFifoThreshold(&huart3, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
     {
     Error_Handler();
     }
     if (HAL_UARTEx_SetRxFifoThreshold(&huart3, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
     {
     Error_Handler();
     }
     if (HAL_UARTEx_DisableFifoMode(&huart3) != HAL_OK)
     {
     Error_Handler();
     }
     /* USER CODE BEGIN USART3_Init 2 */
     
     /* USER CODE END USART3_Init 2 */
     
    }
     
    /**
     * @brief GPIO Initialization Function
     * @param None
     * @retval None
     */
    static void MX_GPIO_Init(void)
    {
     GPIO_InitTypeDef GPIO_InitStruct = {0};
     
     /* GPIO Ports Clock Enable */
     __HAL_RCC_GPIOA_CLK_ENABLE();
     __HAL_RCC_GPIOB_CLK_ENABLE();
     
     /*Configure GPIO pin Output Level */
     HAL_GPIO_WritePin(LD1_GPIO_Port, LD1_Pin, GPIO_PIN_RESET);
     
     /*Configure GPIO pin : LD1_Pin */
     GPIO_InitStruct.Pin = LD1_Pin;
     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
     GPIO_InitStruct.Pull = GPIO_NOPULL;
     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
     HAL_GPIO_Init(LD1_GPIO_Port, &GPIO_InitStruct);
     
    }
     
    /* USER CODE BEGIN 4 */
     
    /* USER CODE END 4 */
     
    /**
     * @brief This function is executed in case of error occurrence.
     * @retval None
     */
    void Error_Handler(void)
    {
     /* USER CODE BEGIN Error_Handler_Debug */
     /* User can add his own implementation to report the HAL error return state */
     __disable_irq();
     while (1)
     {
     }
     /* USER CODE END Error_Handler_Debug */
    }
     
    #ifdef USE_FULL_ASSERT
    /**
     * @brief Reports the name of the source file and the source line number
     * where the assert_param error has occurred.
     * @param file: pointer to the source file name
     * @param line: assert_param error line source number
     * @retval None
     */
    void assert_failed(uint8_t *file, uint32_t line)
    {
     /* USER CODE BEGIN 6 */
     /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
     /* USER CODE END 6 */
    }
    #endif /* USE_FULL_ASSERT */
     

    This is the M4 main.c code.

    TDK
    TDKBest answer
    November 25, 2021

    > hspi1.Init.Direction = SPI_DIRECTION_2LINES_RXONLY;

    > Status = HAL_SPI_Transmit(&hspi1, Data, 1,1);

    The code you posted doesn't use HAL_SPI_Receive. Rather, it uses HAL_SPI_Transmit. You have the SPI set up in RX only mode. Hence, the configuration isn't compatible with what you're trying to do.

    I would recommend using 2-line mode and leaving one of the pins uninitialized if you don't need it. The RX-only mode of the hardware is awkward and can lead to unexpected/undesired extra clocks.

    "If you feel a post has answered your question, please click ""Accept as Solution""."
    yuri CH
    yuri CHAuthor
    Senior
    November 25, 2021

    thanks again for responding!

    I'm sorry, i've put up the wrong code, i am using HAL_SPI_Receive() and also as i said tried HAL_SPI_Receive_IT().

    in both cases while observing the clk line on the scope i saw that:

    on the M7 - a clock is generated and the status from function is HAL_OK.

    on the M4 - no clock sinal is generated, the clock goes high for a short time and goes low again.

    i am currently using the receive only master, i will try what you suggested to use the full spi.

    in the meantime could you think of a reason why something that works on the M7 does not work on the M4?