2023-01-09 05:14 AM
in my application I use stm32F407G-DISC1 as a slave. I want to use the board the spi slave full duplexes. the problem is on the first Receive I get all the data from the master then I send data and works but the second time I only get zeros even though I send data from the master.
int main(void)
{
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_DMA_Init();
MX_USART2_UART_Init();
MX_TIM14_Init();
MX_SPI1_Init();
MX_TIM13_Init();
MX_SPI2_Init();
MX_SPI3_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
uart_buf_len = sprintf(uart_buf, "RECEIVE-CMD STATE \r\n");
HAL_UART_Transmit(&huart2, (uint8_t *)uart_buf, uart_buf_len, 100);
cmd_received();
HAL_Delay(2000);
TX_DATA(help_voltage,help_speed,help_pressure,help_current,help_torc);
HAL_Delay(1000);
}
static CMD_RECIEVED_t cmd_received(void)
{
Flush_Buffer(ARR_PARAMS,BUFFERSIZE);
uart_buf_len = sprintf(uart_buf, "CMD_RECEIVE \r\n");
HAL_UART_Transmit(&huart2, (uint8_t *)uart_buf, uart_buf_len, 100);
if (HAL_SPI_GetState(&hspi1) == HAL_SPI_STATE_READY )
{
switch (HAL_SPI_Receive(&hspi1,(uint8_t*)ARR_PARAMS, BUFFERSIZE,HAL_MAX_DELAY))
{
case HAL_OK:
{
for (int i = 0 ; i < BUFFERSIZE ; i++)
{
uart_buf_len = sprintf(uart_buf, "Recieved Params = %f \r\n",ARR_PARAMS[i]);
HAL_UART_Transmit(&huart2, (uint8_t *)uart_buf, uart_buf_len, 100);
}
}
break;
case HAL_ERROR:
Error_Handler();
break;
case HAL_BUSY:
uart_buf_len = sprintf(uart_buf, "SPI Bus Busy \r\n");
HAL_UART_Transmit(&huart2, (uint8_t *)uart_buf, uart_buf_len, 100);
break;
case HAL_TIMEOUT:
uart_buf_len = sprintf(uart_buf, "SPI Timing Error \r\n");
HAL_UART_Transmit(&huart2, (uint8_t *)uart_buf, uart_buf_len, 100);
break;
default :
Error_Handler();
}
}
else {
uart_buf_len = sprintf(uart_buf, "SPI_state = %d \r\n",HAL_SPI_GetState(&hspi1));
HAL_UART_Transmit(&huart2, (uint8_t *)uart_buf, uart_buf_len, 100);
HAL_Delay(2000);
}
}
static void TX_DATA(float volt,float spee , float press ,float curre, float tor)
{
Control_Values[0] = volt;
Control_Values[1] = spee;
Control_Values[2] = press;
Control_Values[3] = curre;
Control_Values[4] = torc;
if (HAL_SPI_GetState(&hspi1) == HAL_SPI_STATE_READY )
{
switch (HAL_SPI_Transmit(&hspi1, (uint8_t*) Control_Values, BUFFERSIZE, HAL_MAX_DELAY))
{
case HAL_OK:
for (int i = 0 ; i<BUFFERSIZE ; i++)
{
uart_buf_len = sprintf(uart_buf, "SEND Params = %f \r\n",Control_Values[i]);
HAL_UART_Transmit(&huart2, (uint8_t *)uart_buf, uart_buf_len, 100);
}
break;
case HAL_ERROR:
Error_Handler();
uart_buf_len = sprintf(uart_buf, "Error \r\n");
HAL_UART_Transmit(&huart2, (uint8_t *)uart_buf, uart_buf_len, 100);
break;
case HAL_BUSY:
uart_buf_len = sprintf(uart_buf, "SPI Bus Busy \r\n");
HAL_UART_Transmit(&huart2, (uint8_t *)uart_buf, uart_buf_len, 100);
break;
case HAL_TIMEOUT:
uart_buf_len = sprintf(uart_buf, "SPI Timing Error \r\n");
HAL_UART_Transmit(&huart2, (uint8_t *)uart_buf, uart_buf_len, 100);
break;
default :
Error_Handler();
}
}
else {
uart_buf_len = sprintf(uart_buf, "Recieved Params = %d \r\n",HAL_SPI_GetState(&hspi1));
HAL_UART_Transmit(&huart2, (uint8_t *)uart_buf, uart_buf_len, 100);
}
}
Output to Serial Monitor
2023-01-09 10:31 AM
There are several generations of spi.
I use spi that has 32 bit fifos, dma in cyclic mode, and exti on nss rise edge to process data packets.
When nss goes up, the dma already filled the fifo... you need to rcc/sys reset spi and reconfigure to flush the fifo. There maybe other ways that I don't recall. Let me know if this makes you progress.
And yes, you csn EXTI on alternate function, even if the HAL doesn't....