Problem with SPI Between 2 STM32F103 MCU [SOLVED] - Page 1
Forum section: electronics / general

author: igtaba
author_rank: Newbie
timestamp: December 01, 2018, 10:28:43 pm
content: Hello People, this is my first post so be gentle please!I'm trying to do a SPI communication between two STM32F103 Processors. The idea is that the Master is sending values to the Slave in order for the Slave to move a Motor with PWM but I can't make it work in anyway. I need this to work for a university project so I beg for help here.I'm using the STM CubeMX to make the template and the programming is done in KEIL uVision 5. Also, I'm using the HAL Libraries (since my teammate has done the PWM Code with that)The pin connection is the next one: SPI1 SCK ----> SPI1 SCKSPI1 MOSI --> SPI1 MOSISPI1 MISO --> SPI1 MISOGPIOA.Pin4 ->SPI1 NSS InputThe master code "seems" to be working fine since is a simple test program. The SPI1 is initialized by the CubeMX, and I select the option "Transmit ONLY master". The code is below:Code:/* Includes ------------------------------------------------------------------*/#include "main.h"#include "stm32f1xx_hal.h"/* Private variables ---------------------------------------------------------*/SPI_HandleTypeDef hspi1;/* Private function prototypes -----------------------------------------------*/void SystemClock_Config(void);static void MX_GPIO_Init(void);static void MX_SPI1_Init(void);int main(void){/* MCU Configuration----------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* Configure the system clock */SystemClock_Config();/* Initialize all configured peripherals */MX_SPI1_Init();MX_GPIO_Init();/* USER CODE BEGIN 2 */uint8_t DataOut1, DataOut2, DataOut3;DataOut1 = 0xFF;DataOut2 = 0x1F;DataOut3 = 0x00;HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET);/* USER CODE END 2 *//* Infinite loop */while (1){//SPI TRANSMIT RECEIVE CODE (CubeMX set to TRANSMIT ONLY MASTER mode)HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);//CS LOWHAL_SPI_Transmit(&hspi1, &DataOut2 , sizeof(DataOut1),HAL_MAX_DELAY);HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET);//CS_HIGHHAL_Delay(10);HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);HAL_Delay(250);HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);HAL_Delay(150);HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);HAL_Delay(250);HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);HAL_Delay(150);}}void SystemClock_Config(void){RCC_OscInitTypeDef RCC_OscInitStruct;RCC_ClkInitTypeDef RCC_ClkInitStruct;/**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_NONE;if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){_Error_Handler(__FILE__, __LINE__);}/**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_HSI;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_0) != HAL_OK){_Error_Handler(__FILE__, __LINE__);}/**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){static void MX_SPI1_Init(void){/* SPI1 parameter configuration*/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_2EDGE;hspi1.Init.NSS = SPI_NSS_HARD_OUTPUT;hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;hspi1.Init.TIMode = SPI_TIMODE_DISABLE;hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;hspi1.Init.CRCPolynomial = 10;if (HAL_SPI_Init(&hspi1) != HAL_OK){_Error_Handler(__FILE__, __LINE__);}}/** Configure pins as* Analog* Input* Output* EVENT_OUT* EXTI*/static void MX_GPIO_Init(void){GPIO_InitTypeDef GPIO_InitStruct;/* GPIO Ports Clock Enable */__HAL_RCC_GPIOC_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();__HAL_RCC_GPIOB_CLK_ENABLE();/*Configure GPIO pin Output Level */HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);/*Configure GPIO pin Output Level */HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET);/*Configure GPIO pin Output Level */HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);/*Configure GPIO pin : PC13 */GPIO_InitStruct.Pin = GPIO_PIN_13;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);/*Configure GPIO pin : PA3 */GPIO_InitStruct.Pin = GPIO_PIN_3;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);/*Configure GPIO pins : PB5 PB6 PB7 */GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;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);}/*** @brief This function is executed in case of error occurrence.* @param file: The file name as string.* @param line: The line in file as a number.* @retval None*/void _Error_Handler(char *file, int line){/* USER CODE BEGIN Error_Handler_Debug *//* User can add his own implementation to report the HAL error return state */while(1){}/* USER CODE END Error_Handler_Debug */}#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,tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* USER CODE END 6 */}#endif /* USE_FULL_ASSERT */The idea is to send different DATAOUT values to test if the Slave is reading the information rightly coz I don't have an oscilloscope.The code for the slave below is where I don't understand when or sometimes how to read the data that has been wrote in the RX buffer by the MasterThe config in the CubeMX is for the SPI1 "Transmit ONLY SLAVE"Code:/* Includes ------------------------------------------------------------------*/#include "main.h"#include "stm32f1xx_hal.h"/* Private variables ---------------------------------------------------------*/SPI_HandleTypeDef hspi1;/* Private function prototypes -----------------------------------------------*/void SystemClock_Config(void);static void MX_GPIO_Init(void);static void MX_SPI1_Init(void);int main(void){/* MCU Configuration----------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* Configure the system clock */SystemClock_Config();/* Initialize all configured peripherals */MX_GPIO_Init();MX_SPI1_Init();HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);HAL_Delay(500);HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);uint8_t DataIn;DataIn = 0x00;/* Infinite loop *//* USER CODE BEGIN WHILE */while (1){//Wait until the SPI FLAG is set (A byte has been received right?)while (__HAL_SPI_GET_FLAG(&hspi1, SPI_FLAG_RXNE) == RESET);//Fetch the data from the buffer and put it in DataInHAL_SPI_Receive(&hspi1, &DataIn, 1,100);//Turn ON a LED according to the value of DataInif(DataIn == 0x00){HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);HAL_Delay(200);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);HAL_Delay(100);}if(DataIn == 0x1F){HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);HAL_Delay(200);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);HAL_Delay(100);}if(DataIn == 0xFF){HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_SET);HAL_Delay(200);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET);HAL_Delay(100);}else{HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);HAL_Delay(250);HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);HAL_Delay(150);HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);HAL_Delay(250);HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);HAL_Delay(150);}}/* USER CODE END 3 */}/*** @brief System Clock Configuration* @retval None*/void SystemClock_Config(void){RCC_OscInitTypeDef RCC_OscInitStruct;RCC_ClkInitTypeDef RCC_ClkInitStruct;/**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_NONE;if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){_Error_Handler(__FILE__, __LINE__);}/**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_HSI;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_0) != HAL_OK){_Error_Handler(__FILE__, __LINE__);}/**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){/* SPI1 parameter configuration*/hspi1.Instance = SPI1;hspi1.Init.Mode = SPI_MODE_SLAVE;hspi1.Init.Direction = SPI_DIRECTION_2LINES;hspi1.Init.DataSize = SPI_DATASIZE_8BIT;hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;hspi1.Init.NSS = SPI_NSS_HARD_INPUT;hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;hspi1.Init.TIMode = SPI_TIMODE_DISABLE;hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;hspi1.Init.CRCPolynomial = 10;if (HAL_SPI_Init(&hspi1) != HAL_OK){_Error_Handler(__FILE__, __LINE__);}}/** Configure pins as* Analog* Input* Output* EVENT_OUT* EXTI*/static void MX_GPIO_Init(void){GPIO_InitTypeDef GPIO_InitStruct;/* GPIO Ports Clock Enable */__HAL_RCC_GPIOC_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();__HAL_RCC_GPIOB_CLK_ENABLE();/*Configure GPIO pin Output Level */HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);/*Configure GPIO pin Output Level */HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET);/*Configure GPIO pin Output Level */HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);/*Configure GPIO pin : PC13 */GPIO_InitStruct.Pin = GPIO_PIN_13;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);/*Configure GPIO pin : PA3 */GPIO_InitStruct.Pin = GPIO_PIN_3;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);/*Configure GPIO pins : PB5 PB6 PB7 */GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;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);}/* USER CODE BEGIN 4 *//* USER CODE END 4 *//*** @brief This function is executed in case of error occurrence.* @param file: The file name as string.* @param line: The line in file as a number.* @retval None*/void _Error_Handler(char *file, int line){/* USER CODE BEGIN Error_Handler_Debug *//* User can add his own implementation to report the HAL error return state */while(1){}/* USER CODE END Error_Handler_Debug */}#ifdef USE_FULL_ASSERTCan someone help on how to read the data that has been wrote in the RXBuffer of the Slave?The master is at least flashing the LED as expect, but the Slave just turn on a LED and stay there freezedSorry for my awful English, is not my first language

