cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 and MEMS motion sensor over SPI

fat031cc
Associate II
Posted on February 26, 2009 at 00:27

STM32 and MEMS motion sensor over SPI

8 REPLIES 8
fat031cc
Associate II
Posted on May 17, 2011 at 13:03

Hello,

Probably my question is offtopic, but I have not found STM forum for motion detector sensors.

I am trying to connect LIS302DL to STM32F103 over SPI.

I am actually programmer so my knowledge in electronics is very limited. I didn't use additional schematics and directly connected CS, SCL, SDx, SDO of LIS302DL to the NSS, SCK, MOSI, MISO respectively.

With osciloscope I can see perfectly shaped square signal on SCL and SDx. But SDO pin (that has to be sensor's output signal) produces saw-like signal where each period looks like triangle.

Does this something to do with absense of necessary circuitry?

Or it is programmatic problem?

A tiny hint will be hugely appreciated.

Andrew

P.S. I will include all information about my SPI configuration if anyone responds to my desperate message :-).

domen2
Associate III
Posted on May 17, 2011 at 13:03

Hi!

Does this help?

http://www.stm32circle.com/resources/stm32primer.php

They use LIS3LV02DL, there are schematics, source code etc.

fat031cc
Associate II
Posted on May 17, 2011 at 13:03

Hello Domen,

thanks a lot for very interesting hint.

Thier schematics is the same (except of the fact that they use SPI2)

But as for SPI initialization their code is quite different. They are making 'SPI resynchronizaiton' that comes down to reinitializing SPI GPIO pins several times until it is possible to read identification byte from MEMS chip. Very interesting. I can't wait to check this on Monday.

Thanks again.

yah996
Associate II
Posted on May 17, 2011 at 13:03

Hi Domen,

i changed my MEMS from the I2C bus today to SPI interface.

This sample from ST32M circle looks strange to me. How ever can the sensor work proberly?

I did:

Set up the SPI as in the sample code.

New, I set the control register:

Reg 0x20 Enable the axis and so on...

Reg 0x21 Set the resolution bit from 12 to 16 bit.

Read out the MEMS as i discribed in the source code.

One time reading the the WHO AM I register should be fine. Even there is no need to read it anyways.

Greets,

K.

fat031cc
Associate II
Posted on May 17, 2011 at 13:03

Hello yah996,

As far as I understood from your post you were able to read from 'who am i' register in MEMS sensor ?

Which sensor did you use (LIS302DL or other)?

Could you please post the code how you did that.

I have been strugling with it for a week... :-Y

Huge thanks in advance.

Andrew.

fat031cc
Associate II
Posted on May 17, 2011 at 13:03

Hello Domen, I checked with the Circle OS code.

The resynchronization they are doing is aimed to restore communication with the sensor after pauses at breakpoints.

The initialization of GPIO and SPI is pretty similar to mine.

I am attaching my code with a slight hope that someone with the exeperience in SPI and LIS302DL could point at error.

Thanks.

Andrew

Code:

void GPIO_Configuration(void) {

GPIO_InitTypeDef GPIO_InitStructure;

/* Configure SPI1 pins: SCK, MOSI ---------------------------------*/

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7; //GPIO_Pin_6 |

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;//GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//GPIO_Mode_Out_PP;//GPIO_Mode_AF_PP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configure SPI1 pins: MISO ---------------------------------*/

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//GPIO_Mode_Out_PP;//GPIO_Mode_AF_PP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Configure SPI1 pins: SS ---------------------------------*/

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

}

Code:

int main(void)

{

RCC_Configuration();

NVIC_Configuration();

GPIO_Configuration();

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_High; // SPI_CPOL_Low;

SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //SPI_CPHA_2Edge;

SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//SPI_NSS_Soft; //SPI_NSS_Hard;//

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;//SPI_BaudRatePrescaler_4;

SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;//SPI_FirstBit_LSB;

SPI_InitStructure.SPI_CRCPolynomial = 7;

SPI_Init(SPI1, &SPI_InitStructure);

/* Enable SPI1 */

SPI_Cmd(SPI1, ENABLE);

u16 request = 0x0F;//0x0F;//0x29; // OutX register

request |= 0x80; // RW bit =1 for reading

// request |= 0x40; // MS bit = 1 for address increment

// request <<= 8;

int i = 0;

int j = 0;

u16 buf[100]={0};

GPIO_ResetBits(GPIOA, GPIO_Pin_4);

while(1)

{

//set low

GPIO_ResetBits(GPIOA, GPIO_Pin_4);

while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

SPI_I2S_SendData(SPI1, request);

while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

SPI_I2S_SendData(SPI1, 0);

//set high

GPIO_SetBits(GPIOA, GPIO_Pin_4);

while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

{

buf[i++] = SPI_I2S_ReceiveData(SPI1);

i%=100;

}

j++;

}

}

domen2
Associate III
Posted on May 17, 2011 at 13:03

You need to configure MISO as AF too, just like MOSI and SCK.

Also, your communication procedure looks a bit weird.

SPI is bidirectional, meaning that data arrives at same time as it's sent.

So one transfer should be something like this:

SS=0

SendByte

RecvByte

SendByte

RecvByte

...

SS=1

fat031cc
Associate II
Posted on May 17, 2011 at 13:03

Hello Domen,

puldown resistor on CS has solved my problem.

Great thanks!