cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F429 SPI 5 Problem - Clock

maharaj
Associate II
Posted on June 08, 2016 at 13:09

Hi all,

I am trying to read the onboard Gyro on STM32F4 discovery board. As mentioned in datasheets, Gyro(L3GD20) is conneted to SPI 5. I have configured everything, but I am not able to generate the required clock  on SCK pin.

SPI 5 SCK,MISO,MOSI and CS pins are - PF7,PF8,PF9,PC1 as per schematic. 

Because of this I thnik, I am not able to recive byte from SPI on my UART.

Pls help...

Thanks ...

void L3GD_CS_init(void)

{

RCC->AHB1ENR |= (RCC_AHB1ENR_GPIOCEN);

GPIOC->MODER |= 0x00000004; //PC1 as O/P

GPIOC->OSPEEDR |= 0x00;

//GPIOC->PUPDR |= 0x00000004; //Pull up PC1

//L3GD20_CS_HIGH;

}

void Spi_pins_set(void)

{

//PF6, PF7,PF8,PF9 as NSS,SCK,MISO,MOSI...refer to schmeatic for L3GD20 to SPI 5 as AF

//PC1 as CS pin for L3GD20...schematic

RCC->AHB1ENR |= (RCC_AHB1ENR_GPIOFEN);

GPIOF->MODER |= 0x000A8000;   

GPIOF->OTYPER |= ((uint16_t)0x0000); 

GPIOF->OSPEEDR |= 0x000A8000;

//GPIOF->PUPDR |= 0x00;

GPIOF->AFR[0] |= 0x50000000;

GPIOF->AFR[1] |= 0x00000055; //AF5 in AFR

}

void Spi_init(void)

{

//APB2 has clock 2x slower than processor speed, in my case I have 168MHz core clock, APB2 has 84MHz

//APB2 communication bus has 84MHz

//And because minimal prescaler for SPI is 2, max frequency is 42MHz

//SPI Clock = APBx / prescaler = 84MHz / 2 = 42MHz

//By default, prescaler 32 is used:

//SPI Clock = APBx / prescaler = 84MHz / 32 = 2.62MHz

/* Enable SPI clock */

RCC->APB2ENR |= RCC_APB2ENR_SPI5EN;

/* Disable first */

SPI5->CR1 &= ~SPI_CR1_SPE;

SPI5->CR1 |= SPI_CR1_BR_0 | SPI_CR1_BR_2 | SPI_CR1_MSTR; //Mode 0 SPI_CR1_LSBFIRST | SPI_CR1_SSM | SPI_CR1_SSI

/* Enable SPI */

SPI5->CR1 |= SPI_CR1_SPE;

}

uint8_t L3GD20_ReadSPI(uint8_t address) 

{

uint8_t data;

L3GD20_CS_LOW;

/* Send address with read command */

SPI_Send_byte(SPI5, address | 0x80);

/* Read data */

data = SPI_Send_byte(SPI5, 0xFF);

//L3GD20_CS_HIGH;

return data;

}

int main(void)

{

uint8_t ch=0;

int i=0;

SystemInit();

usart1_set();

usart1_init();

Spi_init();

Spi_pins_set();

led_init();

L3GD_CS_init();

while(1)

{

ch = L3GD20_ReadSPI(L3GD20_REG_WHO_AM_I); //in L3GD20.h file 

USART1_send_char(ch);

USART1_send_char(0x0D);

for(i=0;i<5000000;i++);

}

}

#discovery #l3gd20 #stm32f429 #spi #gyroscope
1 REPLY 1
slimen
Senior
Posted on June 09, 2016 at 16:05

Hi, 

I suggest that you start from CubeMX where you configure your SPI and generate the initialization code.  Also, you can refer to the examples under the STM32CubeF4 package, which can help you to develop your application:

STM32Cube_FW_F4_V1.12.0\Projects\STM32F4-Discovery\Examples\SPI

Regards