cancel
Showing results for 
Search instead for 
Did you mean: 

I would like to make SPI communication with HAL library. I want to read the incoming data with the Receive function, but I cannot get an answer. Is there anyone who can help?

Cözde.1
Associate II

SPI

13 REPLIES 13
Cözde.1
Associate II

can you throw sample code

My code for SPI is quite rich as it includes a master/slave mode with interrupts and DMA.

So I'll show here the 5% which is for master mode extract code. 4 wire SPI on L4R5 @12MHz with SYSCLK = 48MHz

16 bit SPI mode

HAL_StatusTypeDef SPIP_MasterConfigureSpi(SPI_HandleTypeDef *hspi){
  hspi->Instance               = SPI2;
  hspi->Init.Direction         = SPI_DIRECTION_2LINES;
  hspi->Init.CLKPhase          = SPI_PHASE_1EDGE;
  hspi->Init.CLKPolarity       = SPI_POLARITY_LOW;
  hspi->Init.DataSize          = SPI_DATASIZE_16BIT;
  hspi->Init.FirstBit          = SPI_FIRSTBIT_MSB;
  hspi->Init.TIMode            = SPI_TIMODE_DISABLE;
  hspi->Init.CRCCalculation    = SPI_CRCCALCULATION_DISABLE;
  hspi->Init.CRCPolynomial     = 7;
  hspi->Init.CRCLength         = SPI_CRC_LENGTH_8BIT;
  hspi->Init.NSS               = SPI_NSS_SOFT;
  hspi->Init.NSSPMode          = SPI_NSS_PULSE_DISABLE;
  hspi->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;//16;
  hspi->Init.Mode = SPI_MODE_MASTER;
  return (HAL_SPI_Init(hspi));
}
 
void SPIP_MasterConfigureDma(SPI_HandleTypeDef *hspi){
  /* Configure the DMA handler for Transmission process */
  pSPIP->hdma_tx->Instance                 = DMA1_Channel2;//DMA1_Channel5;
  pSPIP->hdma_tx->Init.Request             = DMA_REQUEST_SPI2_TX;//DMA_REQUEST_1;
  pSPIP->hdma_tx->Init.Direction           = DMA_MEMORY_TO_PERIPH;
  pSPIP->hdma_tx->Init.PeriphInc           = DMA_PINC_DISABLE;
  pSPIP->hdma_tx->Init.MemInc              = DMA_MINC_ENABLE;
  pSPIP->hdma_tx->Init.PeriphDataAlignment = /*DMA_PDATAALIGN_BYTE;*/ DMA_PDATAALIGN_HALFWORD;
  pSPIP->hdma_tx->Init.MemDataAlignment    = /*DMA_MDATAALIGN_BYTE;*/ DMA_MDATAALIGN_HALFWORD;
  pSPIP->hdma_tx->Init.Mode                = DMA_NORMAL;
  pSPIP->hdma_tx->Init.Priority            = DMA_PRIORITY_HIGH;
  HAL_DMA_Init(pSPIP->hdma_tx);
  /* Associate the initialized DMA handle to the the SPI handle */
  __HAL_LINKDMA(hspi, hdmatx, *(pSPIP->hdma_tx));
 
  /* Configure the DMA handler for Reception process */
  pSPIP->hdma_rx->Instance                 = DMA1_Channel1;//DMA1_Channel4;
  pSPIP->hdma_rx->Init.Request             = DMA_REQUEST_SPI2_RX;//DMA_REQUEST_1;
  pSPIP->hdma_rx->Init.Direction           = DMA_PERIPH_TO_MEMORY;
  pSPIP->hdma_rx->Init.PeriphInc           = DMA_PINC_DISABLE;
  pSPIP->hdma_rx->Init.MemInc              = DMA_MINC_ENABLE;
  pSPIP->hdma_rx->Init.PeriphDataAlignment = /*DMA_PDATAALIGN_BYTE;*/ DMA_PDATAALIGN_HALFWORD;
  pSPIP->hdma_rx->Init.MemDataAlignment    = /*DMA_MDATAALIGN_BYTE;*/ DMA_MDATAALIGN_HALFWORD;
  pSPIP->hdma_rx->Init.Mode                = DMA_NORMAL;
  pSPIP->hdma_rx->Init.Priority            = DMA_PRIORITY_HIGH;
  HAL_DMA_Init(pSPIP->hdma_rx);
  /* Associate the initialized DMA handle to the the SPI handle */
  __HAL_LINKDMA(hspi, hdmarx, *(pSPIP->hdma_rx));
}
 
// this function kicks normally within interrupt for multi-block transfer
HAL_StatusTypeDef SPIP_MasterSerialTransferStart_ISR(SPIP_t* pSPIP){
  if(HAL_SPI_TransmitReceive_DMA(pSPIP->hspi, pSPIP->pMasterSerialTxBuffer, pSPIP->pMasterSerialRxBuffer, SERIAL_BLOCK_SIZE_WORD) != HAL_OK)
     TrapError();
  return HAL_OK;
}
 

Cözde.1
Associate II

thank you but I want to do it without using DMA

Cözde.1
Associate II

thank you but I want to do it without using DMA