cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407 SPI1 Initialize wrong

Sany
Associate III

Hello,

I have problems to initialize my SPI1 interface, and send a request. My Logic Analyzer Says that, after powering without a STLink.

0693W00000QKCjRQAX.png 

SPI_HandleTypeDef hspi;
 
RFM23::RFM23()
{}
 
bool RFM23::init()
{
	  hspi.Instance = SPI1;
	  hspi.Init.Mode = SPI_MODE_MASTER;
	  hspi.Init.Direction = SPI_DIRECTION_2LINES;
	  hspi.Init.DataSize = SPI_DATASIZE_8BIT;
	  hspi.Init.CLKPolarity = SPI_POLARITY_LOW;
	  hspi.Init.CLKPhase = SPI_PHASE_1EDGE;
	  hspi.Init.NSS = SPI_NSS_SOFT;
	  hspi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
	  hspi.Init.FirstBit = SPI_FIRSTBIT_MSB;
	  hspi.Init.TIMode = SPI_TIMODE_DISABLE;
	  hspi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
	  hspi.Init.CRCPolynomial = 7;
	  if (HAL_SPI_Init(&hspi) != HAL_OK)
	  {
	    Error_Handler();
	  }
 
 
	if(this->read_register(RH_RF22_REG_00_DEVICE_TYPE) != 0x8)
		return false;
}
 
uint8_t RFM23::read_register(uint8_t addr)
{
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
	uint8_t val = 0x00;
 
	if(HAL_SPI_Transmit(&hspi, (uint8_t *)&addr, 1, 100) != HAL_OK)
		return false;
 
	if(HAL_SPI_Receive(&hspi, (uint8_t *)&val,1,100) != HAL_OK)
		return false;
 
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
 
	return val;
}
 
bool RFM23::write_register(uint8_t addr, uint8_t val)
{
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
	uint8_t ret;
 
	addr |= (1<<7);
 
 
	if(HAL_SPI_Transmit(&hspi, (uint8_t *)&addr, 1,100) != HAL_OK)
		return false;
 
	if(HAL_SPI_Transmit(&hspi, (uint8_t *)&val, 1, 100) != HAL_OK)
		return false;
 
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
 
	return true;
}

Now when I run my code with a STLink and Run:

0693W00000QKCjSQAX.png 

So, can any one help me to try out the Problem?

My Clock Config:

0693W00000QKClhQAH.png(I tested a blink example, that runs fine without STLink, and 1 seconds pulse)

my Main.cpp:

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();
  MX_GPIO_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_ADC1_Init();
  MX_CAN1_Init();
  MX_USART2_UART_Init();
  MX_RTC_Init();
  /* USER CODE BEGIN 2 */
  //initGpio();
 
  RFM23 *rfm23 = new RFM23();
 
  /* USER CODE END 2 */
 
  /* Init scheduler */
  osKernelInitialize();
 
  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */
 
  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */
 
  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */
 
  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */
 
  /* Create the thread(s) */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
 
  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */
 
  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
 
  if(rfm23->init())
  {
	  radioTaskHandle = osThreadNew(StartRadioTask, NULL, &radioTask_attributes);
  }
  else
  {
	  HAL_GPIO_WritePin(LED_ERROR_GPIO_Port, LED_ERROR_Pin, GPIO_PIN_SET);
  }
 
  /* USER CODE END RTOS_EVENTS */
 
  /* Start scheduler */
  osKernelStart();
 
  /* We should never get here as control is now taken by the scheduler */
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
 
  while (1)
  {
    /* 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};
 
  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
 
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 72;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 3;
  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_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

Thank you :)

Regards Daniel

3 REPLIES 3

Why do you think this has anything to do with SPI?

Debug in the usual way: toggle a pin at strategic positions of the code, e.g. between calls to individual initialization functions, and observe using oscilloscope/LA.

JW

my code only contents SPI things…

and the clockrate is 8MHZ and the same code with stduino+platformio works without problems… and by the spi things, are many different….

> my code only contents SPI things…

This

MX_GPIO_Init();
  MX_ADC1_Init();
  MX_CAN1_Init();
  MX_USART2_UART_Init();
  MX_RTC_Init();
  /* USER CODE BEGIN 2 */
  //initGpio();
 
  RFM23 *rfm23 = new RFM23();
 
  /* USER CODE END 2 */
 
  /* Init scheduler */
  osKernelInitialize();
[etc.]

to me does not sound like "only SPI".

Debug in the usual way: toggle a pin at strategic positions of the code, e.g. between calls to individual initialization functions, and observe using oscilloscope/LA.

JW