请教大家,使用HAL库做SPI的双机通信,主机使用401,SPI时钟0.65625MHz,SPI单次传输2字节;从机使用405;库函数版的主机和从机程序数据接收和发送都正常(DMA中断接收);
改成HAL版本的时候,使用HAL_SPI_TransmitReceive_DMA函数发现一次只能接受一个字节,例如我主机发送的是 0x2211,从机只能收到 0x11
请大家帮忙看下原因;
从机配置如下:
#define SPIx SPI3
SpiHandle.Instance = SPIx;
SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE;
SpiHandle.Init.CLKPolarity = SPI_POLARITY_HIGH;
SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
SpiHandle.Init.CRCPolynomial = 7;
SpiHandle.Init.DataSize = SPI_DATASIZE_16BIT;
SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
SpiHandle.Init.NSS = SPI_NSS_SOFT;
SpiHandle.Init.TIMode = SPI_TIMODE_DISABLE;
// HAL_SPI_MspInit 函数中的 相关代码:
hdma_tx.Init.PeriphDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_tx.Init.Mode = DMA_NORMAL;
hdma_tx.Init.Priority = DMA_PRIORITY_LOW;
hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_tx.Init.MemBurst = DMA_MBURST_SINGLE;
hdma_tx.Init.PeriphBurst = DMA_PBURST_SINGLE;
hdma_rx.Init.PeriphDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_rx.Init.Mode = DMA_NORMAL;
hdma_rx.Init.Priority = DMA_PRIORITY_HIGH;
hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_rx.Init.MemBurst = DMA_MBURST_SINGLE;
hdma_rx.Init.PeriphBurst = DMA_PBURST_SINGLE;
// 中断接收函数
void SPIx_DMA_RX_IRQHandler(void)
{
HAL_DMA_IRQHandler(SpiHandle.hdmarx);
HAL_SPI_TransmitReceive_DMA(&SpiHandle, (uint8_t*)aTxBuffer, (uint8_t *)aRxBuffer, 4);
}
如果把 HAL_SPI_MspInit 函数中的 相关代码改成:
// 发送
hdma_tx.Init.PeriphDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_tx.Init.Mode = DMA_NORMAL;
hdma_tx.Init.Priority = DMA_PRIORITY_LOW;
hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_tx.Init.MemBurst = DMA_MBURST_SINGLE;
hdma_tx.Init.PeriphBurst = DMA_PBURST_SINGLE;
// 接收
hdma_rx.Init.PeriphDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_rx.Init.Mode = DMA_NORMAL;
hdma_rx.Init.Priority = DMA_PRIORITY_HIGH;
hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_rx.Init.MemBurst = DMA_MBURST_SINGLE;
hdma_rx.Init.PeriphBurst = DMA_PBURST_SINGLE;
就会报错 assert_failed...
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
hdma_tx.Init.PeriphDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_rx.Init.PeriphDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
应该写成:
hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
复制粘贴的时候给粘贴错了...
现在单次接收2字节主机数据已经正常了,还在调试发送...
一周热门 更多>