cancel
Showing results for 
Search instead for 
Did you mean: 

SPI Loopback test on stm32h563zit6

Berlin-raj123
Associate III

HI, STM Experts,

                Using STM32CubeIDE, I configured a project with default settings for the STM32H563ZIT6 (8 MHz external clock), running at 250 MHz. Additionally, I've enabled SPI1 for full duplex communication (transmitting and receiving simultaneously), with a data size of 8 bits, clock polarity (clk pol) = 0, clock phase (clk ph) = 1, and set the prescaler to 2, resulting in a baud rate of 125.0 Mbits/s. It's usually possible to wire MOSI to MISO. However, when I try to receive data from SPI1, it doesn't work. I don't know where I made a mistake. Could anyone please support me?

uint8_t tx_buffer[] = { 'H', 'e', 'l', 'l', 'o', '\r', '\n' };
uint8_t rx_buffer[20];

while (1)
   {
	  //HAL_UART_Transmit(&huart7, tx_buffer, sizeof(tx_buffer), 100);
     HAL_Delay(1000);
     for (int i = 0; i < sizeof(tx_buffer); i++)
     {
       HAL_SPI_TransmitReceive(&hspi1, tx_buffer + i, rx_buffer, 1, 100);
       HAL_UART_Transmit(&huart7, rx_buffer, sizeof(rx_buffer), 100);
       HAL_Delay(200);
     }
   }
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_1EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  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 = 0x7;
  hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
  hspi1.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
  hspi1.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
  hspi1.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
  hspi1.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
  hspi1.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
  hspi1.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
  hspi1.Init.IOSwap = SPI_IO_SWAP_DISABLE;
  hspi1.Init.ReadyMasterManagement = SPI_RDY_MASTER_MANAGEMENT_INTERNALLY;
  hspi1.Init.ReadyPolarity = SPI_RDY_POLARITY_HIGH;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
}
14 REPLIES 14

Can you show us what you do?

#include "main.h"
#include "string.h"
#include "stdbool.h"

SPI_HandleTypeDef hspi1;
SPI_HandleTypeDef hspi2;

UART_HandleTypeDef huart7;

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ICACHE_Init(void);
static void MX_SPI1_Init(void);
static void MX_UART7_Init(void);
static void MX_SPI2_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

uint8_t tx_buffer[] = "Hitman Sharma\r\n";
uint8_t rx_buffer[20];
bool rx_complete = false;
int main(void)
{
    /* MCU Configuration--------------------------------------------------------*/
    HAL_Init();
    SystemClock_Config();

    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_ICACHE_Init();
    MX_UART7_Init();
    MX_SPI1_Init();
    MX_SPI2_Init();

    while (1)
    {
    	HAL_Delay(1000);

    	HAL_SPI_Receive_IT(&hspi1, rx_buffer, sizeof(rx_buffer));

    	HAL_SPI_Transmit(&hspi1, tx_buffer, sizeof(tx_buffer), 100);

    	HAL_Delay(200);
    	if(rx_complete == true)
    	{
          HAL_UART_Transmit(&huart7, rx_buffer, sizeof(rx_buffer), 100);
    	}
    }
}
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
{
  if(hspi->Instance == SPI1)
  {
    rx_complete = true;
  }
}
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_1EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 0x7;
  hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
  hspi1.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
  hspi1.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
  hspi1.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
  hspi1.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
  hspi1.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
  hspi1.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
  hspi1.Init.IOSwap = SPI_IO_SWAP_DISABLE;
  hspi1.Init.ReadyMasterManagement = SPI_RDY_MASTER_MANAGEMENT_INTERNALLY;
  hspi1.Init.ReadyPolarity = SPI_RDY_POLARITY_HIGH;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI1_Init 2 */

  /* USER CODE END SPI1_Init 2 */

}

You're sending 16 bytes, and waiting until 20 are received. Can you see why this is not working?

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

sorry for my late reply

uint8_t tx_buffer[] = "Hitman Sharma\r\n";

uint8_t rx_buffer[16];

bool rx_complete = false;

i changed its not working

Try uint8_t rx_buffer[15];