Skip to main content
MLemb.2
Associate II
May 1, 2023
Solved

STM32F1 - no HAL error, no SPI signal

  • May 1, 2023
  • 6 replies
  • 2022 views

Hello!

I'm trying to run SPI in Transmit Only Master mode.

It's strange - result of HAL_SPI_Transmit is HAL_OK but I can't see any signals on SCK and MOSI pins.

main.c

int main(void)
{
 /* USER CODE BEGIN 1 */
 
 /* USER CODE END 1 */
 
 /* MCU Configuration--------------------------------------------------------*/
 
 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 HAL_Init();
 
 /* USER CODE BEGIN Init */
 
 /* USER CODE END Init */
 
 /* Configure the system clock */
 SystemClock_Config();
 
 /* USER CODE BEGIN SysInit */
 
 /* USER CODE END SysInit */
 
 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_SPI3_Init();
 /* USER CODE BEGIN 2 */
 
 /* USER CODE END 2 */
 
 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
		uint8_t dacRegisterAddress = 0xAA;
		uint8_t dacRegisterData = 0xAA;
		uint8_t sendData[2] = {dacRegisterAddress, dacRegisterData};
 
		HAL_GPIO_WritePin(GPIOB, STATUS_LED_Pin, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(GPIOC, GPIO_PIN_12, GPIO_PIN_RESET);
		while (HAL_SPI_GetState(&hspi3) != HAL_SPI_STATE_READY);
		if (HAL_SPI_Transmit(&hspi3, &sendData[0], 2, 1000) != HAL_OK)
		{
	 	HAL_GPIO_WritePin(GPIOA, ERROR_LED_Pin, GPIO_PIN_RESET);
		}
		HAL_GPIO_WritePin(GPIOB, STATUS_LED_Pin, GPIO_PIN_SET);
		HAL_GPIO_WritePin(GPIOC, GPIO_PIN_12, GPIO_PIN_SET);
		HAL_Delay(200);
 
 /* USER CODE END WHILE */
 
 /* USER CODE BEGIN 3 */
 }
 /* USER CODE END 3 */
}
 
/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
 /** Initializes the RCC Oscillators according to the specified parameters
 * in the RCC_OscInitTypeDef structure.
 */
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 {
 Error_Handler();
 }
 
 /** Initializes the CPU, AHB and APB buses clocks
 */
 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
 {
 Error_Handler();
 }
}
 
/**
 * @brief SPI3 Initialization Function
 * @param None
 * @retval None
 */
static void MX_SPI3_Init(void)
{
 
 /* USER CODE BEGIN SPI3_Init 0 */
 
 /* USER CODE END SPI3_Init 0 */
 
 /* USER CODE BEGIN SPI3_Init 1 */
 
 /* USER CODE END SPI3_Init 1 */
 /* SPI3 parameter configuration*/
 hspi3.Instance = SPI3;
 hspi3.Init.Mode = SPI_MODE_MASTER;
 hspi3.Init.Direction = SPI_DIRECTION_2LINES;
 hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
 hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
 hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
 hspi3.Init.NSS = SPI_NSS_SOFT;
 hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
 hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
 hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
 hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
 hspi3.Init.CRCPolynomial = 10;
 if (HAL_SPI_Init(&hspi3) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN SPI3_Init 2 */
 
 /* USER CODE END SPI3_Init 2 */
 
}

HAL_SPI_MspInit in stm32f1xx_hal_msp.c

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
 GPIO_InitTypeDef GPIO_InitStruct = {0};
 if(hspi->Instance==SPI3)
 {
 /* USER CODE BEGIN SPI3_MspInit 0 */
 
 /* USER CODE END SPI3_MspInit 0 */
 /* Peripheral clock enable */
 __HAL_RCC_SPI3_CLK_ENABLE();
 
 __HAL_RCC_GPIOB_CLK_ENABLE();
 /**SPI3 GPIO Configuration
 PB3 ------> SPI3_SCK
 PB5 ------> SPI3_MOSI
 */
 GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_5;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
 /* USER CODE BEGIN SPI3_MspInit 1 */
 
 /* USER CODE END SPI3_MspInit 1 */
 }
 
}

I've checked this with many official and users examples. I can't find mistake. I checked this on STM32F0 too and behaviour is the same.

I updated CubeIDE - no result.

What I'm doing wrong?

This topic has been closed for replies.
Best answer by waclawek.jan

This looks all OK.

Try to set given pins as GPIO outputs and toggle them in a simple loop, while observing the output on the pins.

JW

6 replies

waclawek.jan
Super User
May 1, 2023

Which 'F1 exactly? And which 'F0 exactly?

Read out and check/post content of SPI and relevant GPIO/AFIO registers.

JW

MLemb.2
MLemb.2Author
Associate II
May 1, 2023

Thank you for reply 

STM32 model is STM32F103RCT6

Registers before init:


_legacyfs_online_stmicro_images_0693W00000bj810QAA.png 

Registers after SPI init:


_legacyfs_online_stmicro_images_0693W00000bj815QAA.pngRegisters after transmit:


_legacyfs_online_stmicro_images_0693W00000bj81AQAQ.png Is it correct?

waclawek.jan
waclawek.janBest answer
Super User
May 1, 2023

This looks all OK.

Try to set given pins as GPIO outputs and toggle them in a simple loop, while observing the output on the pins.

JW

MLemb.2
MLemb.2Author
Associate II
May 1, 2023

Good idea! I tried this:

SPI3 MISO (PB5) - toggle ok

SPI3 SCK (PB3) - no reaction

SCK is also SYS_JTDO so I add to HAL_MspInit:

__HAL_AFIO_REMAP_SWJ_NOJTAG();

After that SCK toggle too. When I turn on SPI I don't have any signal again :\

I fund in F1 HAL documentation "__HAL_AFIO_REMAP_SPI3_DISABLE" but it isn't available in my MCU.

Should I use more configuration to run SPI3?

AScha.3
Super User
May 1, 2023


_legacyfs_online_stmicro_images_0693W00000bj8lhQAA.pngyou did...prior configure spi3 ?

If you feel a post has answered your question, please click on " Best Answer ".
MLemb.2
MLemb.2Author
Associate II
May 1, 2023

I disabled JTAG in HAL_MspInit function, so before SPI init, but...

I aslo want to use PB4 as GPIO so I added "__HAL_AFIO_REMAP_SWJ_NONJTRST();" too.

Then I have value 0x060 in SWJ_CFG register.

I find table in RM0008 where is no 011 value. So I use only "__HAL_AFIO_REMAP_SWJ_NOJTAG();" and now SPI is working.


_legacyfs_online_stmicro_images_0693W00000bj8q5QAA.pngThank you AScha.3 and waclawek.jan for help! :)