cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with SPI5 in stm32h747-Disco

SLuit.1
Associate III

Hi,

I have imported the example project TX_CMSIS_Wrapper and I wanted to integrate SPI5 with it. After initialising SPI5 I could not see any clock signal generated. I am using STM32H747-DISCO development board.

void MX_SPI5_Init(void)

{

hspi5.Instance = SPI5;

hspi5.Init.Mode = SPI_MODE_MASTER;

hspi5.Init.Direction = SPI_DIRECTION_2LINES;

hspi5.Init.DataSize = SPI_DATASIZE_8BIT;

hspi5.Init.CLKPolarity = SPI_POLARITY_HIGH;

hspi5.Init.CLKPhase = SPI_PHASE_1EDGE;

hspi5.Init.NSS = SPI_NSS_SOFT;

hspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;

hspi5.Init.FirstBit = SPI_FIRSTBIT_MSB;

hspi5.Init.TIMode = SPI_TIMODE_DISABLED;

hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;

hspi5.Init.CRCPolynomial = 7;

hspi5.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;

hspi5.Init.NSSPMode = SPI_NSS_PULSE_ENABLED;

 HAL_SPI_Init(&hspi5);

 if(HAL_SPI_Init(&hspi5) != HAL_OK)

 {

  /* Initialization Error */

HAL_UART_Transmit(&huart3,(void *)"SPI_Init_error\r\n",18,100);

  Error_Handler();

 }

 if(HAL_SPI_Init(&hspi5) == HAL_OK)

 {

  /* Initialization Error */

HAL_UART_Transmit(&huart3,(void *)"SPI_Init_success\r\n",18,100);

  Error_Handler();

 }

}

After the initialisation function is called, HAL_SPI_Init(&hspi5) sends HAL_OK and in terminal I can see SPI_Init_success message.

Accroding to the user manual of the development board, SPI_SCK is in D13 pin of CN5 connector. When I saw the pin in oscilloscope clock is not generated at all. Could you please let me know the thing that I am missing in this case and why the clock is not generated.

16 REPLIES 16
CPINA
ST Employee

Hi,

Did you configure properly the GPIO (PK0) in output selecting AF5 as alternate function number to map the clock of SPI5 on the MCU IO Port? Normally, the clock should be sent as you plan as soon as the write into the transmit buffer is performed after SPI intialization.

BR

According to the user manual of stm32h747-disco, SPI5_SCK is in PK03 pin and I have configured it as follows:

 GPIO_InitStruct.Pin    = GPIO_PIN_3;

 GPIO_InitStruct.Mode   = GPIO_MODE_AF_PP;

 //GPIO_InitStruct.Pull   = GPIO_NOPULL;

 GPIO_InitStruct.Speed   = GPIO_SPEED_MEDIUM;

 GPIO_InitStruct.Alternate = GPIO_AF5_SPI5;

 HAL_GPIO_Init(GPIOK, &GPIO_InitStruct);

Am I doing something wrong?

CPINA
ST Employee

Please try with PK0 instead of PK3... It is probably a typo error (PK03). You can see on schematic that this is mapped on PK0, confirmed by the Datasheet.

Br

SLuit.1
Associate III

Do I need to configure the MOSI and MISO as in output selecting AF5 as alternate function as well ?

Indeed, as you are master, MOSI has to be configured in OUTPUT with AF5 on PJ10 and MISO as input with AF5 on PJ11.

Br

How to separate as INPUT and OUTPUT for MISO and MOSI with AF5 as the pin mode with AF is either GPIO_MODE_AF_PP or GPIO_MODE_AF_OD. You don't have the input option for AF.

Total GPIO mode defined in hal driver are:

#define GPIO_MODE_INPUT            (0x00000000U)  /*!< Input Floating Mode          */

#define GPIO_MODE_OUTPUT_PP          (0x00000001U)  /*!< Output Push Pull Mode         */

#define GPIO_MODE_OUTPUT_OD          (0x00000011U)  /*!< Output Open Drain Mode        */

#define GPIO_MODE_AF_PP            (0x00000002U)  /*!< Alternate Function Push Pull Mode   */

#define GPIO_MODE_AF_OD            (0x00000012U)  /*!< Alternate Function Open Drain Mode  */

