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

thank you for your answer,

Actually I need to read acceleration and magnetic components using G071RB and LSM303AGR with STM32CubeIDE.

I tried to understand anything reading the Datasheet, but it is really hard (well, I am not familiar with such devices. I rather deal with different things usually). I managed to get proper who am I values, and reading the Acceleration components and their status register give some values (they change in time). The velues of magnetometer are constantly 0 and their status is constantly 0xFF, although the reading procedures git HAL_OK as output:

0693W00000GZJoAQAX.pngI did initialize the registers as it was suggested in Datasheet:

0693W00000GZK76QAH.png 

0693W00000GZKQhQAP.png 

where HAL*feed_1B is:

0693W00000GZKTCQA5.png 

That is why I decided to try to use some existing solutions. Alas that what I find here is not usable.

Ok so it is my understandment you did not manage to use the drivers provided in the link. (https://github.com/STMicroelectronics/lsm303agr/tree/b03141c1fb94fe2d6a523d01af792184863ed0e8)

If so, what seems to be the problem with them?

Basically (from my quick read) the driver just needs you to define the body of these functions 

/** Please note that is MANDATORY: return 0 -> no Error.**/
 
int32_t platform_write(void *handle, uint8_t Reg, const uint8_t *Bufp, uint16_t len)
 
int32_t platform_read(void *handle, uint8_t Reg, uint8_t *Bufp, uint16_t len)

So you, in your main I guess, must write the content of them. In this case there will be an HAL_I2C function, so read about STM32 HAL before.

It should be like that, for example:

int32_t platform_write(void *handle, uint8_t Reg, const uint8_t *Bufp, uint16_t len)
{
HAL_I2C_Mem_Write((I2C_HandleTypeDef*)handle, DEVICE_ADDR, Reg, 1U, Bufp, len, 1000);
return 0;
}

But i see you have something like that in the source you linked a few days ago, so get inspired by that.

Well,

the problem is that nobody nowhere wrote what I should place between the braces in the platform_(write|read). Your example is very interesting, but I see there the variable DEVICE_ADDR which is not included in the formal parameter list. I suppose that is supposed to be a slave address, but what if there are two slaves as in the case now ??

As it concerns the sources you are probably asking for, I get there compilation error and a warning that reads the compiler does knot the example function:

Nobody nowhere wrote it because you should be able to write your own communication function.

Or to understand those implemented in your attached source from few days ago.

One could use any function/any platform as long as it manages all the platform_(write|read) variables.

As for the multi addressing, you could use a global variable and assigning the address value before calling the sensor driver's functions.

I don't know what your main job (if you don't mind telling me, I'm curious) is, but, estabilished you're not mainly a firmware developer, I suggest to start somewhere, somewhat. Forget the fact that you have 2 sensors, forget even the sensor if you did not grasp HAL_I2C functions yet. Practice a bit. Once you understand how to use HAL and how it works, move on. My example should work.

When something works, you can improve it.

For the compiler errors I don't know. Can you leave a screenshot?

Listen i tried importing the files and i can correctly compile and everything so, follow me:

  1. Create a blank project for desired MCU (G071RB)
  2. Activate the desired i2c peripheral
  3. Generate code
  4. Copy lsm303agr_reg files in Inc (.h) and Src (.c)
  5. Write your own platform write/read (really you should use my example, even if it's not flexible) in "main.c" between /* USER CODE BEGIN 0 */ and /* USER CODE END 0 */
  6. In your main function, do something like this
  stmdev_ctx_t myDev;
  myDev.read_reg = platform_read;
  myDev.write_reg= platform_read;
  myDev.handle = &hi2c1;

Then you can call the functions appropriately passing &myDev and eventually &buf.

  uint8_t buf;
  lsm303agr_act_threshold_get(&myDev, &buf);

If this does not at least compile, i don't know what will.