Skip to main content
Graduate II
March 11, 2024
Solved

On board SPI to SPI in STM32F207ZGT6

  • March 11, 2024
  • 1 reply
  • 714 views

#include "main.h"

SPI_HandleTypeDef hspi1;

SPI_HandleTypeDef hspi3;

 

UART_HandleTypeDef huart3;

 

/* USER CODE BEGIN PV */

char spi_data_tx[1]={'1'};

char spi_data_rx[1]={'0'};

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_SPI1_Init(void);

static void MX_SPI3_Init(void);

static void MX_USART3_UART_Init(void);

int main(void)

{

 

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */

HAL_Init();

 

/* USER CODE BEGIN Init */

 

/* USER CODE END Init */

 

/* Configure the system clock */

SystemClock_Config();

 

/* USER CODE BEGIN SysInit */

 

/* USER CODE END SysInit */

 

/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_SPI1_Init();

MX_SPI3_Init();

MX_USART3_UART_Init();

/* USER CODE BEGIN 2 */

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, GPIO_PIN_SET); //CS ->High

/* USER CODE END 2 */

 

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

spi_data_rx[0]='0';

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, GPIO_PIN_RESET); //CS -> Low

HAL_SPI_Transmit(&hspi1, spi_data_tx, 1,10);

HAL_SPI_Receive(&hspi3, &spi_data_rx,1,10);

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, GPIO_PIN_SET); //CS ->High

HAL_UART_Transmit(&huart3, spi_data_rx,1,10);

/* USER CODE END WHILE */

 

/* USER CODE BEGIN 3 */

}

/* USER CODE END 3 */

}


The below code is my logic for onboard_SPI_to_SPI Communication. It is not working Properly.
If there is any mistakes, please let me know!

thank you

 

    This topic has been closed for replies.
    Best answer by Sarra.S

    Hello @vbk22398

    First, it seems that you are not checking the return values of the HAL_SPI_Transmit and HAL_SPI_Receive functions. These functions return an error code that can help you diagnose any issues with the SPI communication.

    Are you purposely transmitting and receiving only one byte of data at a time?

    >>HAL_SPI_Receive(&hspi3, &spi_data_rx,1,10);

    you're passing in &spi_data_rx instead of spi_data_rx. Since spi_data_rxis already an array, you don't need to pass in its address.

    Also, you can use HAL_SPI_TransmitRecieve as used in the STM32Cube examples SPI_FullDuplex_ComPolling for STM32F3 or SPI example with LL drivers for STM32F2.

    HAL_SPI_TransmitReceive(&SpiHandle, (uint16_t*)aTxBuffer, (uint16_t *)aRxBuffer, buffersize, uint32_t Timeout)

    1 reply

    Sarra.SAnswer
    ST Employee
    March 11, 2024

    Hello @vbk22398

    First, it seems that you are not checking the return values of the HAL_SPI_Transmit and HAL_SPI_Receive functions. These functions return an error code that can help you diagnose any issues with the SPI communication.

    Are you purposely transmitting and receiving only one byte of data at a time?

    >>HAL_SPI_Receive(&hspi3, &spi_data_rx,1,10);

    you're passing in &spi_data_rx instead of spi_data_rx. Since spi_data_rxis already an array, you don't need to pass in its address.

    Also, you can use HAL_SPI_TransmitRecieve as used in the STM32Cube examples SPI_FullDuplex_ComPolling for STM32F3 or SPI example with LL drivers for STM32F2.

    HAL_SPI_TransmitReceive(&SpiHandle, (uint16_t*)aTxBuffer, (uint16_t *)aRxBuffer, buffersize, uint32_t Timeout)