cancel
Showing results for 
Search instead for 
Did you mean: 

Hi, I need to communicate the LSM202AGR accelerometer with I2C . Can you help? Some hints ? The info included in datasheet is not very usefull.

DrAK
Associate II
 
24 REPLIES 24
Peter BENSCH
ST Employee

Welcome, @DrAK​, to the community!

Maybe that's a typo and you mean LSM303AGR?

You can find the driver as source code on Github.

Good luck!

If the problem is resolved, please mark this topic as answered by selecting Select as best. This will help other users find that answer faster.

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
DrAK
Associate II

och, indeed I meant LSM303AGR.

The driver alone won't help me much. I need an example how to use it.

thanks anyway,

Hi Peter,

I found the Examples and a kind of description how to use them. There are however a few points which are not quite clear:

The first is, what shall I uncomment here:

0693W00000GXTgBQAX.pngMy board is nucleo-G071RB

Here

0693W00000GXTg1QAH.pngI do not see stm32f4xx_hal.h

Can you advice ??

To keep it clear, the example mentioned focuses on three typical platforms and clearly shows how to use it. But it also mentions directly above in the source code at line 47:

/* STMicroelectronics evaluation boards definition
 *
 * Please uncomment ONLY the evaluation boards in use.
 * If a different hardware is used please comment all
 * following target board and redefine yours.
 */

There are currently around 55 matching NUCLEO boards, so that the source text with all its options would be too confusing.

Your board NUCLEO-G071RB is very similar to the NUCLEO_F411RE, so you might use the conditional definitions

#if defined (NUCLEO_F411RE)

as a template and copy/change them to

#if defined (NUCLEO_G071RB)

Once you understand how the NUCLEO and the sensor work, it will be easy for you to make the changes.

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
I did what you did and that does not work: [image: image.png] I do not see the *stm32f4xx_hal.h* file anywhere. should one not *#include* to avoid the last warning ?? pon., 15 lis 2021 o 08:33 ST Community napisał(a):

Unfortunately, your picture was not inserted correctly so that nothing can be seen here.

Well, my hints weren't a recipe that you just have to paste, but hints as to which code blocks you have to make changes in.

The #define and #if defined are used to make hardware-specific settings in the source code. So you should have a look at stm32f4xx_hal.h from the repository (locally on your computer if STM32F4 firmware is installed, alternatively on Github), plus the schematics and description of the NUCLEO-F411RE.

  1. Then include stm32g0xx_hal.h and copy e.g. the define blocks that were made for the NUCLEO-F411RE, but provide them with new name, e.g. #if defined (NUCLEO_G071RB) for the NUCLEO-G071RB.

Finally compare the NUCLEO-F411RE with your NUCLEO-G071RB (schematics, description) and make the appropriate changes to the copied blocks.

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
DrAK
Associate II

Well, you did not

I did what you had suggested,. That is, I copied the fragments concerning NUCLEO-F411RE and changed them appropriately. In particular, there I got:

/* Includes ------------------------------------------------------------------*/
#include "lsm303agr_reg.h"
#include <string.h>
#include <stdio.h>
 
#if defined(NUCLEO_F411RE)
#include "stm32f4xx_hal.h"
#include "usart.h"
#include "gpio.h"
#include "i2c.h"
 
#elif defined(NUCLEO_G071RB)
#include "stm32g0xx_hal.h"
//#include "usart.h"
//#include "gpio.h"
//#include "i2c.h"
 
#elif defined(STEVAL_MKI109V3)
#include "stm32f4xx_hal.h"
#include "usbd_cdc_if.h"
#include "gpio.h"
#include "spi.h"
#include "tim.h"
 
#elif defined(SPC584B_DIS)
#include "components.h"
#endif

It turned out that without commenting out the lines 14-16 above I got messages that the h-files do not exist.

This however did not help, for after that I get the error:

0693W00000GY4MNQA1.png 

Well, the variable hi2c1 is defined here:

0693W00000GY4NuQAL.pngin main but but it is not seen in the very example function .

I do enclose the ZIP with the whole src directory. please make necessary corrections and share.

DrAK
Associate II

Hi, Peter,

will you try to answer my question, please?

I tried to buy the board NUCLEO-F411RE but they are out of store:

0693W00000GYbPkQAL.pngwe need more pieces than just only a few.

If I will not be able to run the LSM303ARG with NUCLEO_G071RB, we will have to give up the idea of using your boards our project (we are talking about many pieces, not only one or three).

so please, try to help me relatively quickly.

Sorry, I didn't see a question other than a request to correct the code. However, rarely someone will change the given code here, but will give hints on how to do it yourself.

However: if the compiler cannot find the .h files, they are not included in the project. In your case, this is due to the fact that you probably created the frame program with STM32CubeIDE, whereby the default setting in the Project Manager ensures that the code for all peripherals is put together in one main.c/.h. If you check Generate peripheral initialization as a pair... in Project Manager > Code Generator, you will get the .c/.h files you are looking for, e.g. gpio.c/.h, i2c.c/.h, etc. But every programmer does this the way he likes it and in your case it is not absolutely necessary to change this setting, just leave the missing .h files commented out.

But it would be helpful to read the introductions that you can find at the very bottom of the main Github site and at the device driver site, in your case the driver site of the LSM303AGR. The main site also contains an example .ioc file for the NucleoF411, see here, which can be used as a template.

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.