cancel
Showing results for 
Search instead for 
Did you mean: 

16 bit SPI slave to DMA not working with Half word.

fabces
Associate

Hi,

I'm using an STM32F301 chip to write to a display, so I need a lot of data to be transferred to it.

The chip is configured as an SPI slave and the data is received via DMA.

My issue is that the DMA receive buffer takes too much space in RAM becaue the only way I can get it to work is by having it configured as using half word for peripheral data alignement and word for memory data alignment. I would like to use half word for both peripheral and memory and save half of the size in RAM, but I can't get it to work at all like that. Here is my configuration, do you guys have any Idea why it isn't working?

I used HAL to configure everything.

Any help is greatly appreciated!

Thanks

static void MX_SPI2_Init(void)
    {
    	/* SPI2 parameter configuration*/
    	hspi2.Instance = SPI2;
    	hspi2.Init.Mode = SPI_MODE_SLAVE;
    	hspi2.Init.Direction = SPI_DIRECTION_2LINES;
    	hspi2.Init.DataSize = SPI_DATASIZE_16BIT;
    	hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
    	hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
    	hspi2.Init.NSS = SPI_NSS_HARD_INPUT;
    	hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
    	hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
    	hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
    	hspi2.Init.CRCPolynomial = 7;
    	hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
    	hspi2.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
    	if (HAL_SPI_Init(&hspi2) != HAL_OK)
    	{
    		Error_Handler();
    	}
    }
static void MX_DMA_Init(void)
    {
    	/* DMA controller clock enable */
    	__HAL_RCC_DMA1_CLK_ENABLE();
     
    	/* DMA interrupt init */
    	/* DMA1_Channel4_IRQn interrupt configuration */
    	HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0);
    	HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn);
    	/* DMA1_Channel5_IRQn interrupt configuration */
    	HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);
    	HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);
    }
void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	if(hspi->Instance==SPI2)
	{
		/* Peripheral clock enable */
		__HAL_RCC_SPI2_CLK_ENABLE();
		__HAL_RCC_GPIOB_CLK_ENABLE();
		/**SPI2 GPIO Configuration
		 * PB12     ------> SPI2_NSS
		 * PB13     ------> SPI2_SCK
		 * PB14     ------> SPI2_MISO
		 * PB15     ------> SPI2_MOSI
		 */
		GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
		GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
		GPIO_InitStruct.Pull = GPIO_NOPULL;
		GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
		GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
		HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
		/* SPI2 DMA Init */
		/* SPI2_RX Init */
		hdma_spi2_rx.Instance = DMA1_Channel4;
		hdma_spi2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
		hdma_spi2_rx.Init.PeriphInc = DMA_PINC_DISABLE;
		hdma_spi2_rx.Init.MemInc = DMA_MINC_ENABLE;
		hdma_spi2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
		hdma_spi2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
		hdma_spi2_rx.Init.Mode = DMA_NORMAL;
		hdma_spi2_rx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
		if (HAL_DMA_Init(&hdma_spi2_rx) != HAL_OK)
		{
			Error_Handler();
		}
 
		__HAL_LINKDMA(hspi,hdmarx,hdma_spi2_rx);
 
		/* SPI2_TX Init */
		hdma_spi2_tx.Instance = DMA1_Channel5;
		hdma_spi2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
		hdma_spi2_tx.Init.PeriphInc = DMA_PINC_DISABLE;
		hdma_spi2_tx.Init.MemInc = DMA_MINC_ENABLE;
		hdma_spi2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
		hdma_spi2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
		hdma_spi2_tx.Init.Mode = DMA_NORMAL;
		hdma_spi2_tx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
		if (HAL_DMA_Init(&hdma_spi2_tx) != HAL_OK)
		{
			Error_Handler();
		}
 
		__HAL_LINKDMA(hspi,hdmatx,hdma_spi2_tx);
 
		/* SPI2 interrupt Init */
		HAL_NVIC_SetPriority(SPI2_IRQn, 0, 0);
		HAL_NVIC_EnableIRQ(SPI2_IRQn);
	}
}

0 REPLIES 0