cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f407 SPI problem.

ciel076
Associate II
Posted on August 04, 2015 at 09:54

Hello. I'm using stm32f407 eval board.

I want to communicate with gyro sensor in the board via SPI.

But I can't get any signal from SPI AFIO pins.

Please tell me if you find any errors from my codes.

// SPI1 SCK(PA5), SPI1 MOSI(PA7), SPI1 MISO(PA6)

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6| GPIO_Pin_7;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_OType =  GPIO_OType_OD ;  

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);

//Chip select for LIS302DL (PE3)

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

 GPIO_Init(GPIOE, &GPIO_InitStructure);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA |RCC_AHB1Periph_GPIOB |RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD |RCC_AHB1Periph_GPIOE,ENABLE);

void SPI_Configuration(void)

{

SPI_InitTypeDef SPI_WITHROBOT;

SPI_WITHROBOT.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

SPI_WITHROBOT.SPI_Mode = SPI_Mode_Master;

SPI_WITHROBOT.SPI_DataSize = SPI_DataSize_8b;

SPI_WITHROBOT.SPI_CPOL = SPI_CPOL_High;  //for stm32f407 eval board

SPI_WITHROBOT.SPI_CPHA = SPI_CPHA_2Edge; //for stm32f407 eval board

SPI_WITHROBOT.SPI_NSS = SPI_NSS_Soft;

SPI_WITHROBOT.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; // smt32f407 eval board max :  10Mhz Baud Rate

SPI_WITHROBOT.SPI_FirstBit = SPI_FirstBit_MSB;

SPI_WITHROBOT.SPI_CRCPolynomial = 7;

SPI_Init(SPI1,&SPI_WITHROBOT);

SPI_Cmd(SPI1,ENABLE);

}

int SPI1_Read(int address)

{

int data;

GPIOE->BSRRH = GPIO_Pin_3;  //nss low

while((SPI1->SR & SPI_I2S_FLAG_TXE) == RESET);

SPI1->DR = (address|0x80);

while((SPI1->SR & SPI_I2S_FLAG_TXE) == RESET);

SPI1->DR = (0xFF);

while((SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET);

data = SPI1->DR;

while((SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET);

data = SPI1->DR;

while((SPI1->SR & SPI_I2S_FLAG_TXE) == RESET);

while((SPI1->SR & SPI_I2S_FLAG_BSY) == SET);

GPIOE->BSRRL = GPIO_Pin_3;  //nss high

return data;

}

Thank you

1 REPLY 1
megahercas6
Senior
Posted on August 04, 2015 at 11:11

void SPI_FPGA_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI4, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource2, GPIO_AF_SPI4);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource5, GPIO_AF_SPI4); 
GPIO_PinAFConfig(GPIOE, GPIO_PinSource6, GPIO_AF_SPI4);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_5|GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOE, &GPIO_InitStructure);
SPI_I2S_DeInit(SPI4);
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
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_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI4, &SPI_InitStructure);
SPI_Cmd(SPI4, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIOE->BSRRL=GPIO_Pin_0|GPIO_Pin_1; 
}
uint16_t f[3]={0,0,0};
void FPGA_Update(void)
{
GPIOE->BSRRH=GPIO_Pin_0; //CS LOW
while (SPI_I2S_GetFlagStatus(SPI4, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI4, 1);
while (SPI_I2S_GetFlagStatus(SPI4, SPI_I2S_FLAG_RXNE) == RESET);
f[0]=SPI_I2S_ReceiveData(SPI4);
while (SPI_I2S_GetFlagStatus(SPI4, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI4, 1);
while (SPI_I2S_GetFlagStatus(SPI4, SPI_I2S_FLAG_RXNE) == RESET);
f[1] = SPI_I2S_ReceiveData(SPI4);
while (SPI_I2S_GetFlagStatus(SPI4, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI4, 2);
while (SPI_I2S_GetFlagStatus(SPI4, SPI_I2S_FLAG_RXNE) == RESET);
f[2]=SPI_I2S_ReceiveData(SPI4);
GPIOE->BSRRL=GPIO_Pin_0; //CS HIGH
}

Just modify pin, port, and clock supply enable functions as well as SPI, and it should work with no problems