cancel
Showing results for 
Search instead for 
Did you mean: 

First attempt at SPI DMA not working

craig
Associate II
Posted on August 07, 2013 at 05:34

I've lurked for quite some time and learned a ton but am stumped on this one and can't find a solution. I'm new to ARM (8-bit AVR convert) and I'm afraid something simple is escaping me on this one. I've got a fully functional blocking SPI version of my code sending data to an LED strip. However, upgrading it to DMA has proven a challenge (my first time using DMA). I've hooked up my logic analyzer and confirmed that there is absolutely no activity on SCK/MOSI with the code below when calling the DMA_beginWrite(). Any ideas why?

PS - My code was more/less ported from the example here (though he's using SPI2 and of course DMA1):

http://www.micromouseonline.com/2012/03/11/adding-dma-to-the-spi-driver-with-the-stm32f4/#axzz2bESa5DIM

void DMA_init()
{
//Start with a fresh DMA config
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA2_Stream3);
DMA_StructInit(&DMA_InitStructure); 
//Enable the clock
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
//Setup SPI DMA for TX
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(SPI1->DR); 
DMA_InitStructure.DMA_Channel = DMA_Channel_3; 
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; 
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 
DMA_InitStructure.DMA_Memory0BaseAddr = 0; //Setting only needed to suppress in case assert macro is active
DMA_InitStructure.DMA_BufferSize = 1; //Setting only needed to suppress in case assert macro is active
DMA_Init(DMA2_Stream3, &DMA_InitStructure);
DMA_ITConfig(DMA2_Stream3, DMA_IT_TC, ENABLE);
//Setup interrupt for completed DMA transfers
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); 
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE); //Enable DMA TX request
}
void SPI_PORT_DMA_TX_IRQHandler()
{
//DMA stream transfer complete?
if (DMA_GetITStatus(DMA2_Stream3, DMA_IT_TCIF4))
{
DMA_ClearITPendingBit(DMA2_Stream3, DMA_IT_TCIF4);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) { }; 
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET) { };
}
}
void DMA_beginWrite()
{
DMA2_Stream3->NDTR = (uint32_t) sizeof(_pixelData);
DMA2_Stream3->M0AR = (uint32_t) _pixelDataPtr;
DMA_Cmd (DMA2_Stream3, ENABLE);
}
void SPI_init()
{
//Enable clock for GPIOA
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
//Configure GPIO pins for SPI1 (PA5=SCK, PA7=MOSI)
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_5;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
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);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
// 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_InitTypeDef SPI_InitStruct;
SPI_InitStruct.SPI_Direction = SPI_Direction_1Line_Tx; // set to TX only (MOSI used)
SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // transmit in master mode, NSS pin has to be always high
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 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; // set the NSS management to internal and pull internal NSS high
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; // SPI frequency is APB2 frequency / 2
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
//SPI_InitStruct.SPI_CRCPolynomial = 0; //No CRC used
SPI_Init(SPI1, &SPI_InitStruct); 
SPI_Cmd(SPI1, ENABLE); // enable SPI1 
}
void SPI_write(uint8_t data)
{
while( !(SPI1->SR & SPI_I2S_FLAG_TXE) ); // wait until transmit complete
SPI1->DR = data; // write data to be transmitted to the SPI data register
}

0 REPLIES 0