AnsweredAssumed Answered

SPI Clock Frequency

Question asked by brian on Oct 18, 2012
Latest reply on Oct 19, 2012 by malund.erik
Hi,

I'm having a bit of trouble configuring the SPI2 clock frequency on an STM32F407VG, or perhaps I am confused as to how it works.

Using a 25Mhz external oscillator, boosting up to system clock frequency of 168MHz.
AHB prescaler is 1, so the AHB clock is 168MHz.
APB1 prescaler is 4, so the APB1 clock (PCLK1) should be 42MHz.

I'm setting up the SPI2 peripheral with this bit of code:
     SPI_InitStructure.SPI_Direction = 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_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
     SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
     SPI_InitStructure.SPI_CRCPolynomial = 7;
     SPI_Init(SPI2, &SPI_InitStructure);

And then I am just sending a fixed byte:
     while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE)==RESET);
     SPI_I2S_SendData(SPI2, 0xA5);

When I scope the SCLK output, I am seeing 8Mhz.  I thought that the baud rate was supposed to be derived from PCLK1, so a prescaler of 2 should give me a rate of 21MHz.

The clock setup was generated using the tool provided by STM.

Anyone know what I might be doing wrong or if I am misunderstanding something?

Outcomes