stm32 DMA cannot send data to SPI1 DR (Cannot use DMA to send data to SPI)
dma, spi, stm32, stm32f4discovery
Solution
Such a line has been missed:
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
It enables SPI1 to use the DMA.
Problem
I am trying to use DMA to send data to SPI1. SPI1 will then control DAC for voltage update. The chip used is STM32F407. Therefore, the corresponding channel/stream is: channel3/stream5, as is shown in reference manual. However, when the DMA is enabled, no data is shown in SPI1->DR and there are no results shown in the oscilloscope. The SPI works fine when SPI1->DR is written by software. Could anyone help to check out what has happened? Here comes the code: ``` uint16_t DACData[1]; void InitDMA(void) { DMA_InitTypeDef DMA_InitStructure; DMA_Cmd(DMA2_Stream2, DISABLE); while (DMA2_Stream2->CR & DMA_SxCR_EN); //SPI1 Tx DMA DMA_DeInit(DMA2_Stream5); DMA_StructInit(&DMA_InitStructure); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); //Init DMA clock DMA_InitStructure.DMA_Channel = DMA_Channel_3;//SPI1 Tx DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(SPI1->DR); //Set the SPI1 Tx DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&DACData; //Set the memory location DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; // DMA_InitStructure.DMA_BufferSize = 1; //Define the number of bytes to send DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //Normal mode (not circular) DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //Operate in 'direct mode' DMA_InitStructure.DMA_MemoryBurst =DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst =DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream5, &DMA_InitStructure); //DMA2, Channel 3, stream 5 //Enable the transfer complete interrupt for DMA2 Stream5 DMA_ITConfig(DMA2_Stream5, DMA_IT_TC, ENABLE); } ``` Here is how SPI1 initialized ``` void InitSPI1(void) { GPIO_InitTypeDef GPIO_InitStruct; SPI_InitTypeDef SPI_InitStruct; // enable clock for used IO pins RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStruct); // connect SPI1 pins to SPI alternate function GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1); //SCK GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1); //MISO GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1); //MOSI // enable peripheral clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); /* configure SPI1 in Mode 0 * CPOL = 0 --> clock is low when idle * CPHA = 0 --> data is sampled at the first edge */ SPI_InitStruct.SPI_Direction = SPI_Direction_1Line_Tx; // one line transmission SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // transmit in master mode, NSS pin has to be always high SPI_InitStruct.SPI_DataSize = SPI_DataSize_16b; // one packet of data is 16 bits wide SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; // clock is low when idle SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge; // data sampled at first edge SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;; // set the NSS management to hardware SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; // SPI frequency is APB2 frequency / 4 = 21MHz SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first SPI_Init(SPI1, &SPI_InitStruct); SPI_Cmd(SPI1, ENABLE); // enable SPI1 } ``` I try to use these commands to start DMA: ``` DACData[0]=0xff00; DMA_Cmd(DMA2_Stream5, ENABLE); ``` In case it may help, here comes the register values for DMA2, Stream 5