2025-01-15 07:14 PM - edited 2025-01-15 07:20 PM
I am writing sample demo Application to test data transfer between SPI Master(Nucleo F411RE) and SPI Slavee(Nucleo H755ZI-Q) device.
Master SPI:
/**SPI2 GPIO Configuration
PC2 ------> SPI2_MISO
PC3 ------> SPI2_MOSI
PB10 ------> SPI2_SCK
Slave SPI:
/**SPI1 GPIO Configuration
PA5 ------> SPI1_SCK
PA6 ------> SPI1_MISO
PB5 ------> SPI1_MOSI
For validating what data Slave SPI device receiving over SPI I am sending this data over UART to display it on Host PC
Here is SLAVE SPI device UART response:
What I am seeing is that SPI receives wrong data instead of 0x41 (Send by master)
Slave Received SPI data: 0x0D
Slave Transmit SPI data: 0xAA
Slave Received SPI data: 0x0D
Slave Transmit SPI data: 0xAA
Slave Received SPI data: 0x0D
Slave Transmit SPI data: 0xAA
Slave Received SPI data: 0x0D
Slave Transmit SPI data: 0xAA
Slave Received SPI data: 0x0D
Slave Transmit SPI data: 0xAA
Slave Received SPI data: 0x0D
Slave Transmit SPI data: 0xAA
Slave Received SPI data: 0x0D
Slave Transmit SPI data: 0xAA
Slave Received SPI data: 0x0D
Slave Transmit SPI data: 0xAA
Slave Received SPI data: 0x0D
Slave Transmit SPI data: 0xAA
Slave Received SPI data: 0x0D
Slave Transmit SPI data: 0xAA
Slave Received SPI data: 0x0D
Slave Transmit SPI data: 0xAA
Slave Received SPI data: 0x0D
Master SPI Code:
char message = 0x41;
int counter = 0;
while (1)
{
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
HAL_SPI_Transmit(&hspi2, message, 1, HAL_MAX_DELAY);
HAL_Delay(500);
counter++;
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
static void MX_SPI2_Init(void)
{
/* USER CODE BEGIN SPI2_Init 0 */
/* USER CODE END SPI2_Init 0 */
/* USER CODE BEGIN SPI2_Init 1 */
/* USER CODE END SPI2_Init 1 */
/* 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_32;
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();
}
/* USER CODE BEGIN SPI2_Init 2 */
/* USER CODE END SPI2_Init 2 */
}
SLAVE SPI code:
uint8_t txData = 0xAA; // Data to send to the master
uint8_t rxData = 0x00; // Buffer to store received data
char tx_uart_buff[50];
while (1)
{
// Wait for data from the Master
if (HAL_SPI_Receive(&hspi1, &rxData, 1, HAL_MAX_DELAY) == HAL_OK) {
// Data received successfully, respond to master
snprintf(tx_uart_buff, sizeof(tx_uart_buff), "Slave Received SPI data: 0x%02X\r\n", rxData);
HAL_UART_Transmit(&huart3,&tx_uart_buff,sizeof(tx_uart_buff),10);
HAL_GPIO_TogglePin(LD1_GPIO_Port, LD1_Pin); // Example: Toggle an LED
HAL_SPI_Transmit(&hspi1, &txData, 1, HAL_MAX_DELAY); // Send response to the master
snprintf(tx_uart_buff, sizeof(tx_uart_buff), "Slave Transmit SPI data: 0x%02X\r\n", txData);
HAL_UART_Transmit(&huart3,&tx_uart_buff,sizeof(tx_uart_buff),10);
} else {
snprintf(tx_uart_buff, sizeof(tx_uart_buff), "Slave Receive SPI error\r\n");
HAL_UART_Transmit(&huart3,&tx_uart_buff,sizeof(tx_uart_buff),10);
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
static void MX_SPI1_Init(void)
{
/* USER CODE BEGIN SPI1_Init 0 */
/* USER CODE END SPI1_Init 0 */
/* USER CODE BEGIN SPI1_Init 1 */
/* USER CODE END SPI1_Init 1 */
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_SLAVE;
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.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 0x0;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
hspi1.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
hspi1.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
hspi1.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
hspi1.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
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;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI1_Init 2 */
/* USER CODE END SPI1_Init 2 */
}
I have also attached full Master and Slave CubeIDE Project.
If anyone can help me to figure out issue here as SPI Slave always reads wrong data.
@Andrew Neil @tjaekel