cancel
Showing results for 
Search instead for 
Did you mean: 

spi receive data via dma

azizbarkoui
Associate
Posted on December 02, 2013 at 18:15

i traied to recive data from another stm32f4 via SPI that's why i used the dma to receive data from SPI  

GPIO_InitTypeDef  GPIO_InitStructure;

uint16_t value;

void init_SPI(void)

{

GPIO_InitTypeDef GPIO_InitStruct;

SPI_InitTypeDef SPI_InitStruct;

 DMA_InitTypeDef       DMA_InitStructure;

// enable clock for used IO pins

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

/* DMA2 Stream0 channel0 configuration **************************************/

DMA_InitStructure.DMA_Channel = DMA_Channel_0;

DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Buffer;

DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(SPI1->DR); 

DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;

DMA_InitStructure.DMA_PeripheralDataSize = DMA_MemoryDataSize_Byte;

DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;

DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;

DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

DMA_InitStructure.DMA_BufferSize = 6*84;

DMA_InitStructure.DMA_Priority = DMA_Priority_High;

DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;

DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;

DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; 

DMA_Init(DMA1_Stream4, &DMA_InitStructure);

DMA_Cmd(DMA1_Stream4, ENABLE);

 

// ...

SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);

/* configure pins used by SPI1

* PA5 = SCK

* PA6 = MISO

* PA7 = MOSI

*/

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);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_SPI1);

// enable clock for used IO pins

/* Configure the chip select pin

  in this case we will use PA4 */

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;

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_UP;

GPIO_Init(GPIOA, &GPIO_InitStruct);

// 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_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines

SPI_InitStruct.SPI_Mode = SPI_Mode_Slave;     // 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 | SPI_NSSInternalSoft_Set; // set the NSS management to internal and pull internal NSS high

SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; // SPI frequency is APB2 frequency / 4

SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first

SPI_Init(SPI1, &SPI_InitStruct); 

SPI_Cmd(SPI1, ENABLE); // enable SPI1

}

int main(void)

{

init_SPI();

  STM32F4_Discovery_LEDInit(LED3);

  STM32F4_Discovery_LEDInit(LED4);

  STM32F4_Discovery_LEDInit(LED5); 

STM32F4_Discovery_LEDInit(LED6);

while(1) {

value=SPI_I2S_ReceiveData(SPI1);

if (value<400)

STM32F4_Discovery_LEDOn(LED5),

 STM32F4_Discovery_LEDOff(LED6);

else if (value >400)

 STM32F4_Discovery_LEDOn(LED6),

STM32F4_Discovery_LEDOff(LED5);

;}

 

}

2 REPLIES 2
Posted on December 02, 2013 at 18:43

I'm confused, DMA RX on SPI1 in Slave Mode?

Look to be using the wrong DMA, Channel and Stream for that. Not enabling DMAx unit clock. Data direction of DMA is wrong. The code seems to be a random mix of cut-n-paste with no cohesive plan.

SPI1 would use DMA2, Channel 3, Stream 0 or 2 for reception of data, to send data (from a slave to master) you'd transmit via DMA2, Channel 3, Stream 3 or 5
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
M_1
Associate II
Posted on December 03, 2013 at 10:13

DMA_InitStructure.DMA_DIR = DMA_DIR_

Peripheral

ToMemory;

DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(SPI1->DR));