AnsweredAssumed Answered

STM32L0 SPI DMA receive issue, clocks too many data

Question asked by Jorgensen.Martin on Mar 26, 2017

I use SPI and DMA transfer.

 

I use the following functions: 

HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);
HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size);

 

I have observed, that "HAL_SPI_Receive_DMA" clocks 16 bits, even though only 1 byte is read. The SPI is set up for 8 bit data.

 

Is this a known issue?

 

void HAL_SPI_Setup(void)
{
  Log_RegisterZone(LogZoneName, &u8logZoneLogLevel, &u8logZoneId, NULL);
  LOG_TRACE("Setup");

  SelectChannel(SPIQueue_Channel_None);
  SPIQueue_Init(HAL_SPI_NewDataQueuedSPI1Handler);

  __HAL_SPI_RESET_HANDLE_STATE(&HAL_SPI_SpiHandle);
  HAL_SPI_SpiHandle.Instance               = SPI1;
  HAL_SPI_SpiHandle.Init.Mode              = SPI_MODE_MASTER;
  HAL_SPI_SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
  HAL_SPI_SpiHandle.Init.Direction         = SPI_DIRECTION_1LINE;
  HAL_SPI_SpiHandle.Init.CLKPhase          = SPI_PHASE_2EDGE;
  HAL_SPI_SpiHandle.Init.CLKPolarity       = SPI_POLARITY_HIGH;
  HAL_SPI_SpiHandle.Init.CRCCalculation    = SPI_CRCCALCULATION_DISABLED;
  HAL_SPI_SpiHandle.Init.CRCPolynomial     = 7;
  HAL_SPI_SpiHandle.Init.DataSize          = SPI_DATASIZE_8BIT;
  HAL_SPI_SpiHandle.Init.FirstBit          = SPI_FIRSTBIT_MSB;
  HAL_SPI_SpiHandle.Init.NSS               = SPI_NSS_SOFT;
  HAL_SPI_SpiHandle.Init.TIMode            = SPI_TIMODE_DISABLED;

  if(HAL_SPI_Init(&HAL_SPI_SpiHandle) != HAL_OK)
  {
    // Initialization Error
    LOG_ERROR("Init");
  }
}


void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
{
  GPIO_InitTypeDef  GPIO_InitStruct;

  if (hspi->Instance == SPI1)
  {
    // Enable peripherals and GPIO Clocks
    __HAL_RCC_GPIOA_CLK_ENABLE();

    // Enable SPI clock
    __HAL_RCC_SPI1_CLK_ENABLE();

    // Enable DMA1 clock
    __HAL_RCC_DMA1_CLK_ENABLE();

    // Configure peripheral GPIO
    HAL_IO_SetupOutputPin(&AcceleroMagneto_SPC, GPIO_MODE_AF_PP, GPIO_NOPULL, GPIO_SPEED_FREQ_MEDIUM, GPIO_AF0_SPI1, GPIO_PIN_SET);
    HAL_IO_SetupOutputPin(&AcceleroMagneto_SDIO, GPIO_MODE_AF_PP, GPIO_NOPULL, GPIO_SPEED_FREQ_MEDIUM, GPIO_AF0_SPI1, GPIO_PIN_SET);

    // Configure the DMA handler for Transmission process
    hdma_tx.Instance                 = DMA1_Channel3;
    hdma_tx.Init.Request             = DMA_REQUEST_1;
    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_NORMAL;
    hdma_tx.Init.Priority            = DMA_PRIORITY_LOW;
    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                 = DMA1_Channel2;
    hdma_rx.Init.Request             = DMA_REQUEST_1;
    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_NORMAL;
    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);
 
    // Configure the NVIC for SPI
    // NVIC configuration for DMA transfer complete interrupt (SPI1_TX / SPI1_RX)
    HAL_NVIC_SetPriority(DMA1_Channel2_3_IRQn, 3, 0);
    EnableIRQ();
  }
}

void Test(void)
{
  uint8_t pu8data[10];

  // Clocks 8 bits
  HAL_SPI_Transmit_DMA(&HAL_SPI_SpiHandle, pu8data, 1);

  // Clocks 16 bits !!!
  HAL_SPI_Receive_DMA(&HAL_SPI_SpiHandle, pu8data, 1);
}

Outcomes