AnsweredAssumed Answered

STM32F746, SPI

Question asked by s.boris on Oct 10, 2016
Latest reply on Oct 11, 2016 by ForumSTM32.ForumSTM3
Hello, I have a question about SPI. I start spi in receive mode only with dma. I receive correct data but if I want to receive only one sample, I see 32 clocks instead of 16 clock.

void spi_init()
{
spi_handler.Instance               = SPI2;
    spi_handler.Init.Mode              = SPI_MODE_MASTER;
    spi_handler.Init.Direction         = SPI_DIRECTION_2LINES_RXONLY; // Receive mode only
    spi_handler.Init.DataSize          = SPI_DATASIZE_16BIT;
    spi_handler.Init.CLKPolarity       = SPI_POLARITY_LOW;
    spi_handler.Init.CLKPhase          = SPI_PHASE_1EDGE;
    spi_handler.Init.NSS               = SPI_NSS_HARD_OUTPUT;
    spi_handler.Init.NSSPMode          = SPI_NSS_PULSE_ENABLE;
    spi_handler.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64; 
    spi_handler.Init.FirstBit          = SPI_FIRSTBIT_MSB;
    spi_handler.Init.TIMode            = SPI_TIMODE_DISABLE;
    spi_handler.Init.CRCCalculation    = SPI_CRCCALCULATION_DISABLE;
 
    HAL_SPI_Init(&spi_handler);
}
 
void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
{
    GPIO_InitTypeDef  gpio_instruct;
    if (hspi->Instance == SPI2)
    {
        //Enable RX clock for SPI
        __HAL_RCC_GPIOI_CLK_ENABLE(); // Enable SCK clk
        __HAL_RCC_GPIOB_CLK_ENABLE(); // Enable MISO pin
 
        // Enable SPI2 clock
        __HAL_RCC_SPI2_CLK_ENABLE();
 
        // Enable DMA clock
        __HAL_RCC_DMA1_CLK_ENABLE();
 
        // SPI CLK pin init
        gpio_instruct.Pin       = GPIO_PIN_1;
        gpio_instruct.Mode      = GPIO_MODE_AF_PP;
        gpio_instruct.Pull      = GPIO_PULLDOWN;
        gpio_instruct.Speed     = GPIO_SPEED_HIGH;
        gpio_instruct.Alternate = GPIO_AF5_SPI2;
        HAL_GPIO_Init(GPIOI, &gpio_instruct);
 
        // SPI CS pin init
        gpio_instruct.Pin       = GPIO_PIN_0;
        gpio_instruct.Mode      = GPIO_MODE_AF_PP;
        gpio_instruct.Pull      = GPIO_PULLUP;
        gpio_instruct.Speed     = GPIO_SPEED_HIGH;
        gpio_instruct.Alternate = GPIO_AF5_SPI2;
        HAL_GPIO_Init(GPIOI, &gpio_instruct);
 
        // SPI MISO gpio pin init
        gpio_instruct.Pin = GPIO_PIN_14;
        gpio_instruct.Mode = GPIO_MODE_AF_PP;
        gpio_instruct.Pull = GPIO_PULLUP;
        gpio_instruct.Alternate = GPIO_AF5_SPI2;
        HAL_GPIO_Init(GPIOB, &gpio_instruct);
 
        // Config DMA for receive process
        dma_handler_rx.Instance                 = DMA1_Stream3;
        dma_handler_rx.Init.Channel             = DMA_CHANNEL_0;
        dma_handler_rx.Init.FIFOMode            = DMA_FIFOMODE_ENABLE;
        dma_handler_rx.Init.FIFOThreshold       = DMA_FIFO_THRESHOLD_HALFFULL;
        dma_handler_rx.Init.MemBurst            = DMA_MBURST_SINGLE;
        dma_handler_rx.Init.PeriphBurst         = DMA_PBURST_SINGLE;
        dma_handler_rx.Init.Direction           = DMA_PERIPH_TO_MEMORY;
        dma_handler_rx.Init.PeriphInc           = DMA_PINC_DISABLE;
        dma_handler_rx.Init.MemInc              = DMA_MINC_ENABLE;
        dma_handler_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
        dma_handler_rx.Init.MemDataAlignment    = DMA_MDATAALIGN_HALFWORD;
        dma_handler_rx.Init.Mode                = DMA_NORMAL;      
        dma_handler_rx.Init.Priority            = DMA_PRIORITY_HIGH;
 
        HAL_DMA_Init(&dma_handler_rx);
 
        // Associate the initialized DMA handle to the the SPI handle
        __HAL_LINKDMA(hspi, hdmarx, dma_handler_rx);
 
        // Set DMA1 stream 3 priority.
        // When 2 interrupts have same preemption priority, then he one who has higher sub priority will be execute first.
        // If both interrupts both have same preemption priority and sub priority, the one comes first will be execute first
        HAL_NVIC_SetPriority(DMA1_Stream3_IRQn, 1, 1);
        HAL_NVIC_EnableIRQ(DMA1_Stream3_IRQn);
 
        // Set SPI2 receive priority.
        // When 2 interrupts have same preemption priority, then he one who has higher sub priority will be execute first.
        // If both interrupts both have same preemption priority and sub priority, the one comes first will be execute first
        HAL_NVIC_SetPriority(SPI2_IRQn, 1, 0);
        HAL_NVIC_EnableIRQ(SPI2_IRQn);
    }
}

Outcomes