Electrical – Garbage in SPI RX Buffer with DMA (STM32)

cmsisdmamicrocontrollerstm32stm32f0

MCU: STM32f030f4p6

There is garbage in reception buffer, when I use SPI and 2 DMAs (for reception and transmission).

void sendMsg2(uint32_t size, uint8_t* rx, uint8_t* tx){
DMA1_Channel2->CMAR=rx;
DMA1_Channel2->CNDTR=size;
DMA1_Channel2->CCR=DMA_CCR_MINC
        |DMA_CCR_TCIE
        |DMA_CCR_EN;

SCB->SCR=SCB_SCR_SLEEPONEXIT_Msk;
GPIOA->BSRR=GPIO_BSRR_BR_4;

DMA1_Channel3->CMAR=tx;
DMA1_Channel3->CNDTR=size;
DMA1_Channel3->CCR=DMA_CCR_MINC
        |DMA_CCR_DIR
        |DMA_CCR_EN;

__asm volatile("wfi");

DMA1_Channel3->CCR=0;
DMA1_Channel2->CCR=0;
}

But when I use 1 loop and 1 DMA it works.

EX1:

void sendMsg2(uint32_t size, uint8_t* rx, uint8_t* tx){
DMA1_Channel2->CMAR=rx;
DMA1_Channel2->CNDTR=size;
DMA1_Channel2->CCR=DMA_CCR_MINC
        |DMA_CCR_TCIE
        |DMA_CCR_EN;

for(register uint32_t i=0; i<size; i++){
    while(!(SPI1->SR&SPI_SR_RXNE));
    rx[i]=SPI1_DR_8;
}

DMA1_Channel2->CCR=0;
}

EX2:

void sendMsg2(uint32_t size, uint8_t* rx, uint8_t* tx){
DMA1_Channel3->CMAR=tx;
DMA1_Channel3->CNDTR=size;
DMA1_Channel3->CCR=DMA_CCR_MINC
        |DMA_CCR_DIR
        |DMA_CCR_EN;

for(register uint32_t i=0; i<size; i++){
    while(!(SPI1->SR&SPI_SR_TXE));
    SPI1_DR_8=tx[i];
}

DMA1_Channel3->CCR=0;
}

Best Answer

your init function does not set the periph register address in the DMA configuration.

So it takes the data from the unknown address.