2013-03-21 11:21 AM
I am using an STM32F4, and I'm having difficulty getting SPI to transmit using DMA. If I manually write to the SPI data register, I see the appropriate activity on the logic analyzer, but if I use DMA to initiate the transaction, I see nothing.
I want to transmit on SPI2, so according to the reference manual, I am using DMA 1 Stream 4, Channel 0. Setting a breakpoint in DMA_GetCmdStatus shows that the CR bit never goes high indicating a problem with the DMA initialization. I've enabled the DMA clock and initialized all the parameters I could think of, but it does not work. I've compared my initialization to other examples and I can't spot any glaring errors. Am I missing something in initialization?// Includes
#include ''stm32f4xx.h''
// SPI2 Interface pins
#define SPI2_SCK_PIN GPIO_Pin_13
#define SPI2_SCK_SOURCE GPIO_PinSource13
#define SPI2_SCK_GPIO_PORT GPIOB
#define SPI2_SCK_GPIO_CLK RCC_AHB1Periph_GPIOB
#define SPI2_MISO_PIN GPIO_Pin_14
#define SPI2_MISO_SOURCE GPIO_PinSource14
#define SPI2_MISO_GPIO_PORT GPIOB
#define SPI2_MISO_GPIO_CLK RCC_AHB1Periph_GPIOB
#define SPI2_MOSI_PIN GPIO_Pin_15
#define SPI2_MOSI_SOURCE GPIO_PinSource15
#define SPI2_MOSI_GPIO_PORT GPIOB
#define SPI2_MOSI_GPIO_CLK RCC_AHB1Periph_GPIOB
#define SPI_BUFFER_SIZE 4
// Private functions
void
delay(uint32_t delayTicks)
{
while
( delayTicks-- != 0 );
}
void
Spi2Init(uint8_t* txBuffer)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
// Enable the SPI clocks
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_AHB1PeriphClockCmd(SPI2_SCK_GPIO_CLK, ENABLE);
RCC_AHB1PeriphClockCmd(SPI2_MISO_GPIO_CLK, ENABLE);
RCC_AHB1PeriphClockCmd(SPI2_MOSI_GPIO_CLK, ENABLE);
// Connect SPI pins to alternate functions
GPIO_PinAFConfig(SPI2_SCK_GPIO_PORT, SPI2_SCK_SOURCE, GPIO_AF_SPI2);
GPIO_PinAFConfig(SPI2_MISO_GPIO_PORT, SPI2_MISO_SOURCE, GPIO_AF_SPI2);
GPIO_PinAFConfig(SPI2_MOSI_GPIO_PORT, SPI2_MOSI_SOURCE, GPIO_AF_SPI2);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
// SPI SCK pin configuration
GPIO_InitStructure.GPIO_Pin = SPI2_SCK_PIN;
GPIO_Init(SPI2_SCK_GPIO_PORT, &GPIO_InitStructure);
// SPI MISO pin configuration
GPIO_InitStructure.GPIO_Pin = SPI2_MISO_PIN;
GPIO_Init(SPI2_MISO_GPIO_PORT, &GPIO_InitStructure);
// SPI MOSI pin configuration
GPIO_InitStructure.GPIO_Pin = SPI2_MOSI_PIN;
GPIO_Init(SPI2_MOSI_GPIO_PORT, &GPIO_InitStructure);
// SPI configuration
SPI_I2S_DeInit(SPI2);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_Init(SPI2, &SPI_InitStructure);
// Configure DMA for transmit
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
DMA_DeInit(DMA1_Stream4);
DMA_StructInit(&DMA_InitStructure);
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) (&SPI2->DR);
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)txBuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = SPI_BUFFER_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_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream4, &DMA_InitStructure);
DMA_ITConfig(DMA1_Stream4, DMA_IT_TC, ENABLE);
// Configure DMA1 Stream4 interrupt
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// Enable SPI2
SPI_Cmd(SPI2, ENABLE);
// Enable DMA request
SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);
}
uint8_t Spi2SendByte(uint8_t byte)
{
// Loop while DR register in not empty
while
(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
// Send byte through the SPI2 peripheral
SPI_I2S_SendData(SPI2, byte);
// Wait to receive a byte
while
(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
// Return the byte read from the SPI bus
return
SPI_I2S_ReceiveData(SPI2);
}
int
main(
void
)
{
uint8_t i = 0;
static
uint8_t dataToTransmit[SPI_BUFFER_SIZE];
for
(i = 0; i < SPI_BUFFER_SIZE; i++)
{
dataToTransmit[i] = i;
}
// Initialize the SPI
Spi2Init(dataToTransmit);
while
(1)
{
//Spi2SendByte(i++);
DMA_Cmd(DMA1_Stream4, ENABLE);
DMA_GetCmdStatus(DMA1_Stream4);
delay(200000);
}
}
2013-03-22 02:09 AM
I tried your code here and it works as expected - it results in 4 bytes being transmitted by SPI. You probably expect that transactions will repeat inifinitely as per the while(1) loop, but you'd need to rewrite the DMA_SxNDTR register after finding out the transfer is complete, and also clear the flags in DMA_HIFCR, to achieve that.
I don't know what do you mean by CR bit, but if you meant EN bit in the DMA_SxCR register, then oh, it's long gone until you get into that routine, as the DMA machine won't wait just because you use a debugging gadget. JW2013-03-28 08:48 AM
I am having a very similar problem. The first block is transmitted out the SPI, and I get the DMA TC interrupt. But then I cannot get it to send the next block.
In DMA1_Stream3_IRQHandler() I clear the TCIF with: DMA_ClearITPendingBit(DMA1_Stream3, DMA_IT_TCIF3); And then elsewhere on a timer: DMA_ITConfig(DMA1_Stream3, DMA_IT_TC, ENABLE); But I don't understand how you get the next batch to start transferring. SPI1->CR1 already has SPI_CR1_SPE set. And SPI1->CR2 has SPI_I2S_DMAReq_Tx still set after the DMA TC interrupt. Does anyone know? Bill2013-03-28 08:51 AM
You'd need to reprogram the next DMA transaction.
2013-03-28 09:50 AM
How?
I'm setting: DMA1_Stream3->NDTR = len; DMA1_Stream3->M0AR = &buf; DMA1_Stream3->CR |= DMA_SxCR_EN;2013-03-28 09:53 AM
Actually I don't know if SPI is sending anything.
All I know is I get the DMA TC interrupt. But at the point the NDTR still contains the original programmed value. Shouldn't it have counted down to 0 before I got the TC interrupt?2013-03-28 10:01 AM
How?
I'd start by using the DMA_Init() method. If that works then I might go trimming down what needs changing.2013-03-28 10:39 AM
I started this by porting code from another STM32 device. Not the F4xx.
Now I see the DMA stream associated with SPI1 is DMA2, not DMA1 that I'm using. That would explain a lot.2013-03-28 12:14 PM
Working now.
Had the wrong DMA and DMA channel, and the GPIO clock wasn't enabled before I set the alternate pin function for the SPI Tx pin.2014-02-07 11:28 AM
I posted this problem separately, and then came across this one which describes my scenario exactly. I have built the project above, compiled for the STM32F427, with only two pin changes:
// SPI2 Interface pins #define SPI2_SCK_PIN GPIO_Pin_3 #define SPI2_SCK_SOURCE GPIO_PinSource3 #define SPI2_SCK_GPIO_PORT GPIOD #define SPI2_SCK_GPIO_CLK RCC_AHB1Periph_GPIOD #define SPI2_MISO_PIN GPIO_Pin_14 #define SPI2_MISO_SOURCE GPIO_PinSource14 #define SPI2_MISO_GPIO_PORT GPIOB #define SPI2_MISO_GPIO_CLK RCC_AHB1Periph_GPIOB #define SPI2_MOSI_PIN GPIO_Pin_15 #define SPI2_MOSI_SOURCE GPIO_PinSource15 #define SPI2_MOSI_GPIO_PORT GPIOB #define SPI2_MOSI_GPIO_CLK RCC_AHB1Periph_GPIOB I get NO traffic on the SPI when using the DMA. I understand OP's program is supposed to send four bytes, and not loop, but I get NO traffic. As the OP noted, if I manually send data via SPI2, I get SPI traffic as expected: while(1) { SPI_I2S_SendData(SPI2, 0x77); }