cancel
Showing results for 
Search instead for 
Did you mean: 

On board SPI to SPI in STM32F207ZGT6

vbk22398
Associate III

#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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Sarra.S
ST Employee

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)

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

1 REPLY 1
Sarra.S
ST Employee

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)

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.