簡體   English   中英

STM32:使用DMA接收SPI數據-Rx緩沖區中的字節數總是錯誤

[英]STM32: Receiving SPI data with DMA - always wrong number of bytes in Rx buffer

對於STM32F764,我希望以只讀模式通過SPI從11個菊花鏈式24位ADC接收33個字節。 接收到的數據看起來還不錯,但是緩沖區中有37到39個字節。 同樣,對於其他字節數,我接收的字節數比預期的多4到6個字節。

有誰知道為什么會這樣?

當33個字節准備就緒時,GPIO中斷將從外部ADC調用HAL_SPI_Receive_DMA。 完成DMA后,將在Sampling()中處理緩沖區內容。

外部GPIO中斷處理程序是否是調用HAL_SPI_Receive_DMA的正確位置?

/*SPI init for external ADCs (only Rx)*/
uint8_t SPI3_Init_ADC(void)
{
    hspi3.Instance = SPI3;
    hspi3.Init.Mode = SPI_MODE_MASTER;
    hspi3.Init.Direction = SPI_DIRECTION_2LINES_RXONLY;
    hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
    hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
    hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
    hspi3.Init.NSS = SPI_NSS_SOFT;
    hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; /*SPI3_CLK = 12.5 MHz*/
    hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
    hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
    hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
    hspi3.Init.CRCPolynomial = 7;
    hspi3.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
    hspi3.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
    if (HAL_SPI_Init(&hspi3) != HAL_OK)
        return ERR;
    return OK;
}
    /*SPI MSP and DMA Initialization*/
void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    /*SPI3: ADCs*/
    if(hspi->Instance==SPI3)
    {
        __HAL_RCC_SPI3_CLK_ENABLE(); /* Peripheral clock enable */
        __HAL_RCC_GPIOB_CLK_ENABLE();
        /*PB3 --> SPI3_SCK*/
        /*PB4 --> SPI3_MISO*/
        GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4;
        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(GPIOB, &GPIO_InitStruct);
        /*DMA 1 / channel 0 / stream 0 transfers data from external ADC to sample registers*/
        __HAL_RCC_DMA1_CLK_ENABLE(); /*DMA clock*/
        hdma_spi3_rx.Init.Channel = DMA_CHANNEL_0;
        hdma_spi3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; /*from SPI to memory*/
        hdma_spi3_rx.Init.PeriphInc = DMA_PINC_DISABLE; /*no increment on SPI side*/
        hdma_spi3_rx.Init.MemInc = DMA_MINC_ENABLE; /*increment sample memory address*/
        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_VERY_HIGH;
        hdma_spi3_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
        hdma_spi3_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_HALFFULL;
        hdma_spi3_rx.Init.MemBurst = DMA_PBURST_SINGLE;
        hdma_spi3_rx.Init.PeriphBurst = DMA_PBURST_SINGLE;
        hdma_spi3_rx.Instance = DMA1_Stream0;
        HAL_DMA_Init(&hdma_spi3_rx);
        HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 0, 0); /*highest priority for DMA interrupt*/
        HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn); /*enable DMA interrupt*/
        __HAL_LINKDMA(hspi, hdmarx, hdma_spi3_rx); /*link DMA1 to SPI3*/
    }

/*INTERRUPT HANDLERS*/

/*Data ready interrupt (/ADC_/DRDY) of external ADCs*/
void EXTI9_5_IRQHandler(void)
{
    HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_7); /*clear interrupt*/
    HAL_SPI_Receive_DMA(&hspi3, SplRxBuff, 33); /*get 33 bytes ADC data*/
}

/*DMA1 stream0 global interrupt (Rx of external ADCs complete)*/
void SPI_DMA_Rx_IRQHandler(void)
{
    HAL_DMA_IRQHandler(&hdma_spi3_rx);
    Sampling();
}

屏幕截圖:

紅色部分是多余的數據。 ADC從未使用的第12個通道在此處傳送實際數據。

我敢打賭,因為您不使用任何同步機制(標志,信號量等)來保護啟動傳輸函數調用,因此在其他傳輸完成之前會觸發中斷。 同樣,DMA處理程序中的神秘函數采樣可能會做一些奇怪的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM