cancel
Showing results for 
Search instead for 
Did you mean: 

SPI clock initialisation problem

Posted on June 30, 2015 at 10:05

Hi everybody,

I have a problem while doing the configuration of the SPI on my stm32f051r8. Indeed, I've written the hole initialization code but I can't see the the clock with a scope.. Can you help me? Thank s a lot

void spi_conf()
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/* Enable SPI clock, SPI1 */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
/* SPI SCK, MOSI, MISO pin configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_1; // 10 MHz
GPIO_Init(GPIOB, &GPIO_InitStructure);
// Configure CS pin as output floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_0); // SPI1 SCK
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_0); // SPI1 MOSI
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_0); // SPI1 MISO
/* SPI configuration -------------------------------------------------------*/
SPI_I2S_DeInit(SPI2);
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_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_SSOutputCmd(SPI2, ENABLE);
SPI_Cmd(SPI2, ENABLE);
}
int main(void)
{
spi_conf();
while(1)
{
}
}

3 REPLIES 3
stm322399
Senior
Posted on June 30, 2015 at 11:18

SPI clock is not permanent. Send a byte and you'll get some chance to see the clock signal.

Posted on June 30, 2015 at 11:32

Thx for your answer, I just added in the while(1) of the main

SPI_SendByte(10);

The definition of the function is:

uint8_t SPI_SendByte(uint x)
{
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); // TX Empty (Front Test)
SPI_SendData8(SPI1, x);
// while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); // Wait for data to shift out
return(SPI_ReceiveData8(SPI2)); // Read to clear RXNE
}

But I still can't see any clock...
Posted on June 30, 2015 at 11:45

I just forgot to change a spi1 in spi2, it works now!

thank you a lot!