cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 SPI problem (MOSI doesn´t work) for SPI 3 and 4

mst0mst
Associate II

Greetings,

I am dealing with a STM32H743ZI MCU and many of its embedded peripherals, the board is a NUCLEO H743ZI2

I encounter a strange behaviour regarding SPI configuration, for SPI 1 the MOSI pinout works as expected, but for SPI 3 and SPI 4 the MOSI doesn´t work; there are no output from MOSI pin.

I am using the GPIO alternate function configuration from the STM32H742xI/G STM32H743xI/G document (its a 357 pages one), there are several tables that list the corresponding AF for each pin and desired functionality.

The initialization code (for SPI4) I am using is this one (the same for SPI 1 works well, obviously with corresponding GPIO and AF for SPI 1):

/* SPI handler 4 declaration */
SPI_HandleTypeDef SpiHandle4 __attribute__((section(".ramd2")));
 
void SPI4_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;
 
  /* Chip Select pin is controlled by the SPI4 code */
 
  /* Enable GPIOE clock */
  __HAL_RCC_GPIOE_CLK_ENABLE();
 
  /* Configure PE.2 pin as output */
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.Pin = GPIO_PIN_2;
  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
 
  /* Configure pinout for SCK, MISO y MOSI of SPI4 */
  /*##-1- Enable peripherals and GPIO Clocks #################################*/
  /* Enable GPIO TX/RX clock and SPI4 clock*/
  __HAL_RCC_GPIOE_CLK_ENABLE();        /* SCK Port */
  __HAL_RCC_GPIOE_CLK_ENABLE();        /* MISO Port */
  __HAL_RCC_GPIOE_CLK_ENABLE();        /* MOSI Port */
  __HAL_RCC_SPI4_CLK_ENABLE();
 
  /*##-2- Configure peripheral GPIO ##########################################*/
  /* SPI SCK GPIO pin configuration  */
  GPIO_InitStruct.Pin = GPIO_PIN_2;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF5_SPI4;
  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
 
  /* SPI MISO GPIO pin configuration  */
  GPIO_InitStruct.Pin = GPIO_PIN_5;
  GPIO_InitStruct.Alternate = GPIO_AF5_SPI4;
  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
 
  /* SPI MOSI GPIO pin configuration  */
  GPIO_InitStruct.Pin = GPIO_PIN_6;
  GPIO_InitStruct.Alternate = GPIO_AF5_SPI4;
  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
 
  /*##-1- Configure the SPI4 peripheral #######################################*/
  /* Set the SPI4 parameters */
  SpiHandle4.Instance = SPI4;
  SpiHandle4.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
  SpiHandle4.Init.Direction = SPI_DIRECTION_2LINES;
  SpiHandle4.Init.DataSize = SPI_DATASIZE_8BIT;
  SpiHandle4.Init.FirstBit = SPI_FIRSTBIT_MSB;
  SpiHandle4.Init.TIMode = SPI_TIMODE_DISABLE;
  SpiHandle4.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  SpiHandle4.Init.CRCPolynomial = 7;
  SpiHandle4.Init.CRCLength = SPI_CRC_LENGTH_8BIT;
  SpiHandle4.Init.NSS = SPI_NSS_SOFT;
  SpiHandle4.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  SpiHandle4.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_ENABLE;/* Recommended setting to avoid glitches */
 
  SpiHandle4.Init.Mode = SPI_MODE_MASTER;
 
  HAL_SPI_DeInit(&SpiHandle4);
 
  if (HAL_SPI_Init(&SpiHandle4) != HAL_OK) {
    /* Initialization Error */
    Error_Handler();
  }
}
 
void SPI4_select(void)
{
  HAL_GPIO_WritePin(GPIOE, (uint16_t)GPIO_PIN_2, GPIO_PIN_RESET);
}
 
void SPI4_deselect(void)
{
  HAL_GPIO_WritePin(GPIOE, (uint16_t)GPIO_PIN_2, GPIO_PIN_SET);
}

Each MOSI pin works as GPIO output if I configure these for this test purpose, in order to check if the pin has any electric problem.

For SPI 3 the initialization code is:

