cancel
Showing results for 
Search instead for 
Did you mean: 

Receiving blocks of data using SPI and DMA

stmail
Associate
Posted on March 19, 2009 at 13:04

Receiving blocks of data using SPI and DMA

1 REPLY 1
stmail
Associate
Posted on May 17, 2011 at 13:07

I want to receive 48bit blocks of data using the STM32 as a SPI slave. The following code works fine:

(SPI2_DR_Address=SPI data register,SPI_RxBuffer_size=6

[...]

DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)SPI2_DR_Address;

DMA_InitStructure.DMA_MemoryBaseAddr = (u32)g_SPI_RxBuffer.list;

DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;

DMA_InitStructure.DMA_BufferSize = SPI_RxBuffer_size;

DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;

DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;

DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;

DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;

DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

DMA_Init(DMA1_Channel4, &DMA_InitStructure);

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;

SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;

SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;

SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;

SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;

SPI_Init(SPI2, &SPI_InitStructure);

[...]

But I think it's rather inefficient as there are six bus accesses.

I tried to change it to the the following:

[...]

DMA_InitStructure.DMA_BufferSize = 3;

[...]

DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;

DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;

[...]

SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;

[...]

Should only need three accesses, but instead of the real data, I only received zeroes...

Any reasons or ideas for that strange behaviour?