我用的软件是STM32CUBUMX,用SPI1做从机,中断接收数据,并在中断中处理数据,然后通过SPI3(主机)将数据发送出去。问题是用串口看接收到了数据,用逻辑分析仪连接的主机,检测不出数据,测试了几次,觉得问题应该出现在中断中,程序如下:
中断:
/**
* @brief This function handles SPI1 global interrupt.
*/
void SPI1_IRQHandler(void)
{
/* USER CODE BEGIN SPI1_IRQn 0 */
SpiReceive_RXNE(&hspi1);
/* USER CODE END SPI1_IRQn 0 */
HAL_SPI_IRQHandler(&hspi1);
/* USER CODE BEGIN SPI1_IRQn 1 */
/* USER CODE END SPI1_IRQn 1 */
}
void SpiReceive_RXNE(SPI_HandleTypeDef *hspi)
{
uint32_t temp;
if((__HAL_SPI_GET_FLAG(hspi,SPI_FLAG_RXNE) == RESET))
{
HAL_SPI_DMAStop(&hspi1);
temp = hspi1.hdmarx->Instance->NDTR;
SpiType1.rx_len = receivelen - temp;
SpiType1.rec_flag=1;
HAL_SPI_Receive_DMA(&hspi1,SpiType1.SPI_DMA_rxBuf,receivelen);
}
}
主程序:
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_SPI1_Init();
MX_USART1_UART_Init();
MX_SPI3_Init();
/* USER CODE BEGIN 2 */
HAL_SPI_Receive_DMA(&hspi1,SpiType1.SPI_DMA_rxBuf,receivelen);
__HAL_SPI_ENABLE_IT(&hspi1, SPI_IT_RXNE);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if(SpiType1.rec_flag==1)
{
SpiType1.rec_flag=0;
HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi3,SpiType1.SPI_DMA_rxBuf,SpiType1.rx_len,1000);
HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_SET);
}
/*printf("\n\r %d,%d,%d,%d,%d",SpiType1.SPI_DMA_rxBuf[0],SpiType1.SPI_DMA_rxBuf[1],SpiType1.SPI_DMA_rxBuf[2],SpiType1.SPI_DMA_rxBuf[3],SpiType1.SPI_DMA_rxBuf[4]);
HAL_Delay(500);*/
}
/* USER CODE END 3 */
}
SPI_FLAG_RXNE,它的标志位没有清除,因为在库函数中没有清除这个标志位的函数,百度过,说可以把SPI_FLAG_RXNE改成SPI_FLAG_BSY,改后的数据也不对,逻辑分析仪上只能检测出一个数据,是发送的最后一个数据。请问有什么办法可以解决?感谢
|