兄台,能不能看下我的spi传输的代码,我的dma传输一直收不到数据,请看看有什么问题,谢谢了
void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
{
GPIO_InitTypeDef GPIO_Initure;
RCC_PeriphCLKInitTypeDef SPI2ClkInit;
__HAL_RCC_SPI2_CLK_ENABLE(); //使能SPI2时钟
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE(); //使能GPIOF时钟
SPI2ClkInit.PeriphClockSelection=RCC_PERIPHCLK_SPI2;
SPI2ClkInit.Spi123ClockSelection=RCC_SPI123CLKSOURCE_PLL;
HAL_RCCEx_PeriphCLKConfig(&SPI2ClkInit);
__HAL_RCC_DMA1_CLK_ENABLE();
//PB13,15 (SCK|MISO|MOSI)
GPIO_Initure.Pin=GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
GPIO_Initure.Mode=GPIO_MODE_AF_PP; //复用推挽输出
GPIO_Initure.Pull=GPIO_PULLUP; //上拉
GPIO_Initure.Speed=GPIO_SPEED_FREQ_VERY_HIGH; //快速
GPIO_Initure.Alternate=GPIO_AF5_SPI2; //复用为SPI2
HAL_GPIO_Init(GPIOB,&GPIO_Initure); //初始化
//PB9(NSS引脚,用作其他)
GPIO_Initure.Pin=GPIO_PIN_9;
GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP; //推挽输出
GPIO_Initure.Pull=GPIO_PULLUP; //上拉
GPIO_Initure.Speed=GPIO_SPEED_FREQ_VERY_HIGH; //高速
HAL_GPIO_Init(GPIOB,&GPIO_Initure);
//W25Q256片选信号拉高,防止W25Q256干扰
GPIO_Initure.Pin=GPIO_PIN_10; //PF10
HAL_GPIO_Init(GPIOF,&GPIO_Initure); //初始化
/*##-3- Configure the DMA ##################################################*/
/* Configure the DMA handler for Transmission process */
hdma_tx.Instance = SPIx_TX_DMA_STREAM;
hdma_tx.Init.Request = SPIx_TX_DMA_REQUEST;
hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
// hdma_tx.Init.MemBurst = DMA_MBURST_INC4;
// hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4;
hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_tx.Init.Mode = DMA_CIRCULAR;
hdma_tx.Init.Priority = DMA_PRIORITY_HIGH;
HAL_DMA_Init(&hdma_tx);
/* Associate the initialized DMA handle to the the SPI handle */
__HAL_LINKDMA(hspi, hdmatx, hdma_tx);
/* Configure the DMA handler for Transmission process */
hdma_rx.Instance = SPIx_RX_DMA_STREAM;
hdma_rx.Init.Request = SPIx_RX_DMA_REQUEST;
hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
// hdma_rx.Init.MemBurst = DMA_MBURST_INC4;
// hdma_rx.Init.PeriphBurst = DMA_PBURST_INC4;
hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_rx.Init.Mode = DMA_CIRCULAR;
hdma_rx.Init.Priority = DMA_PRIORITY_HIGH;
HAL_DMA_Init(&hdma_rx);
/* Associate the initialized DMA handle to the the SPI handle */
__HAL_LINKDMA(hspi, hdmarx, hdma_rx);
/*##-4- Configure the NVIC for DMA #########################################*/
/* NVIC configuration for DMA transfer complete interrupt (SPI1_TX) */
HAL_NVIC_SetPriority(SPIx_DMA_TX_IRQn, 1, 1);
HAL_NVIC_DisableIRQ(SPIx_DMA_TX_IRQn);
/* NVIC configuration for DMA transfer complete interrupt (SPI1_RX) */
HAL_NVIC_SetPriority(SPIx_DMA_RX_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(SPIx_DMA_RX_IRQn);
}
void SPI2_Init(void)
{
SPI2_Handler.Instance=SPI2; //SP2
SPI2_Handler.Init.Mode=SPI_MODE_MASTER ; //设置SPI工作模式,设置为主模式
SPI2_Handler.Init.Direction=SPI_DIRECTION_2LINES;//设置SPI单向或者双向的数据模式:SPI设置为双线模式
SPI2_Handler.Init.DataSize=SPI_DATASIZE_8BIT; //设置SPI的数据大小:SPI发送接收8位帧结构
SPI2_Handler.Init.CLKPolarity=SPI_POLARITY_LOW; //串行同步时钟的空闲状态为低电平
SPI2_Handler.Init.CLKPhase=SPI_PHASE_1EDGE; //串行同步时钟的第1个跳变沿(此处指上升沿)数据被采样
SPI2_Handler.Init.NSS=SPI_NSS_SOFT; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制,主模式SSI必须置位1
SPI2_Handler.Init.NSSPMode=SPI_NSS_PULSE_DISABLE;//NSS输出信号脉冲失能,配置成主模式才有用,目的是让ns引脚成为普通IO
SPI2_Handler.Init.NSSPolarity=SPI_NSS_POLARITY_LOW;
//SPI2_Handler.Init.MasterInterDataIdleness=SPI_MASTER_SS_IDLENESS_15CYCLE;
//SPI2_Handler.Init.s = SPI_MASTER_SS_IDLENESS_04CYCLE;
SPI2_Handler.Init.MasterKeepIOState=SPI_MASTER_KEEP_IO_STATE_ENABLE; //SPI主模式IO状态保持使能
SPI2_Handler.Init.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_16;//定义波特率预分频的值:波特率预分频值为16
SPI2_Handler.Init.FirstBit=SPI_FIRSTBIT_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始
SPI2_Handler.Init.TIMode=SPI_TIMODE_DISABLE; //关闭TI模式
SPI2_Handler.Init.CRCCalculation=SPI_CRCCALCULATION_DISABLE;//关闭硬件CRC校验
SPI2_Handler.Init.CRCPolynomial=7; //CRC值计算的多项式
if (HAL_SPI_DeInit(&SPI2_Handler) != HAL_OK)
{
printf("error\t\n");
}
if(HAL_SPI_Init(&SPI2_Handler) != HAL_OK)
{
/* Initialization Error */
printf("error\t\n");
}
__HAL_SPI_ENABLE(&SPI2_Handler);
SPI2_ReadWriteByte(0Xff); //启动传输
}
在主程序中,
if(HAL_SPI_TransmitReceive_DMA(&SPI2_Handler, (uint8_t *)SpiTxBuffer, (uint8_t *)SpiRxBuffer, BUFFERSIZE) != HAL_OK)
{
printf("error\t\n");
}
//printf("");
while(1)
{
}
而且又设置了
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
{
SCB_InvalidateDCache_by_Addr ((uint32_t *)SpiRxBuffer, BUFFERSIZE);
printf("Received: %s \t\n", SpiRxBuffer);
//HAL_SPI_DMAStop(&SPI2_Handler);
}
但是进入中断接收时,一直为空,这是什么原因呢? |