Skip to main content
Sbhad.1
Associate
January 19, 2022
Solved

STM32F429 Nucleo-144 board - Can't get SPI2 to work! SCK OK, but no signal on MOSI (PB15) or MISO (PB14).

  • January 19, 2022
  • 2 replies
  • 3334 views

I have configured SPI2 in Stm32CubeMx and am coding in Stm32CubeIDE. PD3 as SCK, PB15 as MOSI and PB14 as MISO are configured, and i have also checked corresponding code in stm32F4xx_hal_msp.c as below.

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
 GPIO_InitTypeDef GPIO_InitStruct = {0};
 if(hspi->Instance==SPI2)
 {
 /* USER CODE BEGIN SPI2_MspInit 0 */
 
 /* USER CODE END SPI2_MspInit 0 */
 /* Peripheral clock enable */
 __HAL_RCC_SPI2_CLK_ENABLE();
 
 __HAL_RCC_GPIOB_CLK_ENABLE();
 __HAL_RCC_GPIOD_CLK_ENABLE(); /**SPI2 GPIO Configuration
 PB14 ------> SPI2_MISO
 PB15 ------> SPI2_MOSI
 PD3 ------> SPI2_SCK
 */
 GPIO_InitStruct.Pin = GPIO_PIN_14;
 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_SPI2;
 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
 GPIO_InitStruct.Pin = GPIO_PIN_15;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_PULLUP;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
 GPIO_InitStruct.Pin = GPIO_PIN_3;
 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_SPI2;
 HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

Additionally, the debug breakpoints placed in the above code show that this is executed. I am sending test SPI data, but on a DSO I can only see SCK pulses, but the PB14 (MISO) is always low, and PB15 (MOSI) is always high. Nothing is connected to those pins. I made sure there are no other conflicting peripherals like USB OTG, either configured or connected to the Nucleo board. SPi speed is 1.2 MHz.

Any clue about this? Thanks in advance.

This topic has been closed for replies.
Best answer by MM..1
Ok and what is in wr_buf? Is returned HAL_OK?

2 replies

MM..1
Chief III
January 19, 2022

If you configure and use SPI2 as master then MISO low is OK .

How code you call to test send?

Sbhad.1
Sbhad.1Author
Associate
January 20, 2022

Thanks for your reply. Yes, i use SPI2 as master in full duplex mode.

For testing I am using the following code :

HAL_GPIO_WritePin(SPI2_NSS_GPIO_Port, SPI2_NSS_Pin, GPIO_PIN_RESET);
if (HAL_SPI_TransmitReceive(&hspi2, wr_buf, (uint8_t *)spi_rx_buffer, 8, 5000) != HAL_OK) {
	printf("-1\n");
}
HAL_GPIO_WritePin(SPI2_NSS_GPIO_Port, SPI2_NSS_Pin, GPIO_PIN_SET);

MM..1
MM..1Best answer
Chief III
January 20, 2022
Ok and what is in wr_buf? Is returned HAL_OK?
waclawek.jan
Super User
January 19, 2022

> I made sure there are no other conflicting peripherals like USB OTG

How?

Read out and check/post content of OTG_HS registers (mainly OTG_HS_GCCFG).

JW

Sbhad.1
Sbhad.1Author
Associate
January 20, 2022

Good question! I only depended on CubeMX to configure OTG as disabled.

How do i check that in the registers? I am not so familiar with interacting with registers in bit-language.