AnsweredAssumed Answered

stm32f407 SPI problem.

Question asked by park.homin on Aug 4, 2015
Latest reply on Aug 4, 2015 by karpavicius.linas
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

Outcomes