AnsweredAssumed Answered

SPI1 in DMA2 on STM32F4

Question asked by rocca.stephane on Oct 29, 2012
Latest reply on Oct 31, 2012 by rocca.stephane
Hi,
I'm trying to switch SPI1 to DMA mode. First I just try to enable on RX channel only, but I tried both RX TX, and many configuration, and I can't figure why it doesn't want to start...

#define          NBR_BYTES_               15
uint8_t SPI_MASTER_Buffer_Tx[NBR_BYTES_] = { ADDRESS_TO_READ, 0,0,0,0,0,0, 0,0,0,0,0,0,0,0 };
uint8_t SPI_MASTER_Buffer_Rx[NBR_BYTES_] = {99,99,99,99,99,99,99,99,99,99,99,99,99,99,99};

SPI_InitTypeDef  SPI_InitStructure;

SPI_I2S_DeInit(SPI1);     

SPI_InitStructure.SPI_Mode                          = SPI_Mode_Master;
SPI_InitStructure.SPI_Direction                = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_DataSize                     = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL                          = SPI_CPOL_High; /// cad      
SPI_InitStructure.SPI_CPHA                          = SPI_CPHA_2Edge;/// cad      
SPI_InitStructure.SPI_NSS                          = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler      = SPI_BaudRatePrescaler_128;
SPI_InitStructure.SPI_FirstBit                     = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial               = 7;     
SPI_Init(SPI1, &SPI_InitStructure);

SPI_Cmd(SPI1, ENABLE);


Init_device_in_poll_mode(); /// works ok


RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_DMA2, ENABLE); 

DMA_Cmd(DMA2_Stream0, DISABLE);
DMA_Cmd(DMA2_Stream5, DISABLE);

/////////////// RX DMA
DMA_InitTypeDef DMA_InitStructure;
DMA_InitStructure.DMA_BufferSize = NBR_BYTES_;
DMA_InitStructure.DMA_Channel = DMA_Channel_3;
DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)SPI_MASTER_Buffer_Rx;
DMA_InitStructure.DMA_PeripheralBaseAddr = 0x4001300C;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
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_Normal; // DMA_Mode_Circular; //DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);

/////////////// TX DMA
// DMA_InitStructure.DMA_BufferSize = NBR_BYTES_;
// DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)SPI_MASTER_Buffer_Tx;
// DMA_InitStructure.DMA_PeripheralBaseAddr = 0x4001300C;

// DMA_InitStructure.DMA_Channel = DMA_Channel_3;
// DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
// 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_Normal; // DMA_Mode_Circular; //DMA_Mode_Normal;
// DMA_InitStructure.DMA_Priority = DMA_Priority_High;
// DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
// DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
// DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
// DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
// DMA_Init(DMA2_Stream5, &DMA_InitStructure);

// DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE);
// DMA_ITConfig(DMA2_Stream5, DMA_IT_TC, ENABLE);

while(SPI1->SR & SPI_SR_TXE == 0);                // wait for empty TX buffer 
SPI1->DR = ADDRESS_TO_READ;                                

// DMA_Cmd(DMA2_Stream5, ENABLE);
DMA_Cmd(DMA2_Stream0, ENABLE);

GPIO_ResetBits(GPIOB, GPIO_Pin_15); ///// debug blue led

while(!DMA_GetFlagStatus(DMA2_Stream0, DMA_FLAG_TCIF0));

GPIO_SetBits(GPIOB, GPIO_Pin_15); ///// debug blue led


Tx for your help !

Outcomes