AnsweredAssumed Answered

spi, stm32f7

Question asked by s.anto on Sep 7, 2016
Latest reply on Jul 20, 2017 by Imen D
Hello, I have a problem with SPI communication for stm32f746-discovery board. I didnt read any value, and when I looked NSS pin is not moving. Only clock is running. SPI is in receive mode only. Function hw_spi_communination() is called on every 1ms.
static void hw_spi_result()
{
    for (i = 0; i < 5; i++)
    {
        spi_res += spi_convert_value[i];
    }
 
          spi_status->result = spi_res / 5;
}
 
void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
{
    GPIO_InitTypeDef  gpio_instruct;
 
        //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 SCK pin init
        gpio_instruct.Pin       = GPIO_PIN_1|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.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_DISABLE;
        dma_handler_rx.Init.FIFOThreshold       = DMA_FIFO_THRESHOLD_FULL;
        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_MDATAALIGN_BYTE;
        dma_handler_rx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
        dma_handler_rx.Init.Mode                = DMA_CIRCULAR;
        dma_handler_rx.Init.Priority            = DMA_PRIORITY_MEDIUM;
 
        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, 0);
        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);
 
}
 
void hw_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_8BIT;
    spi_handler.Init.CLKPolarity       = SPI_POLARITY_HIGH;
    spi_handler.Init.CLKPhase          = SPI_PHASE_1EDGE;
    spi_handler.Init.NSS               = SPI_NSS_SOFT;
    spi_handler.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;  // Baud rate = APB1 / BRprescaler. Max Fsample of MCP3301 is 1,7MHz
    spi_handler.Init.FirstBit          = SPI_FIRSTBIT_MSB;
    spi_handler.Init.TIMode            = SPI_TIMODE_DISABLE;
    spi_handler.Init.CRCCalculation    = SPI_CRCCALCULATION_ENABLE;
    spi_handler.Init.CRCPolynomial     = 6;
 
    HAL_SPI_Init(&spi_handler);
 
}
 
void hw_spi_communination()
{
    // Start full duplex communication process.
    HAL_SPI_Receive_DMA(&spi_handler, (U8*)spi_convert_value, 5);
}
 
void DMA1_Stream3_IRQHandler(void)
{
  HAL_DMA_IRQHandler(spi_handler.hdmarx);
}
 
void SPI2_IRQHandler(void)
{
  HAL_SPI_IRQHandler(&spi_handler);
}
 
/*****************************************************************************/
/*****************************************************************************/
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
{
    hw_spi_result();
}

Outcomes