本帖最后由 烟雨梦 于 2019-6-13 11:44 编辑 STM32F4用的HAL库,SPI3+DMA方式读取25FL512S片子数据,读出来的数据错误,之前没用DMA的时候读出来的数据是对的,为了提高速度换成DMA出现问题了,请大神们帮忙分析下?下面是代码:/* SPI3 init function */ void MX_SPI3_Init(void) { hspi3.Instance = SPI3; hspi3.Init.Mode = SPI_MODE_MASTER; hspi3.Init.Direction = SPI_DIRECTION_2LINES; hspi3.Init.DataSize = SPI_DATASIZE_8BIT; hspi3.Init.CLKPolarity = SPI_POLARITY_HIGH; hspi3.Init.CLKPhase = SPI_PHASE_2EDGE; hspi3.Init.NSS = SPI_NSS_SOFT; hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi3.Init.TIMode = SPI_TIMODE_DISABLE; hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; hspi3.Init.CRCPolynomial = 10; HAL_SPI_Init(&hspi3); } void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi) { GPIO_InitTypeDef GPIO_InitStruct; if(hspi->Instance==SPI3) { /* USER CODE BEGIN SPI3_MspInit 0 */ /* USER CODE END SPI3_MspInit 0 */ /* Peripheral clock enable */ __HAL_RCC_SPI3_CLK_ENABLE(); /* DMA controller clock enable */ __HAL_RCC_DMA1_CLK_ENABLE(); /**SPI3 GPIO Configuration PA15 ------> SPI3_NSS1 PD2 ------> SPI3_NSS2 PD0 ------> SPI3_WP PD1 ------> SPI3_HOLD PD4 ------> SPI3_RST PC10 ------> SPI3_SCK PC11 ------> SPI3_MISO PC12 ------> SPI3_MOSI */ GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF6_SPI3; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /* SPI3 DMA Init */ /* SPI3_RX Init */ hdma_spi3_rx.Instance = DMA1_Stream0; hdma_spi3_rx.Init.Channel = DMA_CHANNEL_0; hdma_spi3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_spi3_rx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_spi3_rx.Init.MemInc = DMA_MINC_ENABLE; hdma_spi3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_spi3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_spi3_rx.Init.Mode = DMA_NORMAL; hdma_spi3_rx.Init.Priority = DMA_PRIORITY_HIGH; hdma_spi3_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; hdma_spi3_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; hdma_spi3_rx.Init.MemBurst = DMA_MBURST_INC4; hdma_spi3_rx.Init.PeriphBurst = DMA_PBURST_INC4; HAL_DMA_Init(&hdma_spi3_rx); __HAL_LINKDMA(hspi,hdmarx,hdma_spi3_rx); /* SPI3_TX Init */ hdma_spi3_tx.Instance = DMA1_Stream5; hdma_spi3_tx.Init.Channel = DMA_CHANNEL_0; hdma_spi3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma_spi3_tx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_spi3_tx.Init.MemInc = DMA_MINC_ENABLE; hdma_spi3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_spi3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_spi3_tx.Init.Mode = DMA_NORMAL; hdma_spi3_tx.Init.Priority = DMA_PRIORITY_MEDIUM; hdma_spi3_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; hdma_spi3_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; hdma_spi3_tx.Init.MemBurst = DMA_MBURST_INC4; hdma_spi3_tx.Init.PeriphBurst = DMA_PBURST_INC4; HAL_DMA_Init(&hdma_spi3_tx); __HAL_LINKDMA(hspi,hdmatx,hdma_spi3_tx); /* DMA interrupt init */ /* DMA1_Stream0_IRQn interrupt configuration */ //HAL_NVIC_SetPriority(SPI3_IRQn, 6, 0); //HAL_NVIC_EnableIRQ(SPI3_IRQn); /* DMA interrupt init */ /* DMA1_Stream0_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 6, 0); HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn); /* DMA1_Stream5_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 6, 1); HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn); /* USER CODE BEGIN SPI3_MspInit 1 */ /* USER CODE END SPI3_MspInit 1 */ } } void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi) { if(hspi->Instance==SPI3) { /* USER CODE BEGIN SPI3_MspDeInit 0 */ /* USER CODE END SPI3_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_SPI3_CLK_DISABLE(); /* DMA controller clock enable */ __HAL_RCC_DMA1_CLK_DISABLE(); /**SPI3 GPIO Configuration PC10 ------> SPI3_SCK PC11 ------> SPI3_MISO PC12 ------> SPI3_MOSI */ HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12); /* SPI3 DMA DeInit */ HAL_DMA_DeInit(hspi->hdmarx); /HAL_DMA_DeInit(hspi->hdmatx); HAL_NVIC_DisableIRQ(DMA1_Stream0_IRQn); HAL_NVIC_DisableIRQ(DMA1_Stream5_IRQn); //HAL_NVIC_DisableIRQ(SPI3_IRQn); /* USER CODE BEGIN SPI3_MspDeInit 1 */ /* USER CODE END SPI3_MspDeInit 1 */ } } #include "stm32f4xx_it.c"文件下 /** * @brief This function handles DMA1 stream0 global interrupt. */ void DMA1_Stream0_IRQHandler(void) { /* USER CODE BEGIN DMA1_Stream0_IRQn 0 */ /* USER CODE END DMA1_Stream0_IRQn 0 */ HAL_DMA_IRQHandler(&hdma_spi3_rx); /* USER CODE BEGIN DMA1_Stream0_IRQn 1 */ /* USER CODE END DMA1_Stream0_IRQn 1 */ } /** * @brief This function handles DMA1 stream5 global interrupt. */ void DMA1_Stream5_IRQHandler(void) { /* USER CODE BEGIN DMA1_Stream5_IRQn 0 */ /* USER CODE END DMA1_Stream5_IRQn 0 */ HAL_DMA_IRQHandler(&hdma_spi3_tx); /* USER CODE BEGIN DMA1_Stream5_IRQn 1 */ /* USER CODE END DMA1_Stream5_IRQn 1 */ } main函数下定时读取数据调用的函数 void SPI_FLASH_BufferRead(UINT8 chipNum,UINT8* pBuffer, UINT32 ReadAddr, UINT16 NumByteToRead) { if(ReadAddr+RX_DATA_LEN <= S25F_FLASH_SIZE) { /* 选择芯片 */ SPI_FLASH_Select(chipNum); /*使能片选*/ SPI_FLASH_CS_ENABLE(); /*发送读数据指令*/ SPI_FLASH_SendByte(S25F_ReadData); /*发送32位数据地址*/ SPI_FLASH_Send4Bytes(ReadAddr); if(NumByteToRead> RX_DATA_LEN ) { NumByteToRead = RX_DATA_LEN ; } HAL_SPI_Receive_DMA(&SpiHandle,pBuffer,NumByteToRead); //取消片选信号 SPI_FLASH_CS_DISABLE(); } } HAL_SPI_Receive_DMA这边读出来的数据一直不对,另外我的HAL_SPI_RxCpltCallback函数除了库文件Stm32f4xx_hal_spi.c下,没在别的文件里重新编写。 请各位帮忙分析下什么原因? |
用的库函数HAL_SPI_Receive_DMA(&SpiHandle,pBuffer,NumByteToRead),这个函数不是一次可以读取固定个长度的字节吗