I have configure the sclk, mosi and miso to map with SPI5 as you have mentioned. Could you please have a look if there is something wrong with it

/*Configuring pin PK0: It is configured as AF5 alternate function for SPI5_SCK*/

 GPIO_InitStruct.Pin    = GPIO_PIN_0;

 GPIO_InitStruct.Mode   = GPIO_MODE_AF_PP;

 //GPIO_InitStruct.Pull   = GPIO_NOPULL;

 GPIO_InitStruct.Speed   = GPIO_SPEED_MEDIUM;

 GPIO_InitStruct.Alternate = GPIO_AF5_SPI5;

 HAL_GPIO_Init(GPIOK, &GPIO_InitStruct);

/* Configuring pin PJ10: It is configured as AF5 alternate function for SPI5_MOSI*/

  GPIO_InitStruct.Pin    = GPIO_PIN_10;

  GPIO_InitStruct.Mode   = GPIO_MODE_OUTPUT_PP;

  //GPIO_InitStruct.Pull   = GPIO_NOPULL;

  GPIO_InitStruct.Speed   = GPIO_SPEED_MEDIUM;

  GPIO_InitStruct.Alternate = GPIO_AF5_SPI5;

  HAL_GPIO_Init(GPIOJ, &GPIO_InitStruct);

  /* Configuring pin PJ11: It is configured as AF5 alternate function for SPI5_MISO*/

    GPIO_InitStruct.Pin    = GPIO_PIN_11;

    GPIO_InitStruct.Mode   = GPIO_MODE_INPUT;

    //GPIO_InitStruct.Pull   = GPIO_NOPULL;

    GPIO_InitStruct.Speed   = GPIO_SPEED_MEDIUM;

    GPIO_InitStruct.Alternate = GPIO_AF5_SPI5;

    HAL_GPIO_Init(GPIOJ, &GPIO_InitStruct);

for PJ10 and PJ_11 you need GPIO_InitStruct.Mode   = GPIO_MODE_AF_PP;

Even if they are output for MOSI and Input for MISO, they are linked to the alternate function 5.

/* SPI SCK GPIO pin configuration */

  GPIO_InitStruct.Pin    = SPIx_SCK_PIN;

  GPIO_InitStruct.Mode   = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull   = GPIO_PULLDOWN;

  GPIO_InitStruct.Speed   = GPIO_SPEED_FREQ_HIGH;

  GPIO_InitStruct.Alternate = SPIx_SCK_AF;

  HAL_GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStruct);

  /* SPI MISO GPIO pin configuration */

  GPIO_InitStruct.Pin = SPIx_MISO_PIN;

  GPIO_InitStruct.Alternate = SPIx_MISO_AF;

  HAL_GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStruct);

  /* SPI MOSI GPIO pin configuration */

  GPIO_InitStruct.Pin = SPIx_MOSI_PIN;

  GPIO_InitStruct.Alternate = SPIx_MOSI_AF;

  HAL_GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStruct);

You may be found some SPI example inside the examples of the HAL library for which I strongly advise you to refer. All this configurations are defined into the stM32h7xx_hal_msp.c and main.h as you can find in HAL library examples

Take care also about the Clock enable on the GPIO port on which the SPI is connected before configuring these pins.

So GPIO PK and GPIO PJ should have their clock enabled before configuring their respective pins

__HAL_RCC_GPIOJ_CLK_ENABLE();

__HAL_RCC_GPIOK_CLK_ENABLE(

Hi, I have found this on stm32h7xx_hal_spi.c file and I could not find how to Enable the SPIx interface clock.

            ##### How to use this driver #####

 ==============================================================================

  [..]

   The SPI HAL driver can be used as follows:

   (#) Declare a SPI_HandleTypeDef handle structure, for example:

     SPI_HandleTypeDef hspi;

   (#)Initialize the SPI low level resources by implementing the HAL_SPI_MspInit() API:

     (##) Enable the SPIx interface clock

     (##) SPI pins configuration

       (+++) Enable the clock for the SPI GPIOs

Could you let me know how to enable the SPIx interface clock in this one. I went through the examples also and I could not find where they have enabled it.