STM32 SPI troubles (f7-nucleo) - Page 1
Forum section: electronics / general

author: init
author_rank: Contributor
timestamp: December 14, 2016, 12:04:05 am
content: I'm trying to get the SPI1 bus on the STM32f767ZI nucleo board working using CubeMX and System Workbench, to no avail. What I am doing is setting up the necessary initialisation processes for SPI1 then sending some basic hex values to the bus to view on an oscilloscope. During data transmission, I can see the SCK pulsing to the correct number of bits for the transfer but MOSI has no data on it at all. I have followed the guide fromSTM32 documentationbut SPI1 doesn't seem to work.The code is as follows:main.cCode:/**/* Includes ------------------------------------------------------------------*/#include "main.h"#include <TFT.h>#include "stm32f7xx_hal.h"SPI_HandleTypeDef hspi1;void SystemClock_Config(void);void Error_Handler(void);static void MX_GPIO_Init(void);static void MX_SPI1_Init(void);int main(void){HAL_Init();SystemClock_Config();MX_GPIO_Init();MX_SPI1_Init();uint8_t xy = {0x23,0x45,0x7A,0xE6};begin();while (1){HAL_SPI_Transmit(&hspi1,(uint8_t*)xy,4,1000);while(HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);}}/** System Clock Configuration*/void SystemClock_Config(void){RCC_OscInitTypeDef RCC_OscInitStruct;RCC_ClkInitTypeDef RCC_ClkInitStruct;RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;/**Configure the main internal regulator output voltage*/__HAL_RCC_PWR_CLK_ENABLE();__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);/**Initializes the CPU, AHB and APB busses clocks*/RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;RCC_OscInitStruct.HSIState = RCC_HSI_ON;RCC_OscInitStruct.HSICalibrationValue = 16;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;RCC_OscInitStruct.PLL.PLLM = 8;RCC_OscInitStruct.PLL.PLLN = 108;RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV6;RCC_OscInitStruct.PLL.PLLQ = 2;if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){Error_Handler();}/**Initializes the CPU, AHB and APB busses 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_DIV4;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK){Error_Handler();}PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C2;PeriphClkInitStruct.I2c2ClockSelection = RCC_I2C2CLKSOURCE_PCLK1;if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK){Error_Handler();}/**Configure the Systick interrupt time*/HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);/**Configure the Systick*/HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);/* SysTick_IRQn interrupt configuration */HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);}/* SPI1 init function */static void MX_SPI1_Init(void){hspi1.Instance = SPI1;hspi1.Init.Mode = SPI_MODE_MASTER;hspi1.Init.Direction = SPI_DIRECTION_2LINES;hspi1.Init.DataSize = SPI_DATASIZE_8BIT;hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;hspi1.Init.NSS = SPI_NSS_SOFT;hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;hspi1.Init.TIMode = SPI_TIMODE_DISABLE;hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;hspi1.Init.CRCPolynomial = 7;hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;hspi1.Init.NSSPMode = SPI_NSS_SOFT;HAL_SPI_MspInit(&hspi1);if (HAL_SPI_Init(&hspi1) != HAL_OK){Error_Handler();}}/** Configure pins as* Analog* Input* Output* EVENT_OUT* EXTIPD8 ------> USART3_TXPD9 ------> USART3_RXPA8 ------> USB_OTG_FS_SOFPA9 ------> USB_OTG_FS_VBUSPA10 ------> USB_OTG_FS_IDPA11 ------> USB_OTG_FS_DMPA12 ------> USB_OTG_FS_DP*/static void MX_GPIO_Init(void){GPIO_InitTypeDef GPIO_InitStruct;/* GPIO Ports Clock Enable */__HAL_RCC_GPIOE_CLK_ENABLE();__HAL_RCC_GPIOC_CLK_ENABLE();__HAL_RCC_GPIOF_CLK_ENABLE();__HAL_RCC_GPIOH_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();__HAL_RCC_GPIOB_CLK_ENABLE();__HAL_RCC_GPIOD_CLK_ENABLE();__HAL_RCC_GPIOG_CLK_ENABLE();/*Configure GPIO pin : User_Blue_Button_Pin */GPIO_InitStruct.Pin = User_Blue_Button_Pin;GPIO_InitStruct.Mode = GPIO_MODE_INPUT;GPIO_InitStruct.Pull = GPIO_NOPULL;HAL_GPIO_Init(User_Blue_Button_GPIO_Port, &GPIO_InitStruct);/*Configure GPIO pin : PF12 */GPIO_InitStruct.Pin = GPIO_PIN_12;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);/*Configure GPIO pins : LD3_Pin LD2_Pin */GPIO_InitStruct.Pin = LD3_Pin|LD2_Pin;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);/*Configure GPIO pins : STLK_RX_Pin STLK_TX_Pin */GPIO_InitStruct.Pin = STLK_RX_Pin|STLK_TX_Pin;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;GPIO_InitStruct.Alternate = GPIO_AF7_USART3;HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);/*Configure GPIO pin : USB_PowerSwitchOn_Pin */GPIO_InitStruct.Pin = USB_PowerSwitchOn_Pin;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;HAL_GPIO_Init(USB_PowerSwitchOn_GPIO_Port, &GPIO_InitStruct);/*Configure GPIO pin : USB_OverCurrent_Pin */GPIO_InitStruct.Pin = USB_OverCurrent_Pin;GPIO_InitStruct.Mode = GPIO_MODE_INPUT;GPIO_InitStruct.Pull = GPIO_NOPULL;HAL_GPIO_Init(USB_OverCurrent_GPIO_Port, &GPIO_InitStruct);/*Configure GPIO pins : USB_SOF_Pin USB_ID_Pin USB_DM_Pin USB_DP_Pin */GPIO_InitStruct.Pin = USB_SOF_Pin|USB_ID_Pin|USB_DM_Pin|USB_DP_Pin;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);/*Configure GPIO pin : USB_VBUS_Pin */GPIO_InitStruct.Pin = USB_VBUS_Pin;GPIO_InitStruct.Mode = GPIO_MODE_INPUT;GPIO_InitStruct.Pull = GPIO_NOPULL;HAL_GPIO_Init(USB_VBUS_GPIO_Port, &GPIO_InitStruct);/*Configure GPIO pin Output Level */HAL_GPIO_WritePin(GPIOF, GPIO_PIN_12, GPIO_PIN_RESET);/*Configure GPIO pin Output Level */HAL_GPIO_WritePin(GPIOB, LD3_Pin|LD2_Pin, GPIO_PIN_RESET);/*Configure GPIO pin Output Level */HAL_GPIO_WritePin(USB_PowerSwitchOn_GPIO_Port, USB_PowerSwitchOn_Pin, GPIO_PIN_RESET);}/* USER CODE BEGIN 4 *//* USER CODE END 4 *//*** @brief This function is executed in case of error occurrence.* @param None* @retval None*/void Error_Handler(void){/* USER CODE BEGIN Error_Handler *//* User can add his own implementation to report the HAL error return state */while(1){}/* USER CODE END Error_Handler */}#ifdef USE_FULL_ASSERT/*** @brief Reports the name of the source file and the source line number* where the assert_param error has occurred.* @param file: pointer to the source file name* @param line: assert_param error line source number* @retval None*/void assert_failed(uint8_t* file, uint32_t line){/* USER CODE BEGIN 6 *//* User can add his own implementation to report the file name and line number,ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* USER CODE END 6 */}#endif/*** @}*//*** @}*//************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/And the low level initialisation function HAL_SPI_MspInit() in stm32f7xx_hal_msp.c is:Code:void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi){GPIO_InitTypeDef GPIO_InitStruct;if(hspi->Instance==SPI1){/* USER CODE BEGIN SPI1_MspInit 0 *//* USER CODE END SPI1_MspInit 0 *//* Peripheral clock enable */__HAL_RCC_SPI1_CLK_ENABLE();/**SPI1 GPIO ConfigurationPA5 ------> SPI1_SCKPA6 ------> SPI1_MISOPD7 ------> SPI1_MOSI*/GPIO_InitStruct.Pin = GPIO_PIN_5;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_SPI1;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);GPIO_InitStruct.Pin = GPIO_PIN_6;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_SPI1;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);GPIO_InitStruct.Pin = GPIO_PIN_7;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_SPI1;HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);/* USER CODE BEGIN SPI1_MspInit 1 *//* USER CODE END SPI1_MspInit 1 */}}An example of my oscilloscope measurement of a 4 byte data transfer is shown below where yellow is SCK and blue is MOSI.Does anyone have an idea why I'm not seeing data on MOSI during the transfer?

author: dgtl
author_rank: Regular Contributor
timestamp: December 14, 2016, 09:28:53 am
content: Are you probing the right pin (in the code, it looks that the MOSI is muxed to PD7, not PA7). Maybe try setting the pin via GPIO and see if the hardware works and that you can see the pin move, only then continue with SPI.

author: init
author_rank: Contributor
timestamp: December 15, 2016, 12:07:56 am
content: Quote from: dgtl on December 14, 2016, 09:28:53 amThank you so much, I assumed that CubeMX had set the MOSI pin to be (logically) the one next to the other SPI1 peripherals that are labelled on the silkscreen in a line. I have no idea why it set it to PD7 (which is on an entirely different connector bus) instead of PA7 but changing it to PA7 works now.