/* SPI handler 3 declaration */
SPI_HandleTypeDef SpiHandle3 __attribute__((section(".ramd2")));
 
void SPI3_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;
 
  /* Chip Select pin is controlled by the SPI3 code */
 
  /* Enable GPIOE clock */
  __HAL_RCC_GPIOE_CLK_ENABLE();
 
  /* Configure PE.2 pin as output */
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.Pin = GPIO_PIN_2;
  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
 
  /* Configure pinout for SCK, MISO y MOSI of SPI3 */
  /*##-1- Enable peripherals and GPIO Clocks #################################*/
  /* Enable GPIO TX/RX clock and SPI3 clock*/
  __HAL_RCC_GPIOC_CLK_ENABLE();        /* SCK Port */
  __HAL_RCC_GPIOC_CLK_ENABLE();        /* MISO Port */
  __HAL_RCC_GPIOC_CLK_ENABLE();        /* MOSI Port */
  __HAL_RCC_SPI3_CLK_ENABLE();
 
  /*##-2- Configure peripheral GPIO ##########################################*/
  /* SPI SCK GPIO pin configuration  */
  GPIO_InitStruct.Pin = GPIO_PIN_10;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
 
  /* SPI MISO GPIO pin configuration  */
  GPIO_InitStruct.Pin = GPIO_PIN_11;
  GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
 
  /* SPI MOSI GPIO pin configuration  */
  GPIO_InitStruct.Pin = GPIO_PIN_12;
  GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
 
  /*##-1- Configure the SPI3 peripheral #######################################*/
  /* Set the SPI3 parameters */
  SpiHandle3.Instance = SPI3;
  SpiHandle3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
  SpiHandle3.Init.Direction = SPI_DIRECTION_2LINES;
  SpiHandle3.Init.DataSize = SPI_DATASIZE_8BIT;
  SpiHandle3.Init.FirstBit = SPI_FIRSTBIT_MSB;
  SpiHandle3.Init.TIMode = SPI_TIMODE_DISABLE;
  SpiHandle3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  SpiHandle3.Init.CRCPolynomial = 7;
  SpiHandle3.Init.CRCLength = SPI_CRC_LENGTH_8BIT;
  SpiHandle3.Init.NSS = SPI_NSS_SOFT;
  SpiHandle3.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  SpiHandle3.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_ENABLE;/* Recommended setting to avoid glitches */
  SpiHandle3.Init.Mode = SPI_MODE_MASTER;
 
  HAL_SPI_DeInit(&SpiHandle3);
 
  if (HAL_SPI_Init(&SpiHandle3) != HAL_OK) {
    /* Initialization Error */
    Error_Handler();
  }
}
 
void SPI3_select(void)
{
  HAL_GPIO_WritePin(GPIOE, (uint16_t)GPIO_PIN_2, GPIO_PIN_RESET);
}
 
void SPI3_deselect(void)
{
  HAL_GPIO_WritePin(GPIOE, (uint16_t)GPIO_PIN_2, GPIO_PIN_SET);
}

Do you now what is the reason of non working MOSI pin?

Thanks for yout support mates,

4 REPLIES 4

SCK work?

Read out and check GPIO and SPI registers (compare to working "instances")

JW

mst0mst
Associate II

Yes, SCK work.

Also, I detected with the scope another strange bahaviour;

as you know, each SPIx_SCK pin has some alternative GPIO options;

for SPI4 I detected that SCK is provided on two GPIO at same time (when it is configured only on PE2 ¿? )

the SPI4 is configured to provide its SCK output on PE2, but the SCK signal is visible with the scope on PE2 and PE12; but PE12 is not configured at all!

I tested my own program code (you can see initialization code upside, the software chip select for SPI4 is set to PC10, not PE2; sorry for the mistake)

and the firmware examples for NUCLEO-H743ZI; on both the only working SPI is SPI1, if I change the example code to use SPI3 or 4, it performs the same way.

SPI 2 is not tested, but the interest are for SPI 3 and 4. These need to be used on a ready routed and manufactured PCB.

Thanks for the answers,

mst0mst
Associate II

the board is a NUCLEO H743ZI2

Read out and check GPIO and SPI registers