Showing results for 
Search instead for 
Did you mean: 

SPI2 STM32H745

Associate III

Hi everyone 

I can find help on how to connect SPI2 STM32H745. 

I have read the datasheet and did everything written on it.

Please can someone help me 

Chief II


>I have read the datasheet and did everything written on it.

What you did ?

>how to connect SPI2 STM32H745

In Cube - you set : SPI2 ? 

If you feel a post has answered your question, please click "Accept as Solution".
ST Employee

Hello @fru999 

Below is an example of how you might set up and use SPI2 with the HAL library on an STM32H745 microcontroller.


Step 1: Initialize SPI2
First, you need to create an SPI_HandleTypeDef structure and initialize it with the desired SPI parameters.

#include "stm32h7xx_hal.h"

SPI_HandleTypeDef hspi2;

void SPI2_Init(void) {
    // SPI2 parameter configuration
    hspi2.Instance = SPI2;
    hspi2.Init.Mode = SPI_MODE_MASTER;
    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_SOFT;
    hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16; // Adjust the prescaler value as needed
    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) {
        // Initialization Error

void Error_Handler(void) {
    // User can add his own implementation to report the HAL error return state
    while(1) {

Step 2: Configure GPIO Pins for SPI2
You need to configure the GPIO pins for SPI2. This is typically done in the HAL_SPI_MspInit function, which is called by HAL_SPI_Init.

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi) {
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    if(hspi->Instance==SPI2) {
        // Enable clocks for SPI2 and GPIO

        // SPI2 GPIO Configuration
        // PB13     ------> SPI2_SCK
        // PB14     ------> SPI2_MISO
        // PB15     ------> SPI2_MOSI
        GPIO_InitStruct.Pin = GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
        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_SPI2;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

Step 3: Transmit and Receive Data
To transmit and receive data using the HAL library, you can use the HAL_SPI_Transmit, HAL_SPI_Receive, or HAL_SPI_TransmitReceive functions.

uint8_t data_to_send = 0xAA; // Example data to send
uint8_t received_data = 0x00;

// Transmit data
if (HAL_SPI_Transmit(&hspi2, &data_to_send, 1, HAL_MAX_DELAY) != HAL_OK) {
    // Transmission Error

// Receive data
if (HAL_SPI_Receive(&hspi2, &received_data, 1, HAL_MAX_DELAY) != HAL_OK) {
    // Reception Error


For configuring the SPI2 peripheral on your STM32H745, I recommend using STM32CubeMX. This tool simplifies peripheral setup and pin configuration, ensuring an error-free initialization process.




If your question is answered, please close this topic by clicking "Accept as Solution".


I initialize the SPI throught the CUBE MX 

I have my SPI_init and everything OSAKE.1 talks about but I have the impression my PIN are not the good ones.

The usual test for this: disable SPI setting, set PIN as gpio output and write in small loop hi/lo to the pin. Check with scope, what is on the pin then.

If you feel a post has answered your question, please click "Accept as Solution".