cancel
Showing results for 
Search instead for 
Did you mean: 

How to configure communication with SPI Clock

davide_litrico
Associate II
Posted on November 26, 2012 at 13:07

Hi fella. I want established a communication between a board stm32f4 discovery and a sensor LIS3LV02DQ by SPI interface. But I don't understand how to configure the clock to sincronize the board and the sensor. I try to configurate SPI interface throught the following code:

'' GPIO_InitTypeDef GPIO_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); // GRUPPO C

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;

GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOC, GPIO_PinSource15, GPIO_AF_SPI2);

/*PULISCO SPI2*/

SPI_I2S_DeInit(SPI2);

SPI_InitStructure.SPI_Direction = SPI_Direction_1Lines_Rx;

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_InitStructure.SPI_Mode = SPI_Mode_Master;

SPI_Init(SPI2, &SPI_InitStructure);''

Can you help me to understand how setup correctly the SPI interface? Thanks to all

#google-is-your-friend #clock #spi #overdrawn
5 REPLIES 5
Posted on November 26, 2012 at 17:10

You'd need to set up the MISO, MOSI and CS pins presumably, and no clock is generated until you send data across the bus.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
davide_litrico
Associate II
Posted on November 26, 2012 at 19:37

thanks for your answer, but i don't understand why i must set up both miso and mosi pin? my board must only read data from sensors. This sensors has also a pin SCK for connect the clock of the master (i think), this pin must be leave free?

Andrew Neil
Chief II
Posted on November 27, 2012 at 19:29

''i don't understand why i must set up both miso and mosi pin?''

 

In general, SPI is 2-way - so it requires both MOSI and MISO.

''my board must only read data from sensors.''

 

You did not previously mention that!

''This sensors has also a pin SCK for connect the clock of the master (i think)''

 

You had better study the documentation for this sensor carefully until you are sure of the function of this pin!

If in doubt, contact the sensor manufacturer.

''this pin must be leave free?''

No:  it must be connected to the SCK output form the Master. The clock in SPI always comes from the Master.

You are sure that your sensor really is an SPI slave - aren't you?

Normally, the SPI Master will only generate clocks while it is transmitting - so you need to transmit ''dummy'' data while you are reading.

Not sure if the STM32 would actually require the MOSI pin to be configured in this case?

Andrew Neil
Chief II
Posted on November 27, 2012 at 19:33

This board features the same accelerometer connected to an STM32:

http://www.st.com/internet/evalboard/product/183579.jsp

davide_litrico
Associate II
Posted on November 27, 2012 at 20:45

hi thank you for the answer. In the datasheet of the sensor is wrote that ''LIS3LV02DQ SPI is a bus slave''. 

If i understand correctly your answer i must setup both miso and mosi pins, using the SPI_Direction=''SPI_Direction_2Lines_FullDuplex'', but i have other two questions:

1)why in the libraries there is the modalidity ''SPI_Direction_1Line_Rx''? (is useful if the board stm32f4 is used as slave??) 

2)If I setup both MISO and MOSI pins, and send ''dummy'' data to generate the clock, this ''dummy'' data must be send continously during the reading? or enough just send a byte and than read from spi register?