cancel
Showing results for 
Search instead for 
Did you mean: 

Stm32F412 SPI interrupt full duplex slave.

Kpand.1
Associate II

Hi I am working on stm32F4 on spi communication. I am using stm32F4 as slave module and i have other qualcomm board as master module. as in master module there are 5 pins as MOSI,MISO,SCK,CS,INTR. when I am generating the code for stm32f4 using cubemx there are only 4 pins as MOSI,MISO,SCK,NSS. Now I dont know where to connect INT(interrupt pin) in stm32.

as I am very new in spi communication please help.

8 REPLIES 8
Jack Peacock_2
Senior III

The slave board uses the master board INTR interrupt request to notify the master board it has data available. Use a GPIO pin set to output to drive INTR. When you have prepared the SPI slave buffer and DMA is initialized for the transfer then assert INTR. The master will perform a data transfer. When your local DMA TC flag asserts the transfer is complete and you deassert the INTR request.

Jack Peacock

Hi Jack thanks for quick reply,

as per your suggestion i made pd2 pin as gpio_output pin and generated code as below i made changes in main.c as below

MISO->MISO

MOSI->MOSI

CS->NSS

SCK->CLK

INTR->PD2

but its not working as expected.

main.c

/**

 ******************************************************************************

 * File Name     : main.c

 * Description    : Main program body

 ******************************************************************************

 *

 * COPYRIGHT(c) 2020 STMicroelectronics

 *

 * Redistribution and use in source and binary forms, with or without modification,

 * are permitted provided that the following conditions are met:

 *  1. Redistributions of source code must retain the above copyright notice,

 *   this list of conditions and the following disclaimer.

 *  2. Redistributions in binary form must reproduce the above copyright notice,

 *   this list of conditions and the following disclaimer in the documentation

 *   and/or other materials provided with the distribution.

 *  3. Neither the name of STMicroelectronics nor the names of its contributors

 *   may be used to endorse or promote products derived from this software

 *   without specific prior written permission.

 *

 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"

 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE

 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE

 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE

 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL

 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR

 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER

 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,

 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE

 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 *

 ******************************************************************************

 */

/* Includes ------------------------------------------------------------------*/

#include "main.h"

#include "stm32f4xx_hal.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/

SPI_HandleTypeDef hspi2;

/* USER CODE BEGIN PV */

/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

void Error_Handler(void);

static void MX_GPIO_Init(void);

static void MX_SPI2_Init(void);

uint8_t buff_tx[10]={31,32,33,34,35,36,37,38,39};

uint8_t buff_rx[10];

/* USER CODE BEGIN PFP */

/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

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();

 /* Configure the system clock */

 SystemClock_Config();

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_SPI2_Init();

 /* USER CODE BEGIN 2 */

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

 /* USER CODE END WHILE */

   

  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);

  HAL_Delay(500);

  HAL_SPI_Receive(&hspi2,buff_rx,10,1000);

  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);

  HAL_Delay(500);

 /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

/** System Clock Configuration

*/

void SystemClock_Config(void)

{

 RCC_OscInitTypeDef RCC_OscInitStruct;

  /**Configure the main internal regulator output voltage 

  */

 __HAL_RCC_PWR_CLK_ENABLE();

 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /**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();

 }

  /**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);

}

/* SPI2 init function */

static void MX_SPI2_Init(void)

{

 hspi2.Instance = SPI2;

 hspi2.Init.Mode = SPI_MODE_SLAVE;

 hspi2.Init.Direction = SPI_DIRECTION_2LINES;

 hspi2.Init.DataSize = SPI_DATASIZE_8BIT;

 hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;

 hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;

 hspi2.Init.NSS = SPI_NSS_HARD_INPUT;

 hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;

 hspi2.Init.TIMode = SPI_TIMODE_DISABLE;

 hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

 hspi2.Init.CRCPolynomial = 10;

 if (HAL_SPI_Init(&hspi2) != HAL_OK)

 {

  Error_Handler();

 }

}

/** 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_GPIOB_CLK_ENABLE();

 __HAL_RCC_GPIOD_CLK_ENABLE();

 /*Configure GPIO pin Output Level */

 HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);

 /*Configure GPIO pin : PD2 */

 GPIO_InitStruct.Pin = GPIO_PIN_2;

 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

 GPIO_InitStruct.Pull = GPIO_NOPULL;

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

 HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

}

/* 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****/

berendi
Principal

> its not working as expected.

What is expected?

Hi berendi thanks for reply,

As expected it should receive the data send by host. But even host gets stuck and there is nothing coming to slave side.

> i have other qualcomm board as master module

What is it?

Why do you think it should communicate as SPI master?

Any link to datasheet or manual?

JW

Hi wwaclawek thanks for reply,

Other board is Qualcomm qca4020 which is working as a master. I already have tested with both master and slave as two same qca4020 Qualcomm boards and that is working fine. Now I just want to replace that slave board with stm32. As in two Qualcomm board as master and slave spi communication is working so I just replaced the slave board with stm and didn't touch anything on the master side.

LMI2
Lead

Have you tried connecting MISO and MOSI from slave board together. Then you test your TX and RX code there.

MISO->MISO

MOSI->MOSI

CS->NSS

SCK->CLK

INTR->PD2

surely GND also.

I have had SPI problems, I loaned a logic analyzer to see what is going on with HW.

Hi lmi thanks for reply. I made connection exactly as

Below

MISO->MISO

MOSI->MOSI

CS->NSS

SCK->CLK

INTR->PD2

GND->GND