STM32F4 DMA SPI TX Problemi - Çözüldü

Başlatan baran123, 01 Mayıs 2018, 17:40:37

baran123

Selamlar,

STM32F4 Discovery kartı ile SPI1 ve DMA2 kullanarak bir diziyi transfer etmeye çalışıyorum.
Bunu yaparken Logic Analyzer ile incelediğimde anlamsız bir karşılık görüyorum.
Sanırım konfigurasyon kısmında bir hata yapıyorum.
Çıktı ve kodlar aşağıda incelerseniz çok memnun olurum.



#define SPIx                           SPI1
#define SPIx_DMA                       DMA2
#define SPIx_TX_DMA_CHANNEL            DMA_Channel_3
#define SPIx_TX_DMA_STREAM             DMA2_Stream3
#define SPIx_TX_DMA_FLAG_TCIF          DMA_FLAG_TCIF3
#define SPIx_TX_BUFFER_SIZE            5

uint8_t aTxBuffer[SPIx_TX_BUFFER_SIZE] = {'B', 'A', 'R', 'A', 'N'};

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);

GPIO_InitTypeDef	GPIO_InitStructure;

GPIO_DeInit(GPIOA);
GPIO_DeInit(GPIOD);
GPIO_StructInit(&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Mode 	= GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin 	= GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_PuPd 	= GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Mode 	= GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType	= GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin	= GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_PuPd	= GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1); // SCLK
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1); // MISO
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1); // MOSI

SPI_InitTypeDef	SPI_InitStructure;
DMA_InitTypeDef 	DMA_InitStructure;

SPI_I2S_DeInit(SPIx);
SPI_InitStructure.SPI_Direction 				= SPI_Direction_Tx;
SPI_InitStructure.SPI_DataSize 				= SPI_DataSize_8b;
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_64;
SPI_InitStructure.SPI_FirstBit 				= SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial 		= 7;

DMA_DeInit(SPIx_TX_DMA_STREAM);

DMA_InitStructure.DMA_BufferSize 			= SPIx_TX_BUFFER_SIZE;
DMA_InitStructure.DMA_FIFOMode 				= DMA_FIFOMode_Disable ;
DMA_InitStructure.DMA_FIFOThreshold 		= DMA_FIFOThreshold_1QuarterFull;
DMA_InitStructure.DMA_MemoryBurst 			= DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_MemoryDataSize 		= DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_MemoryInc 				= DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_Mode 					= DMA_Mode_Normal;
DMA_InitStructure.DMA_PeripheralBaseAddr 	= (uint32_t)(&(SPIx->DR));
DMA_InitStructure.DMA_PeripheralBurst 		= DMA_PeripheralBurst_Single;
DMA_InitStructure.DMA_PeripheralDataSize	= DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_PeripheralInc 		= DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_Priority 				= DMA_Priority_VeryHigh;

DMA_InitStructure.DMA_Channel					= SPIx_TX_DMA_CHANNEL;
DMA_InitStructure.DMA_DIR 						= DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_Memory0BaseAddr		= (uint32_t)aTxBuffer;
DMA_Init(SPIx_TX_DMA_STREAM, &DMA_InitStructure);

SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

SPI_Init(SPIx, &SPI_InitStructure);
DMA_Cmd(SPIx_TX_DMA_STREAM, ENABLE);
SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, ENABLE);
SPI_Cmd(SPIx, ENABLE);

while (DMA_GetFlagStatus(SPIx_TX_DMA_STREAM, SPIx_TX_DMA_FLAG_TCIF) == RESET);

DMA_ClearFlag(SPIx_TX_DMA_STREAM, SPIx_TX_DMA_FLAG_TCIF);
DMA_Cmd(SPIx_TX_DMA_STREAM, DISABLE);
SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, DISABLE);
SPI_Cmd(SPIx, DISABLE);

baran123

#1
Konu çözülmüştür

const uint32_t SPIx_TX_BUFFER_SIZE = 4;
uint8_t src[SPIx_TX_BUFFER_SIZE] = {'T', 'E', 'S', 'T'};

SPI_InitTypeDef		SPI_InitStructure;
DMA_InitTypeDef 	DMA_InitStructure;
NVIC_InitTypeDef 	NVIC_InitStructure;

SPI_InitStructure.SPI_Direction 			= SPI_Direction_1Line_Tx;// SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode 					= SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize 				= SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL 					= SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA 					= SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS 					= SPI_NSS_Soft | SPI_NSSInternalSoft_Set;
SPI_InitStructure.SPI_BaudRatePrescaler		= SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_FirstBit 				= SPI_FirstBit_MSB;
SPI_Init(SPI1, &SPI_InitStructure);

DMA_StructInit(&DMA_InitStructure);
DMA_InitStructure.DMA_Channel 				= DMA_Channel_3;
DMA_InitStructure.DMA_PeripheralBaseAddr  	= (uint32_t)&(SPI1->DR);
DMA_InitStructure.DMA_Memory0BaseAddr  		= (uint32_t)&src;
DMA_InitStructure.DMA_DIR 					= DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize  			= SPIx_TX_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_High;
DMA_InitStructure.DMA_FIFOMode  			= DMA_FIFOMode_Disable;
DMA_Init(DMA2_Stream5, &DMA_InitStructure);
DMA_ITConfig(DMA2_Stream5, DMA_IT_TC, ENABLE);

NVIC_InitStructure.NVIC_IRQChannel 						= DMA2_Stream5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority	= 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority			= 1;
NVIC_InitStructure.NVIC_IRQChannelCmd 					= ENABLE;
NVIC_Init(&NVIC_InitStructure);

SPI_Cmd(SPI1, ENABLE);

// Herhangi bir yerde şu kodu çalıştırın. src burada veri dizisi oluyor
DMA2_Stream5->M0AR = (uint32_t)&src;
DMA_Cmd(DMA2_Stream5, ENABLE);
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);