author: mikerj
author_rank: Super Contributor
timestamp: December 01, 2018, 11:39:03 pm
content: Quote from: igtaba on December 01, 2018, 10:28:43 pmSince your slave is only receiving, this seems like a good place to start!

author: igtaba
author_rank: Newbie
timestamp: December 02, 2018, 12:15:54 am
content: Thanks , I have changed that to "RECEIVE ONLY SLAVE" in the CubeMX (to myself) but this is not working, Is the logic of my simple code right in the Slave side of things?

author: newbrain
author_rank: Super Contributor
timestamp: December 02, 2018, 03:27:01 pm
content: Quote from: igtaba on December 02, 2018, 12:15:54 amThere only one thing that is redundant, rather than wrong: no need to check for RXNE flag, HAL_SPI_Receive() will do that for you.Since the code is sitting there waiting for the flag, just pass HAL_MAX_DELAY as the timeout value.That said, I find this strange (emphasis mine):Quote from: igtaba on December 01, 2018, 10:28:43 pmI see no path in the posted code where this could happen: all the GPIO manipulations end with a reset.Have you tried following the slave code in a debugger?

author: igtaba
author_rank: Newbie
timestamp: December 02, 2018, 09:28:10 pm
content: Quote from: newbrain on December 02, 2018, 03:27:01 pmI was using the HAL_MAX_DELAY before, but the idea in using the flag was because I wasn't sure the HAL was doing it, i put a certain valueQuote from: newbrain on December 02, 2018, 03:27:01 pmI have a ST Link debugger (a Chinese one I think) and I can't do anything once I enter the debug session, so I'm programming blind with no debugging. But the problem was: the CubeMX didn't initialized the PC13 LED GPIO, so now I can send one byte but then in the second run at the while(1) it gets stuck in the while for the RXNE flag, so I'm gonna try removing that line and letting the HAL_SPI_Receive handle that.Thanks for the answer, and after the try out I would be reporting the results o asking more newbiee questions, thanks again!

author: igtaba
author_rank: Newbie
timestamp: December 03, 2018, 01:00:48 pm
content: Quote from: newbrain on December 02, 2018, 03:27:01 pmI've the solution to my problem, and as you just said, it was the RXNE Flag that was being set perpetually by the while and since is just the HAL_SPI_Receive() the communication is been done without a flaw. Thanks Again.PD: how can i close the post and select your reply like the answer?

author: newbrain
author_rank: Super Contributor
timestamp: December 03, 2018, 03:14:39 pm
content: Glad to see you have solved your problem!The while loop was in fact blocking (contrary to what I suggested in my answer), as HAL_SPI_Init() leaves the SPI disabled, and it will be enabled at the first HAL_SPI_Receive(), or if one does it manually with __HAL_SPI_ENABLE().This forum does not foresee to mark an answer as the solution, but it's common practice to edit the original post title adding .