4
votes

I try to send data with SPI using DMA channel. When I send without DMA everything is OK, but with DMA sth is wrong. When I debug my program SPI DR register is always 0. I would like to use dma circular mode to send my array all the time. There is my code GPIO INIT:

GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(SPI_PERIPH_CLOCK, ENABLE);
RCC_AHBPeriphClockCmd(GPIO_PERIPH_CLOCK, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

GPIO_PinAFConfig(GPIOB, AF_PIN_SOURCE, GPIO_AF);

GPIO_InitStructure.GPIO_Pin = GPIO_PIN_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOB, &GPIO_InitStructure);

SPI INIT

SPI_I2S_DeInit(SPI1);

SPI_InitTypeDef SPI_InitStructure;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);

SPI_Cmd(SPI1, ENABLE);

DMA INIT:

    DMA_InitTypeDef dma;
    DMA_DeInit(DMA1_Channel1);
    DMA_StructInit(&dma);

    dma.DMA_PeripheralBaseAddr = (uint32_t)&SPI1->DR;
    dma.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    dma.DMA_MemoryBaseAddr = (uint32_t)spi_tx_buffer;
    dma.DMA_MemoryInc = DMA_MemoryInc_Enable;
    dma.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    dma.DMA_DIR = DMA_DIR_PeripheralDST;
    dma.DMA_BufferSize = BUFFERSIZE;   
    dma.DMA_Mode = DMA_Mode_Circular;
    dma.DMA_M2M = DMA_M2M_Disable;

    DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);

    NVIC_InitTypeDef NVIC_InitStructure;

    NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    DMA_Init(DMA1_Channel1, &dma);
    SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);

    DMA_Cmd(DMA1_Channel1, ENABLE);

simple main

int main(void) {

periph_init();

while (1) {     
    for(int j=0; j<10000000; j++){
        j++;
        j--;
    }
}
return 0;
}

What I'm doing wrong?

1
check this, page 282. I am not sure if your implementations are correct but according to datasheet DMA1_Channel1 has no relation with SPIMrMuMu
It works on channel 3. Thanks a lot.kmbm
@NanonA You should post it as an answer.Bence Kaulics

1 Answers

5
votes

As I mentioned in comments, your SPI has no connection with DMA1_Channel1. According to this data sheet, you should use DMA1_Channel3.